delegatable_credentials 0.8.0

Schemes used to develop DAC (Delegatable Anonymous Credentials)
Documentation
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
//! Credential issuance as described in Fig 2 of the Protego paper. The user creates a
//! `SignatureRequest` in the Obtain phase of the protocol and sends it to the issuer which sends a signature
//! on the successful verification of the request. The user uses this signature and the original request to
//! create a credential

use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup};
use ark_ff::{PrimeField, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{io::Write, ops::Neg, rand::RngCore, vec, vec::Vec, UniformRand};
use dock_crypto_utils::{elgamal::PublicKey as AuditorPublicKey, msm::WindowTable};

use schnorr_pok::discrete_log::{PokDiscreteLog, PokDiscreteLogProtocol};

use crate::{
    error::DelegationError,
    mercurial_sig::Signature,
    protego::keys::{IssuerSecretKey, PreparedIssuerPublicKey, UserPublicKey, UserSecretKey},
    set_commitment::{
        PreparedSetCommitmentSRS, SetCommitment, SetCommitmentOpening, SetCommitmentSRS,
    },
};

#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct RevocationRequestProtocol<E: Pairing> {
    /// Schnorr protocol to prove knowledge of revocation secret key
    pub usk_rev_protocol: PokDiscreteLogProtocol<E::G1Affine>,
    /// Schnorr protocol to prove knowledge of revocation secret key with base Q
    pub usk_rev_protocol_Q: PokDiscreteLogProtocol<E::G1Affine>,
    pub C4: E::G1Affine,
    pub C5: E::G1Affine,
    pub nym: E::ScalarField,
}

/// Part of signature request corresponding to revocation
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct RevocationRequest<E: Pairing> {
    pub C4: E::G1Affine,
    pub C5: E::G1Affine,
    pub nym: E::ScalarField,
    /// Schnorr proof to prove knowledge of revocation secret key
    pub usk_rev_proof: PokDiscreteLog<E::G1Affine>,
    /// Schnorr proof to prove knowledge of revocation secret key with base Q
    pub usk_rev_proof_Q: PokDiscreteLog<E::G1Affine>,
}

/// Part of credential corresponding to revocation
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct RevocationCredential<E: Pairing> {
    pub C4: E::G1Affine,
    pub C5: E::G1Affine,
    pub nym: E::ScalarField,
}

/// Protocol to request a signature from an issuer which will then be part of a credential.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct SignatureRequestProtocol<E: Pairing> {
    /// Schnorr protocol to prove knowledge of secret key
    pub usk_protocol: PokDiscreteLogProtocol<E::G1Affine>,
    pub rev: Option<RevocationRequestProtocol<E>>,
    /// Whether requesting an auditable signature, i.e. signer signs user's public key
    pub auditable_sig: bool,
}

/// A request to obtain a signature from a credential issuer.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct SignatureRequest<E: Pairing> {
    /// Commitment to all attributes
    pub C1: E::G1Affine,
    /// Randomized C1
    pub C2: E::G1Affine,
    /// Schnorr proof to prove knowledge of secret key
    pub usk_proof: PokDiscreteLog<E::G1Affine>,
    /// Only present when the signer is issuing a revocable credential
    pub rev: Option<RevocationRequest<E>>,
    /// Whether requesting an auditable signature, i.e. signer signs user's public key
    pub auditable_sig: bool,
}

#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct SignatureRequestOpening<E: Pairing> {
    pub r4: E::ScalarField,
    /// Opening to the commitment C1
    pub set_comm_opening: SetCommitmentOpening<E>,
}

#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct Credential<E: Pairing> {
    pub attributes: Vec<E::ScalarField>,
    /// Commitment to the attributes
    pub C1: E::G1Affine,
    pub opening: SignatureRequestOpening<E>,
    pub signature: Signature<E>,
    /// Only present when the credential is revocable
    pub rev: Option<RevocationCredential<E>>,
    /// Whether the signature is auditable, i.e. signer signs user's public key
    pub auditable_sig: bool,
}

impl<E: Pairing> SignatureRequestProtocol<E> {
    /// Initialize protocol for creating a signature request. The signature won't support revocation
    pub fn init<R: RngCore>(
        rng: &mut R,
        user_sk: &UserSecretKey<E>,
        auditable_sig: bool,
        P1: &E::G1Affine,
    ) -> Self {
        let r1 = E::ScalarField::rand(rng);
        Self {
            usk_protocol: PokDiscreteLogProtocol::init(user_sk.0, r1, P1),
            rev: None,
            auditable_sig,
        }
    }

    /// Initialize protocol for creating a signature request where the signature supports revocation
    pub fn init_with_revocation<R: RngCore>(
        rng: &mut R,
        nym: E::ScalarField,
        user_sk: &UserSecretKey<E>,
        auditable_sig: bool,
        P1: &E::G1Affine,
        s_P1: &E::G1Affine,
        Q: &E::G1Affine,
    ) -> Result<Self, DelegationError> {
        let usk2 = user_sk
            .1
            .ok_or_else(|| DelegationError::KeyDoesNotSupportRevocation)?;
        let r1 = E::ScalarField::rand(rng);
        let r2 = E::ScalarField::rand(rng);
        // C4 = P1*usk2*(s - nym) where s is the trapdoor of the set commitment SRS
        let mut C4 = P1.mul_bigint(nym.into_bigint()).neg();
        C4 += s_P1;
        C4 *= usk2;
        Ok(Self {
            usk_protocol: PokDiscreteLogProtocol::init(user_sk.0, r1, P1),
            rev: Some(RevocationRequestProtocol {
                // Note: This deviates from the paper which uses a different `r3` as blinding for
                // `usk_rev_protocol_Q` but since it should be proven that same usk2 is the witness in
                // both `usk_rev_protocol` and `usk_rev_protocol_Q`, same blinding is used.
                usk_rev_protocol: PokDiscreteLogProtocol::init(usk2, r2, P1),
                usk_rev_protocol_Q: PokDiscreteLogProtocol::init(usk2, r2, Q),
                C4: C4.into_affine(),
                // C5 = usk2*Q
                C5: Q.mul_bigint(usk2.into_bigint()).into_affine(),
                nym,
            }),
            auditable_sig,
        })
    }

    pub fn challenge_contribution<W: Write>(
        &self,
        user_pk: &UserPublicKey<E>,
        P1: &E::G1Affine,
        Q: Option<&E::G1Affine>,
        mut writer: W,
    ) -> Result<(), DelegationError> {
        Self::compute_challenge_contribution(
            self.auditable_sig,
            &self.usk_protocol,
            user_pk,
            P1,
            &mut writer,
        )?;
        if let Some(rev) = self.rev.as_ref() {
            let Q = Q.ok_or_else(|| DelegationError::AccumulatorPublicParamsNotProvided)?;
            Self::compute_challenge_contribution_for_revocable(
                &rev.usk_rev_protocol,
                &rev.usk_rev_protocol_Q,
                user_pk,
                &rev.C5,
                P1,
                Q,
                &mut writer,
            )?;
        }
        Ok(())
    }

    pub fn compute_challenge_contribution<W: Write>(
        auditable_sig: bool,
        usk_protocol: &PokDiscreteLogProtocol<E::G1Affine>,
        user_pk: &UserPublicKey<E>,
        P1: &E::G1Affine,
        mut writer: W,
    ) -> Result<(), DelegationError> {
        auditable_sig.serialize_compressed(&mut writer)?;
        usk_protocol
            .challenge_contribution(P1, &user_pk.0, &mut writer)
            .map_err(|e| e.into())
    }

    pub fn compute_challenge_contribution_for_revocable<W: Write>(
        usk_rev_protocol: &PokDiscreteLogProtocol<E::G1Affine>,
        usk_rev_protocol2: &PokDiscreteLogProtocol<E::G1Affine>,
        user_pk: &UserPublicKey<E>,
        C5: &E::G1Affine,
        P1: &E::G1Affine,
        Q: &E::G1Affine,
        mut writer: W,
    ) -> Result<(), DelegationError> {
        let upk_rev = user_pk
            .1
            .ok_or_else(|| DelegationError::KeyDoesNotSupportRevocation)?;
        usk_rev_protocol.challenge_contribution(P1, &upk_rev, &mut writer)?;
        usk_rev_protocol2
            .challenge_contribution(Q, C5, &mut writer)
            .map_err(|e| e.into())
    }

    /// Creates a signature request by committing the attributes in a set commitment and then signing that
    /// commitment along with other values. The attributes are expected to be unique as the are committed
    /// using a set commitment scheme. One approach is to encode attributes as pairs with 1st element of the
    /// pair as an index and the 2nd element as the actual attribute value like `(0, attribute[0]), (1, attribute[1]), (2, attribute[2]), (n, attribute[n])`
    pub fn gen_request<R: RngCore>(
        self,
        rng: &mut R,
        attributes: Vec<E::ScalarField>,
        user_sk: &UserSecretKey<E>,
        challenge: &E::ScalarField,
        set_comm_srs: &SetCommitmentSRS<E>,
    ) -> Result<(SignatureRequest<E>, SignatureRequestOpening<E>), DelegationError> {
        if attributes.len() > set_comm_srs.size() {
            return Err(DelegationError::InsufficientSetCommitmentSRSSize(
                attributes.len(),
                set_comm_srs.size(),
            ));
        }
        let attr_set = attributes.into_iter().collect();
        let r4 = E::ScalarField::rand(rng);

        let (comm, opening) =
            SetCommitment::new_with_given_randomness(user_sk.0, attr_set, set_comm_srs)?;
        let C2 = comm.0.mul_bigint(r4.into_bigint()).into_affine();
        let req = SignatureRequest {
            C1: comm.0,
            C2,
            usk_proof: self.usk_protocol.gen_proof(challenge),
            rev: self.rev.map(|rev| RevocationRequest {
                C4: rev.C4,
                C5: rev.C5,
                nym: rev.nym,
                usk_rev_proof: rev.usk_rev_protocol.gen_proof(challenge),
                usk_rev_proof_Q: rev.usk_rev_protocol_Q.gen_proof(challenge),
            }),
            auditable_sig: self.auditable_sig,
        };
        Ok((
            req,
            SignatureRequestOpening {
                r4,
                set_comm_opening: opening,
            },
        ))
    }
}

impl<E: Pairing> SignatureRequest<E> {
    /// Signer verifies the signature request before creating a signature
    pub fn verify(
        &self,
        attributes: Vec<E::ScalarField>,
        user_public_key: &UserPublicKey<E>,
        challenge: &E::ScalarField,
        Q: Option<&E::G1Affine>,
        s_P2_from_accumulator: Option<&E::G2Affine>,
        set_comm_srs: impl Into<PreparedSetCommitmentSRS<E>>,
    ) -> Result<(), DelegationError> {
        let set_comm_srs = set_comm_srs.into();

        if attributes.len() > set_comm_srs.size() {
            return Err(DelegationError::InsufficientSetCommitmentSRSSize(
                attributes.len(),
                set_comm_srs.size(),
            ));
        }

        let P1_table = WindowTable::new(attributes.len(), set_comm_srs.get_P1().into_group());
        let s_P1 = set_comm_srs.get_s_P1().into_group();
        for s in attributes.iter() {
            if P1_table.multiply(s) == s_P1 {
                return Err(DelegationError::ShouldNotContainTrapdoor);
            }
        }

        let upk = &user_public_key.0;
        let prep_P2 = set_comm_srs.prepared_P2.clone();

        let attr_set = attributes.into_iter().collect();
        let e = E::G2Prepared::from(set_comm_srs.eval_P2(attr_set));
        // Check if `e(C1, P2) == (upk, Ch(attr_set)*P2)`
        if !E::multi_pairing(
            [self.C1, (-upk.into_group()).into_affine()],
            [prep_P2.clone(), e],
        )
        .is_zero()
        {
            return Err(DelegationError::InvalidSignatureRequest);
        }

        // Check if `upk == P1*usk`
        if !self.usk_proof.verify(upk, set_comm_srs.get_P1(), challenge) {
            return Err(DelegationError::InvalidSignatureRequest);
        }

        // For revocation
        if self.supports_revocation() {
            let rev = self.rev.as_ref().unwrap();
            let Q = Q.ok_or_else(|| DelegationError::AccumulatorPublicParamsNotProvided)?;
            let s_P2 = s_P2_from_accumulator
                .ok_or_else(|| DelegationError::AccumulatorPublicParamsNotProvided)?;
            let upk2 = user_public_key
                .1
                .ok_or_else(|| DelegationError::KeyDoesNotSupportRevocation)?;
            // e2 = P2 * (s - nym)
            let P2_nym = set_comm_srs
                .get_P2()
                .mul_bigint(rev.nym.into_bigint())
                .neg();
            let e2 = E::G2Prepared::from(P2_nym + s_P2);
            // Check if e(C4, P2) == (upk2, P2 * (s - nym)) => e(C4, P2) * (-upk2, P2 * (s - nym)) == 1
            if !E::multi_pairing([rev.C4, (-upk2.into_group()).into_affine()], [prep_P2, e2])
                .is_zero()
            {
                return Err(DelegationError::InvalidRevocationRequest);
            }

            // Check if `upk2 == P1*usk`
            if !rev
                .usk_rev_proof
                .verify(&upk2, set_comm_srs.get_P1(), challenge)
            {
                return Err(DelegationError::InvalidRevocationRequest);
            }

            // Check if `C5 == Q*usk`
            if !rev.usk_rev_proof_Q.verify(&rev.C5, Q, challenge) {
                return Err(DelegationError::InvalidRevocationRequest);
            }

            // Check if response is same in both Schnorr proofs to ensure same witness (secret key)
            if rev.usk_rev_proof.response != rev.usk_rev_proof_Q.response {
                return Err(DelegationError::InvalidRevocationRequest);
            }
        }

        Ok(())
    }

    /// After verifying the signature request, signer creates a signature using the request
    pub fn sign<R: RngCore>(
        self,
        rng: &mut R,
        issuer_sk: &IssuerSecretKey<E>,
        user_pk: Option<&UserPublicKey<E>>,
        auditor_pk: Option<&AuditorPublicKey<E::G1Affine>>,
        P1: &E::G1Affine,
        P2: &E::G2Affine,
    ) -> Result<Signature<E>, DelegationError> {
        if self.auditable_sig & !issuer_sk.supports_audit {
            return Err(DelegationError::IssuerKeyDoesNotSupportAuditableSignature);
        }
        let messages = self.create_msgs(user_pk, auditor_pk, *P1)?;
        Signature::new(rng, &messages, &issuer_sk.secret_key, P1, P2)
    }

    /// Prepare messages (commitments) to sign
    pub fn create_msgs(
        self,
        user_pk: Option<&UserPublicKey<E>>,
        auditor_pk: Option<&AuditorPublicKey<E::G1Affine>>,
        P1: E::G1Affine,
    ) -> Result<Vec<E::G1Affine>, DelegationError> {
        let mut messages = vec![];
        messages.push(self.C1);
        messages.push(self.C2);
        messages.push(P1);
        if let Some(rev) = self.rev {
            messages.push(rev.C4);
            messages.push(rev.C5);
        }
        if self.auditable_sig {
            let upk = user_pk
                .map(|pk| pk.0)
                .ok_or(DelegationError::NeedUserPublicKey)?;
            messages.push(upk);
            let apk = auditor_pk
                .map(|pk| pk.0)
                .ok_or(DelegationError::NeedAuditorPublicKey)?;
            messages.push(apk);
        }
        Ok(messages)
    }

    pub fn supports_revocation(&self) -> bool {
        self.rev.is_some()
    }
}

impl<E: Pairing> Credential<E> {
    /// Create a new credential using the created signature request and the received signature. It will
    /// verify the signature before creating the credential.
    pub fn new(
        sig_req: SignatureRequest<E>,
        sig_req_opn: SignatureRequestOpening<E>,
        sig: Signature<E>,
        attributes: Vec<E::ScalarField>,
        issuer_pk: impl Into<PreparedIssuerPublicKey<E>>,
        user_pk: Option<&UserPublicKey<E>>,
        auditor_pk: Option<&AuditorPublicKey<E::G1Affine>>,
        P1: &E::G1Affine,
        P2: impl Into<E::G2Prepared>,
    ) -> Result<Self, DelegationError> {
        let C1 = sig_req.C1;
        let rev = sig_req.rev.as_ref().map(|rev| RevocationCredential {
            C4: rev.C4,
            C5: rev.C5,
            nym: rev.nym,
        });
        let auditable_sig = sig_req.auditable_sig;

        let msgs = sig_req.create_msgs(user_pk, auditor_pk, *P1)?;
        sig.verify(&msgs, issuer_pk.into().public_key, P1, P2.into())?;
        Ok(Self {
            attributes,
            C1,
            rev,
            opening: sig_req_opn,
            signature: sig,
            auditable_sig,
        })
    }

    pub fn supports_revocation(&self) -> bool {
        self.rev.is_some()
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use ark_bls12_381::{Bls12_381, Fr, G1Affine, G2Affine};
    use ark_std::rand::{rngs::StdRng, SeedableRng};
    use blake2::Blake2b512;

    use crate::protego::keys::IssuerPublicKey;
    use dock_crypto_utils::elgamal::SecretKey as AuditorSecretKey;
    use schnorr_pok::compute_random_oracle_challenge;

    pub fn keygen(
        rng: &mut StdRng,
        auditable: bool,
        supports_revocation: bool,
        set_comm_srs: &SetCommitmentSRS<Bls12_381>,
    ) -> (
        AuditorSecretKey<Fr>,
        AuditorPublicKey<G1Affine>,
        IssuerSecretKey<Bls12_381>,
        IssuerPublicKey<Bls12_381>,
        UserSecretKey<Bls12_381>,
        UserPublicKey<Bls12_381>,
    ) {
        let ask = AuditorSecretKey::new(rng);
        let apk = AuditorPublicKey::new(&ask, set_comm_srs.get_P1());

        let isk = IssuerSecretKey::<Bls12_381>::new::<StdRng>(rng, supports_revocation, auditable)
            .unwrap();
        let ipk = IssuerPublicKey::new(&isk, set_comm_srs.get_P2());

        let usk = UserSecretKey::new(rng, supports_revocation);
        let upk = UserPublicKey::new(&usk, set_comm_srs.get_P1());

        (ask, apk, isk, ipk, usk, upk)
    }

    pub fn setup(
        rng: &mut StdRng,
        max_attributes: u32,
        auditable: bool,
        supports_revocation: bool,
    ) -> (
        SetCommitmentSRS<Bls12_381>,
        Fr,
        AuditorSecretKey<Fr>,
        AuditorPublicKey<G1Affine>,
        IssuerSecretKey<Bls12_381>,
        IssuerPublicKey<Bls12_381>,
        UserSecretKey<Bls12_381>,
        UserPublicKey<Bls12_381>,
    ) {
        let (set_comm_srs, trapdoor) = SetCommitmentSRS::<Bls12_381>::generate_with_random_trapdoor::<
            StdRng,
            Blake2b512,
        >(rng, max_attributes, Some("Protego".as_bytes()));
        let (ask, apk, isk, ipk, usk, upk) =
            keygen(rng, auditable, supports_revocation, &set_comm_srs);
        (set_comm_srs, trapdoor, ask, apk, isk, ipk, usk, upk)
    }

    pub fn issuance(
        rng: &mut StdRng,
        max_attributes: u32,
        attributes: Vec<Fr>,
        auditable: bool,
        Q: Option<&G1Affine>,
        nym: Option<&Fr>,
        s_P1_from_accumulator: Option<&G1Affine>,
        s_P2_from_accumulator: Option<&G2Affine>,
    ) -> Credential<Bls12_381> {
        assert!((Q.is_some() && nym.is_some()) || (Q.is_none() && nym.is_none()));
        let supports_revocation = Q.is_some() && nym.is_some();
        let (set_comm_srs, _, _, apk, isk, ipk, usk, upk) =
            setup(rng, max_attributes, auditable, supports_revocation);
        issuance_given_setup(
            rng,
            attributes,
            auditable,
            &apk,
            &isk,
            &ipk,
            &usk,
            &upk,
            Q,
            nym,
            s_P1_from_accumulator,
            s_P2_from_accumulator,
            &set_comm_srs,
        )
    }

    pub fn issuance_given_setup(
        rng: &mut StdRng,
        attributes: Vec<Fr>,
        auditable: bool,
        apk: &AuditorPublicKey<G1Affine>,
        isk: &IssuerSecretKey<Bls12_381>,
        ipk: &IssuerPublicKey<Bls12_381>,
        usk: &UserSecretKey<Bls12_381>,
        upk: &UserPublicKey<Bls12_381>,
        Q: Option<&G1Affine>,
        nym: Option<&Fr>,
        s_P1_from_accumulator: Option<&G1Affine>,
        s_P2_from_accumulator: Option<&G2Affine>,
        set_comm_srs: &SetCommitmentSRS<Bls12_381>,
    ) -> Credential<Bls12_381> {
        assert!((Q.is_some() && nym.is_some()) || (Q.is_none() && nym.is_none()));
        let supports_revocation = Q.is_some() && nym.is_some();

        let prep_set_comm_srs = PreparedSetCommitmentSRS::from(set_comm_srs.clone());
        let prep_ipk = PreparedIssuerPublicKey::from(ipk.clone());

        let sig_req_p = if supports_revocation {
            SignatureRequestProtocol::init_with_revocation(
                rng,
                *(nym.unwrap()),
                usk,
                auditable,
                set_comm_srs.get_P1(),
                s_P1_from_accumulator.unwrap(),
                Q.unwrap(),
            )
            .unwrap()
        } else {
            SignatureRequestProtocol::init(rng, usk, auditable, set_comm_srs.get_P1())
        };

        let mut chal_bytes = vec![];
        sig_req_p
            .challenge_contribution(upk, set_comm_srs.get_P1(), Q, &mut chal_bytes)
            .unwrap();
        let challenge = compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_bytes);
        let (sig_req, sig_req_opn) = sig_req_p
            .gen_request(rng, attributes.clone(), usk, &challenge, set_comm_srs)
            .unwrap();
        sig_req
            .verify(
                attributes.clone(),
                upk,
                &challenge,
                Q,
                s_P2_from_accumulator,
                prep_set_comm_srs.clone(),
            )
            .unwrap();
        assert!(!(sig_req.supports_revocation() ^ supports_revocation));
        assert!(!(sig_req.auditable_sig ^ auditable));

        let sig = sig_req
            .clone()
            .sign(
                rng,
                isk,
                auditable.then_some(upk),
                auditable.then_some(apk),
                set_comm_srs.get_P1(),
                set_comm_srs.get_P2(),
            )
            .unwrap();
        let cred = Credential::new(
            sig_req,
            sig_req_opn,
            sig,
            attributes,
            prep_ipk,
            auditable.then_some(upk),
            auditable.then_some(apk),
            set_comm_srs.get_P1(),
            prep_set_comm_srs.prepared_P2,
        )
        .unwrap();
        assert_eq!(cred.supports_revocation(), supports_revocation);
        assert_eq!(cred.auditable_sig, auditable);
        cred
    }

    #[test]
    fn issuance_without_revocation() {
        let mut rng = StdRng::seed_from_u64(0u64);

        let max_attributes = 10;
        let attributes = (0..max_attributes)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();
        let attributes_1 = (0..max_attributes - 2)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();

        issuance(
            &mut rng,
            max_attributes,
            attributes.clone(),
            false,
            None,
            None,
            None,
            None,
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes_1.clone(),
            false,
            None,
            None,
            None,
            None,
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes,
            true,
            None,
            None,
            None,
            None,
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes_1,
            true,
            None,
            None,
            None,
            None,
        );
    }

    #[test]
    fn issuance_with_revocation() {
        let mut rng = StdRng::seed_from_u64(0u64);

        let max_attributes = 10;
        let attributes = (0..max_attributes)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();
        let attributes_1 = (0..max_attributes - 2)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();

        let Q = <Bls12_381 as Pairing>::G1Affine::rand(&mut rng);
        let nym = Fr::rand(&mut rng);

        let (accum_srs, _) = SetCommitmentSRS::<Bls12_381>::generate_with_random_trapdoor::<
            StdRng,
            Blake2b512,
        >(&mut rng, 100, Some("Protego".as_bytes()));

        issuance(
            &mut rng,
            max_attributes,
            attributes.clone(),
            false,
            Some(&Q),
            Some(&nym),
            Some(accum_srs.get_s_P1()),
            Some(accum_srs.get_s_P2()),
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes_1.clone(),
            false,
            Some(&Q),
            Some(&nym),
            Some(accum_srs.get_s_P1()),
            Some(accum_srs.get_s_P2()),
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes,
            true,
            Some(&Q),
            Some(&nym),
            Some(accum_srs.get_s_P1()),
            Some(accum_srs.get_s_P2()),
        );
        issuance(
            &mut rng,
            max_attributes,
            attributes_1,
            true,
            Some(&Q),
            Some(&nym),
            Some(accum_srs.get_s_P1()),
            Some(accum_srs.get_s_P2()),
        );
    }
}