jf_relation/gadgets/ultraplonk/
non_native_gates.rs

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
// Copyright (c) 2022 Espresso Systems (espressosys.com)
// This file is part of the Jellyfish library.

// You should have received a copy of the MIT License
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

//! This module implements non-native circuits that are mainly
//! useful for rescue hash function.

use super::mod_arith::{FpElem, FpElemVar};
use crate::{Circuit, CircuitError, PlonkCircuit};
use ark_ff::{BigInteger, PrimeField};
use ark_std::{format, vec::Vec};

impl<F: PrimeField> PlonkCircuit<F> {
    /// generate a non-native circuit for the statement x^11 = y
    ///
    /// Input:
    ///  - variable representation of x over a target field `T` whose order is
    ///    less than F.
    ///  - variable representation of x^11 over a same field
    ///
    /// Cost: 5 mod_mul + 2 equal gate
    pub fn non_native_power_11_gate<T: PrimeField>(
        &mut self,
        x: &FpElemVar<F>,
        x_to_11: &FpElemVar<F>,
    ) -> Result<(), CircuitError> {
        self.check_var_bound(x.components().0)?;
        self.check_var_bound(x.components().1)?;
        self.check_var_bound(x_to_11.components().0)?;
        self.check_var_bound(x_to_11.components().1)?;

        if T::MODULUS_BIT_SIZE >= F::MODULUS_BIT_SIZE {
            return Err(CircuitError::NotSupported(format!(
                "Target field size ({}) is greater than evaluation field size (P{})",
                T::MODULUS_BIT_SIZE,
                F::MODULUS_BIT_SIZE
            )));
        }

        // x^11 = y
        let y = self.non_native_power_11_gen::<T>(x)?;
        self.enforce_equal(x_to_11.components().0, y.components().0)?;
        self.enforce_equal(x_to_11.components().1, y.components().1)
    }

    /// generate a non-native circuit for the statement x^11 = y
    ///
    /// Input: variable representation of x over a target
    /// field `T` whose order is less than F.
    ///
    /// Output: variable representation of x^11
    ///
    /// Cost: 5 mod_mul
    pub fn non_native_power_11_gen<T: PrimeField>(
        &mut self,
        x: &FpElemVar<F>,
    ) -> Result<FpElemVar<F>, CircuitError> {
        // // checks already done by the caller
        // if T::MODULUS_BIT_SIZE >= F::MODULUS_BIT_SIZE {
        //     return Err(CircuitError::NotSupported(format!(
        //         "Target field size ({}) is greater than evaluation field size (P{})",
        //         T::MODULUS_BIT_SIZE,
        //         F::MODULUS_BIT_SIZE
        //     ))
        //     .into());
        // }

        // convert T::MODULUS into an element in F
        // Guaranteed without mod reduction since T::MODULUS_BIT_SIZE <
        // F::MODULUS_BIT_SIZE
        let t_modulus = F::from_le_bytes_mod_order(T::MODULUS.to_bytes_le().as_ref());

        // convert t_modulus into FpElem
        let m = x.param_m();
        let two_power_m = Some(x.two_power_m());
        let p = FpElem::new(&t_modulus, m, two_power_m)?;

        // x^11 = y
        let x2 = self.mod_mul(x, x, &p)?;
        let x3 = self.mod_mul(&x2, x, &p)?;
        let x4 = self.mod_mul(&x2, &x2, &p)?;
        let x8 = self.mod_mul(&x4, &x4, &p)?;
        self.mod_mul(&x3, &x8, &p)
    }

    /// generate a non-native circuit for the statement x^5 = y
    ///
    /// Input: variable representation of x over a target
    /// field `T` whose order is less than F.
    ///
    /// Output: variable representation of x^5
    ///
    /// Cost: 3 mod_mul
    pub fn non_native_power_5_gen<T: PrimeField>(
        &mut self,
        x: &FpElemVar<F>,
    ) -> Result<FpElemVar<F>, CircuitError> {
        // checks already done by the caller
        if T::MODULUS_BIT_SIZE >= F::MODULUS_BIT_SIZE {
            return Err(CircuitError::NotSupported(format!(
                "Target field size ({}) is greater than evaluation field size (P{})",
                T::MODULUS_BIT_SIZE,
                F::MODULUS_BIT_SIZE
            )));
        }

        // convert T::MODULUS into an element in F
        // Guaranteed without mod reduction since T::MODULUS_BIT_SIZE <
        // F::MODULUS_BIT_SIZE
        let t_modulus = F::from_le_bytes_mod_order(T::MODULUS.to_bytes_le().as_ref());

        // convert t_modulus into FpElem
        let m = x.param_m();
        let two_power_m = Some(x.two_power_m());
        let p = FpElem::new(&t_modulus, m, two_power_m)?;

        // x^5 = y
        let x2 = self.mod_mul(x, x, &p)?;
        let x3 = self.mod_mul(&x2, x, &p)?;
        self.mod_mul(&x2, &x3, &p)
    }

    /// Input vector x and y, and a constant c,
    /// generate a non-native circuit for the statement
    ///     var_output = inner_product(x, y) + c
    /// Input: variable representation of x, y, c over a target
    /// field `T` whose order is less than F.
    ///
    /// Cost: 4 mod_mul_constant + 1 mod_add_internal
    #[allow(clippy::many_single_char_names)]
    pub fn non_native_linear_gen<T: PrimeField>(
        &mut self,
        x: &[FpElemVar<F>],
        y: &[FpElem<F>],
        c: &FpElem<F>,
    ) -> Result<FpElemVar<F>, CircuitError> {
        let m = c.param_m();
        let two_power_m = Some(c.two_power_m());

        // check the correctness of parameters
        if T::MODULUS_BIT_SIZE >= F::MODULUS_BIT_SIZE {
            return Err(CircuitError::NotSupported(format!(
                "Target field size ({}) is greater than evaluation field size ({})",
                T::MODULUS_BIT_SIZE,
                F::MODULUS_BIT_SIZE
            )));
        }

        if x.len() != y.len() {
            return Err(CircuitError::ParameterError(format!(
                "inputs x any y has different length ({} vs {})",
                x.len(),
                y.len()
            )));
        }
        for e in x {
            if m != e.param_m() {
                return Err(CircuitError::ParameterError(format!(
                    "inputs x any c has different m parameter ({} vs {})",
                    e.param_m(),
                    m
                )));
            }
        }
        for e in y {
            if m != e.param_m() {
                return Err(CircuitError::ParameterError(format!(
                    "inputs y any c has different m parameter ({} vs {})",
                    e.param_m(),
                    m
                )));
            }
        }

        // convert T::MODULUS into an element in F
        // Guaranteed without mod reduction since T::MODULUS_BIT_SIZE <
        // F::MODULUS_BIT_SIZE
        let t_modulus = F::from_le_bytes_mod_order(T::MODULUS.to_bytes_le().as_ref());

        // convert t_modulus into FpElem
        let p = FpElem::new(&t_modulus, m, two_power_m)?;

        // generate the linear statement
        // (\sum x[i] * y[i]) + c
        let xiyi: Vec<FpElemVar<F>> = x
            .iter()
            .zip(y)
            .map(|(xi, yi)| self.mod_mul_constant(xi, yi, &p))
            .collect::<Result<Vec<FpElemVar<F>>, _>>()?;
        let sum_xiyi = self.mod_add_vec(xiyi.as_ref(), &p)?;
        self.mod_add_constant(&sum_xiyi, c, &p)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Variable;
    use ark_bls12_377::Fq as Fq377;
    use ark_ed_on_bls12_377::Fq as FqEd377;
    use jf_utils::test_rng;

    const RANGE_BIT_LEN_FOR_TEST: usize = 8;

    #[test]
    fn test_non_native_power_11_gen() -> Result<(), CircuitError> {
        // use bls12-377 base field to prove rescue over jubjub377 base field
        test_non_native_power_11_gen_helper::<FqEd377, Fq377>()
    }

    fn test_non_native_power_11_gen_helper<T: PrimeField, F: PrimeField>(
    ) -> Result<(), CircuitError> {
        let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_ultra_plonk(RANGE_BIT_LEN_FOR_TEST);

        let mut rng = test_rng();
        let x_t = T::rand(&mut rng);
        let y_t = x_t.pow([11]);
        let x_p = F::from_le_bytes_mod_order(x_t.into_bigint().to_bytes_le().as_ref());
        let y_p = F::from_le_bytes_mod_order(y_t.into_bigint().to_bytes_le().as_ref());

        let m = (T::MODULUS_BIT_SIZE as usize / 2 / RANGE_BIT_LEN_FOR_TEST + 1)
            * RANGE_BIT_LEN_FOR_TEST;

        let x_var = circuit.create_variable(x_p)?;
        let y_var = circuit.create_variable(y_p)?;

        let x_split_vars = FpElemVar::new_unchecked(&mut circuit, x_var, m, None)?;
        let x11_split_vars = circuit.non_native_power_11_gen::<T>(&x_split_vars)?;
        let x11_var = x11_split_vars.convert_to_var(&mut circuit)?;

        // good path
        circuit.enforce_equal(x11_var, y_var)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_ok());

        // bad path: wrong witness should fail
        let witness = circuit.witness(y_var)?;
        *circuit.witness_mut(y_var) = F::rand(&mut rng);
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());
        *circuit.witness_mut(y_var) = witness;

        // bad path: wrong value should fail
        let y_wrong = F::rand(&mut rng);
        let y_wrong_var = circuit.create_variable(y_wrong)?;
        circuit.enforce_equal(x11_var, y_wrong_var)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());

        Ok(())
    }

    #[test]
    fn test_non_native_power_5_gen() -> Result<(), CircuitError> {
        // use bls12-377 base field to prove rescue over jubjub377 base field
        test_non_native_power_5_gen_helper::<FqEd377, Fq377>()
    }

    fn test_non_native_power_5_gen_helper<T: PrimeField, F: PrimeField>() -> Result<(), CircuitError>
    {
        let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_ultra_plonk(RANGE_BIT_LEN_FOR_TEST);

        let mut rng = test_rng();
        let x_t = T::rand(&mut rng);
        let y_t = x_t.pow([5]);
        let x_p = F::from_le_bytes_mod_order(x_t.into_bigint().to_bytes_le().as_ref());
        let y_p = F::from_le_bytes_mod_order(y_t.into_bigint().to_bytes_le().as_ref());

        let m = (T::MODULUS_BIT_SIZE as usize / 2 / RANGE_BIT_LEN_FOR_TEST + 1)
            * RANGE_BIT_LEN_FOR_TEST;

        let x_var = circuit.create_variable(x_p)?;
        let y_var = circuit.create_variable(y_p)?;

        let x_split_vars = FpElemVar::new_unchecked(&mut circuit, x_var, m, None)?;
        let x5_split_vars = circuit.non_native_power_5_gen::<T>(&x_split_vars)?;
        let x5_var = x5_split_vars.convert_to_var(&mut circuit)?;

        // good path
        circuit.enforce_equal(x5_var, y_var)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_ok());

        // bad path: wrong witness should fail
        let witness = circuit.witness(y_var)?;
        *circuit.witness_mut(y_var) = F::rand(&mut rng);
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());
        *circuit.witness_mut(y_var) = witness;

        // bad path: wrong value should fail
        let y_wrong = F::rand(&mut rng);
        let y_wrong_var = circuit.create_variable(y_wrong)?;
        circuit.enforce_equal(x5_var, y_wrong_var)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());

        Ok(())
    }

    #[test]
    fn test_non_native_power_11_gate() -> Result<(), CircuitError> {
        // use bls12-377 base field to prove rescue over bls scalar field
        test_non_native_power_11_gate_helper::<FqEd377, Fq377>()
    }

    fn test_non_native_power_11_gate_helper<T: PrimeField, F: PrimeField>(
    ) -> Result<(), CircuitError> {
        let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_ultra_plonk(RANGE_BIT_LEN_FOR_TEST);

        let mut rng = test_rng();
        let x_t = T::rand(&mut rng);
        let y_t = x_t.pow([11]);
        let x_p = F::from_le_bytes_mod_order(x_t.into_bigint().to_bytes_le().as_ref());
        let y_p = F::from_le_bytes_mod_order(y_t.into_bigint().to_bytes_le().as_ref());

        let m = (T::MODULUS_BIT_SIZE as usize / 2 / RANGE_BIT_LEN_FOR_TEST + 1)
            * RANGE_BIT_LEN_FOR_TEST;

        let x_var = circuit.create_variable(x_p)?;
        let y_var = circuit.create_variable(y_p)?;

        let x_split_vars = FpElemVar::new_unchecked(&mut circuit, x_var, m, None)?;
        let y_split_vars =
            FpElemVar::new_unchecked(&mut circuit, y_var, m, Some(x_split_vars.two_power_m()))?;

        circuit.non_native_power_11_gate::<T>(&x_split_vars, &y_split_vars)?;

        // good path
        assert!(circuit.check_circuit_satisfiability(&[]).is_ok());

        // bad path: wrong witness should fail
        let witness = circuit.witness(y_var)?;
        *circuit.witness_mut(y_var) = F::rand(&mut rng);
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());
        *circuit.witness_mut(y_var) = witness;

        // bad path: wrong value should fail
        let y_wrong = F::rand(&mut rng);
        let y_wrong_var = circuit.create_variable(y_wrong)?;
        let y_wrong_split_vars = FpElemVar::new_unchecked(
            &mut circuit,
            y_wrong_var,
            m,
            Some(x_split_vars.two_power_m()),
        )?;
        circuit.non_native_power_11_gate::<T>(&x_split_vars, &y_wrong_split_vars)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());

        Ok(())
    }

    #[test]
    fn test_non_native_linear_gate() -> Result<(), CircuitError> {
        // use bls12-377 base field to prove rescue over jubjub377 base field
        test_non_native_linear_gate_helper::<FqEd377, Fq377>()
    }

    fn test_non_native_linear_gate_helper<T: PrimeField, F: PrimeField>() -> Result<(), CircuitError>
    {
        let mut circuit: PlonkCircuit<F> = PlonkCircuit::new_ultra_plonk(RANGE_BIT_LEN_FOR_TEST);

        let m = (T::MODULUS_BIT_SIZE as usize / 2 / RANGE_BIT_LEN_FOR_TEST + 1)
            * RANGE_BIT_LEN_FOR_TEST;

        let mut rng = test_rng();

        let x_t: Vec<T> = (0..4).map(|_| T::rand(&mut rng)).collect();
        let y_t: Vec<T> = (0..4).map(|_| T::rand(&mut rng)).collect();
        let c_t = T::rand(&mut rng);
        let mut res_t = c_t;
        for (&xi, &yi) in x_t.iter().zip(y_t.iter()) {
            res_t += xi * yi;
        }
        let res_p = F::from_le_bytes_mod_order(res_t.into_bigint().to_bytes_le().as_ref());

        let x_p: Vec<F> = x_t
            .iter()
            .map(|x| F::from_le_bytes_mod_order(x.into_bigint().to_bytes_le().as_ref()))
            .collect();
        let y_p: Vec<F> = y_t
            .iter()
            .map(|y| F::from_le_bytes_mod_order(y.into_bigint().to_bytes_le().as_ref()))
            .collect();
        let c_p = F::from_le_bytes_mod_order(c_t.into_bigint().to_bytes_le().as_ref());

        let x_vars: Vec<Variable> = x_p
            .iter()
            .map(|x| circuit.create_variable(*x))
            .collect::<Result<Vec<Variable>, _>>()?;

        let x_split_vars: Vec<FpElemVar<F>> = x_vars
            .iter()
            .map(|x| FpElemVar::new_unchecked(&mut circuit, *x, m, None))
            .collect::<Result<Vec<FpElemVar<F>>, _>>()?;
        let y_split: Vec<FpElem<F>> = y_p
            .iter()
            .map(|y| FpElem::new(y, m, Some(x_split_vars[0].two_power_m())))
            .collect::<Result<Vec<FpElem<F>>, _>>()?;
        let c_split = FpElem::new(&c_p, m, Some(x_split_vars[0].two_power_m()))?;

        // check the result is correct
        let res_split_var =
            circuit.non_native_linear_gen::<T>(&x_split_vars, &y_split, &c_split)?;
        let res_var = res_split_var.convert_to_var(&mut circuit)?;
        assert_eq!(circuit.witness(res_var)?, res_p);

        // good path: the circuit is satisfied
        let res_var2 = circuit.create_variable(res_p)?;
        circuit.enforce_equal(res_var, res_var2)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_ok());

        // bad path: wrong witness should fail
        let witness = circuit.witness(x_vars[0])?;
        *circuit.witness_mut(x_vars[0]) = F::rand(&mut rng);
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());
        *circuit.witness_mut(x_vars[0]) = witness;

        // bad path: wrong value should fail
        let res_var3 = F::rand(&mut rng);
        let res_var3 = circuit.create_variable(res_var3)?;
        circuit.enforce_equal(res_var, res_var3)?;
        assert!(circuit.check_circuit_satisfiability(&[]).is_err());

        Ok(())
    }
}