short_group_sig/
common.rs

1use ark_ec::{
2    pairing::{Pairing, PairingOutput},
3    AffineRepr,
4};
5use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
6use ark_std::{io::Write, rand::RngCore, vec::Vec, UniformRand};
7use digest::Digest;
8use dock_crypto_utils::{
9    affine_group_element_from_byte_slices, concat_slices,
10    hashing_utils::affine_group_elem_from_try_and_incr, serde_utils::ArkObjectBytes,
11};
12use schnorr_pok::{error::SchnorrError, SchnorrChallengeContributor};
13use serde::{Deserialize, Serialize};
14use serde_with::serde_as;
15
16/// Public parameters for creating and verifying BB signatures
17#[serde_as]
18#[derive(
19    Clone, PartialEq, Eq, Debug, CanonicalSerialize, CanonicalDeserialize, Serialize, Deserialize,
20)]
21pub struct SignatureParams<E: Pairing> {
22    #[serde_as(as = "ArkObjectBytes")]
23    pub g1: E::G1Affine,
24    #[serde_as(as = "ArkObjectBytes")]
25    pub g2: E::G2Affine,
26}
27
28/// `SignatureParams` with pre-computation done for protocols to be more efficient
29#[derive(Clone, PartialEq, Eq, Debug, CanonicalSerialize, CanonicalDeserialize)]
30pub struct SignatureParamsWithPairing<E: Pairing> {
31    pub g1: E::G1Affine,
32    pub g2: E::G2Affine,
33    pub g2_prepared: E::G2Prepared,
34    /// pairing e(g1, g2)
35    pub g1g2: PairingOutput<E>,
36}
37
38impl<E: Pairing> SignatureParams<E> {
39    pub fn new<D: Digest>(label: &[u8]) -> Self {
40        let g1 =
41            affine_group_elem_from_try_and_incr::<E::G1Affine, D>(&concat_slices![label, b" : g1"]);
42        let g2 =
43            affine_group_elem_from_try_and_incr::<E::G2Affine, D>(&concat_slices![label, b" : g2"]);
44        Self { g1, g2 }
45    }
46
47    pub fn generate_using_rng<R: RngCore>(rng: &mut R) -> Self {
48        Self {
49            g1: E::G1::rand(rng).into(),
50            g2: E::G2::rand(rng).into(),
51        }
52    }
53
54    pub fn is_valid(&self) -> bool {
55        !(self.g1.is_zero() || self.g2.is_zero())
56    }
57}
58
59impl<E: Pairing> From<SignatureParams<E>> for SignatureParamsWithPairing<E> {
60    fn from(params: SignatureParams<E>) -> Self {
61        let g1g2 = E::pairing(params.g1, params.g2);
62        Self {
63            g1: params.g1,
64            g2: params.g2,
65            g2_prepared: E::G2Prepared::from(params.g2),
66            g1g2,
67        }
68    }
69}
70
71impl<E: Pairing> AsRef<E::G1Affine> for SignatureParams<E> {
72    fn as_ref(&self) -> &E::G1Affine {
73        &self.g1
74    }
75}
76
77impl<E: Pairing> AsRef<E::G1Affine> for SignatureParamsWithPairing<E> {
78    fn as_ref(&self) -> &E::G1Affine {
79        &self.g1
80    }
81}
82
83/// The public parameters used during the proof of knowledge of signature are called `ProvingKey`. These are mutually
84/// agreed upon by the prover and verifier and can be same or different between different provers and verifiers
85#[serde_as]
86#[derive(
87    Clone, PartialEq, Eq, Debug, CanonicalSerialize, CanonicalDeserialize, Serialize, Deserialize,
88)]
89pub struct ProvingKey<G: AffineRepr> {
90    /// Called `u` in the paper
91    #[serde_as(as = "ArkObjectBytes")]
92    pub X: G,
93    /// Called `v` in the paper
94    #[serde_as(as = "ArkObjectBytes")]
95    pub Y: G,
96    /// Called `h` in the paper
97    #[serde_as(as = "ArkObjectBytes")]
98    pub Z: G,
99}
100
101impl<G: AffineRepr> ProvingKey<G> {
102    /// Generate using a random number generator
103    pub fn generate_using_rng<R: RngCore>(rng: &mut R) -> ProvingKey<G> {
104        ProvingKey {
105            X: G::Group::rand(rng).into(),
106            Y: G::Group::rand(rng).into(),
107            Z: G::Group::rand(rng).into(),
108        }
109    }
110
111    /// Generate by hashing known strings
112    pub fn generate_using_hash<D: Digest>(label: &[u8]) -> ProvingKey<G> {
113        // 3 G1 elements
114        ProvingKey {
115            X: affine_group_element_from_byte_slices![label, b" : X"],
116            Y: affine_group_element_from_byte_slices![label, b" : Y"],
117            Z: affine_group_element_from_byte_slices![label, b" : Z"],
118        }
119    }
120}
121
122impl<G: AffineRepr> AsRef<ProvingKey<G>> for ProvingKey<G> {
123    fn as_ref(&self) -> &ProvingKey<G> {
124        &self
125    }
126}
127
128impl<G: AffineRepr> SchnorrChallengeContributor for ProvingKey<G> {
129    fn challenge_contribution<W: Write>(&self, mut writer: W) -> Result<(), SchnorrError> {
130        self.X.serialize_compressed(&mut writer)?;
131        self.Y.serialize_compressed(&mut writer)?;
132        self.Z
133            .serialize_compressed(&mut writer)
134            .map_err(|e| e.into())
135    }
136}