ggm.rs 27 KB

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