level_up.rs 34 KB

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