level_up.rs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*! A module for the protocol for the user to increase their trust level
  2. (from a level at least 1; use the trust promotion protocol to go from
  3. untrusted (level 0) to minimally trusted (level 1).
  4. They are allowed to do this as long as some amount of time (depending on
  5. their current level) has elapsed since their last level change, and they
  6. have a Bucket Reachability credential for their current bucket and
  7. today's date. (Such credentials are placed daily in the encrypted
  8. bridge table.)
  9. The user presents their current Lox credential:
  10. - id: revealed
  11. - bucket: blinded
  12. - trust_level: revealed, and must be at least 1
  13. - level_since: blinded, but proved in ZK that it's at least the
  14. appropriate number of days ago
  15. - invites_remaining: blinded
  16. - blockages: blinded, but proved in ZK that it's at most the appropriate
  17. blockage limit for the target trust level
  18. and a Bucket Reachability credential:
  19. - date: revealed to be today
  20. - bucket: blinded, but proved in ZK that it's the same as in the Lox
  21. credential above
  22. and a new Lox credential to be issued:
  23. - id: jointly chosen by the user and BA
  24. - bucket: blinded, but proved in ZK that it's the same as in the Lox
  25. credential above
  26. - trust_level: revealed to be one more than the trust level above
  27. - level_since: today
  28. - invites_remaining: revealed to be the number of invites for the new
  29. level (note that the invites_remaining from the previous credential
  30. are _not_ carried over)
  31. - blockages: blinded, but proved in ZK that it's the same as in the
  32. Lox credential above
  33. */
  34. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  35. use curve25519_dalek::ristretto::RistrettoPoint;
  36. use curve25519_dalek::scalar::Scalar;
  37. use curve25519_dalek::traits::IsIdentity;
  38. use zkp::CompactProof;
  39. use zkp::ProofError;
  40. use zkp::Transcript;
  41. use super::super::cred;
  42. use super::super::dup_filter::SeenType;
  43. use super::super::{pt_dbl, scalar_dbl, scalar_u32};
  44. use super::super::{BridgeAuth, IssuerPubKey};
  45. use super::super::{CMZ_A, CMZ_A_TABLE, CMZ_B, CMZ_B_TABLE};
  46. /// The maximum trust level in the system. A user can run this level
  47. /// upgrade protocol when they're already at the max level; they will
  48. /// get a fresh invites_remaining batch, and reset their level_since
  49. /// field to today's date, but will remain in the max level.
  50. pub const MAX_LEVEL: usize = 4;
  51. /// LEVEL_INTERVAL\[i\] for i >= 1 is the minimum number of days a user
  52. /// must be at trust level i before advancing to level i+1 (or as above,
  53. /// remain at level i if i == MAX_LEVEL). Note that the
  54. /// LEVEL_INTERVAL\[0\] entry is a dummy; the trust_promotion protocol
  55. /// is used instead of this one to move from level 0 to level 1.
  56. pub const LEVEL_INTERVAL: [u32; MAX_LEVEL + 1] = [0, 14, 28, 56, 84];
  57. /// LEVEL_INVITATIONS\[i\] for i >= 1 is the number of invitations a
  58. /// user will be eligible to issue upon advancing from level i to level
  59. /// i+1. Again the LEVEL_INVITATIONS\[0\] entry is a dummy, as for
  60. /// LEVEL_INTERVAL.
  61. pub const LEVEL_INVITATIONS: [u32; MAX_LEVEL + 1] = [0, 2, 4, 6, 8];
  62. /// MAX_BLOCKAGES\[i\] for i >= 1 is the maximum number of bucket
  63. /// blockages this credential is allowed to have recorded in order to
  64. /// advance from level i to level i+1. Again the LEVEL_INVITATIONS\[0\]
  65. /// entry is a dummy, as for LEVEL_INTERVAL.
  66. // If you change this to have a number greater than 7, you need to add
  67. // one or more bits to the ZKP.
  68. pub const MAX_BLOCKAGES: [u32; MAX_LEVEL + 1] = [0, 4, 3, 2, 2];
  69. pub struct Request {
  70. // Fields for blind showing the Lox credential
  71. P: RistrettoPoint,
  72. id: Scalar,
  73. CBucket: RistrettoPoint,
  74. level: Scalar,
  75. CSince: RistrettoPoint,
  76. CInvRemain: RistrettoPoint,
  77. CBlockages: RistrettoPoint,
  78. CQ: RistrettoPoint,
  79. // Fields for blind showing the Bucket Reachability credential
  80. P_reach: RistrettoPoint,
  81. CBucket_reach: RistrettoPoint,
  82. CQ_reach: RistrettoPoint,
  83. // Fields for the inequality proof
  84. // level_since + LEVEL_INTERVAL[level] <= today
  85. CG1: RistrettoPoint,
  86. CG2: RistrettoPoint,
  87. CG3: RistrettoPoint,
  88. CG4: RistrettoPoint,
  89. CG5: RistrettoPoint,
  90. CG6: RistrettoPoint,
  91. CG7: RistrettoPoint,
  92. CG8: RistrettoPoint,
  93. CG0sq: RistrettoPoint,
  94. CG1sq: RistrettoPoint,
  95. CG2sq: RistrettoPoint,
  96. CG3sq: RistrettoPoint,
  97. CG4sq: RistrettoPoint,
  98. CG5sq: RistrettoPoint,
  99. CG6sq: RistrettoPoint,
  100. CG7sq: RistrettoPoint,
  101. CG8sq: RistrettoPoint,
  102. // Fields for the inequality proof
  103. // blockages <= MAX_BLOCKAGES[level]
  104. CH1: RistrettoPoint,
  105. CH2: RistrettoPoint,
  106. CH0sq: RistrettoPoint,
  107. CH1sq: RistrettoPoint,
  108. CH2sq: RistrettoPoint,
  109. // Fields for user blinding of the Lox credential to be issued
  110. D: RistrettoPoint,
  111. EncIdClient: (RistrettoPoint, RistrettoPoint),
  112. EncBucket: (RistrettoPoint, RistrettoPoint),
  113. EncBlockages: (RistrettoPoint, RistrettoPoint),
  114. // The combined ZKP
  115. piUser: CompactProof,
  116. }
  117. #[derive(Debug)]
  118. pub struct State {
  119. d: Scalar,
  120. D: RistrettoPoint,
  121. EncIdClient: (RistrettoPoint, RistrettoPoint),
  122. EncBucket: (RistrettoPoint, RistrettoPoint),
  123. EncBlockages: (RistrettoPoint, RistrettoPoint),
  124. id_client: Scalar,
  125. bucket: Scalar,
  126. level: Scalar,
  127. invremain: Scalar,
  128. blockages: Scalar,
  129. }
  130. pub struct Response {
  131. // The fields for the new Lox credential; the new trust level is one
  132. // more than the old trust level, so we don't have to include it
  133. // here explicitly
  134. P: RistrettoPoint,
  135. EncQ: (RistrettoPoint, RistrettoPoint),
  136. id_server: Scalar,
  137. level_since: Scalar,
  138. TId: RistrettoPoint,
  139. TBucket: RistrettoPoint,
  140. TBlockages: RistrettoPoint,
  141. // The ZKP
  142. piBlindIssue: CompactProof,
  143. }
  144. define_proof! {
  145. requestproof,
  146. "Level Upgrade Request",
  147. (bucket, since, invremain, blockages, zbucket, zsince, zinvremain,
  148. zblockages, negzQ,
  149. zbucket_reach, negzQ_reach,
  150. d, eid_client, ebucket, eblockages, id_client,
  151. g0, g1, g2, g3, g4, g5, g6, g7, g8,
  152. zg0, zg1, zg2, zg3, zg4, zg5, zg6, zg7, zg8,
  153. wg0, wg1, wg2, wg3, wg4, wg5, wg6, wg7, wg8,
  154. yg0, yg1, yg2, yg3, yg4, yg5, yg6, yg7, yg8,
  155. h0, h1, h2,
  156. zh0, zh1, zh2,
  157. wh0, wh1, wh2,
  158. yh0, yh1, yh2),
  159. (P, CBucket, CSince, CInvRemain, CBlockages, V, Xbucket, Xsince,
  160. Xinvremain, Xblockages,
  161. P_reach, CBucket_reach, V_reach, Xbucket_reach,
  162. D, EncIdClient0, EncIdClient1, EncBucket0, EncBucket1,
  163. EncBlockages0, EncBlockages1,
  164. CG0, CG1, CG2, CG3, CG4, CG5, CG6, CG7, CG8,
  165. CG0sq, CG1sq, CG2sq, CG3sq, CG4sq, CG5sq, CG6sq, CG7sq, CG8sq,
  166. CH0, CH1, CH2,
  167. CH0sq, CH1sq, CH2sq),
  168. (A, B) :
  169. // Blind showing of the Lox credential
  170. CBucket = (bucket*P + zbucket*A),
  171. CSince = (since*P + zsince*A),
  172. CInvRemain = (invremain*P + zinvremain*A),
  173. CBlockages = (blockages*P + zblockages*A),
  174. // Blind showing of the Bucket Reachability credential; note the
  175. // same bucket is used in the proof
  176. CBucket_reach = (bucket*P_reach + zbucket_reach*A),
  177. // User blinding of the Lox credential to be issued
  178. D = (d*B),
  179. EncIdClient0 = (eid_client*B),
  180. EncIdClient1 = (id_client*B + eid_client*D),
  181. EncBucket0 = (ebucket*B),
  182. EncBucket1 = (bucket*B + ebucket*D),
  183. EncBlockages0 = (eblockages*B),
  184. EncBlockages1 = (blockages*B + eblockages*D),
  185. // Prove CSince encodes a value at least LEVEL_INTERVAL
  186. // days ago (and technically at most LEVEL_INTERVAL+511 days
  187. // ago): first prove each of g0, ..., g8 is a bit by proving that
  188. // gi = gi^2
  189. CG0 = (g0*P + zg0*A), CG0sq = (g0*CG0 + wg0*A), CG0sq = (g0*P + yg0*A),
  190. CG1 = (g1*P + zg1*A), CG1sq = (g1*CG1 + wg1*A), CG1sq = (g1*P + yg1*A),
  191. CG2 = (g2*P + zg2*A), CG2sq = (g2*CG2 + wg2*A), CG2sq = (g2*P + yg2*A),
  192. CG3 = (g3*P + zg3*A), CG3sq = (g3*CG3 + wg3*A), CG3sq = (g3*P + yg3*A),
  193. CG4 = (g4*P + zg4*A), CG4sq = (g4*CG4 + wg4*A), CG4sq = (g4*P + yg4*A),
  194. CG5 = (g5*P + zg5*A), CG5sq = (g5*CG5 + wg5*A), CG5sq = (g5*P + yg5*A),
  195. CG6 = (g6*P + zg6*A), CG6sq = (g6*CG6 + wg6*A), CG6sq = (g6*P + yg6*A),
  196. CG7 = (g7*P + zg7*A), CG7sq = (g7*CG7 + wg7*A), CG7sq = (g7*P + yg7*A),
  197. CG8 = (g8*P + zg8*A), CG8sq = (g8*CG8 + wg8*A), CG8sq = (g8*P + yg8*A),
  198. // Then we'll check that CSince + LEVEL_INTERVAL*P + CG0 + 2*CG1
  199. // + 4*CG2 + 8*CG3 + ... + 256*CG8 = today*P by having the verifier
  200. // plug in today*P - (CSince + LEVEL_INTERVAL*P + 2*CG1 + 4*CG2
  201. // + ... + 256*CG8) as its value of CG0.
  202. // Prove CBlockage encodes a value at most MAX_BLOCKAGES (and at least
  203. // MAX_BLOCKAGES-7)
  204. CH0 = (h0*P + zh0*A), CH0sq = (h0*CH0 + wh0*A), CH0sq = (h0*P + yh0*A),
  205. CH1 = (h1*P + zh1*A), CH1sq = (h1*CH1 + wh1*A), CH1sq = (h1*P + yh1*A),
  206. CH2 = (h2*P + zh2*A), CH2sq = (h2*CH2 + wh2*A), CH2sq = (h2*P + yh2*A)
  207. // Then we'll check that CBlockage + CH0 + 2*CH1 + 4*CH2 =
  208. // MAX_BLOCKAGES*P by having the verifier plug in MAX_BLOCKAGES*P -
  209. // (CBlockage - 2*CH1 - 4*CH2) as its value of CH0.
  210. }
  211. define_proof! {
  212. blindissue,
  213. "Level Upgrade Issuing",
  214. (x0, x0tilde, xid, xbucket, xlevel, xsince, xinvremain, xblockages,
  215. s, b, tid, tbucket, tblockages),
  216. (P, EncQ0, EncQ1, X0, Xid, Xbucket, Xlevel, Xsince, Xinvremain,
  217. Xblockages, Plevel, Psince, Pinvremain, TId, TBucket, TBlockages,
  218. D, EncId0, EncId1, EncBucket0, EncBucket1, EncBlockages0, EncBlockages1),
  219. (A, B):
  220. Xid = (xid*A),
  221. Xid = (xid*A),
  222. Xlevel = (xlevel*A),
  223. Xbucket = (xbucket*A),
  224. Xsince = (xsince*A),
  225. Xinvremain = (xinvremain*A),
  226. Xblockages = (xblockages*A),
  227. X0 = (x0*B + x0tilde*A),
  228. P = (b*B),
  229. TId = (b*Xid),
  230. TId = (tid*A),
  231. TBucket = (b*Xbucket),
  232. TBucket = (tbucket*A),
  233. TBlockages = (b*Xblockages),
  234. TBlockages = (tblockages*A),
  235. EncQ0 = (s*B + tid*EncId0 + tbucket*EncBucket0 + tblockages*EncBlockages0),
  236. EncQ1 = (s*D + tid*EncId1 + tbucket*EncBucket1
  237. + tblockages*EncBlockages1 + x0*P + xlevel*Plevel + xsince*Psince
  238. + xinvremain*Pinvremain)
  239. }
  240. pub fn request(
  241. lox_cred: &cred::Lox,
  242. reach_cred: &cred::BucketReachability,
  243. lox_pub: &IssuerPubKey,
  244. reach_pub: &IssuerPubKey,
  245. today: u32,
  246. ) -> Result<(Request, State), ProofError> {
  247. let A: &RistrettoPoint = &CMZ_A;
  248. let B: &RistrettoPoint = &CMZ_B;
  249. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  250. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  251. // Ensure the credential can be correctly shown: it must be the case
  252. // that level_since + LEVEL_INTERVAL[level] <= today.
  253. let level_since: u32 = match scalar_u32(&lox_cred.level_since) {
  254. Some(v) => v,
  255. None => return Err(ProofError::VerificationFailure),
  256. };
  257. // The trust level has to be at least 1
  258. let trust_level: u32 = match scalar_u32(&lox_cred.trust_level) {
  259. Some(v) => v,
  260. None => return Err(ProofError::VerificationFailure),
  261. };
  262. if trust_level < 1 || (trust_level as usize) > MAX_LEVEL {
  263. return Err(ProofError::VerificationFailure);
  264. }
  265. // The trust level has to be no higher than the highest level
  266. let level_interval: u32 = match LEVEL_INTERVAL.get(trust_level as usize) {
  267. Some(&v) => v,
  268. None => return Err(ProofError::VerificationFailure),
  269. };
  270. if level_since + level_interval > today {
  271. return Err(ProofError::VerificationFailure);
  272. }
  273. // The credential can't be _too_ old
  274. let diffdays = today - (level_since + level_interval);
  275. if diffdays > 511 {
  276. return Err(ProofError::VerificationFailure);
  277. }
  278. // The current number of blockages
  279. let blockages: u32 = match scalar_u32(&lox_cred.blockages) {
  280. Some(v) => v,
  281. None => return Err(ProofError::VerificationFailure),
  282. };
  283. if blockages > MAX_BLOCKAGES[trust_level as usize] {
  284. return Err(ProofError::VerificationFailure);
  285. }
  286. let blockage_diff = MAX_BLOCKAGES[trust_level as usize] - blockages;
  287. // The buckets in the Lox and Bucket Reachability credentials have
  288. // to match
  289. if lox_cred.bucket != reach_cred.bucket {
  290. return Err(ProofError::VerificationFailure);
  291. }
  292. // The Bucket Reachability credential has to be dated today
  293. let reach_date: u32 = match scalar_u32(&reach_cred.date) {
  294. Some(v) => v,
  295. None => return Err(ProofError::VerificationFailure),
  296. };
  297. if reach_date != today {
  298. return Err(ProofError::VerificationFailure);
  299. }
  300. // The new trust level
  301. let new_level = if (trust_level as usize) < MAX_LEVEL {
  302. trust_level + 1
  303. } else {
  304. trust_level
  305. };
  306. // Blind showing the Lox credential
  307. // Reblind P and Q
  308. let mut rng = rand::thread_rng();
  309. let t = Scalar::random(&mut rng);
  310. let P = t * lox_cred.P;
  311. let Q = t * lox_cred.Q;
  312. // Form Pedersen commitments to the blinded attributes
  313. let zbucket = Scalar::random(&mut rng);
  314. let zsince = Scalar::random(&mut rng);
  315. let zinvremain = Scalar::random(&mut rng);
  316. let zblockages = Scalar::random(&mut rng);
  317. let CBucket = lox_cred.bucket * P + &zbucket * Atable;
  318. let CSince = lox_cred.level_since * P + &zsince * Atable;
  319. let CInvRemain = lox_cred.invites_remaining * P + &zinvremain * Atable;
  320. let CBlockages = lox_cred.blockages * P + &zblockages * Atable;
  321. // Form a Pedersen commitment to the MAC Q
  322. // We flip the sign of zQ from that of the Hyphae paper so that
  323. // the ZKP has a "+" instead of a "-", as that's what the zkp
  324. // macro supports.
  325. let negzQ = Scalar::random(&mut rng);
  326. let CQ = Q - &negzQ * Atable;
  327. // Compute the "error factor"
  328. let V = zbucket * lox_pub.X[2]
  329. + zsince * lox_pub.X[4]
  330. + zinvremain * lox_pub.X[5]
  331. + zblockages * lox_pub.X[6]
  332. + &negzQ * Atable;
  333. // Blind showing the Bucket Reachability credential
  334. // Reblind P and Q
  335. let t_reach = Scalar::random(&mut rng);
  336. let P_reach = t_reach * reach_cred.P;
  337. let Q_reach = t_reach * reach_cred.Q;
  338. // Form Pedersen commitments to the blinded attributes
  339. let zbucket_reach = Scalar::random(&mut rng);
  340. let CBucket_reach = reach_cred.bucket * P_reach + &zbucket_reach * Atable;
  341. // Form a Pedersen commitment to the MAC Q
  342. // We flip the sign of zQ from that of the Hyphae paper so that
  343. // the ZKP has a "+" instead of a "-", as that's what the zkp
  344. // macro supports.
  345. let negzQ_reach = Scalar::random(&mut rng);
  346. let CQ_reach = Q_reach - &negzQ_reach * Atable;
  347. // Compute the "error factor"
  348. let V_reach = zbucket_reach * reach_pub.X[2] + &negzQ_reach * Atable;
  349. // User blinding for the Lox certificate to be issued
  350. // Pick an ElGamal keypair
  351. let d = Scalar::random(&mut rng);
  352. let D = &d * Btable;
  353. // Pick a random client component of the id
  354. let id_client = Scalar::random(&mut rng);
  355. // Encrypt it (times the basepoint B) to the ElGamal public key D we
  356. // just created
  357. let eid_client = Scalar::random(&mut rng);
  358. let EncIdClient = (&eid_client * Btable, &id_client * Btable + eid_client * D);
  359. // Encrypt the other blinded fields (times B) to D as well
  360. let ebucket = Scalar::random(&mut rng);
  361. let EncBucket = (&ebucket * Btable, &lox_cred.bucket * Btable + ebucket * D);
  362. let newinvites: Scalar = LEVEL_INVITATIONS[trust_level as usize].into();
  363. let eblockages = Scalar::random(&mut rng);
  364. let EncBlockages = (
  365. &eblockages * Btable,
  366. &lox_cred.blockages * Btable + eblockages * D,
  367. );
  368. // The range proof that 0 <= diffdays <= 511
  369. // Extract the 9 bits from diffdays
  370. let g0: Scalar = (diffdays & 1).into();
  371. let g1: Scalar = ((diffdays >> 1) & 1).into();
  372. let g2: Scalar = ((diffdays >> 2) & 1).into();
  373. let g3: Scalar = ((diffdays >> 3) & 1).into();
  374. let g4: Scalar = ((diffdays >> 4) & 1).into();
  375. let g5: Scalar = ((diffdays >> 5) & 1).into();
  376. let g6: Scalar = ((diffdays >> 6) & 1).into();
  377. let g7: Scalar = ((diffdays >> 7) & 1).into();
  378. let g8: Scalar = ((diffdays >> 8) & 1).into();
  379. // Pick random factors for the Pedersen commitments
  380. let wg0 = Scalar::random(&mut rng);
  381. let zg1 = Scalar::random(&mut rng);
  382. let wg1 = Scalar::random(&mut rng);
  383. let zg2 = Scalar::random(&mut rng);
  384. let wg2 = Scalar::random(&mut rng);
  385. let zg3 = Scalar::random(&mut rng);
  386. let wg3 = Scalar::random(&mut rng);
  387. let zg4 = Scalar::random(&mut rng);
  388. let wg4 = Scalar::random(&mut rng);
  389. let zg5 = Scalar::random(&mut rng);
  390. let wg5 = Scalar::random(&mut rng);
  391. let zg6 = Scalar::random(&mut rng);
  392. let wg6 = Scalar::random(&mut rng);
  393. let zg7 = Scalar::random(&mut rng);
  394. let wg7 = Scalar::random(&mut rng);
  395. let zg8 = Scalar::random(&mut rng);
  396. let wg8 = Scalar::random(&mut rng);
  397. // Compute zg0 to cancel things out as
  398. // zg0 = -(zsince + 2*zg1 + 4*zg2 + 8*zg3 + 16*zg4 + 32*zg5 + 64*zg6 + 128*zg7 + 256*zg8)
  399. // but use Horner's method
  400. let zg0 = -(scalar_dbl(
  401. &(scalar_dbl(
  402. &(scalar_dbl(
  403. &(scalar_dbl(
  404. &(scalar_dbl(
  405. &(scalar_dbl(&(scalar_dbl(&(scalar_dbl(&zg8) + zg7)) + zg6)) + zg5),
  406. ) + zg4),
  407. ) + zg3),
  408. ) + zg2),
  409. ) + zg1),
  410. ) + zsince);
  411. let yg0 = wg0 + g0 * zg0;
  412. let yg1 = wg1 + g1 * zg1;
  413. let yg2 = wg2 + g2 * zg2;
  414. let yg3 = wg3 + g3 * zg3;
  415. let yg4 = wg4 + g4 * zg4;
  416. let yg5 = wg5 + g5 * zg5;
  417. let yg6 = wg6 + g6 * zg6;
  418. let yg7 = wg7 + g7 * zg7;
  419. let yg8 = wg8 + g8 * zg8;
  420. let CG0 = g0 * P + &zg0 * Atable;
  421. let CG1 = g1 * P + &zg1 * Atable;
  422. let CG2 = g2 * P + &zg2 * Atable;
  423. let CG3 = g3 * P + &zg3 * Atable;
  424. let CG4 = g4 * P + &zg4 * Atable;
  425. let CG5 = g5 * P + &zg5 * Atable;
  426. let CG6 = g6 * P + &zg6 * Atable;
  427. let CG7 = g7 * P + &zg7 * Atable;
  428. let CG8 = g8 * P + &zg8 * Atable;
  429. let CG0sq = g0 * P + &yg0 * Atable;
  430. let CG1sq = g1 * P + &yg1 * Atable;
  431. let CG2sq = g2 * P + &yg2 * Atable;
  432. let CG3sq = g3 * P + &yg3 * Atable;
  433. let CG4sq = g4 * P + &yg4 * Atable;
  434. let CG5sq = g5 * P + &yg5 * Atable;
  435. let CG6sq = g6 * P + &yg6 * Atable;
  436. let CG7sq = g7 * P + &yg7 * Atable;
  437. let CG8sq = g8 * P + &yg8 * Atable;
  438. // The range proof that 0 <= blockage_diff <= 7
  439. // Extract the 3 bits from blockage_diff
  440. let h0: Scalar = (blockage_diff & 1).into();
  441. let h1: Scalar = ((blockage_diff >> 1) & 1).into();
  442. let h2: Scalar = ((blockage_diff >> 2) & 1).into();
  443. // Pick random factors for the Pedersen commitments
  444. let wh0 = Scalar::random(&mut rng);
  445. let zh1 = Scalar::random(&mut rng);
  446. let wh1 = Scalar::random(&mut rng);
  447. let zh2 = Scalar::random(&mut rng);
  448. let wh2 = Scalar::random(&mut rng);
  449. // Compute zh0 to cancel things out as
  450. // zh0 = -(zblockages + 2*zh1 + 4*zh2)
  451. // but use Horner's method
  452. let zh0 = -(scalar_dbl(&(scalar_dbl(&zh2) + zh1)) + zblockages);
  453. let yh0 = wh0 + h0 * zh0;
  454. let yh1 = wh1 + h1 * zh1;
  455. let yh2 = wh2 + h2 * zh2;
  456. let CH0 = h0 * P + &zh0 * Atable;
  457. let CH1 = h1 * P + &zh1 * Atable;
  458. let CH2 = h2 * P + &zh2 * Atable;
  459. let CH0sq = h0 * P + &yh0 * Atable;
  460. let CH1sq = h1 * P + &yh1 * Atable;
  461. let CH2sq = h2 * P + &yh2 * Atable;
  462. // Construct the proof
  463. let mut transcript = Transcript::new(b"level upgrade request");
  464. let piUser = requestproof::prove_compact(
  465. &mut transcript,
  466. requestproof::ProveAssignments {
  467. A: &A,
  468. B: &B,
  469. P: &P,
  470. CBucket: &CBucket,
  471. CSince: &CSince,
  472. CInvRemain: &CInvRemain,
  473. CBlockages: &CBlockages,
  474. V: &V,
  475. Xbucket: &lox_pub.X[2],
  476. Xsince: &lox_pub.X[4],
  477. Xinvremain: &lox_pub.X[5],
  478. Xblockages: &lox_pub.X[6],
  479. P_reach: &P_reach,
  480. CBucket_reach: &CBucket_reach,
  481. V_reach: &V_reach,
  482. Xbucket_reach: &reach_pub.X[2],
  483. D: &D,
  484. EncIdClient0: &EncIdClient.0,
  485. EncIdClient1: &EncIdClient.1,
  486. EncBucket0: &EncBucket.0,
  487. EncBucket1: &EncBucket.1,
  488. EncBlockages0: &EncBlockages.0,
  489. EncBlockages1: &EncBlockages.1,
  490. CG0: &CG0,
  491. CG1: &CG1,
  492. CG2: &CG2,
  493. CG3: &CG3,
  494. CG4: &CG4,
  495. CG5: &CG5,
  496. CG6: &CG6,
  497. CG7: &CG7,
  498. CG8: &CG8,
  499. CG0sq: &CG0sq,
  500. CG1sq: &CG1sq,
  501. CG2sq: &CG2sq,
  502. CG3sq: &CG3sq,
  503. CG4sq: &CG4sq,
  504. CG5sq: &CG5sq,
  505. CG6sq: &CG6sq,
  506. CG7sq: &CG7sq,
  507. CG8sq: &CG8sq,
  508. CH0: &CH0,
  509. CH1: &CH1,
  510. CH2: &CH2,
  511. CH0sq: &CH0sq,
  512. CH1sq: &CH1sq,
  513. CH2sq: &CH2sq,
  514. bucket: &lox_cred.bucket,
  515. since: &lox_cred.level_since,
  516. invremain: &lox_cred.invites_remaining,
  517. blockages: &lox_cred.blockages,
  518. zbucket: &zbucket,
  519. zsince: &zsince,
  520. zinvremain: &zinvremain,
  521. zblockages: &zblockages,
  522. negzQ: &negzQ,
  523. zbucket_reach: &zbucket_reach,
  524. negzQ_reach: &negzQ_reach,
  525. d: &d,
  526. eid_client: &eid_client,
  527. ebucket: &ebucket,
  528. eblockages: &eblockages,
  529. id_client: &id_client,
  530. g0: &g0,
  531. g1: &g1,
  532. g2: &g2,
  533. g3: &g3,
  534. g4: &g4,
  535. g5: &g5,
  536. g6: &g6,
  537. g7: &g7,
  538. g8: &g8,
  539. zg0: &zg0,
  540. zg1: &zg1,
  541. zg2: &zg2,
  542. zg3: &zg3,
  543. zg4: &zg4,
  544. zg5: &zg5,
  545. zg6: &zg6,
  546. zg7: &zg7,
  547. zg8: &zg8,
  548. wg0: &wg0,
  549. wg1: &wg1,
  550. wg2: &wg2,
  551. wg3: &wg3,
  552. wg4: &wg4,
  553. wg5: &wg5,
  554. wg6: &wg6,
  555. wg7: &wg7,
  556. wg8: &wg8,
  557. yg0: &yg0,
  558. yg1: &yg1,
  559. yg2: &yg2,
  560. yg3: &yg3,
  561. yg4: &yg4,
  562. yg5: &yg5,
  563. yg6: &yg6,
  564. yg7: &yg7,
  565. yg8: &yg8,
  566. h0: &h0,
  567. h1: &h1,
  568. h2: &h2,
  569. zh0: &zh0,
  570. zh1: &zh1,
  571. zh2: &zh2,
  572. wh0: &wh0,
  573. wh1: &wh1,
  574. wh2: &wh2,
  575. yh0: &yh0,
  576. yh1: &yh1,
  577. yh2: &yh2,
  578. },
  579. )
  580. .0;
  581. Ok((
  582. Request {
  583. P,
  584. id: lox_cred.id,
  585. CBucket,
  586. level: lox_cred.trust_level,
  587. CSince,
  588. CInvRemain,
  589. CBlockages,
  590. CQ,
  591. P_reach,
  592. CBucket_reach,
  593. CQ_reach,
  594. D,
  595. EncIdClient,
  596. EncBucket,
  597. EncBlockages,
  598. CG1,
  599. CG2,
  600. CG3,
  601. CG4,
  602. CG5,
  603. CG6,
  604. CG7,
  605. CG8,
  606. CG0sq,
  607. CG1sq,
  608. CG2sq,
  609. CG3sq,
  610. CG4sq,
  611. CG5sq,
  612. CG6sq,
  613. CG7sq,
  614. CG8sq,
  615. CH1,
  616. CH2,
  617. CH0sq,
  618. CH1sq,
  619. CH2sq,
  620. piUser,
  621. },
  622. State {
  623. d,
  624. D,
  625. EncIdClient,
  626. EncBucket,
  627. EncBlockages,
  628. id_client,
  629. bucket: lox_cred.bucket,
  630. level: new_level.into(),
  631. invremain: newinvites,
  632. blockages: lox_cred.blockages,
  633. },
  634. ))
  635. }
  636. impl BridgeAuth {
  637. /// Receive a level up request
  638. pub fn handle_level_up(&mut self, req: Request) -> Result<Response, ProofError> {
  639. let A: &RistrettoPoint = &CMZ_A;
  640. let B: &RistrettoPoint = &CMZ_B;
  641. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  642. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  643. if req.P.is_identity() {
  644. return Err(ProofError::VerificationFailure);
  645. }
  646. let today: Scalar = self.today().into();
  647. // Get the level and ensure it's at most MAX_LEVEL
  648. let level: usize = match scalar_u32(&req.level) {
  649. Some(l) if l as usize <= MAX_LEVEL => l as usize,
  650. _ => return Err(ProofError::VerificationFailure),
  651. };
  652. // Recompute the "error factors" using knowledge of our own
  653. // (the issuer's) private key instead of knowledge of the
  654. // hidden attributes
  655. let Vprime =
  656. (self.lox_priv.x[0] + self.lox_priv.x[1] * req.id + self.lox_priv.x[3] * req.level)
  657. * req.P
  658. + self.lox_priv.x[2] * req.CBucket
  659. + self.lox_priv.x[4] * req.CSince
  660. + self.lox_priv.x[5] * req.CInvRemain
  661. + self.lox_priv.x[6] * req.CBlockages
  662. - req.CQ;
  663. let Vprime_reach = (self.reachability_priv.x[0] + self.reachability_priv.x[1] * today)
  664. * req.P_reach
  665. + self.reachability_priv.x[2] * req.CBucket_reach
  666. - req.CQ_reach;
  667. // Recompute CG0 using Horner's method
  668. let unt: Scalar = LEVEL_INTERVAL[level].into();
  669. let CG0prime = (today - unt) * req.P
  670. - req.CSince
  671. - pt_dbl(
  672. &(pt_dbl(
  673. &(pt_dbl(
  674. &(pt_dbl(
  675. &(pt_dbl(
  676. &(pt_dbl(&(pt_dbl(&(pt_dbl(&req.CG8) + req.CG7)) + req.CG6))
  677. + req.CG5),
  678. ) + req.CG4),
  679. ) + req.CG3),
  680. ) + req.CG2),
  681. ) + req.CG1),
  682. );
  683. // Recompute CH0 using Horner's method
  684. let mblk: Scalar = MAX_BLOCKAGES[level].into();
  685. let CH0prime = mblk * req.P - req.CBlockages - pt_dbl(&(pt_dbl(&req.CH2) + req.CH1));
  686. // Verify the ZKP
  687. let mut transcript = Transcript::new(b"level upgrade request");
  688. requestproof::verify_compact(
  689. &req.piUser,
  690. &mut transcript,
  691. requestproof::VerifyAssignments {
  692. A: &A.compress(),
  693. B: &B.compress(),
  694. P: &req.P.compress(),
  695. CBucket: &req.CBucket.compress(),
  696. CSince: &req.CSince.compress(),
  697. CInvRemain: &req.CInvRemain.compress(),
  698. CBlockages: &req.CBlockages.compress(),
  699. V: &Vprime.compress(),
  700. Xbucket: &self.lox_pub.X[2].compress(),
  701. Xsince: &self.lox_pub.X[4].compress(),
  702. Xinvremain: &self.lox_pub.X[5].compress(),
  703. Xblockages: &self.lox_pub.X[6].compress(),
  704. P_reach: &req.P_reach.compress(),
  705. CBucket_reach: &req.CBucket_reach.compress(),
  706. V_reach: &Vprime_reach.compress(),
  707. Xbucket_reach: &self.reachability_pub.X[2].compress(),
  708. D: &req.D.compress(),
  709. EncIdClient0: &req.EncIdClient.0.compress(),
  710. EncIdClient1: &req.EncIdClient.1.compress(),
  711. EncBucket0: &req.EncBucket.0.compress(),
  712. EncBucket1: &req.EncBucket.1.compress(),
  713. EncBlockages0: &req.EncBlockages.0.compress(),
  714. EncBlockages1: &req.EncBlockages.1.compress(),
  715. CG0: &CG0prime.compress(),
  716. CG1: &req.CG1.compress(),
  717. CG2: &req.CG2.compress(),
  718. CG3: &req.CG3.compress(),
  719. CG4: &req.CG4.compress(),
  720. CG5: &req.CG5.compress(),
  721. CG6: &req.CG6.compress(),
  722. CG7: &req.CG7.compress(),
  723. CG8: &req.CG8.compress(),
  724. CG0sq: &req.CG0sq.compress(),
  725. CG1sq: &req.CG1sq.compress(),
  726. CG2sq: &req.CG2sq.compress(),
  727. CG3sq: &req.CG3sq.compress(),
  728. CG4sq: &req.CG4sq.compress(),
  729. CG5sq: &req.CG5sq.compress(),
  730. CG6sq: &req.CG6sq.compress(),
  731. CG7sq: &req.CG7sq.compress(),
  732. CG8sq: &req.CG8sq.compress(),
  733. CH0: &CH0prime.compress(),
  734. CH1: &req.CH1.compress(),
  735. CH2: &req.CH2.compress(),
  736. CH0sq: &req.CH0sq.compress(),
  737. CH1sq: &req.CH1sq.compress(),
  738. CH2sq: &req.CH2sq.compress(),
  739. },
  740. )?;
  741. // Ensure the id has not been seen before, and add it to the
  742. // seen list.
  743. if self.id_filter.filter(&req.id) == SeenType::Seen {
  744. return Err(ProofError::VerificationFailure);
  745. }
  746. // Blind issuing of the new Lox credential
  747. // Choose a random server id component to add to the client's
  748. // (blinded) id component
  749. let mut rng = rand::thread_rng();
  750. let id_server = Scalar::random(&mut rng);
  751. let EncId = (req.EncIdClient.0, req.EncIdClient.1 + &id_server * Btable);
  752. // Create the trust_level attrubute (Scalar), which will be
  753. // one more than the current level, unless the current level is
  754. // MAX_LEVEL, in which case it stays the same
  755. let new_level = if level < MAX_LEVEL { level + 1 } else { level };
  756. let trust_level: Scalar = (new_level as u64).into();
  757. // Create the level_since attribute (Scalar), which is today's
  758. // Julian date
  759. let level_since: Scalar = self.today().into();
  760. // Create the invitations_remaining attribute (Scalar), which is
  761. // the number of invitations at the new level
  762. let invitations_remaining: Scalar = LEVEL_INVITATIONS[level].into();
  763. // Compute the MAC on the visible attributes
  764. let b = Scalar::random(&mut rng);
  765. let P = &b * Btable;
  766. let QHc = (self.lox_priv.x[0]
  767. + self.lox_priv.x[3] * trust_level
  768. + self.lox_priv.x[4] * level_since
  769. + self.lox_priv.x[5] * invitations_remaining)
  770. * P;
  771. // El Gamal encrypt it to the public key req.D
  772. let s = Scalar::random(&mut rng);
  773. let EncQHc = (&s * Btable, QHc + s * req.D);
  774. // Homomorphically compute the part of the MAC corresponding to
  775. // the blinded attributes
  776. let tid = self.lox_priv.x[1] * b;
  777. let TId = &tid * Atable;
  778. let EncQId = (tid * EncId.0, tid * EncId.1);
  779. let tbucket = self.lox_priv.x[2] * b;
  780. let TBucket = &tbucket * Atable;
  781. let EncQBucket = (tbucket * req.EncBucket.0, tbucket * req.EncBucket.1);
  782. let tblockages = self.lox_priv.x[6] * b;
  783. let TBlockages = &tblockages * Atable;
  784. let EncQBlockages = (
  785. tblockages * req.EncBlockages.0,
  786. tblockages * req.EncBlockages.1,
  787. );
  788. let EncQ = (
  789. EncQHc.0 + EncQId.0 + EncQBucket.0 + EncQBlockages.0,
  790. EncQHc.1 + EncQId.1 + EncQBucket.1 + EncQBlockages.1,
  791. );
  792. let mut transcript = Transcript::new(b"level upgrade issuing");
  793. let piBlindIssue = blindissue::prove_compact(
  794. &mut transcript,
  795. blindissue::ProveAssignments {
  796. A: &A,
  797. B: &B,
  798. P: &P,
  799. EncQ0: &EncQ.0,
  800. EncQ1: &EncQ.1,
  801. X0: &self.lox_pub.X[0],
  802. Xid: &self.lox_pub.X[1],
  803. Xbucket: &self.lox_pub.X[2],
  804. Xlevel: &self.lox_pub.X[3],
  805. Xsince: &self.lox_pub.X[4],
  806. Xinvremain: &self.lox_pub.X[5],
  807. Xblockages: &self.lox_pub.X[6],
  808. Plevel: &(trust_level * P),
  809. Psince: &(level_since * P),
  810. Pinvremain: &(invitations_remaining * P),
  811. TId: &TId,
  812. TBucket: &TBucket,
  813. TBlockages: &TBlockages,
  814. D: &req.D,
  815. EncId0: &EncId.0,
  816. EncId1: &EncId.1,
  817. EncBucket0: &req.EncBucket.0,
  818. EncBucket1: &req.EncBucket.1,
  819. EncBlockages0: &req.EncBlockages.0,
  820. EncBlockages1: &req.EncBlockages.1,
  821. x0: &self.lox_priv.x[0],
  822. x0tilde: &self.lox_priv.x0tilde,
  823. xid: &self.lox_priv.x[1],
  824. xbucket: &self.lox_priv.x[2],
  825. xlevel: &self.lox_priv.x[3],
  826. xsince: &self.lox_priv.x[4],
  827. xinvremain: &self.lox_priv.x[5],
  828. xblockages: &self.lox_priv.x[6],
  829. s: &s,
  830. b: &b,
  831. tid: &tid,
  832. tbucket: &tbucket,
  833. tblockages: &tblockages,
  834. },
  835. )
  836. .0;
  837. Ok(Response {
  838. P,
  839. EncQ,
  840. id_server,
  841. level_since,
  842. TId,
  843. TBucket,
  844. TBlockages,
  845. piBlindIssue,
  846. })
  847. }
  848. }
  849. /// Handle the response to the request, producing the new Lox credential
  850. /// if successful.
  851. pub fn handle_response(
  852. state: State,
  853. resp: Response,
  854. lox_pub: &IssuerPubKey,
  855. ) -> Result<cred::Lox, ProofError> {
  856. let A: &RistrettoPoint = &CMZ_A;
  857. let B: &RistrettoPoint = &CMZ_B;
  858. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  859. if resp.P.is_identity() {
  860. return Err(ProofError::VerificationFailure);
  861. }
  862. // Add the server's contribution to the id to our own, both in plain
  863. // and encrypted form
  864. let id = state.id_client + resp.id_server;
  865. let EncId = (
  866. state.EncIdClient.0,
  867. state.EncIdClient.1 + &resp.id_server * Btable,
  868. );
  869. // Verify the proof
  870. let mut transcript = Transcript::new(b"level upgrade issuing");
  871. blindissue::verify_compact(
  872. &resp.piBlindIssue,
  873. &mut transcript,
  874. blindissue::VerifyAssignments {
  875. A: &A.compress(),
  876. B: &B.compress(),
  877. P: &resp.P.compress(),
  878. EncQ0: &resp.EncQ.0.compress(),
  879. EncQ1: &resp.EncQ.1.compress(),
  880. X0: &lox_pub.X[0].compress(),
  881. Xid: &lox_pub.X[1].compress(),
  882. Xbucket: &lox_pub.X[2].compress(),
  883. Xlevel: &lox_pub.X[3].compress(),
  884. Xsince: &lox_pub.X[4].compress(),
  885. Xinvremain: &lox_pub.X[5].compress(),
  886. Xblockages: &lox_pub.X[6].compress(),
  887. Plevel: &(state.level * resp.P).compress(),
  888. Psince: &(resp.level_since * resp.P).compress(),
  889. Pinvremain: &(state.invremain * resp.P).compress(),
  890. TId: &resp.TId.compress(),
  891. TBucket: &resp.TBucket.compress(),
  892. TBlockages: &resp.TBlockages.compress(),
  893. D: &state.D.compress(),
  894. EncId0: &EncId.0.compress(),
  895. EncId1: &EncId.1.compress(),
  896. EncBucket0: &state.EncBucket.0.compress(),
  897. EncBucket1: &state.EncBucket.1.compress(),
  898. EncBlockages0: &state.EncBlockages.0.compress(),
  899. EncBlockages1: &state.EncBlockages.1.compress(),
  900. },
  901. )?;
  902. // Decrypt EncQ
  903. let Q = resp.EncQ.1 - (state.d * resp.EncQ.0);
  904. Ok(cred::Lox {
  905. P: resp.P,
  906. Q,
  907. id,
  908. bucket: state.bucket,
  909. trust_level: state.level,
  910. level_since: resp.level_since,
  911. invites_remaining: state.invremain,
  912. blockages: state.blockages,
  913. })
  914. }