schnorr_pok 0.23.0

Schnorr, Okamoto, Chaum-Pedersen protocols for proof of knowledge, equality and inequality of one or more discrete logs. Working in elliptic curve and pairing groups
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
//! Schnorr protocol for proving knowledge of discrete logs
//!
//! The following are specializations of the Schnorr protocol implemented using `SchnorrCommitment` and `SchnorrResponse`.
//! Proving knowledge of 1 or 2 discrete logs is quite common and the following avoid creating vectors.
//!
//! To prove knowledge of a single discrete log, i.e. given public `Y` and `G`, prove knowledge of `x` in `G * x = Y`:
//! 1. Prover chooses a random `r` and computes `T = G * r`
//! 2. Hashes `T` towards getting a challenge `c`.
//! 3. Computes response `s = r + c*x` and sends it to the verifier.
//! 4. Verifier checks if `G * s = T + Y*c`
//!
//! To prove knowledge of 2 discrete logs, i.e. given public `Y`, `G1` and `G2`, prove knowledge of `x1` and `x2` in `G1 * x1 + G2 * x2 = Y`.
//! 1. Prover chooses 2 random `r1` and `r2` and computes `T = G1 * r1 + G2 * r2`
//! 2. Hashes `T` towards getting a challenge `c`.
//! 3. Computes 2 responses `s1 = r1 + c*x1` and `s2 = r2 + c*x2` and sends them to the verifier.
//! 4. Verifier checks if `G1 * s1 + G2 * s2 = T + Y*c`

use crate::error::SchnorrError;
use ark_ec::{AffineRepr, CurveGroup};
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{io::Write, ops::Neg, vec::Vec};
use dock_crypto_utils::{
    randomized_mult_checker::RandomizedMultChecker, serde_utils::ArkObjectBytes,
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Protocol for proving knowledge of discrete log, i.e given public `Y` and `G`, prove knowledge of `x` in `G * x = y`
#[serde_as]
#[derive(
    Default,
    Clone,
    PartialEq,
    Eq,
    Debug,
    CanonicalSerialize,
    CanonicalDeserialize,
    Serialize,
    Deserialize,
    Zeroize,
    ZeroizeOnDrop,
)]
pub struct PokDiscreteLogProtocol<G: AffineRepr> {
    /// Commitment to randomness
    #[zeroize(skip)]
    #[serde_as(as = "ArkObjectBytes")]
    pub t: G,
    /// Randomness chosen by the prover
    #[serde_as(as = "ArkObjectBytes")]
    blinding: G::ScalarField,
    /// Prover's secret `x`
    #[serde_as(as = "ArkObjectBytes")]
    witness: G::ScalarField,
}

/// Proof of knowledge of discrete log
#[serde_as]
#[derive(
    Default,
    Clone,
    PartialEq,
    Eq,
    Debug,
    CanonicalSerialize,
    CanonicalDeserialize,
    Serialize,
    Deserialize,
)]
pub struct PokDiscreteLog<G: AffineRepr> {
    #[serde_as(as = "ArkObjectBytes")]
    pub t: G,
    #[serde_as(as = "ArkObjectBytes")]
    pub response: G::ScalarField,
}

/// Protocol for proving knowledge of 2 discrete logs, i.e given public `Y`, `G1` and `G2`, prove knowledge of `x1` and `x2` in `G1 * x1 + G2 * x2 = Y`
#[serde_as]
#[derive(
    Default,
    Clone,
    PartialEq,
    Eq,
    Debug,
    CanonicalSerialize,
    CanonicalDeserialize,
    Serialize,
    Deserialize,
    Zeroize,
    ZeroizeOnDrop,
)]
pub struct PokPedersenCommitmentProtocol<G: AffineRepr> {
    #[zeroize(skip)]
    #[serde_as(as = "ArkObjectBytes")]
    pub t: G,
    #[serde_as(as = "ArkObjectBytes")]
    pub(crate) blinding1: G::ScalarField,
    #[serde_as(as = "ArkObjectBytes")]
    pub(crate) witness1: G::ScalarField,
    #[serde_as(as = "ArkObjectBytes")]
    pub(crate) blinding2: G::ScalarField,
    #[serde_as(as = "ArkObjectBytes")]
    pub(crate) witness2: G::ScalarField,
}

/// Proof of knowledge of 2 discrete logs
#[serde_as]
#[derive(
    Default,
    Clone,
    PartialEq,
    Eq,
    Debug,
    CanonicalSerialize,
    CanonicalDeserialize,
    Serialize,
    Deserialize,
)]
pub struct PokPedersenCommitment<G: AffineRepr> {
    #[serde_as(as = "ArkObjectBytes")]
    pub t: G,
    #[serde_as(as = "ArkObjectBytes")]
    pub response1: G::ScalarField,
    #[serde_as(as = "ArkObjectBytes")]
    pub response2: G::ScalarField,
}

impl<G: AffineRepr> PokDiscreteLogProtocol<G> {
    pub fn init(witness: G::ScalarField, blinding: G::ScalarField, base: &G) -> Self {
        let t = base.mul_bigint(blinding.into_bigint()).into_affine();
        Self {
            t,
            blinding,
            witness,
        }
    }

    pub fn challenge_contribution<W: Write>(
        &self,
        base: &G,
        y: &G,
        writer: W,
    ) -> Result<(), SchnorrError> {
        Self::compute_challenge_contribution(base, y, &self.t, writer)
    }

    pub fn gen_proof(self, challenge: &G::ScalarField) -> PokDiscreteLog<G> {
        let response = self.blinding + (self.witness * *challenge);
        PokDiscreteLog {
            t: self.t,
            response,
        }
    }

    pub fn compute_challenge_contribution<W: Write>(
        base: &G,
        y: &G,
        t: &G,
        mut writer: W,
    ) -> Result<(), SchnorrError> {
        base.serialize_compressed(&mut writer)?;
        y.serialize_compressed(&mut writer)?;
        t.serialize_compressed(writer).map_err(|e| e.into())
    }
}

impl<G: AffineRepr> PokDiscreteLog<G> {
    pub fn challenge_contribution<W: Write>(
        &self,
        base: &G,
        y: &G,
        writer: W,
    ) -> Result<(), SchnorrError> {
        PokDiscreteLogProtocol::compute_challenge_contribution(base, y, &self.t, writer)
    }

    /// `base*response - y*challenge == t`
    pub fn verify(&self, y: &G, base: &G, challenge: &G::ScalarField) -> bool {
        let mut expected = base.mul_bigint(self.response.into_bigint());
        expected -= y.mul_bigint(challenge.into_bigint());
        expected.into_affine() == self.t
    }

    pub fn verify_using_randomized_mult_checker(
        &self,
        y: G,
        base: G,
        challenge: &G::ScalarField,
        rmc: &mut RandomizedMultChecker<G>,
    ) {
        rmc.add_2(base, &self.response, y, &challenge.neg(), self.t)
    }
}

impl<G: AffineRepr> PokPedersenCommitmentProtocol<G> {
    pub fn init(
        witness1: G::ScalarField,
        blinding1: G::ScalarField,
        base1: &G,
        witness2: G::ScalarField,
        blinding2: G::ScalarField,
        base2: &G,
    ) -> Self {
        let t = (base1.mul_bigint(blinding1.into_bigint())
            + base2.mul_bigint(blinding2.into_bigint()))
        .into_affine();
        Self {
            t,
            blinding1,
            witness1,
            blinding2,
            witness2,
        }
    }

    pub fn challenge_contribution<W: Write>(
        &self,
        base1: &G,
        base2: &G,
        y: &G,
        writer: W,
    ) -> Result<(), SchnorrError> {
        Self::compute_challenge_contribution(base1, base2, y, &self.t, writer)
    }

    pub fn gen_proof(self, challenge: &G::ScalarField) -> PokPedersenCommitment<G> {
        let response1 = self.blinding1 + (self.witness1 * *challenge);
        let response2 = self.blinding2 + (self.witness2 * *challenge);
        PokPedersenCommitment {
            t: self.t,
            response1,
            response2,
        }
    }

    pub fn compute_challenge_contribution<W: Write>(
        base1: &G,
        base2: &G,
        y: &G,
        t: &G,
        mut writer: W,
    ) -> Result<(), SchnorrError> {
        base1.serialize_compressed(&mut writer)?;
        base2.serialize_compressed(&mut writer)?;
        y.serialize_compressed(&mut writer)?;
        t.serialize_compressed(&mut writer)?;
        Ok(())
    }
}

impl<G: AffineRepr> PokPedersenCommitment<G> {
    pub fn challenge_contribution<W: Write>(
        &self,
        base1: &G,
        base2: &G,
        y: &G,
        writer: W,
    ) -> Result<(), SchnorrError> {
        PokPedersenCommitmentProtocol::compute_challenge_contribution(
            base1, base2, y, &self.t, writer,
        )
    }

    /// `base1*response1 + base2*response2 - y*challenge == t`
    pub fn verify(&self, y: &G, base1: &G, base2: &G, challenge: &G::ScalarField) -> bool {
        let mut expected = base1.mul_bigint(self.response1.into_bigint());
        expected += base2.mul_bigint(self.response2.into_bigint());
        expected -= y.mul_bigint(challenge.into_bigint());
        expected.into_affine() == self.t
    }

    /// Same as `Self::verify` except it uses `RandomizedMultChecker` to combine the scalar multiplication checks into a single
    pub fn verify_using_randomized_mult_checker(
        &self,
        y: G,
        base1: G,
        base2: G,
        challenge: &G::ScalarField,
        rmc: &mut RandomizedMultChecker<G>,
    ) {
        rmc.add_3(
            base1,
            &self.response1,
            base2,
            &self.response2,
            y,
            &challenge.neg(),
            self.t,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{pok_generalized_pedersen::compute_random_oracle_challenge, test_serialization};
    use ark_bls12_381::{Bls12_381, Fr};
    use ark_ec::pairing::Pairing;
    use ark_std::{
        rand::{rngs::StdRng, SeedableRng},
        UniformRand,
    };
    use blake2::Blake2b512;

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

        macro_rules! check {
            ($group_affine:ident, $group_projective:ident) => {
                let base = <Bls12_381 as Pairing>::$group_projective::rand(&mut rng).into_affine();
                let witness = Fr::rand(&mut rng);
                let y = base.mul_bigint(witness.into_bigint()).into_affine();
                let blinding = Fr::rand(&mut rng);
                let protocol =
                    PokDiscreteLogProtocol::<<Bls12_381 as Pairing>::$group_affine>::init(
                        witness, blinding, &base,
                    );
                let mut chal_contrib_prover = vec![];
                protocol
                    .challenge_contribution(&base, &y, &mut chal_contrib_prover)
                    .unwrap();

                test_serialization!(
                    PokDiscreteLogProtocol<<Bls12_381 as Pairing>::$group_affine>,
                    protocol
                );

                let challenge_prover =
                    compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_contrib_prover);
                let proof = protocol.gen_proof(&challenge_prover);

                let mut chal_contrib_verifier = vec![];
                proof
                    .challenge_contribution(&base, &y, &mut chal_contrib_verifier)
                    .unwrap();

                let challenge_verifier =
                    compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_contrib_verifier);
                assert!(proof.verify(&y, &base, &challenge_verifier));
                assert_eq!(chal_contrib_prover, chal_contrib_verifier);
                assert_eq!(challenge_prover, challenge_verifier);

                test_serialization!(PokDiscreteLog<<Bls12_381 as Pairing>::$group_affine>, proof);

                let mut checker = RandomizedMultChecker::new_using_rng(&mut rng);
                proof.verify_using_randomized_mult_checker(
                    y,
                    base,
                    &challenge_verifier,
                    &mut checker,
                );
                assert!(checker.verify());

                // Incorrect should fail
                let mut checker = RandomizedMultChecker::new_using_rng(&mut rng);
                proof.verify_using_randomized_mult_checker(
                    base,
                    y,
                    &challenge_verifier,
                    &mut checker,
                );
                assert!(!checker.verify());
            };
        }

        check!(G1Affine, G1);
        check!(G2Affine, G2);
    }

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

        macro_rules! check {
            ($group_affine:ident, $group_projective:ident) => {
                let base1 = <Bls12_381 as Pairing>::$group_projective::rand(&mut rng).into_affine();
                let witness1 = Fr::rand(&mut rng);
                let base2 = <Bls12_381 as Pairing>::$group_projective::rand(&mut rng).into_affine();
                let witness2 = Fr::rand(&mut rng);
                let y = (base1 * witness1 + base2 * witness2).into_affine();
                let blinding1 = Fr::rand(&mut rng);
                let blinding2 = Fr::rand(&mut rng);
                let protocol =
                    PokPedersenCommitmentProtocol::<<Bls12_381 as Pairing>::$group_affine>::init(
                        witness1, blinding1, &base1, witness2, blinding2, &base2,
                    );
                let mut chal_contrib_prover = vec![];
                protocol
                    .challenge_contribution(&base1, &base2, &y, &mut chal_contrib_prover)
                    .unwrap();

                test_serialization!(
                    PokPedersenCommitmentProtocol<<Bls12_381 as Pairing>::$group_affine>,
                    protocol
                );

                let challenge_prover =
                    compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_contrib_prover);
                let proof = protocol.gen_proof(&challenge_prover);

                let mut chal_contrib_verifier = vec![];
                proof
                    .challenge_contribution(&base1, &base2, &y, &mut chal_contrib_verifier)
                    .unwrap();

                let challenge_verifier =
                    compute_random_oracle_challenge::<Fr, Blake2b512>(&chal_contrib_verifier);
                assert!(proof.verify(&y, &base1, &base2, &challenge_verifier));
                assert_eq!(chal_contrib_prover, chal_contrib_verifier);
                assert_eq!(challenge_prover, challenge_verifier);

                test_serialization!(
                    PokPedersenCommitment<<Bls12_381 as Pairing>::$group_affine>,
                    proof
                );

                let mut checker = RandomizedMultChecker::new_using_rng(&mut rng);
                proof.verify_using_randomized_mult_checker(
                    y,
                    base1,
                    base2,
                    &challenge_verifier,
                    &mut checker,
                );
                assert!(checker.verify());

                let mut checker = RandomizedMultChecker::new_using_rng(&mut rng);
                proof.verify_using_randomized_mult_checker(
                    y,
                    base2,
                    base1,
                    &challenge_verifier,
                    &mut checker,
                );
                assert!(!checker.verify());
            };
        }

        check!(G1Affine, G1);
        check!(G2Affine, G2);
    }
}