level_up.rs 31 KB

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