This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
prover.rs
2418 lines (2061 loc) · 190 KB
/
prover.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use bn::BigNumber;
use cl::*;
use cl::constants::*;
use errors::IndyCryptoError;
use pair::*;
use super::helpers::*;
use cl::commitment::get_pedersen_commitment;
use cl::hash::get_hash_as_int;
use std::collections::{HashSet, BTreeMap, BTreeSet};
use std::iter::FromIterator;
/// Credentials owner that can proof and partially disclose the credentials to verifier.
pub struct Prover {}
impl Prover {
/// Creates a master secret.
///
/// # Example
/// ```
/// use indy_crypto::cl::prover::Prover;
///
/// let _master_secret = Prover::new_master_secret().unwrap();
/// ```
pub fn new_master_secret() -> Result<MasterSecret, IndyCryptoError> {
Ok(MasterSecret { ms: bn_rand(LARGE_MASTER_SECRET)? })
}
/// Creates blinded master secret for given issuer key and master secret.
///
/// # Arguments
/// * `credential_pub_key` - Credential public keys.
/// * `credential_key_correctness_proof` - Credential key correctness proof.
/// * `credential_values` - Credential values.
/// * `credential_nonce` - Nonce used for creation of blinded_credential_secrets_correctness_proof.
///
/// # Example
/// ```
/// use indy_crypto::cl::new_nonce;
/// use indy_crypto::cl::issuer::Issuer;
/// use indy_crypto::cl::prover::Prover;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("sex").unwrap();
/// let credential_schema = credential_schema_builder.finalize().unwrap();
///
/// let mut non_credential_schema_builder = Issuer::new_non_credential_schema_builder().unwrap();
/// non_credential_schema_builder.add_attr("master_secret").unwrap();
/// let non_credential_schema_elements = non_credential_schema_builder.finalize().unwrap();
///
/// let (credential_pub_key, _credential_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema_elements, false).unwrap();
///
/// let master_secret = Prover::new_master_secret().unwrap();
/// let credential_nonce = new_nonce().unwrap();
///
/// let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();
/// credential_values_builder.add_value_hidden("master_secret", &master_secret.value().unwrap()).unwrap();
/// let cred_values = credential_values_builder.finalize().unwrap();
///
/// let (_blinded_credential_secrets, _credential_secrets_blinding_factors, _blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&credential_pub_key,
/// &cred_key_correctness_proof,
/// &cred_values,
/// &credential_nonce).unwrap();
/// ```
pub fn blind_credential_secrets(credential_pub_key: &CredentialPublicKey,
credential_key_correctness_proof: &CredentialKeyCorrectnessProof,
credential_values: &CredentialValues,
credential_nonce: &Nonce) -> Result<(BlindedCredentialSecrets,
CredentialSecretsBlindingFactors,
BlindedCredentialSecretsCorrectnessProof), IndyCryptoError> {
trace!("Prover::blind_credential_secrets: >>> credential_pub_key: {:?}, \
credential_key_correctness_proof: {:?}, \
credential_values: {:?}, \
credential_nonce: {:?}",
credential_pub_key,
credential_key_correctness_proof,
credential_values,
credential_nonce
);
Prover::_check_credential_key_correctness_proof(&credential_pub_key.p_key, credential_key_correctness_proof)?;
let blinded_primary_credential_secrets =
Prover::_generate_blinded_primary_credential_secrets_factors(&credential_pub_key.p_key, &credential_values)?;
let blinded_revocation_credential_secrets = match credential_pub_key.r_key {
Some(ref r_pk) => Some(Prover::_generate_blinded_revocation_credential_secrets(r_pk)?),
_ => None
};
let blinded_credential_secrets_correctness_proof =
Prover::_new_blinded_credential_secrets_correctness_proof(&credential_pub_key.p_key,
&blinded_primary_credential_secrets,
&credential_nonce,
&credential_values)?;
let blinded_credential_secrets = BlindedCredentialSecrets {
u: blinded_primary_credential_secrets.u,
ur: blinded_revocation_credential_secrets.as_ref().map(|d| d.ur),
hidden_attributes: blinded_primary_credential_secrets.hidden_attributes,
committed_attributes: blinded_primary_credential_secrets.committed_attributes,
};
let credential_secrets_blinding_factors = CredentialSecretsBlindingFactors {
v_prime: blinded_primary_credential_secrets.v_prime,
vr_prime: blinded_revocation_credential_secrets.map(|d| d.vr_prime)
};
trace!("Prover::blind_credential_secrets: <<< blinded_credential_secrets: {:?}, \
credential_secrets_blinding_factors: {:?}, \
blinded_credential_secrets_correctness_proof: {:?},",
blinded_credential_secrets,
credential_secrets_blinding_factors,
blinded_credential_secrets_correctness_proof
);
Ok((
blinded_credential_secrets,
credential_secrets_blinding_factors,
blinded_credential_secrets_correctness_proof,
))
}
/// Updates the credential signature by a master secret blinding data.
///
/// # Arguments
/// * `credential_signature` - Credential signature generated by Issuer.
/// * `credential_values` - Credential values.
/// * `signature_correctness_proof` - Credential signature correctness proof.
/// * `credential_secrets_blinding_factors` - Master secret blinding data.
/// * `credential_pub_key` - Credential public key.
/// * `nonce` - Nonce was used by Issuer for the creation of signature_correctness_proof.
/// * `rev_key_pub` - (Optional) Revocation registry public key.
/// * `rev_reg` - (Optional) Revocation registry.
/// * `witness` - (Optional) Witness.
///
/// # Example
/// ```
/// use indy_crypto::cl::new_nonce;
/// use indy_crypto::cl::issuer::Issuer;
/// use indy_crypto::cl::prover::Prover;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("sex").unwrap();
/// let credential_schema = credential_schema_builder.finalize().unwrap();
///
/// let mut non_credential_schema_builder = Issuer::new_non_credential_schema_builder().unwrap();
/// non_credential_schema_builder.add_attr("master_secret").unwrap();
/// let non_credential_schema = non_credential_schema_builder.finalize().unwrap();
///
/// let (credential_pub_key, credential_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, false).unwrap();
///
/// let master_secret = Prover::new_master_secret().unwrap();
/// let credential_nonce = new_nonce().unwrap();
///
/// let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();
/// credential_values_builder.add_value_hidden("master_secret", &master_secret.value().unwrap()).unwrap();
/// credential_values_builder.add_dec_known("sex", "5944657099558967239210949258394887428692050081607692519917050011144233115103").unwrap();
/// let credential_values = credential_values_builder.finalize().unwrap();
///
/// let (blinded_credential_secrets, credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&credential_pub_key, &cred_key_correctness_proof, &credential_values, &credential_nonce).unwrap();
///
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let (mut credential_signature, signature_correctness_proof) =
/// Issuer::sign_credential("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &credential_values,
/// &credential_pub_key,
/// &credential_priv_key).unwrap();
///
/// Prover::process_credential_signature(&mut credential_signature,
/// &credential_values,
/// &signature_correctness_proof,
/// &credential_secrets_blinding_factors,
/// &credential_pub_key,
/// &credential_issuance_nonce,
/// None, None, None).unwrap();
/// ```
pub fn process_credential_signature(credential_signature: &mut CredentialSignature,
credential_values: &CredentialValues,
signature_correctness_proof: &SignatureCorrectnessProof,
credential_secrets_blinding_factors: &CredentialSecretsBlindingFactors,
credential_pub_key: &CredentialPublicKey,
nonce: &Nonce,
rev_key_pub: Option<&RevocationKeyPublic>,
rev_reg: Option<&RevocationRegistry>,
witness: Option<&Witness>) -> Result<(), IndyCryptoError> {
trace!("Prover::process_credential_signature: >>> credential_signature: {:?}, \
credential_values: {:?}, \
signature_correctness_proof: {:?}, \
credential_secrets_blinding_factors: {:?}, \
credential_pub_key: {:?}, \
nonce: {:?}, \
rev_key_pub: {:?}, \
rev_reg: {:?}, \
witness: {:?}",
credential_signature,
credential_values,
signature_correctness_proof,
credential_secrets_blinding_factors,
credential_pub_key,
nonce,
rev_key_pub,
rev_reg,
witness
);
Prover::_process_primary_credential(&mut credential_signature.p_credential, &credential_secrets_blinding_factors.v_prime)?;
Prover::_check_signature_correctness_proof(&credential_signature.p_credential,
credential_values,
signature_correctness_proof,
&credential_pub_key.p_key,
nonce)?;
if let (&mut Some(ref mut non_revocation_cred), Some(ref vr_prime), &Some(ref r_key),
Some(ref r_key_pub), Some(ref r_reg), Some(ref witness)) = (&mut credential_signature.r_credential,
credential_secrets_blinding_factors.vr_prime,
&credential_pub_key.r_key,
rev_key_pub,
rev_reg,
witness) {
Prover::_process_non_revocation_credential(non_revocation_cred,
vr_prime,
&r_key,
r_key_pub,
r_reg,
witness)?;
}
trace!("Prover::process_credential_signature: <<<");
Ok(())
}
/// Creates and returns proof builder.
///
/// The purpose of proof builder is building of proof entity according to the given request .
/// # Example
/// ```
/// use indy_crypto::cl::prover::Prover;
///
/// let _proof_builder = Prover::new_proof_builder();
pub fn new_proof_builder() -> Result<ProofBuilder, IndyCryptoError> {
Ok(ProofBuilder {
common_attributes: HashMap::new(),
init_proofs: Vec::new(),
c_list: Vec::new(),
tau_list: Vec::new()
})
}
#[cfg(test)]
pub fn check_credential_key_correctness_proof(pr_pub_key: &CredentialPrimaryPublicKey,
key_correctness_proof: &CredentialKeyCorrectnessProof) -> Result<(), IndyCryptoError> {
Prover::_check_credential_key_correctness_proof(pr_pub_key, key_correctness_proof)
}
fn _check_credential_key_correctness_proof(pr_pub_key: &CredentialPrimaryPublicKey,
key_correctness_proof: &CredentialKeyCorrectnessProof) -> Result<(), IndyCryptoError> {
trace!("Prover::_check_credential_key_correctness_proof: >>> pr_pub_key: {:?}, key_correctness_proof: {:?}",
pr_pub_key,
key_correctness_proof
);
let correctness_names: HashSet<&String> = HashSet::from_iter(key_correctness_proof.xr_cap.iter().map(|&(ref key, ref _v)| key));
for r_key in pr_pub_key.r.keys() {
if !correctness_names.contains(r_key) {
//V1 didn't include "master_secret" in the correctness proof
//so for now if this is the only missing key, its okay
//In the future this "if" statement should be removed
if r_key != "master_secret" {
return Err(IndyCryptoError::InvalidStructure(format!("Value by key '{}' not found in key_correctness_proof.xr_cap", r_key)));
}
}
}
for correctness_name in &correctness_names {
if !pr_pub_key.r.contains_key(correctness_name.as_str()) {
return Err(IndyCryptoError::InvalidStructure(format!("Public key doesn't contains item for {} key in key_correctness_proof.xr_cap", correctness_name)));
}
}
let mut ctx = BigNumber::new_context()?;
let z_inverse = pr_pub_key.z.inverse(&pr_pub_key.n, Some(&mut ctx))?;
let z_cap = get_pedersen_commitment(
&z_inverse,
&key_correctness_proof.c,
&pr_pub_key.s,
&key_correctness_proof.xz_cap,
&pr_pub_key.n,
&mut ctx,
)?;
let mut ordered_r_values = Vec::new();
let mut ordered_r_cap_values = Vec::new();
for &(ref key, ref xr_cap_value) in &key_correctness_proof.xr_cap {
let r_value = &pr_pub_key.r[key];
ordered_r_values.push(r_value.clone()?);
let r_inverse = r_value.inverse(&pr_pub_key.n, Some(&mut ctx))?;
let val = get_pedersen_commitment(&r_inverse, &key_correctness_proof.c,
&pr_pub_key.s, &xr_cap_value, &pr_pub_key.n, &mut ctx)?;
ordered_r_cap_values.push(val);
}
let mut values: Vec<u8> = Vec::new();
values.extend_from_slice(&pr_pub_key.z.to_bytes()?);
for val in ordered_r_values {
values.extend_from_slice(&val.to_bytes()?);
}
values.extend_from_slice(&z_cap.to_bytes()?);
for val in ordered_r_cap_values {
values.extend_from_slice(&val.to_bytes()?);
}
let c = get_hash_as_int(&mut vec![values])?;
let valid = key_correctness_proof.c.eq(&c);
if !valid {
return Err(IndyCryptoError::InvalidStructure(format!("Invalid Credential key correctness proof")));
}
trace!("Prover::_check_credential_key_correctness_proof: <<<");
Ok(())
}
fn _generate_blinded_primary_credential_secrets_factors(p_pub_key: &CredentialPrimaryPublicKey,
credential_values: &CredentialValues) -> Result<PrimaryBlindedCredentialSecretsFactors, IndyCryptoError> {
trace!("Prover::_generate_blinded_primary_credential_secrets_factors: >>> p_pub_key: {:?}, credential_values: {:?}",
p_pub_key,
credential_values
);
let mut ctx = BigNumber::new_context()?;
let v_prime = bn_rand(LARGE_VPRIME)?;
//Hidden attributes are combined in this value
let hidden_attributes = credential_values
.attrs_values
.iter()
.filter(|&(_, v)| v.is_hidden())
.map(|(attr, _)| attr.clone())
.collect::<BTreeSet<String>>();
let u = hidden_attributes.iter().fold(
p_pub_key.s.mod_exp(
&v_prime,
&p_pub_key.n,
Some(&mut ctx),
),
|acc, attr| {
let pk_r = p_pub_key.r.get(&attr.clone()).ok_or(
IndyCryptoError::InvalidStructure(
format!("Value by key '{}' not found in pk.r", attr),
),
)?;
let cred_value = &credential_values.attrs_values[attr];
acc?.mod_mul(
&pk_r.mod_exp(
cred_value.value(),
&p_pub_key.n,
Some(&mut ctx),
)?,
&p_pub_key.n,
Some(&mut ctx),
)
},
)?;
let mut committed_attributes = BTreeMap::new();
for (attr, cv) in credential_values.attrs_values.iter().filter(|&(_, v)| v.is_commitment()) {
if let &CredentialValue::Commitment { ref value, ref blinding_factor } = cv {
committed_attributes.insert(
attr.clone(),
get_pedersen_commitment(
&p_pub_key.s,
blinding_factor,
&p_pub_key.z,
value,
&p_pub_key.n,
&mut ctx,
)?,
);
}
}
let primary_blinded_cred_secrets = PrimaryBlindedCredentialSecretsFactors {
u,
v_prime,
hidden_attributes,
committed_attributes,
};
trace!("Prover::_generate_blinded_primary_credential_secrets_factors: <<< primary_blinded_cred_secrets: {:?}", primary_blinded_cred_secrets);
Ok(primary_blinded_cred_secrets)
}
fn _generate_blinded_revocation_credential_secrets(r_pub_key: &CredentialRevocationPublicKey) -> Result<RevocationBlindedCredentialSecretsFactors, IndyCryptoError> {
trace!("Prover::_generate_blinded_revocation_credential_secrets: >>> r_pub_key: {:?}", r_pub_key);
let vr_prime = GroupOrderElement::new()?;
let ur = r_pub_key.h2.mul(&vr_prime)?;
let revocation_blinded_credential_secrets = RevocationBlindedCredentialSecretsFactors { ur, vr_prime };
trace!("Prover::_generate_blinded_revocation_credential_secrets: <<< revocation_blinded_credential_secrets: {:?}", revocation_blinded_credential_secrets);
Ok(revocation_blinded_credential_secrets)
}
fn _new_blinded_credential_secrets_correctness_proof(p_pub_key: &CredentialPrimaryPublicKey,
blinded_primary_credential_secrets: &PrimaryBlindedCredentialSecretsFactors,
nonce: &BigNumber,
credential_values: &CredentialValues) -> Result<BlindedCredentialSecretsCorrectnessProof, IndyCryptoError> {
trace!("Prover::_new_blinded_credential_secrets_correctness_proof: >>> p_pub_key: {:?}, \
blinded_primary_credential_secrets: {:?}, \
nonce: {:?}, \
credential_values: {:?}",
blinded_primary_credential_secrets,
nonce,
p_pub_key,
credential_values);
let mut ctx = BigNumber::new_context()?;
let v_dash_tilde = bn_rand(LARGE_VPRIME_TILDE)?;
let mut m_tildes = BTreeMap::new();
let mut r_tildes = BTreeMap::new();
let mut values: Vec<u8> = Vec::new();
let mut u_tilde = p_pub_key.s.mod_exp(
&v_dash_tilde,
&p_pub_key.n,
Some(&mut ctx),
)?;
for (attr, cred_value) in credential_values.attrs_values
.iter()
.filter(|&(_, v)| v.is_hidden() || v.is_commitment()) {
let m_tilde = bn_rand(LARGE_MTILDE)?;
let pk_r = p_pub_key.r.get(attr).ok_or(
IndyCryptoError::InvalidStructure(
format!(
"Value by key '{}' not found in pk.r",
attr
),
),
)?;
match *cred_value {
CredentialValue::Hidden { .. } => {
u_tilde = u_tilde.mod_mul(
&pk_r.mod_exp(&m_tilde, &p_pub_key.n, Some(&mut ctx))?,
&p_pub_key.n,
Some(&mut ctx),
)?;
()
}
CredentialValue::Commitment { .. } => {
let r_tilde = bn_rand(LARGE_MTILDE)?;
let commitment_tilde = get_pedersen_commitment(
&p_pub_key.z,
&m_tilde,
&p_pub_key.s,
&r_tilde,
&p_pub_key.n,
&mut ctx,
)?;
r_tildes.insert(attr.clone(), r_tilde);
values.extend_from_slice(&commitment_tilde.to_bytes()?);
let ca_value = blinded_primary_credential_secrets.committed_attributes
.get(attr)
.ok_or(IndyCryptoError::InvalidStructure(format!("Value by key '{}' not found in primary_blinded_cred_secrets.committed_attributes", attr)))?;
values.extend_from_slice(&ca_value.to_bytes()?);
()
}
_ => (),
}
m_tildes.insert(attr.clone(), m_tilde);
}
values.extend_from_slice(&blinded_primary_credential_secrets.u.to_bytes()?);
values.extend_from_slice(&u_tilde.to_bytes()?);
values.extend_from_slice(&nonce.to_bytes()?);
let c = get_hash_as_int(&vec![values])?;
let v_dash_cap = c.mul(&blinded_primary_credential_secrets.v_prime, Some(&mut ctx))?
.add(&v_dash_tilde)?;
let mut m_caps = BTreeMap::new();
let mut r_caps = BTreeMap::new();
for (attr, m_tilde) in &m_tildes {
let ca = credential_values.attrs_values.get(attr).ok_or(
IndyCryptoError::InvalidStructure(format!(
"Value by key '{}' not found in cred_values.committed_attributes",
attr
)),
)?;
match ca {
&CredentialValue::Hidden { ref value } => {
let m_cap = m_tilde.add(&c.mul(value, Some(&mut ctx))?)?;
m_caps.insert(attr.clone(), m_cap);
()
}
&CredentialValue::Commitment {
ref value,
ref blinding_factor,
} => {
let m_cap = m_tilde.add(&c.mul(value, Some(&mut ctx))?)?;
let r_cap = r_tildes[attr].add(&c.mul(blinding_factor, Some(&mut ctx))?)?;
m_caps.insert(attr.clone(), m_cap);
r_caps.insert(attr.clone(), r_cap);
()
}
_ => (),
}
}
let blinded_credential_secrets_correctness_proof =
BlindedCredentialSecretsCorrectnessProof {
c,
v_dash_cap,
m_caps,
r_caps,
};
trace!("Prover::_new_blinded_credential_secrets_correctness_proof: <<< blinded_primary_master_secret_correctness_proof: {:?}", blinded_credential_secrets_correctness_proof);
Ok(blinded_credential_secrets_correctness_proof)
}
fn _process_primary_credential(p_cred: &mut PrimaryCredentialSignature,
v_prime: &BigNumber) -> Result<(), IndyCryptoError> {
trace!("Prover::_process_primary_credential: >>> p_cred: {:?}, v_prime: {:?}", p_cred, v_prime);
p_cred.v = v_prime.add(&p_cred.v)?;
trace!("Prover::_process_primary_credential: <<<");
Ok(())
}
fn _process_non_revocation_credential(r_cred: &mut NonRevocationCredentialSignature,
vr_prime: &GroupOrderElement,
cred_rev_pub_key: &CredentialRevocationPublicKey,
rev_key_pub: &RevocationKeyPublic,
rev_reg: &RevocationRegistry,
witness: &Witness) -> Result<(), IndyCryptoError> {
trace!("Prover::_process_non_revocation_credential: >>> r_cred: {:?}, vr_prime: {:?}, cred_rev_pub_key: {:?}, rev_reg: {:?}, rev_key_pub: {:?}",
r_cred, vr_prime, cred_rev_pub_key, rev_reg, rev_key_pub);
let r_cnxt_m2 = BigNumber::from_bytes(&r_cred.m2.to_bytes()?)?;
r_cred.vr_prime_prime = vr_prime.add_mod(&r_cred.vr_prime_prime)?;
Prover::_test_witness_signature(&r_cred, cred_rev_pub_key, rev_key_pub, rev_reg, witness, &r_cnxt_m2)?;
trace!("Prover::_process_non_revocation_credential: <<<");
Ok(())
}
fn _check_signature_correctness_proof(p_cred_sig: &PrimaryCredentialSignature,
cred_values: &CredentialValues,
signature_correctness_proof: &SignatureCorrectnessProof,
p_pub_key: &CredentialPrimaryPublicKey,
nonce: &Nonce) -> Result<(), IndyCryptoError> {
trace!("Prover::_check_signature_correctness_proof: >>> p_cred_sig: {:?}, \
cred_values: {:?}, \
signature_correctness_proof: {:?}, \
p_pub_key: {:?}, \
nonce: {:?}",
p_cred_sig,
cred_values,
signature_correctness_proof,
p_pub_key,
nonce
);
let mut ctx = BigNumber::new_context()?;
if !p_cred_sig.e.is_prime(Some(&mut ctx))? {
return Err(IndyCryptoError::InvalidStructure(format!("Invalid Signature correctness proof")));
}
if let Some((ref attr, _)) = cred_values.attrs_values
.iter()
.find(|&(ref attr, ref value)|
(value.is_known() || value.is_hidden()) && !p_pub_key.r.contains_key(attr.clone())) {
return Err(IndyCryptoError::InvalidStructure(format!("Value by key '{}' not found in public key", attr)));
}
let rx = cred_values
.attrs_values
.iter()
.filter(|&(ref attr, ref value)| {
(value.is_known() || value.is_hidden()) && p_pub_key.r.contains_key(attr.clone())
})
.fold(
get_pedersen_commitment(
&p_pub_key.s,
&p_cred_sig.v,
&p_pub_key.rctxt,
&p_cred_sig.m_2,
&p_pub_key.n,
&mut ctx,
),
|acc, (attr, value)| {
acc?.mod_mul(
&p_pub_key.r[&attr.clone()].mod_exp(
value.value(),
&p_pub_key.n,
Some(&mut ctx),
)?,
&p_pub_key.n,
Some(&mut ctx),
)
},
)?;
let q = p_pub_key.z.mod_div(&rx, &p_pub_key.n, Some(&mut ctx))?;
let expected_q = p_cred_sig.a.mod_exp(&p_cred_sig.e, &p_pub_key.n, Some(&mut ctx))?;
if !q.eq(&expected_q) {
return Err(IndyCryptoError::InvalidStructure(format!("Invalid Signature correctness proof q != q'")));
}
let degree = signature_correctness_proof.c.add(
&signature_correctness_proof.se.mul(&p_cred_sig.e, Some(&mut ctx))?
)?;
let a_cap = p_cred_sig.a.mod_exp(°ree, &p_pub_key.n, Some(&mut ctx))?;
let mut values: Vec<u8> = Vec::new();
values.extend_from_slice(&q.to_bytes()?);
values.extend_from_slice(&p_cred_sig.a.to_bytes()?);
values.extend_from_slice(&a_cap.to_bytes()?);
values.extend_from_slice(&nonce.to_bytes()?);
let c = get_hash_as_int(&vec![values])?;
let valid = signature_correctness_proof.c.eq(&c);
if !valid {
return Err(IndyCryptoError::InvalidStructure(format!("Invalid Signature correctness proof c != c'")));
}
trace!("Prover::_check_signature_correctness_proof: <<<");
Ok(())
}
fn _test_witness_signature(r_cred: &NonRevocationCredentialSignature,
cred_rev_pub_key: &CredentialRevocationPublicKey,
rev_key_pub: &RevocationKeyPublic,
rev_reg: &RevocationRegistry,
witness: &Witness,
r_cnxt_m2: &BigNumber) -> Result<(), IndyCryptoError> {
trace!("Prover::_test_witness_signature: >>> r_cred: {:?}, cred_rev_pub_key: {:?}, rev_key_pub: {:?}, rev_reg: {:?}, r_cnxt_m2: {:?}",
r_cred, cred_rev_pub_key, rev_key_pub, rev_reg, r_cnxt_m2);
let z_calc = Pair::pair(&r_cred.witness_signature.g_i, &rev_reg.accum)?
.mul(&Pair::pair(&cred_rev_pub_key.g, &witness.omega)?.inverse()?)?;
if z_calc != rev_key_pub.z {
return Err(IndyCryptoError::InvalidStructure("Issuer is sending incorrect data".to_string()));
}
let pair_gg_calc = Pair::pair(&cred_rev_pub_key.pk.add(&r_cred.g_i)?, &r_cred.witness_signature.sigma_i)?;
let pair_gg = Pair::pair(&cred_rev_pub_key.g, &cred_rev_pub_key.g_dash)?;
if pair_gg_calc != pair_gg {
return Err(IndyCryptoError::InvalidStructure("Issuer is sending incorrect data".to_string()));
}
let m2 = GroupOrderElement::from_bytes(&r_cnxt_m2.to_bytes()?)?;
let pair_h1 = Pair::pair(&r_cred.sigma, &cred_rev_pub_key.y.add(&cred_rev_pub_key.h_cap.mul(&r_cred.c)?)?)?;
let pair_h2 = Pair::pair(
&cred_rev_pub_key.h0
.add(&cred_rev_pub_key.h1.mul(&m2)?)?
.add(&cred_rev_pub_key.h2.mul(&r_cred.vr_prime_prime)?)?
.add(&r_cred.g_i)?,
&cred_rev_pub_key.h_cap
)?;
if pair_h1 != pair_h2 {
return Err(IndyCryptoError::InvalidStructure("Issuer is sending incorrect data".to_string()));
}
trace!("Prover::_test_witness_signature: <<<");
Ok(())
}
}
#[derive(Debug)]
pub struct ProofBuilder {
common_attributes: HashMap<String, BigNumber>,
init_proofs: Vec<InitProof>,
c_list: Vec<Vec<u8>>,
tau_list: Vec<Vec<u8>>,
}
impl ProofBuilder {
/// Creates m_tildes for attributes that will be the same across all subproofs
pub fn add_common_attribute(&mut self, attr_name: &str) -> Result<(), IndyCryptoError> {
self.common_attributes.insert(attr_name.to_owned(), bn_rand(LARGE_MVECT)?);
Ok(())
}
/// Adds sub proof request to proof builder which will be used fo building of proof.
/// Part of proof request related to a particular schema-key.
/// The order of sub-proofs is important: both Prover and Verifier should use the same order.
///
/// # Arguments
/// * `proof_builder` - Proof builder.
/// * `sub_proof_request` -Requested attributes and predicates.
/// * `credential_schema` - Credential schema.
/// * `credential_signature` - Credential signature.
/// * `credential_values` - Credential values.
/// * `credential_pub_key` - Credential public key.
/// * `rev_reg_pub` - (Optional) Revocation registry public.
///
/// #Example
/// ```
/// use indy_crypto::cl::new_nonce;
/// use indy_crypto::cl::issuer::Issuer;
/// use indy_crypto::cl::prover::Prover;
/// use indy_crypto::cl::verifier::Verifier;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("sex").unwrap();
/// let credential_schema = credential_schema_builder.finalize().unwrap();
///
/// let mut non_credential_schema_builder = Issuer::new_non_credential_schema_builder().unwrap();
/// non_credential_schema_builder.add_attr("master_secret").unwrap();
/// let non_credential_schema = non_credential_schema_builder.finalize().unwrap();
///
/// let (credential_pub_key, credential_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, false).unwrap();
///
/// let master_secret = Prover::new_master_secret().unwrap();
/// let credential_nonce = new_nonce().unwrap();
///
/// let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();
/// credential_values_builder.add_value_hidden("master_secret", &master_secret.value().unwrap()).unwrap();
/// credential_values_builder.add_dec_known("sex", "5944657099558967239210949258394887428692050081607692519917050011144233115103").unwrap();
/// let credential_values = credential_values_builder.finalize().unwrap();
///
/// let (blinded_credential_secrets, credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&credential_pub_key, &cred_key_correctness_proof, &credential_values, &credential_nonce).unwrap();
///
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let (mut credential_signature, signature_correctness_proof) =
/// Issuer::sign_credential("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &credential_values,
/// &credential_pub_key,
/// &credential_priv_key).unwrap();
///
/// Prover::process_credential_signature(&mut credential_signature,
/// &credential_values,
/// &signature_correctness_proof,
/// &credential_secrets_blinding_factors,
/// &credential_pub_key,
/// &credential_issuance_nonce,
/// None, None, None).unwrap();
///
/// let mut sub_proof_request_builder = Verifier::new_sub_proof_request_builder().unwrap();
/// sub_proof_request_builder.add_revealed_attr("sex").unwrap();
/// let sub_proof_request = sub_proof_request_builder.finalize().unwrap();
///
/// let mut proof_builder = Prover::new_proof_builder().unwrap();
/// proof_builder.add_common_attribute("master_secret").unwrap();
/// proof_builder.add_sub_proof_request(&sub_proof_request,
/// &credential_schema,
/// &non_credential_schema,
/// &credential_signature,
/// &credential_values,
/// &credential_pub_key,
/// None,
/// None).unwrap();
/// ```
pub fn add_sub_proof_request(&mut self,
sub_proof_request: &SubProofRequest,
credential_schema: &CredentialSchema,
non_credential_schema: &NonCredentialSchema,
credential_signature: &CredentialSignature,
credential_values: &CredentialValues,
credential_pub_key: &CredentialPublicKey,
rev_reg: Option<&RevocationRegistry>,
witness: Option<&Witness>) -> Result<(), IndyCryptoError> {
trace!("ProofBuilder::add_sub_proof_request: >>> sub_proof_request: {:?}, \
credential_schema: {:?}, \
non_credential_schema: {:?}, \
credential_signature: {:?}, \
credential_values: {:?}, \
credential_pub_key: {:?}, \
rev_reg: {:?}, \
witness: {:?}",
sub_proof_request,
credential_schema,
non_credential_schema,
credential_signature,
credential_values,
credential_pub_key,
rev_reg,
witness);
ProofBuilder::_check_add_sub_proof_request_params_consistency(
credential_values,
sub_proof_request,
credential_schema,
non_credential_schema,
)?;
let mut non_revoc_init_proof = None;
let mut m2_tilde: Option<BigNumber> = None;
if let (&Some(ref r_cred), &Some(ref r_reg), &Some(ref r_pub_key), &Some(ref witness)) = (&credential_signature.r_credential,
&rev_reg,
&credential_pub_key.r_key,
&witness) {
let proof = ProofBuilder::_init_non_revocation_proof(&r_cred,
&r_reg,
&r_pub_key,
&witness)?;
self.c_list.extend_from_slice(&proof.as_c_list()?);
self.tau_list.extend_from_slice(&proof.as_tau_list()?);
m2_tilde = Some(group_element_to_bignum(&proof.tau_list_params.m2)?);
non_revoc_init_proof = Some(proof);
}
let primary_init_proof = ProofBuilder::_init_primary_proof(&self.common_attributes,
&credential_pub_key.p_key,
&credential_signature.p_credential,
credential_values,
credential_schema,
non_credential_schema,
sub_proof_request,
m2_tilde)?;
self.c_list.extend_from_slice(&primary_init_proof.as_c_list()?);
self.tau_list.extend_from_slice(&primary_init_proof.as_tau_list()?);
let init_proof = InitProof {
primary_init_proof,
non_revoc_init_proof,
credential_values: credential_values.clone()?,
sub_proof_request: sub_proof_request.clone(),
credential_schema: credential_schema.clone(),
non_credential_schema: non_credential_schema.clone(),
};
self.init_proofs.push(init_proof);
trace!("ProofBuilder::add_sub_proof_request: <<<");
Ok(())
}
/// Finalize proof.
///
/// # Arguments
/// * `proof_builder` - Proof builder.
/// * `nonce` - Nonce.
///
/// #Example
/// ```
/// use indy_crypto::cl::new_nonce;
/// use indy_crypto::cl::issuer::Issuer;
/// use indy_crypto::cl::prover::Prover;
/// use indy_crypto::cl::verifier::Verifier;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("sex").unwrap();
/// let credential_schema = credential_schema_builder.finalize().unwrap();
///
/// let mut non_credential_schema_builder = Issuer::new_non_credential_schema_builder().unwrap();
/// non_credential_schema_builder.add_attr("master_secret").unwrap();
/// let non_credential_schema = non_credential_schema_builder.finalize().unwrap();
///
/// let (credential_pub_key, credential_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, false).unwrap();
///
/// let master_secret = Prover::new_master_secret().unwrap();
///
/// let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();
/// credential_values_builder.add_value_hidden("master_secret", &master_secret.value().unwrap());
/// credential_values_builder.add_dec_known("sex", "5944657099558967239210949258394887428692050081607692519917050011144233115103").unwrap();
/// let credential_values = credential_values_builder.finalize().unwrap();
///
/// let credential_nonce = new_nonce().unwrap();
///
/// let credential_nonce = new_nonce().unwrap();
/// let (blinded_credential_secrets, credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&credential_pub_key, &cred_key_correctness_proof, &credential_values, &credential_nonce).unwrap();
///
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let (mut credential_signature, signature_correctness_proof) =
/// Issuer::sign_credential("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &credential_values,
/// &credential_pub_key,
/// &credential_priv_key).unwrap();
///
/// Prover::process_credential_signature(&mut credential_signature,
/// &credential_values,
/// &signature_correctness_proof,
/// &credential_secrets_blinding_factors,
/// &credential_pub_key,
/// &credential_issuance_nonce,
/// None, None, None).unwrap();
///
/// let mut sub_proof_request_builder = Verifier::new_sub_proof_request_builder().unwrap();
/// sub_proof_request_builder.add_revealed_attr("sex").unwrap();
/// let sub_proof_request = sub_proof_request_builder.finalize().unwrap();
///
/// let mut proof_builder = Prover::new_proof_builder().unwrap();
/// proof_builder.add_common_attribute("master_secret").unwrap();
/// proof_builder.add_sub_proof_request(&sub_proof_request,
/// &credential_schema,
/// &non_credential_schema,
/// &credential_signature,
/// &credential_values,
/// &credential_pub_key,
/// None,
/// None).unwrap();
///
/// let proof_request_nonce = new_nonce().unwrap();
/// let _proof = proof_builder.finalize(&proof_request_nonce).unwrap();
/// ```
pub fn finalize(&self, nonce: &Nonce) -> Result<Proof, IndyCryptoError> {
trace!("ProofBuilder::finalize: >>> nonce: {:?}", nonce);
let mut values: Vec<Vec<u8>> = Vec::new();
values.extend_from_slice(&self.tau_list);
values.extend_from_slice(&self.c_list);
values.push(nonce.to_bytes()?);
// In the anoncreds whitepaper, `challenge` is denoted by `c_h`
let challenge = get_hash_as_int(&values)?;
let mut proofs: Vec<SubProof> = Vec::new();
for init_proof in self.init_proofs.iter() {
let mut non_revoc_proof: Option<NonRevocProof> = None;
if let Some(ref non_revoc_init_proof) = init_proof.non_revoc_init_proof {
non_revoc_proof = Some(ProofBuilder::_finalize_non_revocation_proof(&non_revoc_init_proof, &challenge)?);
}
let primary_proof = ProofBuilder::_finalize_primary_proof(
&init_proof.primary_init_proof,
&challenge,
&init_proof.credential_schema,
&init_proof.non_credential_schema,
&init_proof.credential_values,
&init_proof.sub_proof_request,
)?;
let proof = SubProof { primary_proof, non_revoc_proof };
proofs.push(proof);
}
let aggregated_proof = AggregatedProof { c_hash: challenge, c_list: self.c_list.clone() };
let proof = Proof { proofs, aggregated_proof };
trace!("ProofBuilder::finalize: <<< proof: {:?}", proof);
Ok(proof)
}
fn _check_add_sub_proof_request_params_consistency(
cred_values: &CredentialValues,
sub_proof_request: &SubProofRequest,
cred_schema: &CredentialSchema,
non_credential_schema: &NonCredentialSchema,