level_up.rs 35 KB

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