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
use ark_ec::{pairing::Pairing, AffineRepr};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{collections::BTreeMap, vec::Vec};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, Same};

use crate::{error::ProofSystemError, setup_params::SetupParams, statement::Statement};
use bbs_plus::prelude::{PublicKeyG2, SignatureParamsG1};
use dock_crypto_utils::serde_utils::*;

/// Public values like setup params, public key and revealed messages for proving knowledge of BBS+ signature.
#[serde_as]
#[derive(
    Clone, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize, Serialize, Deserialize,
)]
#[serde(bound = "")]
pub struct PoKBBSSignatureG1<E: Pairing> {
    /// Messages being revealed.
    #[serde_as(as = "BTreeMap<Same, ArkObjectBytes>")]
    pub revealed_messages: BTreeMap<usize, E::ScalarField>,
    /// If the statement was created by passing the signature params directly, then it will not be None
    pub signature_params: Option<SignatureParamsG1<E>>,
    /// If the statement was created by passing the public key params directly, then it will not be None
    pub public_key: Option<PublicKeyG2<E>>,
    /// If the statement was created by passing the index of signature params in `SetupParams`, then it will not be None
    pub signature_params_ref: Option<usize>,
    /// If the statement was created by passing the index of public key in `SetupParams`, then it will not be None
    pub public_key_ref: Option<usize>,
}

#[macro_export]
macro_rules! impl_bbs_statement {
    ($params: ident, $stmt: ident, $setup_param_name: ident) => {
        /// Create a statement by passing the signature parameters and public key directly.
        pub fn new_statement_from_params<G: AffineRepr>(
            signature_params: $params<E>,
            public_key: PublicKeyG2<E>,
            revealed_messages: BTreeMap<usize, E::ScalarField>,
        ) -> Statement<E, G> {
            Statement::$stmt(Self {
                revealed_messages,
                signature_params: Some(signature_params),
                public_key: Some(public_key),
                signature_params_ref: None,
                public_key_ref: None,
            })
        }

        /// Create a statement by passing the indices of signature parameters and public key in `SetupParams`.
        pub fn new_statement_from_params_ref<G: AffineRepr>(
            signature_params_ref: usize,
            public_key_ref: usize,
            revealed_messages: BTreeMap<usize, E::ScalarField>,
        ) -> Statement<E, G> {
            Statement::$stmt(Self {
                revealed_messages,
                signature_params: None,
                public_key: None,
                signature_params_ref: Some(signature_params_ref),
                public_key_ref: Some(public_key_ref),
            })
        }

        /// Get signature params for the statement index `s_idx` either from `self` or from given `setup_params`.
        pub fn get_sig_params<'a, G: AffineRepr>(
            &'a self,
            setup_params: &'a [SetupParams<E, G>],
            st_idx: usize,
        ) -> Result<&'a $params<E>, ProofSystemError> {
            extract_param!(
                setup_params,
                &self.signature_params,
                self.signature_params_ref,
                $setup_param_name,
                IncompatibleBBSPlusSetupParamAtIndex,
                st_idx
            )
        }

        /// Get public key for the statement index `s_idx` either from `self` or from given `setup_params`.
        pub fn get_public_key<'a, G: AffineRepr>(
            &'a self,
            setup_params: &'a [SetupParams<E, G>],
            st_idx: usize,
        ) -> Result<&'a PublicKeyG2<E>, ProofSystemError> {
            extract_param!(
                setup_params,
                &self.public_key,
                self.public_key_ref,
                BBSPlusPublicKey,
                IncompatibleBBSPlusSetupParamAtIndex,
                st_idx
            )
        }
    };
}

impl<E: Pairing> PoKBBSSignatureG1<E> {
    impl_bbs_statement!(SignatureParamsG1, PoKBBSSignatureG1, BBSPlusSignatureParams);
}