trust_promotion.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*! A module for the protocol for the user to get promoted from
  2. untrusted (trust level 0) to trusted (trust level 1).
  3. They are allowed to do this as long as UNTRUSTED_INTERVAL days have
  4. passed since they obtained their level 0 Lox credential, and their
  5. bridge (level 0 users get put in a one-bridge bucket) has not been
  6. blocked. (Blocked bridges in one-bridge buckets will have their entries
  7. removed from the bridge authority's migration table.)
  8. The user presents their current Lox credential:
  9. - id: revealed
  10. - bucket: blinded
  11. - trust_level: revealed to be 0
  12. - level_since: blinded, but proved in ZK that it's at least
  13. UNTRUSTED_INTERVAL days ago
  14. - invites_remaining: revealed to be 0
  15. - invites_issued: revealed to be 0
  16. They will receive in return the encrypted MAC (Pk, EncQk) for their
  17. implicit Migration Key credential with attributes id and bucket,
  18. along with a HashMap of encrypted Migration credentials. For each
  19. (from_i, to_i) in the BA's migration list, there will be an entry in
  20. the HashMap with key H1(id, from_attr_i, Qk_i) and value
  21. Enc_{H2(id, from_attr_i, Qk_i)}(to_attr_i, P_i, Q_i). Here H1 and H2
  22. are the first 16 bytes and the second 16 bytes respectively of the
  23. SHA256 hash of the input, P_i and Q_i are a MAC on the Migration
  24. credential with attributes id, from_attr_i, and to_attr_i. Qk_i is the
  25. value EncQk would decrypt to if bucket were equal to from_attr_i. */
  26. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  27. use curve25519_dalek::ristretto::RistrettoPoint;
  28. use curve25519_dalek::scalar::Scalar;
  29. use curve25519_dalek::traits::IsIdentity;
  30. use zkp::CompactProof;
  31. use zkp::ProofError;
  32. use zkp::Transcript;
  33. use super::cred;
  34. use super::IssuerPubKey;
  35. use super::{scalar_dbl, scalar_u64};
  36. use super::{CMZ_A, CMZ_A_TABLE, CMZ_B, CMZ_B_TABLE};
  37. /// The minimum number of days a user has to be at trust level 0
  38. /// (untrusted) with their (single) bridge unblocked before they can
  39. /// move to level 1.
  40. ///
  41. /// The implementation also puts an upper bound of UNTRUSTED_INTERVAL +
  42. /// 511 days, which is not unreasonable; we want users to be engaging
  43. /// with the system in order to move up trust levels.
  44. pub const UNTRUSTED_INTERVAL: u64 = 30;
  45. pub struct Request {
  46. // Fields for blind showing the Lox credential
  47. // We don't need to include trust_level, invites_remaining, or
  48. // invites_issued, since they must be 0
  49. P: RistrettoPoint,
  50. id: Scalar,
  51. CBucket: RistrettoPoint,
  52. CSince: RistrettoPoint,
  53. CQ: RistrettoPoint,
  54. // Fields for user blinding of the Migration Key credential
  55. D: RistrettoPoint,
  56. EncBucket: (RistrettoPoint, RistrettoPoint),
  57. // Fields for the inequality proof (level_since +
  58. // UNTRUSTED_INTERVAL <= today)
  59. CG1: RistrettoPoint,
  60. CG2: RistrettoPoint,
  61. CG3: RistrettoPoint,
  62. CG4: RistrettoPoint,
  63. CG5: RistrettoPoint,
  64. CG6: RistrettoPoint,
  65. CG7: RistrettoPoint,
  66. CG8: RistrettoPoint,
  67. // The combined ZKP
  68. piUser: CompactProof,
  69. }
  70. #[derive(Debug)]
  71. pub struct State {
  72. d: Scalar,
  73. D: RistrettoPoint,
  74. EncBucket: (RistrettoPoint, RistrettoPoint),
  75. id: Scalar,
  76. bucket: Scalar,
  77. }
  78. define_proof! {
  79. requestproof,
  80. "Trust Promotion Request",
  81. (bucket, since, zbucket, zsince, negzQ,
  82. d, ebucket,
  83. g0, g1, g2, g3, g4, g5, g6, g7, g8,
  84. zg0, zg1, zg2, zg3, zg4, zg5, zg6, zg7, zg8,
  85. wg0, wg1, wg2, wg3, wg4, wg5, wg6, wg7, wg8,
  86. yg0, yg1, yg2, yg3, yg4, yg5, yg6, yg7, yg8),
  87. (P, CBucket, CSince, V, Xbucket, Xsince,
  88. EncBucket0, EncBucket1, D,
  89. CG0, CG1, CG2, CG3, CG4, CG5, CG6, CG7, CG8,
  90. CG0sq, CG1sq, CG2sq, CG3sq, CG4sq, CG5sq, CG6sq, CG7sq, CG8sq),
  91. (A, B):
  92. // Blind showing of the Lox credential
  93. CBucket = (bucket*P + zbucket*A),
  94. CSince = (since*P + zsince*A),
  95. V = (zbucket*Xbucket + zsince*Xsince + negzQ*A),
  96. // User blinding of the Migration Key credential
  97. EncBucket0 = (ebucket*B),
  98. EncBucket1 = (bucket*B + ebucket*D),
  99. D = (d*B),
  100. // Prove CSince encodes a value at least UNTRUSTED_INTERVAL
  101. // days ago (at technically at most UNTRUSTED_INTERVAL+511 days
  102. // ago): first prove each of g0, ..., g8 is a bit by proving that
  103. // gi = gi^2
  104. CG0 = (g0*P + zg0*A), CG0sq = (g0*CG0 + wg0*A), CG0sq = (g0*P + yg0*A),
  105. CG1 = (g1*P + zg1*A), CG1sq = (g1*CG1 + wg1*A), CG1sq = (g1*P + yg1*A),
  106. CG2 = (g2*P + zg2*A), CG2sq = (g2*CG2 + wg2*A), CG2sq = (g2*P + yg2*A),
  107. CG3 = (g3*P + zg3*A), CG3sq = (g3*CG3 + wg3*A), CG3sq = (g3*P + yg3*A),
  108. CG4 = (g4*P + zg4*A), CG4sq = (g4*CG4 + wg4*A), CG4sq = (g4*P + yg4*A),
  109. CG5 = (g5*P + zg5*A), CG5sq = (g5*CG5 + wg5*A), CG5sq = (g5*P + yg5*A),
  110. CG6 = (g6*P + zg6*A), CG6sq = (g6*CG6 + wg6*A), CG6sq = (g6*P + yg6*A),
  111. CG7 = (g7*P + zg7*A), CG7sq = (g7*CG7 + wg7*A), CG7sq = (g7*P + yg7*A),
  112. CG8 = (g8*P + zg8*A), CG8sq = (g8*CG8 + wg8*A), CG8sq = (g8*P + yg8*A)
  113. // Then we'll check that CSince + UNTRUSTED_INTERVAL*P + CG0 + 2*CG1
  114. // + 4*CG2 + 8*CG3 + ... + 256*CG8 = today*P by having the verifier
  115. // plug in today*P - (CSince + UNTRUSTED_INTERVAL*P + 2*CG1 + 4*CG2
  116. // + ... + 256*CG8) as its value of CG0.
  117. }
  118. pub fn request(
  119. lox_cred: &cred::Lox,
  120. lox_pub: &IssuerPubKey,
  121. today: u64,
  122. ) -> Result<(Request, State), ProofError> {
  123. let A: &RistrettoPoint = &CMZ_A;
  124. let B: &RistrettoPoint = &CMZ_B;
  125. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  126. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  127. // Ensure the credential can be correctly shown: it must be the case
  128. // that level_since + UNTRUSTED_INTERVAL <= today.
  129. let level_since: u64 = match scalar_u64(&lox_cred.level_since) {
  130. Some(v) => v,
  131. None => return Err(ProofError::VerificationFailure),
  132. };
  133. if level_since + UNTRUSTED_INTERVAL > today {
  134. return Err(ProofError::VerificationFailure);
  135. }
  136. let diffdays = today - (level_since + UNTRUSTED_INTERVAL);
  137. if diffdays > 511 {
  138. return Err(ProofError::VerificationFailure);
  139. }
  140. // Blind showing the Lox credential
  141. // Reblind P and Q
  142. let mut rng = rand::thread_rng();
  143. let t = Scalar::random(&mut rng);
  144. let P = t * lox_cred.P;
  145. let Q = t * lox_cred.Q;
  146. // Form Pedersen commitments to the blinded attributes
  147. let zbucket = Scalar::random(&mut rng);
  148. let zsince = Scalar::random(&mut rng);
  149. let CBucket = lox_cred.bucket * P + &zbucket * Atable;
  150. let CSince = lox_cred.level_since * P + &zsince * Atable;
  151. // Form a Pedersen commitment to the MAC Q
  152. // We flip the sign of zQ from that of the Hyphae paper so that
  153. // the ZKP has a "+" instead of a "-", as that's what the zkp
  154. // macro supports.
  155. let negzQ = Scalar::random(&mut rng);
  156. let CQ = Q - &negzQ * Atable;
  157. // Compute the "error factor"
  158. let V = zbucket * lox_pub.X[2] + zsince * lox_pub.X[4] + &negzQ * Atable;
  159. // User blinding the Migration Key credential
  160. // Pick an ElGamal keypair
  161. let d = Scalar::random(&mut rng);
  162. let D = &d * Btable;
  163. // Encrypt the attributes to be blinded (each times the
  164. // basepoint B) to the public key we just created
  165. let ebucket = Scalar::random(&mut rng);
  166. let EncBucket = (&ebucket * Btable, &lox_cred.bucket * Btable + ebucket * D);
  167. // The range proof that 0 <= diffdays <= 511
  168. // Extract the 9 bits from diffdays
  169. let g0: Scalar = (diffdays & 1).into();
  170. let g1: Scalar = ((diffdays >> 1) & 1).into();
  171. let g2: Scalar = ((diffdays >> 2) & 1).into();
  172. let g3: Scalar = ((diffdays >> 3) & 1).into();
  173. let g4: Scalar = ((diffdays >> 4) & 1).into();
  174. let g5: Scalar = ((diffdays >> 5) & 1).into();
  175. let g6: Scalar = ((diffdays >> 6) & 1).into();
  176. let g7: Scalar = ((diffdays >> 7) & 1).into();
  177. let g8: Scalar = ((diffdays >> 8) & 1).into();
  178. // Pick random factors for the Pedersen commitments
  179. let wg0 = Scalar::random(&mut rng);
  180. let zg1 = Scalar::random(&mut rng);
  181. let wg1 = Scalar::random(&mut rng);
  182. let zg2 = Scalar::random(&mut rng);
  183. let wg2 = Scalar::random(&mut rng);
  184. let zg3 = Scalar::random(&mut rng);
  185. let wg3 = Scalar::random(&mut rng);
  186. let zg4 = Scalar::random(&mut rng);
  187. let wg4 = Scalar::random(&mut rng);
  188. let zg5 = Scalar::random(&mut rng);
  189. let wg5 = Scalar::random(&mut rng);
  190. let zg6 = Scalar::random(&mut rng);
  191. let wg6 = Scalar::random(&mut rng);
  192. let zg7 = Scalar::random(&mut rng);
  193. let wg7 = Scalar::random(&mut rng);
  194. let zg8 = Scalar::random(&mut rng);
  195. let wg8 = Scalar::random(&mut rng);
  196. // Compute zg0 to cancel things out as
  197. // zg0 = -(zsince + 2*zg1 + 4*zg2 + 8*zg3 + 16*zg4 + 32*zg5 + 64*zg6 + 128*zg7 + 256*zg8)
  198. // but use Horner's method
  199. let zg0 = -scalar_dbl(
  200. &(scalar_dbl(
  201. &(scalar_dbl(
  202. &(scalar_dbl(
  203. &(scalar_dbl(
  204. &(scalar_dbl(&(scalar_dbl(&(scalar_dbl(&zg8) + zg7)) + zg6)) + zg5),
  205. ) + zg4),
  206. ) + zg3),
  207. ) + zg2),
  208. ) + zg1),
  209. ) + zsince;
  210. let yg0 = wg0 + g0 * zg0;
  211. let yg1 = wg1 + g1 * zg1;
  212. let yg2 = wg2 + g2 * zg2;
  213. let yg3 = wg3 + g3 * zg3;
  214. let yg4 = wg4 + g4 * zg4;
  215. let yg5 = wg5 + g5 * zg5;
  216. let yg6 = wg6 + g6 * zg6;
  217. let yg7 = wg7 + g7 * zg7;
  218. let yg8 = wg8 + g8 * zg8;
  219. let CG0 = g0 * P + &zg0 * Atable;
  220. let CG1 = g1 * P + &zg1 * Atable;
  221. let CG2 = g2 * P + &zg2 * Atable;
  222. let CG3 = g3 * P + &zg3 * Atable;
  223. let CG4 = g4 * P + &zg4 * Atable;
  224. let CG5 = g5 * P + &zg5 * Atable;
  225. let CG6 = g6 * P + &zg6 * Atable;
  226. let CG7 = g7 * P + &zg7 * Atable;
  227. let CG8 = g8 * P + &zg8 * Atable;
  228. // Construct the proof
  229. let mut transcript = Transcript::new(b"trust promotion request");
  230. let piUser = requestproof::prove_compact(
  231. &mut transcript,
  232. requestproof::ProveAssignments {
  233. A: &A,
  234. B: &B,
  235. P: &P,
  236. CBucket: &CBucket,
  237. CSince: &CSince,
  238. V: &V,
  239. Xbucket: &lox_pub.X[2],
  240. Xsince: &lox_pub.X[4],
  241. EncBucket0: &EncBucket.0,
  242. EncBucket1: &EncBucket.1,
  243. D: &D,
  244. CG0: &CG0,
  245. CG1: &CG1,
  246. CG2: &CG2,
  247. CG3: &CG3,
  248. CG4: &CG4,
  249. CG5: &CG5,
  250. CG6: &CG6,
  251. CG7: &CG7,
  252. CG8: &CG8,
  253. CG0sq: &(g0 * P + &yg0 * Atable),
  254. CG1sq: &(g1 * P + &yg1 * Atable),
  255. CG2sq: &(g2 * P + &yg2 * Atable),
  256. CG3sq: &(g3 * P + &yg3 * Atable),
  257. CG4sq: &(g4 * P + &yg4 * Atable),
  258. CG5sq: &(g5 * P + &yg5 * Atable),
  259. CG6sq: &(g6 * P + &yg6 * Atable),
  260. CG7sq: &(g7 * P + &yg7 * Atable),
  261. CG8sq: &(g8 * P + &yg8 * Atable),
  262. bucket: &lox_cred.bucket,
  263. since: &lox_cred.level_since,
  264. zbucket: &zbucket,
  265. zsince: &zsince,
  266. negzQ: &negzQ,
  267. d: &d,
  268. ebucket: &ebucket,
  269. g0: &g0,
  270. g1: &g1,
  271. g2: &g2,
  272. g3: &g3,
  273. g4: &g4,
  274. g5: &g5,
  275. g6: &g6,
  276. g7: &g7,
  277. g8: &g8,
  278. zg0: &zg0,
  279. zg1: &zg1,
  280. zg2: &zg2,
  281. zg3: &zg3,
  282. zg4: &zg4,
  283. zg5: &zg5,
  284. zg6: &zg6,
  285. zg7: &zg7,
  286. zg8: &zg8,
  287. wg0: &wg0,
  288. wg1: &wg1,
  289. wg2: &wg2,
  290. wg3: &wg3,
  291. wg4: &wg4,
  292. wg5: &wg5,
  293. wg6: &wg6,
  294. wg7: &wg7,
  295. wg8: &wg8,
  296. yg0: &yg0,
  297. yg1: &yg1,
  298. yg2: &yg2,
  299. yg3: &yg3,
  300. yg4: &yg4,
  301. yg5: &yg5,
  302. yg6: &yg6,
  303. yg7: &yg7,
  304. yg8: &yg8,
  305. },
  306. )
  307. .0;
  308. Ok((
  309. Request {
  310. P,
  311. id: lox_cred.id,
  312. CBucket,
  313. CSince,
  314. CQ,
  315. D,
  316. EncBucket,
  317. CG1,
  318. CG2,
  319. CG3,
  320. CG4,
  321. CG5,
  322. CG6,
  323. CG7,
  324. CG8,
  325. piUser,
  326. },
  327. State {
  328. d,
  329. D,
  330. EncBucket,
  331. id: lox_cred.id,
  332. bucket: lox_cred.bucket,
  333. },
  334. ))
  335. }