This repository has been archived by the owner on Jul 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
issuer.rs
1479 lines (1244 loc) · 97 KB
/
issuer.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 errors::HLCryptoError;
use pair::*;
use cl::constants::*;
use cl::helpers::*;
use utils::commitment::*;
use utils::get_hash_as_int;
use std::collections::{HashMap, HashSet};
/// Trust source that provides credentials to prover.
pub struct Issuer {}
impl Issuer {
/// Creates and returns credential schema entity builder.
///
/// The purpose of credential schema builder is building of credential schema entity that
/// represents credential schema attributes set.
///
/// # Example
/// ```
/// use hl_crypto::cl::issuer::Issuer;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("sex").unwrap();
/// credential_schema_builder.add_attr("name").unwrap();
/// let _credential_schema = credential_schema_builder.finalize().unwrap();
/// ```
pub fn new_credential_schema_builder() -> Result<CredentialSchemaBuilder, HLCryptoError> {
let res = CredentialSchemaBuilder::new()?;
Ok(res)
}
pub fn new_non_credential_schema_builder() -> Result<NonCredentialSchemaBuilder, HLCryptoError> {
NonCredentialSchemaBuilder::new()
}
/// Creates and returns credential definition (public and private keys, correctness proof) entities.
///
/// # Arguments
/// * `credential_schema` - Credential schema entity.
/// * `support_revocation` - If true non revocation part of keys will be generated.
///
/// # Example
/// ```
/// use hl_crypto::cl::issuer::Issuer;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("name").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 (_cred_pub_key, _cred_priv_key, _cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();
/// ```
pub fn new_credential_def(credential_schema: &CredentialSchema,
non_credential_schema: &NonCredentialSchema,
support_revocation: bool) -> Result<(CredentialPublicKey,
CredentialPrivateKey,
CredentialKeyCorrectnessProof), HLCryptoError> {
trace!("Issuer::new_credential_def: >>> credential_schema: {:?}, support_revocation: {:?}", credential_schema, support_revocation);
let (p_pub_key, p_priv_key, p_key_meta) =
Issuer::_new_credential_primary_keys(credential_schema, non_credential_schema)?;
let (r_pub_key, r_priv_key) = if support_revocation {
Issuer::_new_credential_revocation_keys()
.map(|(r_pub_key, r_priv_key)| (Some(r_pub_key), Some(r_priv_key)))?
} else {
(None, None)
};
let cred_pub_key = CredentialPublicKey { p_key: p_pub_key, r_key: r_pub_key };
let cred_priv_key = CredentialPrivateKey { p_key: p_priv_key, r_key: r_priv_key };
let cred_key_correctness_proof =
Issuer::_new_credential_key_correctness_proof(&cred_pub_key.p_key,
&cred_priv_key.p_key,
&p_key_meta)?;
trace!("Issuer::new_credential_def: <<< cred_pub_key: {:?}, cred_priv_key: {:?}, cred_key_correctness_proof: {:?}",
cred_pub_key, secret!(&cred_priv_key), cred_key_correctness_proof);
Ok((cred_pub_key, cred_priv_key, cred_key_correctness_proof))
}
/// Creates and returns revocation registry definition (public and private keys, accumulator and tails generator) entities.
///
/// # Arguments
/// * `credential_pub_key` - Credential public key entity.
/// * `max_cred_num` - Max credential number in generated registry.
/// * `issuance_by_default` - Type of issuance.
/// If true all indices are assumed to be issued and initial accumulator is calculated over all indices
/// If false nothing is issued initially accumulator is 1
///
/// # Example
/// ```
/// use hl_crypto::cl::issuer::Issuer;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("name").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 (_cred_pub_key, _cred_priv_key, _cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();
///
/// let (_rev_key_pub, _rev_key_priv, _rev_reg, _rev_tails_generator) = Issuer::new_revocation_registry_def(&_cred_pub_key, 5, false).unwrap();
/// ```
pub fn new_revocation_registry_def(credential_pub_key: &CredentialPublicKey,
max_cred_num: u32,
issuance_by_default: bool) -> Result<(RevocationKeyPublic,
RevocationKeyPrivate,
RevocationRegistry,
RevocationTailsGenerator), HLCryptoError> {
trace!("Issuer::new_revocation_registry_def: >>> credential_pub_key: {:?}, max_cred_num: {:?}, issuance_by_default: {:?}",
credential_pub_key, max_cred_num, issuance_by_default);
let cred_rev_pub_key: &CredentialRevocationPublicKey = credential_pub_key.r_key
.as_ref()
.ok_or(HLCryptoError::InvalidStructure(format!("There are not revocation keys in the credential public key.")))?;
let (rev_key_pub, rev_key_priv) = Issuer::_new_revocation_registry_keys(cred_rev_pub_key, max_cred_num)?;
let rev_reg = Issuer::_new_revocation_registry(cred_rev_pub_key,
&rev_key_priv,
max_cred_num,
issuance_by_default)?;
let rev_tails_generator = RevocationTailsGenerator::new(
max_cred_num,
rev_key_priv.gamma.clone(),
cred_rev_pub_key.g_dash.clone());
trace!("Issuer::new_revocation_registry_def: <<< rev_key_pub: {:?}, rev_key_priv: {:?}, rev_reg: {:?}, rev_tails_generator: {:?}",
rev_key_pub, secret!(&rev_key_priv), rev_reg, rev_tails_generator);
Ok((rev_key_pub, rev_key_priv, rev_reg, rev_tails_generator))
}
/// Creates and returns credential values entity builder.
///
/// The purpose of credential values builder is building of credential values entity that
/// represents credential attributes values map.
///
/// # Example
/// ```
/// use hl_crypto::cl::issuer::Issuer;
///
/// let mut credential_values_builder = Issuer::new_credential_values_builder().unwrap();
/// credential_values_builder.add_dec_known("sex", "5944657099558967239210949258394887428692050081607692519917050011144233115103").unwrap();
/// credential_values_builder.add_dec_known("name", "1139481716457488690172217916278103335").unwrap();
/// let _credential_values = credential_values_builder.finalize().unwrap();
/// ```
pub fn new_credential_values_builder() -> Result<CredentialValuesBuilder, HLCryptoError> {
let res = CredentialValuesBuilder::new()?;
Ok(res)
}
/// Signs credential values with primary keys only.
///
/// # Arguments
/// * `prover_id` - Prover identifier.
/// * `blinded_credential_secrets` - Blinded credential secrets generated by Prover.
/// * `blinded_credential_secrets_correctness_proof` - Blinded credential secrets correctness proof.
/// * `credential_nonce` - Nonce used for verification of blinded_credential_secrets_correctness_proof.
/// * `credential_issuance_nonce` - Nonce used for creation of signature_correctness_proof.
/// * `credential_values` - Credential values to be signed.
/// * `credential_pub_key` - Credential public key.
/// * `credential_priv_key` - Credential private key.
///
/// # Example
/// ```
/// use hl_crypto::cl::new_nonce;
/// use hl_crypto::cl::issuer::Issuer;
/// use hl_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 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 (blinded_credential_secrets, _, 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 (_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();
/// ```
pub fn sign_credential(prover_id: &str,
blinded_credential_secrets: &BlindedCredentialSecrets,
blinded_credential_secrets_correctness_proof: &BlindedCredentialSecretsCorrectnessProof,
credential_nonce: &Nonce,
credential_issuance_nonce: &Nonce,
credential_values: &CredentialValues,
credential_pub_key: &CredentialPublicKey,
credential_priv_key: &CredentialPrivateKey) -> Result<(CredentialSignature, SignatureCorrectnessProof), HLCryptoError> {
trace!("Issuer::sign_credential: >>> prover_id: {:?}\n \
blinded_credential_secrets: {:?}\n \
blinded_credential_secrets_correctness_proof: {:?}\n \
credential_nonce: {:?}\n \
credential_issuance_nonce: {:?}\n \
credential_values: {:?}\n \
credential_pub_key: {:?}\n \
credential_priv_key: {:?}",
prover_id,
blinded_credential_secrets,
blinded_credential_secrets_correctness_proof,
credential_nonce,
credential_issuance_nonce,
secret!(credential_values),
credential_pub_key,
secret!(credential_priv_key));
Issuer::_check_blinded_credential_secrets_correctness_proof(blinded_credential_secrets,
blinded_credential_secrets_correctness_proof,
credential_nonce,
&credential_pub_key.p_key)?;
// In the anoncreds whitepaper, `credential context` is denoted by `m2`
let cred_context = Issuer::_gen_credential_context(prover_id, None)?;
let (p_cred, q) = Issuer::_new_primary_credential(&cred_context,
credential_pub_key,
credential_priv_key,
blinded_credential_secrets,
credential_values)?;
let cred_signature = CredentialSignature { p_credential: p_cred, r_credential: None };
let signature_correctness_proof = Issuer::_new_signature_correctness_proof(&credential_pub_key.p_key,
&credential_priv_key.p_key,
&cred_signature.p_credential,
&q,
credential_issuance_nonce)?;
trace!("Issuer::sign_credential: <<< cred_signature: {:?}, signature_correctness_proof: {:?}",
secret!(&cred_signature), signature_correctness_proof);
Ok((cred_signature, signature_correctness_proof))
}
/// Signs credential values with both primary and revocation keys.
///
/// # Arguments
/// * `prover_id` - Prover identifier.
/// * `blinded_credential_secrets` - Blinded credential secrets generated by Prover.
/// * `blinded_credential_secrets_correctness_proof` - Blinded credential secrets correctness proof.
/// * `credential_nonce` - Nonce used for verification of blinded_credential_secrets_correctness_proof.
/// * `credential_issuance_nonce` - Nonce used for creation of signature_correctness_proof.
/// * `credential_values` - Credential values to be signed.
/// * `credential_pub_key` - Credential public key.
/// * `credential_priv_key` - Credential private key.
/// * `rev_idx` - User index in revocation accumulator. Required for non-revocation credential_signature part generation.
/// * `max_cred_num` - Max credential number in generated registry.
/// * `rev_reg` - Revocation registry.
/// * `rev_key_priv` - Revocation registry private key.
/// * `rev_tails_accessor` - Revocation registry tails accessor.
///
/// # Example
/// ```
/// use hl_crypto::cl::{new_nonce, SimpleTailsAccessor};
/// use hl_crypto::cl::issuer::Issuer;
/// use hl_crypto::cl::prover::Prover;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("name").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 (cred_pub_key, cred_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();
///
/// let max_cred_num = 5;
/// let (_rev_key_pub, rev_key_priv, mut rev_reg, mut rev_tails_generator) = Issuer::new_revocation_registry_def(&cred_pub_key, max_cred_num, false).unwrap();
///
/// let simple_tail_accessor = SimpleTailsAccessor::new(&mut rev_tails_generator).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("name", "1139481716457488690172217916278103335").unwrap();
/// let cred_values = credential_values_builder.finalize().unwrap();
///
/// let credential_nonce = new_nonce().unwrap();
///
/// let (blinded_credential_secrets, _credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&cred_pub_key, &cred_key_correctness_proof, &cred_values, &credential_nonce).unwrap();
///
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let (_cred_signature, _signature_correctness_proof, _rev_reg_delta) =
/// Issuer::sign_credential_with_revoc("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &cred_values,
/// &cred_pub_key,
/// &cred_priv_key,
/// 1,
/// max_cred_num,
/// false,
/// &mut rev_reg,
/// &rev_key_priv,
/// &simple_tail_accessor).unwrap();
/// ```
pub fn sign_credential_with_revoc<RTA>(prover_id: &str,
blinded_credential_secrets: &BlindedCredentialSecrets,
blinded_credential_secrets_correctness_proof: &BlindedCredentialSecretsCorrectnessProof,
credential_nonce: &Nonce,
credential_issuance_nonce: &Nonce,
credential_values: &CredentialValues,
credential_pub_key: &CredentialPublicKey,
credential_priv_key: &CredentialPrivateKey,
rev_idx: u32,
max_cred_num: u32,
issuance_by_default: bool,
rev_reg: &mut RevocationRegistry,
rev_key_priv: &RevocationKeyPrivate,
rev_tails_accessor: &RTA)
-> Result<(CredentialSignature, SignatureCorrectnessProof, Option<RevocationRegistryDelta>),
HLCryptoError> where RTA: RevocationTailsAccessor {
trace!("Issuer::sign_credential: >>> prover_id: {:?}, blinded_credential_secrets: {:?}, blinded_credential_secrets_correctness_proof: {:?},\
credential_nonce: {:?}, credential_issuance_nonce: {:?}, credential_values: {:?}, credential_pub_key: {:?}, credential_priv_key: {:?}, \
rev_idx: {:?}, max_cred_num: {:?}, rev_reg: {:?}, rev_key_priv: {:?}",
prover_id, blinded_credential_secrets, blinded_credential_secrets_correctness_proof, credential_nonce, secret!(credential_values), credential_issuance_nonce,
credential_pub_key, secret!(credential_priv_key), secret!(rev_idx), max_cred_num, rev_reg, secret!(rev_key_priv));
Issuer::_check_blinded_credential_secrets_correctness_proof(blinded_credential_secrets,
blinded_credential_secrets_correctness_proof,
credential_nonce,
&credential_pub_key.p_key)?;
// In the anoncreds whitepaper, `credential context` is denoted by `m2`
let cred_context = Issuer::_gen_credential_context(prover_id, Some(rev_idx))?;
let (p_cred, q) = Issuer::_new_primary_credential(&cred_context,
credential_pub_key,
credential_priv_key,
blinded_credential_secrets,
credential_values)?;
let (r_cred, rev_reg_delta) = Issuer::_new_non_revocation_credential(rev_idx,
&cred_context,
blinded_credential_secrets,
credential_pub_key,
credential_priv_key,
max_cred_num,
issuance_by_default,
rev_reg,
rev_key_priv,
rev_tails_accessor)?;
let cred_signature = CredentialSignature { p_credential: p_cred, r_credential: Some(r_cred) };
let signature_correctness_proof = Issuer::_new_signature_correctness_proof(&credential_pub_key.p_key,
&credential_priv_key.p_key,
&cred_signature.p_credential,
&q,
credential_issuance_nonce)?;
trace!("Issuer::sign_credential: <<< cred_signature: {:?}, signature_correctness_proof: {:?}, rev_reg_delta: {:?}",
secret!(&cred_signature), signature_correctness_proof, rev_reg_delta);
Ok((cred_signature, signature_correctness_proof, rev_reg_delta))
}
/// Revokes a credential by a rev_idx in a given revocation registry.
///
/// # Arguments
/// * `rev_reg` - Revocation registry.
/// * `max_cred_num` - Max credential number in revocation registry.
/// * rev_idx` - Index of the user in the revocation registry.
/// * `rev_tails_accessor` - Revocation registry tails accessor.
///
/// # Example
/// ```
/// use hl_crypto::cl::{new_nonce, SimpleTailsAccessor};
/// use hl_crypto::cl::issuer::Issuer;
/// use hl_crypto::cl::prover::Prover;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("name").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 (cred_pub_key, cred_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();
///
/// let max_cred_num = 5;
/// let (_rev_key_pub, rev_key_priv, mut rev_reg, mut rev_tails_generator) = Issuer::new_revocation_registry_def(&cred_pub_key, max_cred_num, false).unwrap();
///
/// let simple_tail_accessor = SimpleTailsAccessor::new(&mut rev_tails_generator).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("name", "1139481716457488690172217916278103335").unwrap();
/// let cred_values = credential_values_builder.finalize().unwrap();
///
/// let credential_nonce = new_nonce().unwrap();
///
/// let (blinded_credential_secrets, _credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&cred_pub_key, &cred_key_correctness_proof, &cred_values, &credential_nonce).unwrap();
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let rev_idx = 1;
/// let (_cred_signature, _signature_correctness_proof, _rev_reg_delta) =
/// Issuer::sign_credential_with_revoc("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &cred_values,
/// &cred_pub_key,
/// &cred_priv_key,
/// rev_idx,
/// max_cred_num,
/// false,
/// &mut rev_reg,
/// &rev_key_priv,
/// &simple_tail_accessor).unwrap();
/// Issuer::revoke_credential(&mut rev_reg, max_cred_num, rev_idx, &simple_tail_accessor).unwrap();
/// ```
pub fn revoke_credential<RTA>(rev_reg: &mut RevocationRegistry,
max_cred_num: u32,
rev_idx: u32,
rev_tails_accessor: &RTA) -> Result<RevocationRegistryDelta, HLCryptoError> where RTA: RevocationTailsAccessor {
trace!("Issuer::revoke_credential: >>> rev_reg: {:?}, max_cred_num: {:?}, rev_idx: {:?}", rev_reg, max_cred_num, secret!(rev_idx));
let prev_accum = rev_reg.accum.clone();
let index = Issuer::_get_index(max_cred_num, rev_idx);
rev_tails_accessor.access_tail(index, &mut |tail| {
rev_reg.accum = rev_reg.accum.sub(tail).unwrap();
})?;
let rev_reg_delta = RevocationRegistryDelta {
prev_accum: Some(prev_accum),
accum: rev_reg.accum.clone(),
issued: HashSet::new(),
revoked: hashset![rev_idx]
};
trace!("Issuer::revoke_credential: <<< rev_reg_delta: {:?}", rev_reg_delta);
Ok(rev_reg_delta)
}
/// Recovery a credential by a rev_idx in a given revocation registry
///
/// # Arguments
/// * `rev_reg` - Revocation registry.
/// * `max_cred_num` - Max credential number in revocation registry.
/// * rev_idx` - Index of the user in the revocation registry.
/// * `rev_tails_accessor` - Revocation registry tails accessor.
///
/// # Example
/// ```
/// use hl_crypto::cl::{new_nonce, SimpleTailsAccessor};
/// use hl_crypto::cl::issuer::Issuer;
/// use hl_crypto::cl::prover::Prover;
///
/// let mut credential_schema_builder = Issuer::new_credential_schema_builder().unwrap();
/// credential_schema_builder.add_attr("name").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 (cred_pub_key, cred_priv_key, cred_key_correctness_proof) = Issuer::new_credential_def(&credential_schema, &non_credential_schema, true).unwrap();
///
/// let max_cred_num = 5;
/// let (_rev_key_pub, rev_key_priv, mut rev_reg, mut rev_tails_generator) = Issuer::new_revocation_registry_def(&cred_pub_key, max_cred_num, false).unwrap();
///
/// let simple_tail_accessor = SimpleTailsAccessor::new(&mut rev_tails_generator).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("name", "1139481716457488690172217916278103335").unwrap();
/// let cred_values = credential_values_builder.finalize().unwrap();
///
/// let credential_nonce = new_nonce().unwrap();
///
/// let (blinded_credential_secrets, _credential_secrets_blinding_factors, blinded_credential_secrets_correctness_proof) =
/// Prover::blind_credential_secrets(&cred_pub_key, &cred_key_correctness_proof, &cred_values, &credential_nonce).unwrap();
///
/// let credential_issuance_nonce = new_nonce().unwrap();
///
/// let rev_idx = 1;
/// let (_cred_signature, _signature_correctness_proof, _rev_reg_delta) =
/// Issuer::sign_credential_with_revoc("CnEDk9HrMnmiHXEV1WFgbVCRteYnPqsJwrTdcZaNhFVW",
/// &blinded_credential_secrets,
/// &blinded_credential_secrets_correctness_proof,
/// &credential_nonce,
/// &credential_issuance_nonce,
/// &cred_values,
/// &cred_pub_key,
/// &cred_priv_key,
/// rev_idx,
/// max_cred_num,
/// false,
/// &mut rev_reg,
/// &rev_key_priv,
/// &simple_tail_accessor).unwrap();
/// Issuer::revoke_credential(&mut rev_reg, max_cred_num, rev_idx, &simple_tail_accessor).unwrap();
/// Issuer::recovery_credential(&mut rev_reg, max_cred_num, rev_idx, &simple_tail_accessor).unwrap();
/// ```
pub fn recovery_credential<RTA>(rev_reg: &mut RevocationRegistry,
max_cred_num: u32,
rev_idx: u32,
rev_tails_accessor: &RTA) -> Result<RevocationRegistryDelta, HLCryptoError> where RTA: RevocationTailsAccessor {
trace!("Issuer::recovery_credential: >>> rev_reg: {:?}, max_cred_num: {:?}, rev_idx: {:?}", rev_reg, max_cred_num, secret!(rev_idx));
let prev_accum = rev_reg.accum.clone();
let index = Issuer::_get_index(max_cred_num, rev_idx);
rev_tails_accessor.access_tail(index, &mut |tail| {
rev_reg.accum = rev_reg.accum.add(tail).unwrap();
})?;
let rev_reg_delta = RevocationRegistryDelta {
prev_accum: Some(prev_accum),
accum: rev_reg.accum.clone(),
issued: hashset![rev_idx],
revoked: HashSet::new()
};
trace!("Issuer::recovery_credential: <<< rev_reg_delta: {:?}", rev_reg_delta);
Ok(rev_reg_delta)
}
fn _new_credential_primary_keys(credential_schema: &CredentialSchema,
non_credential_schema: &NonCredentialSchema) ->
Result<(CredentialPrimaryPublicKey,
CredentialPrimaryPrivateKey,
CredentialPrimaryPublicKeyMetadata), HLCryptoError> {
trace!("Issuer::_new_credential_primary_keys: >>> credential_schema: {:?}", credential_schema);
let mut ctx = BigNumber::new_context()?;
if credential_schema.attrs.len() == 0 {
return Err(HLCryptoError::InvalidStructure(format!("List of attributes is empty")));
}
let p_safe = generate_safe_prime(LARGE_PRIME)?;
let q_safe = generate_safe_prime(LARGE_PRIME)?;
let p = p_safe.rshift1()?;
let q = q_safe.rshift1()?;
let n = p_safe.mul(&q_safe, Some(&mut ctx))?;
let s = random_qr(&n)?;
let xz = gen_x(&p, &q)?;
let mut xr = HashMap::new();
for non_schema_element in &non_credential_schema.attrs {
xr.insert(non_schema_element.to_string(), gen_x(&p, &q)?);
}
for attribute in &credential_schema.attrs {
xr.insert(attribute.to_string(), gen_x(&p, &q)?);
}
let mut r = HashMap::new();
for (key, xr_value) in xr.iter() {
r.insert(key.to_string(), s.mod_exp(&xr_value, &n, Some(&mut ctx))?);
}
let z = s.mod_exp(&xz, &n, Some(&mut ctx))?;
let rctxt = s.mod_exp(&gen_x(&p, &q)?, &n, Some(&mut ctx))?;
let cred_pr_pub_key = CredentialPrimaryPublicKey { n, s, rctxt, r, z };
let cred_pr_priv_key = CredentialPrimaryPrivateKey { p, q };
let cred_pr_pub_key_metadata = CredentialPrimaryPublicKeyMetadata { xz, xr };
trace!("Issuer::_new_credential_primary_keys: <<< cred_pr_pub_key: {:?}, cred_pr_priv_key: {:?}, cred_pr_pub_key_metadata: {:?}",
cred_pr_pub_key, secret!(&cred_pr_priv_key), cred_pr_pub_key_metadata);
Ok((cred_pr_pub_key, cred_pr_priv_key, cred_pr_pub_key_metadata))
}
fn _new_credential_revocation_keys() -> Result<(CredentialRevocationPublicKey,
CredentialRevocationPrivateKey), HLCryptoError> {
trace!("Issuer::_new_credential_revocation_keys: >>>");
let h = PointG1::new()?;
let h0 = PointG1::new()?;
let h1 = PointG1::new()?;
let h2 = PointG1::new()?;
let htilde = PointG1::new()?;
let g = PointG1::new()?;
let u = PointG2::new()?;
let h_cap = PointG2::new()?;
let x = GroupOrderElement::new()?;
let sk = GroupOrderElement::new()?;
let g_dash = PointG2::new()?;
let pk = g.mul(&sk)?;
let y = h_cap.mul(&x)?;
let cred_rev_pub_key = CredentialRevocationPublicKey { g, g_dash, h, h0, h1, h2, htilde, h_cap, u, pk, y };
let cred_rev_priv_key = CredentialRevocationPrivateKey { x, sk };
trace!("Issuer::_new_credential_revocation_keys: <<< cred_rev_pub_key: {:?}, cred_rev_priv_key: {:?}", cred_rev_pub_key, secret!(&cred_rev_priv_key));
Ok((cred_rev_pub_key, cred_rev_priv_key))
}
fn _new_credential_key_correctness_proof(cred_pr_pub_key: &CredentialPrimaryPublicKey,
cred_pr_priv_key: &CredentialPrimaryPrivateKey,
cred_pr_pub_key_meta: &CredentialPrimaryPublicKeyMetadata) -> Result<CredentialKeyCorrectnessProof, HLCryptoError> {
trace!("Issuer::_new_credential_key_correctness_proof: >>> cred_pr_pub_key: {:?}, cred_pr_priv_key: {:?}, cred_pr_pub_key_meta: {:?}",
cred_pr_pub_key, secret!(cred_pr_priv_key), cred_pr_pub_key_meta);
let mut ctx = BigNumber::new_context()?;
let xz_tilda = gen_x(&cred_pr_priv_key.p, &cred_pr_priv_key.q)?;
let mut xr_tilda = HashMap::new();
for key in cred_pr_pub_key.r.keys() {
xr_tilda.insert(key.to_string(), gen_x(&cred_pr_priv_key.p, &cred_pr_priv_key.q)?);
}
let z_tilda = cred_pr_pub_key.s.mod_exp(&xz_tilda, &cred_pr_pub_key.n, Some(&mut ctx))?;
let mut r_tilda = HashMap::new();
for (key, xr_tilda_value) in xr_tilda.iter() {
r_tilda.insert(key.to_string(), cred_pr_pub_key.s.mod_exp(&xr_tilda_value, &cred_pr_pub_key.n, Some(&mut ctx))?);
}
let mut values: Vec<u8> = Vec::new();
let mut ordered_attrs: Vec<String> = Vec::new();
values.extend_from_slice(&cred_pr_pub_key.z.to_bytes()?);
for (key, val) in cred_pr_pub_key.r.iter() {
values.extend_from_slice(&val.to_bytes()?);
ordered_attrs.push(key.to_owned());
}
values.extend_from_slice(&z_tilda.to_bytes()?);
for attr in &ordered_attrs {
let val = &r_tilda[attr];
values.extend_from_slice(&val.to_bytes()?);
}
let c = get_hash_as_int(&mut vec![values])?;
let xz_cap =
c.mul(&cred_pr_pub_key_meta.xz, Some(&mut ctx))?
.add(&xz_tilda)?;
let mut xr_cap: Vec<(String, BigNumber)> = Vec::new();
for key in ordered_attrs {
let xr_tilda_value = &xr_tilda[&key];
let val =
c.mul(&cred_pr_pub_key_meta.xr[&key], Some(&mut ctx))?
.add(&xr_tilda_value)?;
xr_cap.push((key, val));
}
let key_correctness_proof = CredentialKeyCorrectnessProof { c, xz_cap, xr_cap };
trace!("Issuer::_new_credential_key_correctness_proof: <<< key_correctness_proof: {:?}", key_correctness_proof);
Ok(key_correctness_proof)
}
fn _new_revocation_registry(cred_rev_pub_key: &CredentialRevocationPublicKey,
rev_key_priv: &RevocationKeyPrivate,
max_cred_num: u32,
issuance_by_default: bool) -> Result<RevocationRegistry, HLCryptoError> {
trace!("Issuer::_new_revocation_registry: >>> cred_rev_pub_key: {:?}, rev_key_priv: {:?}, max_cred_num: {:?}, issuance_by_default: {:?}",
cred_rev_pub_key, secret!(rev_key_priv), max_cred_num, issuance_by_default);
let mut accum = Accumulator::new_inf()?;
if issuance_by_default {
for i in 1..max_cred_num + 1 {
let index = Issuer::_get_index(max_cred_num, i);
accum = accum.add(&Tail::new_tail(index, &cred_rev_pub_key.g_dash, &rev_key_priv.gamma)?)?;
}
};
let rev_reg = RevocationRegistry {
accum
};
trace!("Issuer::_new_revocation_registry: <<< rev_reg: {:?}", rev_reg);
Ok(rev_reg)
}
fn _new_revocation_registry_keys(cred_rev_pub_key: &CredentialRevocationPublicKey,
max_cred_num: u32) -> Result<(RevocationKeyPublic, RevocationKeyPrivate), HLCryptoError> {
trace!("Issuer::_new_revocation_registry_keys: >>> cred_rev_pub_key: {:?}, max_cred_num: {:?}",
cred_rev_pub_key, max_cred_num);
let gamma = GroupOrderElement::new()?;
let mut z = Pair::pair(&cred_rev_pub_key.g, &cred_rev_pub_key.g_dash)?;
let mut pow = GroupOrderElement::from_bytes(&transform_u32_to_array_of_u8(max_cred_num + 1))?;
pow = gamma.pow_mod(&pow)?;
z = z.pow(&pow)?;
let rev_key_pub = RevocationKeyPublic { z };
let rev_key_priv = RevocationKeyPrivate { gamma };
trace!("Issuer::_new_revocation_registry_keys: <<< rev_key_pub: {:?}, rev_key_priv: {:?}", rev_key_pub, secret!(&rev_key_priv));
Ok((rev_key_pub, rev_key_priv))
}
fn _check_blinded_credential_secrets_correctness_proof(blinded_cred_secrets: &BlindedCredentialSecrets,
blinded_cred_secrets_correctness_proof: &BlindedCredentialSecretsCorrectnessProof,
nonce: &Nonce,
cred_pr_pub_key: &CredentialPrimaryPublicKey) -> Result<(), HLCryptoError> {
trace!("Issuer::_check_blinded_credential_secrets_correctness_proof: >>> blinded_cred_secrets: {:?}, blinded_cred_secrets_correctness_proof: {:?},\
nonce: {:?}, cred_pr_pub_key: {:?}", blinded_cred_secrets, blinded_cred_secrets_correctness_proof, nonce, cred_pr_pub_key);
let mut values: Vec<u8> = Vec::new();
let mut ctx = BigNumber::new_context()?;
let u_cap = blinded_cred_secrets.hidden_attributes
.iter()
.fold(blinded_cred_secrets.u
.inverse(&cred_pr_pub_key.n, Some(&mut ctx))?
.mod_exp(&blinded_cred_secrets_correctness_proof.c, &cred_pr_pub_key.n, Some(&mut ctx))?
.mod_mul(
&cred_pr_pub_key.s.mod_exp(&blinded_cred_secrets_correctness_proof.v_dash_cap, &cred_pr_pub_key.n, Some(&mut ctx))?,
&cred_pr_pub_key.n,
Some(&mut ctx)
),
|acc, attr| {
let pk_r = cred_pr_pub_key.r
.get(&attr.clone())
.ok_or(HLCryptoError::InvalidStructure(format!("Value by key '{}' not found in cred_pr_pub_key.r", attr)))?;
let m_cap = &blinded_cred_secrets_correctness_proof.m_caps[attr];
acc?.mod_mul(&pk_r.mod_exp(&m_cap, &cred_pr_pub_key.n, Some(&mut ctx))?,
&cred_pr_pub_key.n, Some(&mut ctx))
})?;
for (key, value) in &blinded_cred_secrets.committed_attributes {
let m_cap = &blinded_cred_secrets_correctness_proof.m_caps[key];
let comm_att_cap = value.inverse(&cred_pr_pub_key.n, Some(&mut ctx))?
.mod_exp(&blinded_cred_secrets_correctness_proof.c, &cred_pr_pub_key.n, Some(&mut ctx))?
.mod_mul(&get_pedersen_commitment(&cred_pr_pub_key.z, &m_cap,
&cred_pr_pub_key.s, &blinded_cred_secrets_correctness_proof.r_caps[key],
&cred_pr_pub_key.n, &mut ctx)?,
&cred_pr_pub_key.n, Some(&mut ctx))?;
values.extend_from_slice(&comm_att_cap.to_bytes()?);
values.extend_from_slice(&value.to_bytes()?);
}
values.extend_from_slice(&blinded_cred_secrets.u.to_bytes()?);
values.extend_from_slice(&u_cap.to_bytes()?);
values.extend_from_slice(&nonce.to_bytes()?);
let c = get_hash_as_int(&vec![values])?;
let valid = blinded_cred_secrets_correctness_proof.c.eq(&c);
if !valid {
return Err(HLCryptoError::InvalidStructure(format!("Invalid BlindedCredentialSecrets correctness proof")));
}
trace!("Issuer::_check_blinded_credential_secrets_correctness_proof: <<<");
Ok(())
}
// In the anoncreds whitepaper, `credential context` is denoted by `m2`
fn _gen_credential_context(prover_id: &str, rev_idx: Option<u32>) -> Result<BigNumber, HLCryptoError> {
trace!("Issuer::_calc_m2: >>> prover_id: {:?}, rev_idx: {:?}", prover_id, secret!(rev_idx));
let rev_idx = rev_idx.map(|i| i as i32).unwrap_or(-1);
let prover_id_bn = encode_attribute(prover_id, ByteOrder::Little)?;
let rev_idx_bn = encode_attribute(&rev_idx.to_string(), ByteOrder::Little)?;
let mut values: Vec<u8> = Vec::new();
values.extend_from_slice(&prover_id_bn.to_bytes()?);
values.extend_from_slice(&rev_idx_bn.to_bytes()?);
let credential_context = get_hash_as_int(&vec![values])?;
trace!("Issuer::_gen_credential_context: <<< credential_context: {:?}", secret!(&credential_context));
Ok(credential_context)
}
fn _new_primary_credential(credential_context: &BigNumber,
cred_pub_key: &CredentialPublicKey,
cred_priv_key: &CredentialPrivateKey,
blinded_credential_secrets: &BlindedCredentialSecrets,
cred_values: &CredentialValues) -> Result<(PrimaryCredentialSignature, BigNumber), HLCryptoError> {
trace!("Issuer::_new_primary_credential: >>> credential_context: {:?}, cred_pub_key: {:?}, cred_priv_key: {:?}, blinded_ms: {:?},\
cred_values: {:?}", secret!(credential_context), cred_pub_key, secret!(cred_priv_key), blinded_credential_secrets, secret!(cred_values));
let v = generate_v_prime_prime()?;
let e = generate_prime_in_range(&LARGE_E_START_VALUE, &LARGE_E_END_RANGE_VALUE)?;
let (a, q) = Issuer::_sign_primary_credential(cred_pub_key, cred_priv_key, &credential_context, &cred_values, &v, blinded_credential_secrets, &e)?;
let pr_cred_sig = PrimaryCredentialSignature { m_2: credential_context.clone()?, a, e, v };
trace!("Issuer::_new_primary_credential: <<< pr_cred_sig: {:?}, q: {:?}", secret!(&pr_cred_sig), secret!(&q));
Ok((pr_cred_sig, q))
}
fn _sign_primary_credential(cred_pub_key: &CredentialPublicKey,
cred_priv_key: &CredentialPrivateKey,
cred_context: &BigNumber,
cred_values: &CredentialValues,
v: &BigNumber,
blinded_cred_secrets: &BlindedCredentialSecrets,
e: &BigNumber) -> Result<(BigNumber, BigNumber), HLCryptoError> {
trace!("Issuer::_sign_primary_credential: >>> cred_pub_key: {:?}, \
cred_priv_key: {:?}, \
cred_context: {:?}, \
cred_values: {:?}, \
v: {:?},\
blinded_cred_secrets: {:?}, \
e: {:?}", cred_pub_key, secret!(cred_priv_key), secret!(cred_context), secret!(cred_values),
secret!(v), blinded_cred_secrets, secret!(e));
let p_pub_key = &cred_pub_key.p_key;
let p_priv_key = &cred_priv_key.p_key;
let mut context = BigNumber::new_context()?;
let mut rx = p_pub_key.s.mod_exp(&v, &p_pub_key.n, Some(&mut context))?;
if blinded_cred_secrets.u != BigNumber::from_u32(0)? {
rx = rx.mod_mul(&blinded_cred_secrets.u, &p_pub_key.n, Some(&mut context))?;
}
rx = rx.mod_mul(&p_pub_key.rctxt.mod_exp(&cred_context, &p_pub_key.n, Some(&mut context))?, &p_pub_key.n, Some(&mut context))?;
for (key, attr) in cred_values.attrs_values.iter().filter(|&(_, v)| v.is_known()) {
let pk_r = p_pub_key.r
.get(key)
.ok_or(HLCryptoError::InvalidStructure(format!("Value by key '{}' not found in pk.r", key)))?;
rx = pk_r.mod_exp(attr.value(), &p_pub_key.n, Some(&mut context))?
.mod_mul(&rx, &p_pub_key.n, Some(&mut context))?;
}
let q = p_pub_key.z.mod_div(&rx, &p_pub_key.n, Some(&mut context))?;
let n = p_priv_key.p.mul(&p_priv_key.q, Some(&mut context))?;
let e_inverse = e.inverse(&n, Some(&mut context))?;
let a = q.mod_exp(&e_inverse, &p_pub_key.n, Some(&mut context))?;
trace!("Issuer::_sign_primary_credential: <<< a: {:?}, q: {:?}", secret!(&a), secret!(&q));
Ok((a, q))
}
fn _new_signature_correctness_proof(p_pub_key: &CredentialPrimaryPublicKey,
p_priv_key: &CredentialPrimaryPrivateKey,
p_cred_signature: &PrimaryCredentialSignature,
q: &BigNumber,
nonce: &BigNumber) -> Result<SignatureCorrectnessProof, HLCryptoError> {
trace!("Issuer::_new_signature_correctness_proof: >>> p_pub_key: {:?}, p_priv_key: {:?}, p_cred_signature: {:?}, q: {:?}, nonce: {:?}",
p_pub_key, secret!(p_priv_key), secret!(p_cred_signature), secret!(q), nonce);
let mut ctx = BigNumber::new_context()?;
let n = p_priv_key.p.mul(&p_priv_key.q, Some(&mut ctx))?;
let r = bn_rand_range(&n)?;
let a_cap = q.mod_exp(&r, &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_signature.a.to_bytes()?);
values.extend_from_slice(&a_cap.to_bytes()?);
values.extend_from_slice(&nonce.to_bytes()?);
let c = get_hash_as_int(&mut vec![values])?;
let se = r.mod_sub(
&c.mod_mul(&p_cred_signature.e.inverse(&n, Some(&mut ctx))?, &n, Some(&mut ctx))?,
&n,
Some(&mut ctx)
)?;
let signature_correctness_proof = SignatureCorrectnessProof { c, se };
trace!("Issuer::_new_signature_correctness_proof: <<< signature_correctness_proof: {:?}", signature_correctness_proof);
Ok(signature_correctness_proof)
}
fn _get_index(max_cred_num: u32, rev_idx: u32) -> u32 {
max_cred_num + 1 - rev_idx
}
fn _new_non_revocation_credential(rev_idx: u32,
cred_context: &BigNumber,
blinded_credential_secrets: &BlindedCredentialSecrets,
cred_pub_key: &CredentialPublicKey,
cred_priv_key: &CredentialPrivateKey,
max_cred_num: u32,
issuance_by_default: bool,
rev_reg: &mut RevocationRegistry,
rev_key_priv: &RevocationKeyPrivate,
rev_tails_accessor: &RevocationTailsAccessor)
-> Result<(NonRevocationCredentialSignature, Option<RevocationRegistryDelta>), HLCryptoError> {
trace!("Issuer::_new_non_revocation_credential: >>> rev_idx: {:?}, cred_context: {:?}, blinded_ms: {:?}, cred_pub_key: {:?}, cred_priv_key: {:?}, \
max_cred_num: {:?}, issuance_by_default: {:?}, rev_reg: {:?}, rev_key_priv: {:?}",
secret!(rev_idx), secret!(cred_context), blinded_credential_secrets, cred_pub_key, secret!(cred_priv_key), max_cred_num,
issuance_by_default, rev_reg, secret!(rev_key_priv));
let ur = blinded_credential_secrets.ur
.ok_or(HLCryptoError::InvalidStructure(format!("No revocation part present in blinded master secret.")))?;
let r_pub_key: &CredentialRevocationPublicKey = cred_pub_key.r_key
.as_ref()
.ok_or(HLCryptoError::InvalidStructure(format!("No revocation part present in credential revocation public key.")))?;
let r_priv_key: &CredentialRevocationPrivateKey = cred_priv_key.r_key
.as_ref()
.ok_or(HLCryptoError::InvalidStructure(format!("No revocation part present in credential revocation private key.")))?;
let vr_prime_prime = GroupOrderElement::new()?;
let c = GroupOrderElement::new()?;
let m2 = GroupOrderElement::from_bytes(&cred_context.to_bytes()?)?;
let g_i = {
let i_bytes = transform_u32_to_array_of_u8(rev_idx);
let mut pow = GroupOrderElement::from_bytes(&i_bytes)?;
pow = rev_key_priv.gamma.pow_mod(&pow)?;
r_pub_key.g.mul(&pow)?
};
let sigma =
r_pub_key.h0.add(&r_pub_key.h1.mul(&m2)?)?
.add(&ur)?
.add(&g_i)?
.add(&r_pub_key.h2.mul(&vr_prime_prime)?)?
.mul(&r_priv_key.x.add_mod(&c)?.inverse()?)?;
let sigma_i = r_pub_key.g_dash
.mul(&r_priv_key.sk
.add_mod(&rev_key_priv.gamma
.pow_mod(&GroupOrderElement::from_bytes(&transform_u32_to_array_of_u8(rev_idx))?)?)?
.inverse()?)?;
let u_i = r_pub_key.u
.mul(&rev_key_priv.gamma
.pow_mod(&GroupOrderElement::from_bytes(&transform_u32_to_array_of_u8(rev_idx))?)?)?;