ggm.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // Implementation of CMZ14 credentials (GGM version, which is more
  2. // efficient, but makes a stronger security assumption): "Algebraic MACs
  3. // and Keyed-Verification Anonymous Credentials" (Chase, Meiklejohn,
  4. // and Zaverucha, CCS 2014)
  5. // The notation follows that of the paper "Hyphae: Social Secret
  6. // Sharing" (Lovecruft and de Valence, 2017), Section 4.
  7. // We really want points to be capital letters and scalars to be
  8. // lowercase letters
  9. #![allow(non_snake_case)]
  10. use sha2::Sha512;
  11. use curve25519_dalek::constants as dalek_constants;
  12. use curve25519_dalek::ristretto::RistrettoPoint;
  13. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  14. use curve25519_dalek::scalar::Scalar;
  15. use lazy_static::lazy_static;
  16. lazy_static! {
  17. pub static ref CMZ_A: RistrettoPoint =
  18. RistrettoPoint::hash_from_bytes::<Sha512>(b"CMZ Generator A");
  19. pub static ref CMZ_B: RistrettoPoint =
  20. dalek_constants::RISTRETTO_BASEPOINT_POINT;
  21. pub static ref CMZ_A_TABLE: RistrettoBasepointTable =
  22. RistrettoBasepointTable::create(&CMZ_A);
  23. pub static ref CMZ_B_TABLE: RistrettoBasepointTable =
  24. dalek_constants::RISTRETTO_BASEPOINT_TABLE;
  25. }
  26. #[derive(Clone,Debug)]
  27. pub struct IssuerPrivKey {
  28. x0tilde: Scalar,
  29. x: Vec<Scalar>,
  30. }
  31. impl IssuerPrivKey {
  32. // Create an IssuerPrivKey for credentials with the given number of
  33. // attributes.
  34. pub fn new(n: u16) -> IssuerPrivKey {
  35. let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
  36. let x0tilde: Scalar = Scalar::random(&mut rng);
  37. let mut x: Vec<Scalar> = Vec::with_capacity((n+1) as usize);
  38. // Set x to a vector of n+1 random Scalars
  39. x.resize_with((n+1) as usize, || { Scalar::random(&mut rng) });
  40. IssuerPrivKey { x0tilde, x }
  41. }
  42. }
  43. #[derive(Clone,Debug)]
  44. pub struct IssuerPubKey {
  45. X: Vec<RistrettoPoint>,
  46. }
  47. impl IssuerPubKey {
  48. // Create an IssuerPubKey from the corresponding IssuerPrivKey
  49. pub fn new(privkey: &IssuerPrivKey) -> IssuerPubKey {
  50. let Atable : &RistrettoBasepointTable = &CMZ_A_TABLE;
  51. let Btable : &RistrettoBasepointTable = &CMZ_B_TABLE;
  52. let n_plus_one: usize = privkey.x.len();
  53. let mut X: Vec<RistrettoPoint> = Vec::with_capacity(n_plus_one);
  54. // The first element is a special case; it is
  55. // X[0] = x0tilde*A + x[0]*B
  56. X.push(&privkey.x0tilde * Atable + &privkey.x[0] * Btable);
  57. // The other elements (1 through n) are X[i] = x[i]*A
  58. for i in 1..n_plus_one {
  59. X.push(&privkey.x[i] * Atable);
  60. }
  61. IssuerPubKey { X }
  62. }
  63. }
  64. #[derive(Debug)]
  65. pub struct Issuer {
  66. privkey: IssuerPrivKey,
  67. pub pubkey: IssuerPubKey,
  68. }
  69. impl Issuer {
  70. // Create an issuer for credentials with the given number of
  71. // attributes
  72. pub fn new(n: u16) -> Issuer {
  73. let privkey = IssuerPrivKey::new(n);
  74. let pubkey = IssuerPubKey::new(&privkey);
  75. Issuer { privkey, pubkey }
  76. }
  77. }
  78. #[derive(Debug)]
  79. pub struct Credential5 {
  80. P: RistrettoPoint,
  81. Q: RistrettoPoint,
  82. m1: Scalar,
  83. m2: Scalar,
  84. m3: Scalar,
  85. m4: Scalar,
  86. m5: Scalar,
  87. }
  88. // A submodule for issuing credentials with 5 attributes, none of which
  89. // are blinded to the issuer.
  90. pub mod issue_nonblind_5 {
  91. use curve25519_dalek::ristretto::RistrettoPoint;
  92. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  93. use curve25519_dalek::scalar::Scalar;
  94. use curve25519_dalek::traits::IsIdentity;
  95. use zkp::CompactProof;
  96. use zkp::ProofError;
  97. use zkp::Transcript;
  98. use super::{CMZ_A,CMZ_B,CMZ_B_TABLE,Issuer,IssuerPubKey,Credential5};
  99. #[derive(Debug)]
  100. pub struct CredentialRequest {
  101. m1: Scalar,
  102. m2: Scalar,
  103. m3: Scalar,
  104. m4: Scalar,
  105. m5: Scalar,
  106. }
  107. #[derive(Debug)]
  108. pub struct CredentialRequestState {
  109. m1: Scalar,
  110. m2: Scalar,
  111. m3: Scalar,
  112. m4: Scalar,
  113. m5: Scalar,
  114. }
  115. pub struct CredentialResponse {
  116. P: RistrettoPoint,
  117. Q: RistrettoPoint,
  118. piNonblindIssue: CompactProof,
  119. }
  120. define_proof! {
  121. issue,
  122. "Nonblind 5 issuing proof",
  123. (x0, x0tilde, x1, x2, x3, x4, x5),
  124. (P, Q, X0, X1, X2, X3, X4, X5, P1, P2, P3, P4, P5),
  125. (A, B) :
  126. X1 = (x1*A),
  127. X2 = (x2*A),
  128. X3 = (x3*A),
  129. X4 = (x4*A),
  130. X5 = (x5*A),
  131. X0 = (x0*B + x0tilde*A),
  132. Q = (x0*P + x1*P1 + x2*P2 + x3*P3 + x4*P4 + x5*P5)
  133. }
  134. pub fn request(m1: &Scalar, m2: &Scalar, m3: &Scalar,
  135. m4: &Scalar, m5: &Scalar) -> (CredentialRequest,
  136. CredentialRequestState) {
  137. // For nonblind requests, just send the attributes in the clear
  138. (
  139. CredentialRequest {
  140. m1: *m1,
  141. m2: *m2,
  142. m3: *m3,
  143. m4: *m4,
  144. m5: *m5
  145. },
  146. CredentialRequestState {
  147. m1: *m1,
  148. m2: *m2,
  149. m3: *m3,
  150. m4: *m4,
  151. m5: *m5
  152. }
  153. )
  154. }
  155. impl Issuer {
  156. // Issue a credential with (for example) 5 given attributes. In
  157. // this (nonblinded) version, the issuer sees all of the attributes.
  158. pub fn issue_nonblind_5(&self, req: &CredentialRequest)
  159. -> CredentialResponse {
  160. let A : &RistrettoPoint = &CMZ_A;
  161. let B : &RistrettoPoint = &CMZ_B;
  162. let Btable : &RistrettoBasepointTable = &CMZ_B_TABLE;
  163. let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
  164. let b: Scalar = Scalar::random(&mut rng);
  165. let P: RistrettoPoint = &b * Btable;
  166. // There is a typo in the Hyphae paper: in Section 4.1, Q should
  167. // also have an x0*P term (also in Q'). (You can see that term
  168. // in Section 4.2.)
  169. let Q: RistrettoPoint = (self.privkey.x[0] + (
  170. self.privkey.x[1] * req.m1 +
  171. self.privkey.x[2] * req.m2 +
  172. self.privkey.x[3] * req.m3 +
  173. self.privkey.x[4] * req.m4 +
  174. self.privkey.x[5] * req.m5)) * P;
  175. let mut transcript = Transcript::new(b"Nonblind 5 issuing proof");
  176. let piNonblindIssue: CompactProof = issue::prove_compact(
  177. &mut transcript,
  178. issue::ProveAssignments {
  179. A: &A,
  180. B: &B,
  181. P: &P,
  182. Q: &Q,
  183. X0: &self.pubkey.X[0],
  184. X1: &self.pubkey.X[1],
  185. X2: &self.pubkey.X[2],
  186. X3: &self.pubkey.X[3],
  187. X4: &self.pubkey.X[4],
  188. X5: &self.pubkey.X[5],
  189. P1: &(&req.m1 * &P),
  190. P2: &(&req.m2 * &P),
  191. P3: &(&req.m3 * &P),
  192. P4: &(&req.m4 * &P),
  193. P5: &(&req.m5 * &P),
  194. x0: &self.privkey.x[0],
  195. x1: &self.privkey.x[1],
  196. x2: &self.privkey.x[2],
  197. x3: &self.privkey.x[3],
  198. x4: &self.privkey.x[4],
  199. x5: &self.privkey.x[5],
  200. x0tilde: &self.privkey.x0tilde
  201. }).0;
  202. CredentialResponse { P, Q, piNonblindIssue }
  203. }
  204. }
  205. pub fn verify(state: CredentialRequestState,
  206. resp: CredentialResponse, pubkey: &IssuerPubKey)
  207. -> Result<Credential5, ProofError> {
  208. let A : &RistrettoPoint = &CMZ_A;
  209. let B : &RistrettoPoint = &CMZ_B;
  210. if resp.P.is_identity() {
  211. return Err(ProofError::VerificationFailure);
  212. }
  213. let mut transcript = Transcript::new(b"Nonblind 5 issuing proof");
  214. issue::verify_compact(
  215. &resp.piNonblindIssue,
  216. &mut transcript,
  217. issue::VerifyAssignments {
  218. A: &A.compress(),
  219. B: &B.compress(),
  220. P: &resp.P.compress(),
  221. Q: &resp.Q.compress(),
  222. X0: &pubkey.X[0].compress(),
  223. X1: &pubkey.X[1].compress(),
  224. X2: &pubkey.X[2].compress(),
  225. X3: &pubkey.X[3].compress(),
  226. X4: &pubkey.X[4].compress(),
  227. X5: &pubkey.X[5].compress(),
  228. P1: &(&state.m1 * &resp.P).compress(),
  229. P2: &(&state.m2 * &resp.P).compress(),
  230. P3: &(&state.m3 * &resp.P).compress(),
  231. P4: &(&state.m4 * &resp.P).compress(),
  232. P5: &(&state.m5 * &resp.P).compress(),
  233. }
  234. )?;
  235. Ok(Credential5 {
  236. P: resp.P,
  237. Q: resp.Q,
  238. m1: state.m1,
  239. m2: state.m2,
  240. m3: state.m3,
  241. m4: state.m4,
  242. m5: state.m5,
  243. })
  244. }
  245. }
  246. // A submodule for issuing credentials with 5 attributes, of which
  247. // attributes 1, 2, and 4 are blinded (the issuer does not see them),
  248. // and only attributes 3 and 5 are visible to the issuer.
  249. //
  250. // One might imagine generalizing this submodule using a macro.
  251. // Currently, the number of attributes and the selection of which are
  252. // blinded have to be hardcoded in order to use the very helpful zkp
  253. // proof macros. This shouldn't be a problem in practice, as one
  254. // generally knows the set of statements one will require at compile,
  255. // and not at run, time.
  256. pub mod issue_blind124_5 {
  257. use curve25519_dalek::ristretto::RistrettoPoint;
  258. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  259. use curve25519_dalek::scalar::Scalar;
  260. use curve25519_dalek::traits::IsIdentity;
  261. use zkp::CompactProof;
  262. use zkp::ProofError;
  263. use zkp::Transcript;
  264. use super::{CMZ_A,CMZ_B,CMZ_A_TABLE,CMZ_B_TABLE};
  265. use super::{Issuer,IssuerPubKey,Credential5};
  266. // Example of a 5-attribute credential where the issuer sees attributes
  267. // 3 and 5, but attributes 1, 2, and 4 are blinded.
  268. pub struct CredentialRequest {
  269. D: RistrettoPoint,
  270. Encm1B: (RistrettoPoint, RistrettoPoint),
  271. Encm2B: (RistrettoPoint, RistrettoPoint),
  272. m3: Scalar,
  273. Encm4B: (RistrettoPoint, RistrettoPoint),
  274. m5: Scalar,
  275. piUserBlinding: CompactProof,
  276. }
  277. #[derive(Debug)]
  278. pub struct CredentialRequestState {
  279. d: Scalar,
  280. D: RistrettoPoint,
  281. Encm1B: (RistrettoPoint, RistrettoPoint),
  282. Encm2B: (RistrettoPoint, RistrettoPoint),
  283. Encm4B: (RistrettoPoint, RistrettoPoint),
  284. m1: Scalar,
  285. m2: Scalar,
  286. m3: Scalar,
  287. m4: Scalar,
  288. m5: Scalar,
  289. }
  290. pub struct CredentialResponse {
  291. P: RistrettoPoint,
  292. EncQ: (RistrettoPoint, RistrettoPoint),
  293. T1: RistrettoPoint,
  294. T2: RistrettoPoint,
  295. T4: RistrettoPoint,
  296. piBlindIssue: CompactProof,
  297. }
  298. // The client-created proof that the blinded attributes in the request
  299. // to issue a credential are well formed. If you want the client to
  300. // prove other statements about the blinded attributes (m1, m2, m4 in
  301. // this example), this is where to add them (and in the code that
  302. // creates and verifies this proof of course).
  303. define_proof! {
  304. userblinding,
  305. "Blind124 5 userblind proof",
  306. (d, e1, e2, e4, m1, m2, m4),
  307. (Encm1B0, Encm1B1, Encm2B0, Encm2B1, Encm4B0, Encm4B1, D),
  308. (B) :
  309. Encm1B0 = (e1*B),
  310. Encm1B1 = (m1*B + e1*D),
  311. Encm2B0 = (e2*B),
  312. Encm2B1 = (m2*B + e2*D),
  313. Encm4B0 = (e4*B),
  314. Encm4B1 = (m4*B + e4*D),
  315. D = (d*B)
  316. }
  317. // The issuer-created proof for the same scenario
  318. define_proof! {
  319. blindissue,
  320. "Blind124 5 issuing proof",
  321. (x0, x0tilde, x1, x2, x3, x4, x5, s, b, t1, t2, t4),
  322. (P, EncQ0, EncQ1, X0, X1, X2, X3, X4, X5, P3, P5, T1, T2, T4, D,
  323. Encm1B0, Encm1B1, Encm2B0, Encm2B1, Encm4B0, Encm4B1),
  324. (A, B) :
  325. X1 = (x1*A),
  326. X2 = (x2*A),
  327. X3 = (x3*A),
  328. X4 = (x4*A),
  329. X5 = (x5*A),
  330. X0 = (x0*B + x0tilde*A),
  331. P = (b*B),
  332. T1 = (b*X1),
  333. T2 = (b*X2),
  334. T4 = (b*X4),
  335. T1 = (t1*A),
  336. T2 = (t2*A),
  337. T4 = (t4*A),
  338. EncQ0 = (s*B + t1*Encm1B0 + t2*Encm2B0 + t4*Encm4B0),
  339. EncQ1 = (s*D + t1*Encm1B1 + t2*Encm2B1 + t4*Encm4B1 +
  340. x0*P + x3*P3 + x5*P5)
  341. }
  342. pub fn request(m1: &Scalar, m2: &Scalar, m3: &Scalar,
  343. m4: &Scalar, m5: &Scalar) -> (CredentialRequest,
  344. CredentialRequestState) {
  345. let B : &RistrettoPoint = &CMZ_B;
  346. let Btable : &RistrettoBasepointTable = &CMZ_B_TABLE;
  347. // Pick an ElGamal keypair
  348. let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
  349. let d: Scalar = Scalar::random(&mut rng);
  350. let D: RistrettoPoint = &d * Btable;
  351. let e1: Scalar = Scalar::random(&mut rng);
  352. let e2: Scalar = Scalar::random(&mut rng);
  353. let e4: Scalar = Scalar::random(&mut rng);
  354. let Encm1B = (&e1 * Btable, m1 * Btable + &e1 * D);
  355. let Encm2B = (&e2 * Btable, m2 * Btable + &e2 * D);
  356. let Encm4B = (&e4 * Btable, m4 * Btable + &e4 * D);
  357. let mut transcript = Transcript::new(b"Blind124 5 userblind proof");
  358. let piUserBlinding: CompactProof = userblinding::prove_compact(
  359. &mut transcript,
  360. userblinding::ProveAssignments {
  361. B: &B,
  362. Encm1B0: &Encm1B.0,
  363. Encm1B1: &Encm1B.1,
  364. Encm2B0: &Encm2B.0,
  365. Encm2B1: &Encm2B.1,
  366. Encm4B0: &Encm4B.0,
  367. Encm4B1: &Encm4B.1,
  368. D: &D,
  369. d: &d,
  370. e1: &e1,
  371. e2: &e2,
  372. e4: &e4,
  373. m1: &m1,
  374. m2: &m2,
  375. m4: &m4,
  376. }).0;
  377. (
  378. CredentialRequest {
  379. D: D,
  380. Encm1B, Encm2B, Encm4B, piUserBlinding,
  381. m3: *m3,
  382. m5: *m5,
  383. },
  384. CredentialRequestState {
  385. d, D, Encm1B, Encm2B, Encm4B,
  386. m1: *m1,
  387. m2: *m2,
  388. m3: *m3,
  389. m4: *m4,
  390. m5: *m5,
  391. }
  392. )
  393. }
  394. impl Issuer {
  395. // Issue a credential with 5 attributes, of which attributes 1, 2,
  396. // and 4 are blinded from the issuer, and 3 and 5 are visible.
  397. pub fn issue_blind124_5(&self, req: &CredentialRequest)
  398. -> Result<CredentialResponse, ProofError> {
  399. let A : &RistrettoPoint = &CMZ_A;
  400. let B : &RistrettoPoint = &CMZ_B;
  401. let Atable : &RistrettoBasepointTable = &CMZ_A_TABLE;
  402. let Btable : &RistrettoBasepointTable = &CMZ_B_TABLE;
  403. // First check the proof in the request
  404. let mut transcript = Transcript::new(b"Blind124 5 userblind proof");
  405. userblinding::verify_compact(
  406. &req.piUserBlinding,
  407. &mut transcript,
  408. userblinding::VerifyAssignments {
  409. B: &B.compress(),
  410. Encm1B0: &req.Encm1B.0.compress(),
  411. Encm1B1: &req.Encm1B.1.compress(),
  412. Encm2B0: &req.Encm2B.0.compress(),
  413. Encm2B1: &req.Encm2B.1.compress(),
  414. Encm4B0: &req.Encm4B.0.compress(),
  415. Encm4B1: &req.Encm4B.1.compress(),
  416. D: &req.D.compress(),
  417. }
  418. )?;
  419. // Compute the MAC on the visible attributes
  420. let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
  421. let b: Scalar = Scalar::random(&mut rng);
  422. let P: RistrettoPoint = &b * Btable;
  423. let QHc: RistrettoPoint = (self.privkey.x[0] + (
  424. self.privkey.x[3] * req.m3 +
  425. self.privkey.x[5] * req.m5)) * P;
  426. // El Gamal encrypt it to the public key req.D
  427. let s: Scalar = Scalar::random(&mut rng);
  428. let EncQHc = (&s*Btable, QHc + s*req.D);
  429. // Homomorphically compute the part of the MAC corresponding to
  430. // the blinded attributes
  431. let t1 = self.privkey.x[1] * b;
  432. let T1 = &t1 * Atable;
  433. let EncQ1 = ( t1 * req.Encm1B.0, t1 * req.Encm1B.1 );
  434. let t2 = self.privkey.x[2] * b;
  435. let T2 = &t2 * Atable;
  436. let EncQ2 = ( t2 * req.Encm2B.0, t2 * req.Encm2B.1 );
  437. let t4 = self.privkey.x[4] * b;
  438. let T4 = &t4 * Atable;
  439. let EncQ4 = ( t4 * req.Encm4B.0, t4 * req.Encm4B.1 );
  440. let EncQ = ( EncQHc.0 + EncQ1.0 + EncQ2.0 + EncQ4.0,
  441. EncQHc.1 + EncQ1.1 + EncQ2.1 + EncQ4.1 );
  442. let mut transcript = Transcript::new(b"Blind124 5 issuing proof");
  443. let piBlindIssue: CompactProof = blindissue::prove_compact(
  444. &mut transcript,
  445. blindissue::ProveAssignments {
  446. A: &A,
  447. B: &B,
  448. P: &P,
  449. EncQ0: &EncQ.0,
  450. EncQ1: &EncQ.1,
  451. X0: &self.pubkey.X[0],
  452. X1: &self.pubkey.X[1],
  453. X2: &self.pubkey.X[2],
  454. X3: &self.pubkey.X[3],
  455. X4: &self.pubkey.X[4],
  456. X5: &self.pubkey.X[5],
  457. P3: &(&req.m3 * &P),
  458. P5: &(&req.m5 * &P),
  459. T1: &T1,
  460. T2: &T2,
  461. T4: &T4,
  462. D: &req.D,
  463. Encm1B0: &req.Encm1B.0,
  464. Encm1B1: &req.Encm1B.1,
  465. Encm2B0: &req.Encm2B.0,
  466. Encm2B1: &req.Encm2B.1,
  467. Encm4B0: &req.Encm4B.0,
  468. Encm4B1: &req.Encm4B.1,
  469. x0: &self.privkey.x[0],
  470. x0tilde: &self.privkey.x0tilde,
  471. x1: &self.privkey.x[1],
  472. x2: &self.privkey.x[2],
  473. x3: &self.privkey.x[3],
  474. x4: &self.privkey.x[4],
  475. x5: &self.privkey.x[5],
  476. s: &s,
  477. b: &b,
  478. t1: &t1,
  479. t2: &t2,
  480. t4: &t4
  481. }).0;
  482. Ok(CredentialResponse {
  483. P, EncQ, T1, T2, T4, piBlindIssue
  484. })
  485. }
  486. }
  487. pub fn verify(state: CredentialRequestState,
  488. resp: CredentialResponse, pubkey: &IssuerPubKey)
  489. -> Result<Credential5, ProofError> {
  490. let A : &RistrettoPoint = &CMZ_A;
  491. let B : &RistrettoPoint = &CMZ_B;
  492. if resp.P.is_identity() {
  493. return Err(ProofError::VerificationFailure);
  494. }
  495. let mut transcript = Transcript::new(b"Blind124 5 issuing proof");
  496. blindissue::verify_compact(
  497. &resp.piBlindIssue,
  498. &mut transcript,
  499. blindissue::VerifyAssignments {
  500. A: &A.compress(),
  501. B: &B.compress(),
  502. P: &resp.P.compress(),
  503. EncQ0: &resp.EncQ.0.compress(),
  504. EncQ1: &resp.EncQ.1.compress(),
  505. X0: &pubkey.X[0].compress(),
  506. X1: &pubkey.X[1].compress(),
  507. X2: &pubkey.X[2].compress(),
  508. X3: &pubkey.X[3].compress(),
  509. X4: &pubkey.X[4].compress(),
  510. X5: &pubkey.X[5].compress(),
  511. P3: &(&state.m3 * &resp.P).compress(),
  512. P5: &(&state.m5 * &resp.P).compress(),
  513. T1: &resp.T1.compress(),
  514. T2: &resp.T2.compress(),
  515. T4: &resp.T4.compress(),
  516. D: &state.D.compress(),
  517. Encm1B0: &state.Encm1B.0.compress(),
  518. Encm1B1: &state.Encm1B.1.compress(),
  519. Encm2B0: &state.Encm2B.0.compress(),
  520. Encm2B1: &state.Encm2B.1.compress(),
  521. Encm4B0: &state.Encm4B.0.compress(),
  522. Encm4B1: &state.Encm4B.1.compress(),
  523. }
  524. )?;
  525. // Decrypt EncQ
  526. let Q = &resp.EncQ.1 - (state.d * &resp.EncQ.0);
  527. Ok(Credential5 {
  528. P: resp.P,
  529. Q: Q,
  530. m1: state.m1,
  531. m2: state.m2,
  532. m3: state.m3,
  533. m4: state.m4,
  534. m5: state.m5,
  535. })
  536. }
  537. }
  538. // A submodule for showing credentials with 5 attributes, blinding
  539. // attributes 3, 4, and 5, and displaying attributes 1 and 2.
  540. pub mod show_blind345_5 {
  541. use curve25519_dalek::ristretto::RistrettoPoint;
  542. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  543. use curve25519_dalek::scalar::Scalar;
  544. use curve25519_dalek::traits::IsIdentity;
  545. use zkp::CompactProof;
  546. use zkp::ProofError;
  547. use zkp::Transcript;
  548. use super::{CMZ_A,CMZ_A_TABLE,Issuer,IssuerPubKey,Credential5};
  549. // A typo in the Hyphae paper (Section 4.4): P must also be sent to
  550. // the issuer in the credential presentation message.
  551. pub struct ShowMessage {
  552. P: RistrettoPoint,
  553. m1: Scalar,
  554. m2: Scalar,
  555. Cm3: RistrettoPoint,
  556. Cm4: RistrettoPoint,
  557. Cm5: RistrettoPoint,
  558. CQ: RistrettoPoint,
  559. piCredShow: CompactProof,
  560. }
  561. #[derive(Debug)]
  562. pub struct VerifiedCredential {
  563. m1: Scalar,
  564. m2: Scalar,
  565. Cm3: RistrettoPoint,
  566. Cm4: RistrettoPoint,
  567. Cm5: RistrettoPoint,
  568. }
  569. // If you want to prove additional statements about the blinded
  570. // attributes when showing them, this is the place to add those
  571. // statements (and also the code that creates and verifies this
  572. // proof).
  573. define_proof! {
  574. show,
  575. "Blind345 5 showing proof",
  576. (m3, m4, m5, z3, z4, z5, negzQ),
  577. (P, Cm3, Cm4, Cm5, V, X3, X4, X5),
  578. (A) :
  579. Cm3 = (m3*P + z3*A),
  580. Cm4 = (m4*P + z4*A),
  581. Cm5 = (m5*P + z5*A),
  582. V = (z3*X3 + z4*X4 + z5*X5 + negzQ*A)
  583. }
  584. pub fn show(cred: &Credential5, pubkey: &IssuerPubKey)
  585. -> ShowMessage {
  586. let A : &RistrettoPoint = &CMZ_A;
  587. let Atable : &RistrettoBasepointTable = &CMZ_A_TABLE;
  588. // Reblind P and Q
  589. let mut rng: rand::rngs::ThreadRng = rand::thread_rng();
  590. let t: Scalar = Scalar::random(&mut rng);
  591. let P: RistrettoPoint = &t * &cred.P;
  592. let Q: RistrettoPoint = &t * &cred.Q;
  593. // Form Pedersen commitments to the blinded attributes
  594. let z3: Scalar = Scalar::random(&mut rng);
  595. let z4: Scalar = Scalar::random(&mut rng);
  596. let z5: Scalar = Scalar::random(&mut rng);
  597. let Cm3: RistrettoPoint = &cred.m3 * &P + &z3 * Atable;
  598. let Cm4: RistrettoPoint = &cred.m4 * &P + &z4 * Atable;
  599. let Cm5: RistrettoPoint = &cred.m5 * &P + &z5 * Atable;
  600. // Form a Pedersen commitment to the MAC Q
  601. // We flip the sign of zQ from that of the Hyphae paper so that
  602. // the ZKP has a "+" instead of a "-", as that's what the zkp
  603. // macro supports.
  604. let negzQ: Scalar = Scalar::random(&mut rng);
  605. let CQ: RistrettoPoint = &Q - &negzQ * Atable;
  606. // Compute the "error factor"
  607. let V: RistrettoPoint = &z3 * &pubkey.X[3]
  608. + &z4 * &pubkey.X[4] + &z5 * &pubkey.X[5]
  609. + &negzQ * Atable;
  610. // Create the ZKP
  611. let mut transcript = Transcript::new(b"Blind345 5 showing proof");
  612. let piCredShow: CompactProof = show::prove_compact(
  613. &mut transcript,
  614. show::ProveAssignments {
  615. A: &A,
  616. P: &P,
  617. Cm3: &Cm3,
  618. Cm4: &Cm4,
  619. Cm5: &Cm5,
  620. V: &V,
  621. X3: &pubkey.X[3],
  622. X4: &pubkey.X[4],
  623. X5: &pubkey.X[5],
  624. m3: &cred.m3,
  625. m4: &cred.m4,
  626. m5: &cred.m5,
  627. z3: &z3,
  628. z4: &z4,
  629. z5: &z5,
  630. negzQ: &negzQ
  631. }).0;
  632. ShowMessage {
  633. P,
  634. m1: cred.m1,
  635. m2: cred.m2,
  636. Cm3, Cm4, Cm5, CQ, piCredShow
  637. }
  638. }
  639. impl Issuer {
  640. // Verify a showing of an attribute from a user to the issuer
  641. // with 5 credentials, of which attributes 3, 4, and 5 are
  642. // blinded, and attributes 1 and 2 are revealed. The issuer
  643. // will end up with verified Pedersen commitments Cm3, Cm4, Cm5
  644. // to the blinded attributes, so that additional things can be
  645. // proved about those attributes in zero knowledge if desired.
  646. pub fn verify_blind345_5(&self, showmsg: &ShowMessage)
  647. -> Result<VerifiedCredential, ProofError> {
  648. let A : &RistrettoPoint = &CMZ_A;
  649. if showmsg.P.is_identity() {
  650. return Err(ProofError::VerificationFailure);
  651. }
  652. // Recompute the "error factor" using knowledge of our own
  653. // (the issuer's) private key instead of knowledge of the
  654. // hidden attributes
  655. let Vprime: RistrettoPoint =
  656. (self.privkey.x[0]
  657. + (self.privkey.x[1] * showmsg.m1
  658. + self.privkey.x[2] * showmsg.m2)) * showmsg.P
  659. + self.privkey.x[3] * &showmsg.Cm3
  660. + self.privkey.x[4] * &showmsg.Cm4
  661. + self.privkey.x[5] * &showmsg.Cm5
  662. - &showmsg.CQ;
  663. // Verify the ZKP using Vprime
  664. let mut transcript = Transcript::new(b"Blind345 5 showing proof");
  665. show::verify_compact(
  666. &showmsg.piCredShow,
  667. &mut transcript,
  668. show::VerifyAssignments {
  669. A: &A.compress(),
  670. P: &showmsg.P.compress(),
  671. Cm3: &showmsg.Cm3.compress(),
  672. Cm4: &showmsg.Cm4.compress(),
  673. Cm5: &showmsg.Cm5.compress(),
  674. V: &Vprime.compress(),
  675. X3: &self.pubkey.X[3].compress(),
  676. X4: &self.pubkey.X[4].compress(),
  677. X5: &self.pubkey.X[5].compress(),
  678. }
  679. )?;
  680. Ok(VerifiedCredential {
  681. m1: showmsg.m1,
  682. m2: showmsg.m2,
  683. Cm3: showmsg.Cm3,
  684. Cm4: showmsg.Cm4,
  685. Cm5: showmsg.Cm5,
  686. })
  687. }
  688. }
  689. }