migration.rs 18 KB

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