compressed_sigma 0.0.4

Compressed Sigma Protocols and proofs of k-Out-Of-n Partial Knowledge
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
//! Amortized sigma protocol as described in Appendix B of the paper "Compressed Sigma Protocol Theory..."

use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
use ark_ff::{PrimeField, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{ops::Add, rand::RngCore, vec::Vec, UniformRand};
use digest::Digest;

use dock_crypto_utils::ff::inner_product;

use crate::{
    compressed_linear_form,
    error::CompSigmaError,
    transforms::LinearForm,
    utils::{amortized_response, get_n_powers},
};

#[derive(Clone, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize)]
pub struct RandomCommitment<G: AffineRepr> {
    /// Maximum size of the witness vectors
    pub max_size: usize,
    pub r: Vec<G::ScalarField>,
    pub rho: G::ScalarField,
    pub A: G,
    pub t: G::ScalarField,
}

#[derive(Clone, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize)]
pub struct Response<G: AffineRepr> {
    pub z_tilde: Vec<G::ScalarField>,
    pub phi: G::ScalarField,
}

impl<G> RandomCommitment<G>
where
    G: AffineRepr,
{
    pub fn new<R: RngCore, L: LinearForm<G::ScalarField>>(
        rng: &mut R,
        g: &[G],
        h: &G,
        max_size: usize,
        linear_form: &L,
        blindings: Option<Vec<G::ScalarField>>,
    ) -> Result<Self, CompSigmaError> {
        if g.len() < max_size {
            return Err(CompSigmaError::VectorTooShort);
        }
        let r = if let Some(blindings) = blindings {
            if blindings.len() != max_size {
                return Err(CompSigmaError::VectorLenMismatch);
            }
            blindings
        } else {
            (0..max_size).map(|_| G::ScalarField::rand(rng)).collect()
        };
        let rho = G::ScalarField::rand(rng);
        let t = linear_form.eval(&r);
        // h * rho is done separately to avoid copying g
        let A = G::Group::msm_unchecked(g, &r).add(&h.mul_bigint(rho.into_bigint()));
        Ok(Self {
            max_size,
            r,
            rho,
            A: A.into_affine(),
            t,
        })
    }

    pub fn response(
        &self,
        witnesses: Vec<&[G::ScalarField]>,
        gammas: &[G::ScalarField],
        challenge: &G::ScalarField,
    ) -> Result<Response<G>, CompSigmaError> {
        if witnesses.len() != gammas.len() {
            return Err(CompSigmaError::VectorLenMismatch);
        }
        let count_commitments = witnesses.len();
        // `challenge_powers` is of form [c, c^2, c^3, ..., c^n]
        let challenge_powers = get_n_powers(challenge.clone(), count_commitments);

        // z_tilde_i = r_i + \sum_{j in count_commitments}(witnesses_j_i * challenge^j)
        let z_tilde = amortized_response(self.max_size, &challenge_powers, &self.r, witnesses);

        // phi = rho + \sum_{j}(gamma_j * c^j)
        let phi = self.rho + inner_product(&challenge_powers, gammas);
        Ok(Response { z_tilde, phi })
    }
}

impl<G> Response<G>
where
    G: AffineRepr,
{
    pub fn is_valid<L: LinearForm<G::ScalarField>>(
        &self,
        g: &[G],
        h: &G,
        max_size: usize,
        commitments: &[G],
        y: &[G::ScalarField],
        linear_form: &L,
        A: &G,
        t: &G::ScalarField,
        challenge: &G::ScalarField,
    ) -> Result<(), CompSigmaError> {
        if g.len() < max_size {
            return Err(CompSigmaError::VectorTooShort);
        }
        if commitments.len() != y.len() {
            return Err(CompSigmaError::VectorLenMismatch);
        }
        if self.z_tilde.len() != max_size {
            return Err(CompSigmaError::VectorLenMismatch);
        }

        let count_commitments = commitments.len();
        // `challenge_powers` is of form [c, c^2, c^3, ..., c^n]
        let challenge_powers = get_n_powers(challenge.clone(), count_commitments);

        // P_tilde = A + \sum_{i}(P_i * c^i)
        let mut P_tilde = A.into_group();
        P_tilde += G::Group::msm_unchecked(commitments, &challenge_powers);

        // Check g*z_tilde + h*phi == P_tilde
        let g_z = G::Group::msm_unchecked(g, &self.z_tilde);
        let h_phi = h.mul(self.phi);
        if (g_z + h_phi) != P_tilde {
            return Err(CompSigmaError::InvalidResponse);
        }

        // Check \sum_{i}(y_i * c^i) + t == L(z_tilde)
        let c_y = inner_product(&challenge_powers, y);
        if !(c_y + t - linear_form.eval(&self.z_tilde)).is_zero() {
            return Err(CompSigmaError::InvalidResponse);
        }
        Ok(())
    }

    pub fn compress<D: Digest, L: LinearForm<G::ScalarField>>(
        self,
        g: &[G],
        h: &G,
        k: &G,
        linear_form: &L,
        new_challenge: &G::ScalarField,
    ) -> compressed_linear_form::Response<G> {
        let (g_hat, L_tilde) =
            compressed_linear_form::prepare_generators_and_linear_form_for_compression(
                g,
                h,
                linear_form,
                new_challenge,
            );

        let mut z_hat = self.z_tilde;
        z_hat.push(self.phi);
        compressed_linear_form::RandomCommitment::compressed_response::<D, L>(
            z_hat, g_hat, &k, L_tilde,
        )
    }

    pub fn is_valid_compressed<D: Digest, L: LinearForm<G::ScalarField>>(
        g: &[G],
        h: &G,
        k: &G,
        linear_form: &L,
        Ps: &[G],
        ys: &[G::ScalarField],
        A: &G,
        t: &G::ScalarField,
        challenge: &G::ScalarField,
        new_challenge: &G::ScalarField,
        compressed_resp: &compressed_linear_form::Response<G>,
    ) -> Result<(), CompSigmaError> {
        let (g_hat, L_tilde) =
            compressed_linear_form::prepare_generators_and_linear_form_for_compression(
                g,
                h,
                linear_form,
                new_challenge,
            );
        let Q = calculate_Q(k, Ps, ys, A, t, challenge, new_challenge);
        compressed_resp.validate_compressed::<D, L>(Q, g_hat, L_tilde, &k)
    }
}

pub fn prepare_for_compression<G: AffineRepr, L: LinearForm<G::ScalarField>>(
    g: &[G],
    h: &G,
    k: &G,
    linear_form: &L,
    Ps: &[G],
    ys: &[G::ScalarField],
    A: &G,
    t: &G::ScalarField,
    challenge: &G::ScalarField,
    new_challenge: &G::ScalarField,
) -> (Vec<G>, G::Group, L) {
    // g_hat = (g_0, g_1, ... g_n, h)
    let mut g_hat = g.to_vec();
    g_hat.push(*h);

    // L_tilde = new_challenge * linear_form
    let L_tilde = linear_form.scale(new_challenge);

    let challenge_powers = get_n_powers(challenge.clone(), Ps.len());
    let P = G::Group::msm_unchecked(Ps, &challenge_powers);
    let Y = challenge_powers
        .iter()
        .zip(ys.iter())
        .map(|(c, y)| *c * y)
        .reduce(|a, b| a + b)
        .unwrap();

    // Q = P + k * (new_challenge*(Y + t)) + A
    let Q = P + k.mul(*new_challenge * (Y + t)) + A;
    (g_hat, Q, L_tilde)
}

fn calculate_Q<G: AffineRepr>(
    k: &G,
    Ps: &[G],
    ys: &[G::ScalarField],
    A: &G,
    t: &G::ScalarField,
    challenge: &G::ScalarField,
    new_challenge: &G::ScalarField,
) -> G::Group {
    let challenge_powers = get_n_powers(challenge.clone(), Ps.len());
    let P = G::Group::msm_unchecked(Ps, &challenge_powers);
    let Y = challenge_powers
        .iter()
        .zip(ys.iter())
        .map(|(c, y)| *c * y)
        .reduce(|a, b| a + b)
        .unwrap();

    // Q = P + k * (new_challenge*(Y + t)) + A
    P + k.mul(*new_challenge * (Y + t)) + A
}

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

    type Fr = <Bls12_381 as Pairing>::ScalarField;

    struct TestLinearForm1 {}

    impl LinearForm<Fr> for TestLinearForm1 {
        fn eval(&self, x: &[Fr]) -> Fr {
            x.iter().fold(Fr::zero(), |accum, item| accum + item)
        }

        fn scale(&self, _scalar: &Fr) -> Self {
            TestLinearForm1 {}
        }

        fn add(&self, _other: &Self) -> Self {
            TestLinearForm1 {}
        }

        fn split_in_half(&self) -> (Self, Self) {
            unimplemented!()
        }

        fn size(&self) -> usize {
            unimplemented!()
        }

        fn pad(&self, _new_size: usize) -> Self {
            unimplemented!()
        }
    }

    #[derive(Clone)]
    struct TestLinearForm2 {
        pub constants: Vec<Fr>,
    }

    impl_simple_linear_form!(TestLinearForm2, Fr);

    #[test]
    fn amortization() {
        fn check(max_size: usize) {
            let mut rng = StdRng::seed_from_u64(0u64);
            let linear_form_1 = TestLinearForm1 {};
            let linear_form_2 = TestLinearForm2 {
                constants: (0..max_size)
                    .map(|_| Fr::rand(&mut rng))
                    .collect::<Vec<_>>(),
            };

            let x1 = (0..max_size - 2)
                .map(|_| Fr::rand(&mut rng))
                .collect::<Vec<_>>();
            let gamma1 = Fr::rand(&mut rng);
            let x2 = (0..max_size - 1)
                .map(|_| Fr::rand(&mut rng))
                .collect::<Vec<_>>();
            let gamma2 = Fr::rand(&mut rng);
            let x3 = (0..max_size)
                .map(|_| Fr::rand(&mut rng))
                .collect::<Vec<_>>();
            let gamma3 = Fr::rand(&mut rng);

            let g = (0..max_size)
                .map(|_| <Bls12_381 as Pairing>::G1::rand(&mut rng).into_affine())
                .collect::<Vec<_>>();
            let h = <Bls12_381 as Pairing>::G1::rand(&mut rng).into_affine();

            let comm1 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x1)
                + h.mul_bigint(gamma1.into_bigint()))
            .into_affine();
            let eval1 = linear_form_1.eval(&x1);
            let eval12 = linear_form_2.eval(&x1);

            let comm2 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x2)
                + h.mul_bigint(gamma2.into_bigint()))
            .into_affine();
            let eval2 = linear_form_1.eval(&x2);
            let eval22 = linear_form_2.eval(&x2);

            let comm3 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x3)
                + h.mul_bigint(gamma3.into_bigint()))
            .into_affine();
            let eval3 = linear_form_1.eval(&x3);
            let eval32 = linear_form_2.eval(&x3);

            let rand_comm =
                RandomCommitment::new(&mut rng, &g, &h, max_size, &linear_form_1, None).unwrap();
            assert_eq!(rand_comm.r.len(), max_size);
            let challenge = Fr::rand(&mut rng);
            let response = rand_comm
                .response(vec![&x1, &x2, &x3], &[gamma1, gamma2, gamma3], &challenge)
                .unwrap();
            assert_eq!(response.z_tilde.len(), max_size);
            response
                .is_valid(
                    &g,
                    &h,
                    max_size,
                    &[comm1, comm2, comm3],
                    &[eval1, eval2, eval3],
                    &linear_form_1,
                    &rand_comm.A,
                    &rand_comm.t,
                    &challenge,
                )
                .unwrap();

            let rand_comm =
                RandomCommitment::new(&mut rng, &g, &h, max_size, &linear_form_2, None).unwrap();
            assert_eq!(rand_comm.r.len(), max_size);
            let challenge = Fr::rand(&mut rng);
            let response = rand_comm
                .response(vec![&x1, &x2, &x3], &[gamma1, gamma2, gamma3], &challenge)
                .unwrap();
            assert_eq!(response.z_tilde.len(), max_size);
            response
                .is_valid(
                    &g,
                    &h,
                    max_size,
                    &[comm1, comm2, comm3],
                    &[eval12, eval22, eval32],
                    &linear_form_2,
                    &rand_comm.A,
                    &rand_comm.t,
                    &challenge,
                )
                .unwrap();
        }

        check(3);
        check(7);
        check(15);
        check(31);
    }

    #[test]
    fn amortization_and_compression() {
        let max_size = 7;
        let mut rng = StdRng::seed_from_u64(0u64);
        let mut linear_form = TestLinearForm2 {
            constants: (0..max_size)
                .map(|_| Fr::rand(&mut rng))
                .collect::<Vec<_>>(),
        };
        linear_form.constants.push(Fr::zero());

        let x1 = (0..max_size - 2)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();
        let gamma1 = Fr::rand(&mut rng);
        let x2 = (0..max_size - 1)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();
        let gamma2 = Fr::rand(&mut rng);
        let x3 = (0..max_size)
            .map(|_| Fr::rand(&mut rng))
            .collect::<Vec<_>>();
        let gamma3 = Fr::rand(&mut rng);

        let g = (0..max_size)
            .map(|_| <Bls12_381 as Pairing>::G1::rand(&mut rng).into_affine())
            .collect::<Vec<_>>();
        let h = <Bls12_381 as Pairing>::G1::rand(&mut rng).into_affine();
        let k = <Bls12_381 as Pairing>::G1::rand(&mut rng).into_affine();

        let comm1 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x1)
            + h.mul_bigint(gamma1.into_bigint()))
        .into_affine();
        let eval1 = linear_form.eval(&x1);

        let comm2 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x2)
            + h.mul_bigint(gamma2.into_bigint()))
        .into_affine();
        let eval2 = linear_form.eval(&x2);

        let comm3 = (<Bls12_381 as Pairing>::G1::msm_unchecked(&g, &x3)
            + h.mul_bigint(gamma3.into_bigint()))
        .into_affine();
        let eval3 = linear_form.eval(&x3);

        let comms = [comm1, comm2, comm3];
        let evals = [eval1, eval2, eval3];
        let rand_comm =
            RandomCommitment::new(&mut rng, &g, &h, max_size, &linear_form, None).unwrap();
        assert_eq!(rand_comm.r.len(), max_size);
        let c_0 = Fr::rand(&mut rng);
        let response = rand_comm
            .response(vec![&x1, &x2, &x3], &[gamma1, gamma2, gamma3], &c_0)
            .unwrap();
        assert_eq!(response.z_tilde.len(), max_size);
        response
            .is_valid(
                &g,
                &h,
                max_size,
                &comms,
                &evals,
                &linear_form,
                &rand_comm.A,
                &rand_comm.t,
                &c_0,
            )
            .unwrap();

        let c_1 = Fr::rand(&mut rng);
        let comp_resp = response.compress::<Blake2b512, _>(&g, &h, &k, &linear_form, &c_1);
        Response::is_valid_compressed::<Blake2b512, _>(
            &g,
            &h,
            &k,
            &linear_form,
            &comms,
            &evals,
            &rand_comm.A,
            &rand_comm.t,
            &c_0,
            &c_1,
            &comp_resp,
        )
        .unwrap();
    }
}