level_up.rs 35 KB

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