migration.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*! A module for the protocol for the user to migrate from one bucket to
  2. another and change trust level from untrusted (trust level 0) to trusted
  3. (trust level 1).
  4. The user presents their current Lox credential:
  5. - id: revealed
  6. - bucket: blinded
  7. - trust_level: revealed to be 0
  8. - level_since: blinded
  9. - invites_remaining: revealed to be 0
  10. - blockages: revealed to be 0
  11. and a Migration credential:
  12. - id: revealed as the same as the Lox credential id above
  13. - from_bucket: blinded, but proved in ZK that it's the same as the
  14. bucket in the Lox credential above
  15. - to_bucket: blinded
  16. and a new Lox credential to be issued:
  17. - id: jointly chosen by the user and BA
  18. - bucket: blinded, but proved in ZK that it's the same as the to_bucket
  19. in the Migration credential above
  20. - trust_level: 1
  21. - level_since: today
  22. - invites_remaining: 0
  23. - blockages: 0
  24. */
  25. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  26. use curve25519_dalek::ristretto::RistrettoPoint;
  27. use curve25519_dalek::scalar::Scalar;
  28. use curve25519_dalek::traits::IsIdentity;
  29. use zkp::CompactProof;
  30. use zkp::ProofError;
  31. use zkp::Transcript;
  32. use super::super::cred;
  33. use super::super::dup_filter::SeenType;
  34. use super::super::{BridgeAuth, IssuerPubKey};
  35. use super::super::{CMZ_A, CMZ_A_TABLE, CMZ_B, CMZ_B_TABLE};
  36. pub struct Request {
  37. // Fields for blind showing the Lox credential
  38. // We don't need to include invites_remaining or blockages,
  39. // since they must be 0
  40. P_lox: RistrettoPoint,
  41. id: Scalar,
  42. CBucket: RistrettoPoint,
  43. trust_level: Scalar,
  44. CSince: RistrettoPoint,
  45. CQ_lox: RistrettoPoint,
  46. // Fields for blind showing the Migration credential
  47. P_mig: RistrettoPoint,
  48. CFromBucket: RistrettoPoint,
  49. CToBucket: RistrettoPoint,
  50. CQ_mig: RistrettoPoint,
  51. // Fields for user blinding of the Lox credential to be issued
  52. D: RistrettoPoint,
  53. EncIdClient: (RistrettoPoint, RistrettoPoint),
  54. EncBucket: (RistrettoPoint, RistrettoPoint),
  55. // The combined ZKP
  56. piUser: CompactProof,
  57. }
  58. #[derive(Debug)]
  59. pub struct State {
  60. d: Scalar,
  61. D: RistrettoPoint,
  62. EncIdClient: (RistrettoPoint, RistrettoPoint),
  63. EncBucket: (RistrettoPoint, RistrettoPoint),
  64. id_client: Scalar,
  65. to_bucket: Scalar,
  66. }
  67. pub struct Response {
  68. // The new attributes; trust_level = 1 is implicit
  69. level_since: Scalar,
  70. // The fields for the new Lox credential
  71. P: RistrettoPoint,
  72. EncQ: (RistrettoPoint, RistrettoPoint),
  73. id_server: Scalar,
  74. TId: RistrettoPoint,
  75. TBucket: RistrettoPoint,
  76. // The ZKP
  77. piBlindIssue: CompactProof,
  78. }
  79. define_proof! {
  80. requestproof,
  81. "Migration Request",
  82. (bucket, since, zbucket, zsince, negzQ_lox,
  83. tobucket, zfrombucket, ztobucket, negzQ_mig,
  84. d, eid_client, ebucket, id_client),
  85. (P_lox, CBucket, CSince, V_lox, Xbucket, Xsince,
  86. P_mig, CFromBucket, CToBucket, V_mig, Xfrombucket, Xtobucket,
  87. D, EncIdClient0, EncIdClient1, EncBucket0, EncBucket1),
  88. (A, B):
  89. // Blind showing of the Lox credential
  90. CBucket = (bucket*P_lox + zbucket*A),
  91. CSince = (since*P_lox + zsince*A),
  92. V_lox = (zbucket*Xbucket + zsince*Xsince + negzQ_lox*A),
  93. // Blind showing of the Migration credential; note the use of the
  94. // same "bucket" secret variable
  95. CFromBucket = (bucket*P_mig + zfrombucket*A),
  96. CToBucket = (tobucket*P_mig + ztobucket*A),
  97. V_mig = (zfrombucket*Xfrombucket + ztobucket*Xtobucket + negzQ_mig*A),
  98. // User blinding of the Lox credential to be issued; note the use of
  99. // the same "tobucket" secret variable
  100. D = (d*B),
  101. EncIdClient0 = (eid_client*B),
  102. EncIdClient1 = (id_client*B + eid_client*D),
  103. EncBucket0 = (ebucket*B),
  104. EncBucket1 = (tobucket*B + ebucket*D)
  105. }
  106. define_proof! {
  107. blindissue,
  108. "Migration Blind Issuing",
  109. (x0, x0tilde, xid, xbucket, xlevel, xsince, s, b, tid, tbucket),
  110. (P, EncQ0, EncQ1, X0, Xid, Xbucket, Xlevel, Xsince, Plevel, Psince, TId, TBucket,
  111. D, EncId0, EncId1, EncBucket0, EncBucket1),
  112. (A, B):
  113. Xid = (xid*A),
  114. Xlevel = (xlevel*A),
  115. Xbucket = (xbucket*A),
  116. Xsince = (xsince*A),
  117. X0 = (x0*B + x0tilde*A),
  118. P = (b*B),
  119. TId = (b*Xid),
  120. TId = (tid*A),
  121. TBucket = (b*Xbucket),
  122. TBucket = (tbucket*A),
  123. EncQ0 = (s*B + tid*EncId0 + tbucket*EncBucket0),
  124. EncQ1 = (s*D + tid*EncId1 + tbucket*EncBucket1 + x0*P + xlevel*Plevel + xsince*Psince)
  125. }
  126. pub fn request(
  127. lox_cred: &cred::Lox,
  128. migration_cred: &cred::Migration,
  129. lox_pub: &IssuerPubKey,
  130. migration_pub: &IssuerPubKey,
  131. ) -> Result<(Request, State), ProofError> {
  132. let A: &RistrettoPoint = &CMZ_A;
  133. let B: &RistrettoPoint = &CMZ_B;
  134. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  135. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  136. // Ensure that the credenials can be correctly shown; that is, the
  137. // ids match and the Lox credential bucket matches the Migration
  138. // credential from_bucket
  139. if lox_cred.id != migration_cred.lox_id || lox_cred.bucket != migration_cred.from_bucket {
  140. return Err(ProofError::VerificationFailure);
  141. }
  142. // This protocol only allows migrating from trust level 0 to trust
  143. // level 1
  144. if lox_cred.trust_level != Scalar::zero() {
  145. return Err(ProofError::VerificationFailure);
  146. }
  147. // Blind showing the Lox credential
  148. // Reblind P and Q
  149. let mut rng = rand::thread_rng();
  150. let t_lox = Scalar::random(&mut rng);
  151. let P_lox = t_lox * lox_cred.P;
  152. let Q_lox = t_lox * lox_cred.Q;
  153. // Form Pedersen commitments to the blinded attributes
  154. let zbucket = Scalar::random(&mut rng);
  155. let zsince = Scalar::random(&mut rng);
  156. let CBucket = lox_cred.bucket * P_lox + &zbucket * Atable;
  157. let CSince = lox_cred.level_since * P_lox + &zsince * Atable;
  158. // Form a Pedersen commitment to the MAC Q
  159. // We flip the sign of zQ from that of the Hyphae paper so that
  160. // the ZKP has a "+" instead of a "-", as that's what the zkp
  161. // macro supports.
  162. let negzQ_lox = Scalar::random(&mut rng);
  163. let CQ_lox = Q_lox - &negzQ_lox * Atable;
  164. // Compute the "error factor"
  165. let V_lox = zbucket * lox_pub.X[2] + zsince * lox_pub.X[4] + &negzQ_lox * Atable;
  166. // Blind showing the Migration credential
  167. // Reblind P and Q
  168. let t_mig = Scalar::random(&mut rng);
  169. let P_mig = t_mig * migration_cred.P;
  170. let Q_mig = t_mig * migration_cred.Q;
  171. // Form Pedersen commitments to the blinded attributes
  172. let zfrombucket = Scalar::random(&mut rng);
  173. let ztobucket = Scalar::random(&mut rng);
  174. let CFromBucket = migration_cred.from_bucket * P_mig + &zfrombucket * Atable;
  175. let CToBucket = migration_cred.to_bucket * P_mig + &ztobucket * Atable;
  176. // Form a Pedersen commitment to the MAC Q
  177. // We flip the sign of zQ from that of the Hyphae paper so that
  178. // the ZKP has a "+" instead of a "-", as that's what the zkp
  179. // macro supports.
  180. let negzQ_mig = Scalar::random(&mut rng);
  181. let CQ_mig = Q_mig - &negzQ_mig * Atable;
  182. // Compute the "error factor"
  183. let V_mig =
  184. zfrombucket * migration_pub.X[2] + ztobucket * migration_pub.X[3] + &negzQ_mig * Atable;
  185. // User blinding for the Lox certificate to be issued
  186. // Pick an ElGamal keypair
  187. let d = Scalar::random(&mut rng);
  188. let D = &d * Btable;
  189. // Pick a random client component of the id
  190. let id_client = Scalar::random(&mut rng);
  191. // Encrypt it (times the basepoint B) to the ElGamal public key D we
  192. // just created
  193. let eid_client = Scalar::random(&mut rng);
  194. let EncIdClient = (&eid_client * Btable, &id_client * Btable + eid_client * D);
  195. // Encrypt the bucket field (times B) to D as well
  196. let ebucket = Scalar::random(&mut rng);
  197. let EncBucket = (
  198. &ebucket * Btable,
  199. &migration_cred.to_bucket * Btable + ebucket * D,
  200. );
  201. // Construct the proof
  202. let mut transcript = Transcript::new(b"migration request");
  203. let piUser = requestproof::prove_compact(
  204. &mut transcript,
  205. requestproof::ProveAssignments {
  206. A: &A,
  207. B: &B,
  208. P_lox: &P_lox,
  209. CBucket: &CBucket,
  210. CSince: &CSince,
  211. V_lox: &V_lox,
  212. Xbucket: &lox_pub.X[2],
  213. Xsince: &lox_pub.X[4],
  214. P_mig: &P_mig,
  215. CFromBucket: &CFromBucket,
  216. CToBucket: &CToBucket,
  217. V_mig: &V_mig,
  218. Xfrombucket: &migration_pub.X[2],
  219. Xtobucket: &migration_pub.X[3],
  220. D: &D,
  221. EncIdClient0: &EncIdClient.0,
  222. EncIdClient1: &EncIdClient.1,
  223. EncBucket0: &EncBucket.0,
  224. EncBucket1: &EncBucket.1,
  225. bucket: &lox_cred.bucket,
  226. since: &lox_cred.level_since,
  227. zbucket: &zbucket,
  228. zsince: &zsince,
  229. negzQ_lox: &negzQ_lox,
  230. tobucket: &migration_cred.to_bucket,
  231. zfrombucket: &zfrombucket,
  232. ztobucket: &ztobucket,
  233. negzQ_mig: &negzQ_mig,
  234. d: &d,
  235. eid_client: &eid_client,
  236. ebucket: &ebucket,
  237. id_client: &id_client,
  238. },
  239. )
  240. .0;
  241. Ok((
  242. Request {
  243. P_lox,
  244. id: lox_cred.id,
  245. CBucket,
  246. trust_level: lox_cred.trust_level,
  247. CSince,
  248. CQ_lox,
  249. P_mig,
  250. CFromBucket,
  251. CToBucket,
  252. CQ_mig,
  253. D,
  254. EncIdClient,
  255. EncBucket,
  256. piUser,
  257. },
  258. State {
  259. d,
  260. D,
  261. EncIdClient,
  262. EncBucket,
  263. id_client,
  264. to_bucket: migration_cred.to_bucket,
  265. },
  266. ))
  267. }
  268. impl BridgeAuth {
  269. /// Receive a migration request
  270. pub fn handle_migration(&mut self, req: Request) -> Result<Response, ProofError> {
  271. let A: &RistrettoPoint = &CMZ_A;
  272. let B: &RistrettoPoint = &CMZ_B;
  273. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  274. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  275. if req.P_lox.is_identity() || req.P_mig.is_identity() {
  276. return Err(ProofError::VerificationFailure);
  277. }
  278. // We only currently support migrating from trust level 0
  279. if req.trust_level != Scalar::zero() {
  280. return Err(ProofError::VerificationFailure);
  281. }
  282. // Recompute the "error factors" using knowledge of our own
  283. // (the issuer's) private key instead of knowledge of the
  284. // hidden attributes
  285. let Vprime_lox = (self.lox_priv.x[0]
  286. + self.lox_priv.x[1] * req.id
  287. + self.lox_priv.x[3] * req.trust_level)
  288. * req.P_lox
  289. + self.lox_priv.x[2] * req.CBucket
  290. + self.lox_priv.x[4] * req.CSince
  291. - req.CQ_lox;
  292. let Vprime_mig = (self.migration_priv.x[0] + self.migration_priv.x[1] * req.id) * req.P_mig
  293. + self.migration_priv.x[2] * req.CFromBucket
  294. + self.migration_priv.x[3] * req.CToBucket
  295. - req.CQ_mig;
  296. // Verify the ZKP
  297. let mut transcript = Transcript::new(b"migration request");
  298. requestproof::verify_compact(
  299. &req.piUser,
  300. &mut transcript,
  301. requestproof::VerifyAssignments {
  302. A: &A.compress(),
  303. B: &B.compress(),
  304. P_lox: &req.P_lox.compress(),
  305. CBucket: &req.CBucket.compress(),
  306. CSince: &req.CSince.compress(),
  307. V_lox: &Vprime_lox.compress(),
  308. Xbucket: &self.lox_pub.X[2].compress(),
  309. Xsince: &self.lox_pub.X[4].compress(),
  310. P_mig: &req.P_mig.compress(),
  311. CFromBucket: &req.CFromBucket.compress(),
  312. CToBucket: &req.CToBucket.compress(),
  313. V_mig: &Vprime_mig.compress(),
  314. Xfrombucket: &self.migration_pub.X[2].compress(),
  315. Xtobucket: &self.migration_pub.X[3].compress(),
  316. D: &req.D.compress(),
  317. EncIdClient0: &req.EncIdClient.0.compress(),
  318. EncIdClient1: &req.EncIdClient.1.compress(),
  319. EncBucket0: &req.EncBucket.0.compress(),
  320. EncBucket1: &req.EncBucket.1.compress(),
  321. },
  322. )?;
  323. // Ensure the id has not been seen before, and add it to the
  324. // seen list.
  325. if self.id_filter.filter(&req.id) == SeenType::Seen {
  326. return Err(ProofError::VerificationFailure);
  327. }
  328. // Blind issuing of the new Lox credential
  329. // Choose a random server id component to add to the client's
  330. // (blinded) id component
  331. let mut rng = rand::thread_rng();
  332. let id_server = Scalar::random(&mut rng);
  333. let EncId = (req.EncIdClient.0, req.EncIdClient.1 + &id_server * Btable);
  334. // Create the trust_level attrubute (Scalar), which will be
  335. // level 1
  336. let trust_level: Scalar = Scalar::one();
  337. // Create the level_since attribute (Scalar), which is today's
  338. // Julian date
  339. let level_since: Scalar = self.today().into();
  340. // The invitations_remaining and blockages attributes are 0 for
  341. // level 0 and level 1 Lox credentials, so we don't need to
  342. // explicitly create them.
  343. // Compute the MAC on the visible attributes
  344. let b = Scalar::random(&mut rng);
  345. let P = &b * Btable;
  346. // invites_remaining = blockages = 0
  347. let QHc = (self.lox_priv.x[0]
  348. + self.lox_priv.x[3] * trust_level
  349. + self.lox_priv.x[4] * level_since)
  350. * P;
  351. // El Gamal encrypt it to the public key req.D
  352. let s = Scalar::random(&mut rng);
  353. let EncQHc = (&s * Btable, QHc + s * req.D);
  354. // Homomorphically compute the part of the MAC corresponding to
  355. // the blinded attributes
  356. let tid = self.lox_priv.x[1] * b;
  357. let TId = &tid * Atable;
  358. let EncQId = (tid * EncId.0, tid * EncId.1);
  359. let tbucket = self.lox_priv.x[2] * b;
  360. let TBucket = &tbucket * Atable;
  361. let EncQBucket = (tbucket * req.EncBucket.0, tbucket * req.EncBucket.1);
  362. let EncQ = (
  363. EncQHc.0 + EncQId.0 + EncQBucket.0,
  364. EncQHc.1 + EncQId.1 + EncQBucket.1,
  365. );
  366. let mut transcript = Transcript::new(b"migration issuing");
  367. let piBlindIssue = blindissue::prove_compact(
  368. &mut transcript,
  369. blindissue::ProveAssignments {
  370. A: &A,
  371. B: &B,
  372. P: &P,
  373. EncQ0: &EncQ.0,
  374. EncQ1: &EncQ.1,
  375. X0: &self.lox_pub.X[0],
  376. Xid: &self.lox_pub.X[1],
  377. Xbucket: &self.lox_pub.X[2],
  378. Xlevel: &self.lox_pub.X[3],
  379. Xsince: &self.lox_pub.X[4],
  380. Plevel: &(trust_level * P),
  381. Psince: &(level_since * P),
  382. TId: &TId,
  383. TBucket: &TBucket,
  384. D: &req.D,
  385. EncId0: &EncId.0,
  386. EncId1: &EncId.1,
  387. EncBucket0: &req.EncBucket.0,
  388. EncBucket1: &req.EncBucket.1,
  389. x0: &self.lox_priv.x[0],
  390. x0tilde: &self.lox_priv.x0tilde,
  391. xid: &self.lox_priv.x[1],
  392. xbucket: &self.lox_priv.x[2],
  393. xlevel: &self.lox_priv.x[3],
  394. xsince: &self.lox_priv.x[4],
  395. s: &s,
  396. b: &b,
  397. tid: &tid,
  398. tbucket: &tbucket,
  399. },
  400. )
  401. .0;
  402. Ok(Response {
  403. level_since,
  404. P,
  405. EncQ,
  406. id_server,
  407. TId,
  408. TBucket,
  409. piBlindIssue,
  410. })
  411. }
  412. }
  413. /// Handle the response to the request, producing the new Lox credential
  414. /// if successful.
  415. pub fn handle_response(
  416. state: State,
  417. resp: Response,
  418. lox_pub: &IssuerPubKey,
  419. ) -> Result<cred::Lox, ProofError> {
  420. let A: &RistrettoPoint = &CMZ_A;
  421. let B: &RistrettoPoint = &CMZ_B;
  422. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  423. if resp.P.is_identity() {
  424. return Err(ProofError::VerificationFailure);
  425. }
  426. // Add the server's contribution to the id to our own, both in plain
  427. // and encrypted form
  428. let id = state.id_client + resp.id_server;
  429. let EncId = (
  430. state.EncIdClient.0,
  431. state.EncIdClient.1 + &resp.id_server * Btable,
  432. );
  433. // Verify the proof
  434. let mut transcript = Transcript::new(b"migration issuing");
  435. blindissue::verify_compact(
  436. &resp.piBlindIssue,
  437. &mut transcript,
  438. blindissue::VerifyAssignments {
  439. A: &A.compress(),
  440. B: &B.compress(),
  441. P: &resp.P.compress(),
  442. EncQ0: &resp.EncQ.0.compress(),
  443. EncQ1: &resp.EncQ.1.compress(),
  444. X0: &lox_pub.X[0].compress(),
  445. Xid: &lox_pub.X[1].compress(),
  446. Xbucket: &lox_pub.X[2].compress(),
  447. Xlevel: &lox_pub.X[3].compress(),
  448. Xsince: &lox_pub.X[4].compress(),
  449. // The new trust level is 1
  450. Plevel: &(Scalar::one() * resp.P).compress(),
  451. Psince: &(resp.level_since * resp.P).compress(),
  452. TId: &resp.TId.compress(),
  453. TBucket: &resp.TBucket.compress(),
  454. D: &state.D.compress(),
  455. EncId0: &EncId.0.compress(),
  456. EncId1: &EncId.1.compress(),
  457. EncBucket0: &state.EncBucket.0.compress(),
  458. EncBucket1: &state.EncBucket.1.compress(),
  459. },
  460. )?;
  461. // Decrypt EncQ
  462. let Q = resp.EncQ.1 - (state.d * resp.EncQ.0);
  463. Ok(cred::Lox {
  464. P: resp.P,
  465. Q,
  466. id,
  467. bucket: state.to_bucket,
  468. trust_level: Scalar::one(),
  469. level_since: resp.level_since,
  470. invites_remaining: Scalar::zero(),
  471. blockages: Scalar::zero(),
  472. })
  473. }