level_up.rs 31 KB

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