arnaucube
arnaucube/plonky2-crypto-gadgetsPublic Self-hosted repository
plonky2-crypto-gadgets/src/circuit_gcm.rs
src/circuit_gcm.rsRaw
  1//! For LICENSE check out https://github.com/0xPARC/plonky2-aes/blob/main/LICENSE
  2
  3use anyhow::Result;
  4use std::array;
  5
  6use plonky2::{
  7    field::{goldilocks_field::GoldilocksField as F, types::Field},
  8    iop::target::BoolTarget,
  9    iop::witness::PartialWitness,
 10    plonk::circuit_builder::CircuitBuilder,
 11};
 12
 13use crate::{
 14    D,
 15    circuit_aes::{
 16        ByteTarget, CircuitBuilderAESState, PartialWitnessByteArray, StateTarget,
 17        le_bits_from_byte, sbox_lut, state_mix_matrix_bits, xor,
 18    },
 19    constants::TAG_LEN,
 20};
 21
 22pub struct AesGcmTarget<
 23    const NK: usize,
 24    const NB: usize,
 25    const NR: usize,
 26    // L: max size of plaintext to cipher.
 27    const L: usize,
 28    const TAG: bool,
 29> where
 30    [(); NK * NB]:,
 31    [(); 4 * (NR + 1)]:,
 32{
 33    key: [ByteTarget; NK * NB],
 34    nonce: [ByteTarget; 12],
 35    pt: [ByteTarget; L],
 36
 37    ct: [ByteTarget; L],
 38    tag: [ByteTarget; TAG_LEN / 8],
 39}
 40
 41impl<const NK: usize, const NB: usize, const NR: usize, const L: usize, const TAG: bool>
 42    AesGcmTarget<NK, NB, NR, L, TAG>
 43where
 44    [(); NK * NB]:,
 45    [(); 4 * (NR + 1)]:,
 46{
 47    pub fn new_virtual(builder: &mut CircuitBuilder<F, D>) -> Self {
 48        let key: [ByteTarget; NK * NB] =
 49            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
 50        let nonce: [ByteTarget; 12] =
 51            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
 52        let pt: [ByteTarget; L] =
 53            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
 54
 55        let ct: [ByteTarget; L] =
 56            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
 57        let tag: [ByteTarget; TAG_LEN / 8] =
 58            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
 59
 60        Self {
 61            key,
 62            nonce,
 63            pt,
 64            ct,
 65            tag,
 66        }
 67    }
 68
 69    pub fn build_circuit(&self, builder: &mut CircuitBuilder<F, D>) {
 70        let sbox_lut = sbox_lut(builder);
 71        let mix_matrix = state_mix_matrix_bits(builder);
 72
 73        let a: &[ByteTarget] = &[]; // additional authenticated data
 74
 75        let expanded_key = builder.key_expansion::<NK, NB, NR>(sbox_lut, self.key);
 76
 77        let empty_state = builder.empty_state();
 78
 79        // 1. CIPH_K
 80        let h = builder
 81            .encrypt_block::<NR>(sbox_lut, mix_matrix, empty_state, expanded_key)
 82            .flatten();
 83
 84        // 2. J_0
 85        let zero_byte = builder.zero_byte();
 86        let one_byte = array::from_fn(|i| {
 87            // 0x01
 88            if i == 0 {
 89                builder._true()
 90            } else {
 91                builder._false()
 92            }
 93        });
 94        let j0: [ByteTarget; 16] = if self.nonce.len() * 8 == 96 {
 95            // J_0 = IV || 0^31 || 1
 96            let mut out = [zero_byte; 16];
 97            out[..12].copy_from_slice(self.nonce.as_slice());
 98            out[12..16].copy_from_slice(&[zero_byte, zero_byte, zero_byte, one_byte]);
 99            out
100        } else {
101            panic!("unsuported at initial version; nonce.len()=12 (96 bits)");
102        };
103
104        // 3. C=GCTR()
105        let inc32_j0 = inc32_target(builder, j0);
106        let c = gctr_target::<NR, L>(
107            builder,
108            sbox_lut,
109            mix_matrix,
110            expanded_key,
111            &inc32_j0,
112            &self.pt,
113        );
114
115        // connect the ct value to the external ct value
116        #[allow(clippy::needless_range_loop)]
117        for byte in 0..L {
118            for bit in 0..8 {
119                builder.connect(self.ct[byte][bit].target, c[byte][bit].target);
120            }
121        }
122
123        // the rest of the logic is for the tag; for some use cases we might skip it
124        // (saving a notable amount of gates, about double)
125        if !TAG {
126            return;
127        }
128
129        // 4. u, v
130        let u: usize = 16 * (L as f64 / 16_f64).ceil() as usize - L; // L=x.len()
131        let v: usize = 16 * (a.len() as f64 / 16_f64).ceil() as usize - a.len();
132
133        // 5. S = GHASH()
134        let const_a_len_u8: [u8; 8] = (a.len() * 8).to_be_bytes(); // const
135        let const_c_len_u8: [u8; 8] = (c.len() * 8).to_be_bytes(); // const
136        let a_len: [ByteTarget; 8] = array::from_fn(|byte_i| {
137            let const_bits = le_bits_from_byte(const_a_len_u8[byte_i]);
138            let byte: ByteTarget = array::from_fn(|bit_i| {
139                let bit_goldilocks = if const_bits[bit_i] { F::ONE } else { F::ZERO };
140                BoolTarget::new_unsafe(builder.constant(bit_goldilocks))
141            });
142            byte
143        });
144        let c_len: [ByteTarget; 8] = array::from_fn(|byte_i| {
145            let const_bits = le_bits_from_byte(const_c_len_u8[byte_i]);
146            let byte: ByteTarget = array::from_fn(|bit_i| {
147                let bit_goldilocks = if const_bits[bit_i] { F::ONE } else { F::ZERO };
148                BoolTarget::new_unsafe(builder.constant(bit_goldilocks))
149            });
150            byte
151        });
152        let ghash_input_vec = [
153            a.to_vec(),
154            vec![zero_byte; v],
155            c.to_vec(),
156            vec![zero_byte; u],
157            a_len.to_vec(),
158            c_len.to_vec(),
159        ]
160        .concat();
161        let s = ghash_target(builder, h, ghash_input_vec);
162
163        // 6. T=MSB(GCTR()))
164        let msb_input = gctr_target(builder, sbox_lut, mix_matrix, expanded_key, &j0, &s);
165        let t_vec = msb_t_target(builder, TAG_LEN / 8, &msb_input);
166        let mut t: [ByteTarget; TAG_LEN / 8] = [zero_byte; TAG_LEN / 8];
167        t.copy_from_slice(&t_vec);
168
169        // connect the computed tag value to the external tag value
170        #[allow(clippy::needless_range_loop)]
171        for byte in 0..TAG_LEN / 8 {
172            for bit in 0..8 {
173                builder.connect(self.tag[byte][bit].target, t[byte][bit].target);
174            }
175        }
176    }
177
178    pub fn set_targets(
179        &self,
180        pw: &mut PartialWitness<F>,
181        key: &[u8; NK * NB],
182        nonce: &[u8; 12],
183        pt: &[u8],
184        ct: &[u8],
185        tag: &[u8],
186    ) -> Result<()> {
187        assert!(pt.len() <= L);
188        assert!(ct.len() <= L);
189        assert!(tag.len() <= TAG_LEN / 8);
190
191        // extend to expected sizes
192        let mut pt_arr: [u8; L] = [0u8; L];
193        pt_arr.copy_from_slice(pt);
194        let mut ct_arr: [u8; L] = [0u8; L];
195        ct_arr.copy_from_slice(ct);
196        let mut tag_arr: [u8; TAG_LEN / 8] = [0u8; TAG_LEN / 8];
197        tag_arr.copy_from_slice(tag);
198
199        std::iter::zip(self.key, key).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
200        std::iter::zip(self.nonce, nonce).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
201        std::iter::zip(self.pt, pt_arr).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
202
203        std::iter::zip(self.ct, ct_arr).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
204        std::iter::zip(self.tag, tag_arr).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
205        Ok(())
206    }
207}
208
209/// L: max size of plaintext to cipher.
210fn gctr_target<const NR: usize, const L: usize>(
211    builder: &mut CircuitBuilder<F, D>,
212    sbox_lut_idx: usize,
213    mix_matrix: [[ByteTarget; 4]; 4],
214    key: [[ByteTarget; 4]; 4 * (NR + 1)],
215    icb: &[ByteTarget; 16],
216    x: &[ByteTarget; L],
217) -> [ByteTarget; L] {
218    let n = ((L * 8) as f64 / 128_f64).ceil() as usize;
219
220    let mut y: [ByteTarget; L] = *x;
221    let mut cb_i = *icb;
222
223    let zero_byte = builder.zero_byte();
224    for (i, x_i_raw) in x.chunks(16).enumerate() {
225        if i > 0 {
226            cb_i = inc32_target(builder, cb_i);
227        }
228
229        let l = x_i_raw.len().min(16);
230        let mut x_i = [zero_byte; 16];
231        x_i[..l].copy_from_slice(x_i_raw);
232
233        let ciph_cb_i = builder
234            .encrypt_block::<NR>(sbox_lut_idx, mix_matrix, StateTarget::from_flat(cb_i), key)
235            .flatten();
236
237        let (y_i, n_bytes) = if i < n && x_i_raw.len() == 16 {
238            (xor_blocks(builder, x_i, ciph_cb_i), 16)
239        } else {
240            // last chunk, might be smaller than 16 bytes (L%16 bytes)
241            let msb_res = msb_t_target(builder, L % 16, ciph_cb_i.as_slice());
242            let l = msb_res.len().min(16);
243            let mut m: [ByteTarget; 16] = [zero_byte; 16];
244            m[..l].copy_from_slice(&msb_res);
245            (xor_blocks(builder, x_i, m), l)
246        };
247        y[i * 16..i * 16 + n_bytes].clone_from_slice(&y_i[..n_bytes]);
248    }
249    y
250}
251
252// fn ghash_target<const L: usize>(
253fn ghash_target(
254    builder: &mut CircuitBuilder<F, D>,
255    h: [ByteTarget; 16],
256    // x: &[ByteTarget; L],
257    x: Vec<ByteTarget>,
258) -> [ByteTarget; 16] {
259    // assert!(L.is_multiple_of(16)); // multiple of 128 bits
260    // let m = L / 16;
261    assert!(x.len().is_multiple_of(16)); // multiple of 128 bits
262    let m = x.len() / 16;
263
264    let zero_byte = builder.zero_byte();
265    let mut y = [zero_byte; 16]; // (128 bits)
266    for i in 0..m {
267        let mut xi = [zero_byte; 16];
268        xi.clone_from_slice(&x[i * 16..i * 16 + 16]);
269        let y_xi = xor_blocks(builder, y, xi);
270        y = gf_2_128_mul_target(builder, y_xi, h);
271    }
272    y
273}
274fn gf_2_128_mul_target(
275    builder: &mut CircuitBuilder<F, D>,
276    x: [ByteTarget; 16],
277    y: [ByteTarget; 16],
278) -> [ByteTarget; 16] {
279    let zero_byte = builder.zero_byte();
280    let one_bool = builder._true();
281
282    // R: 10000111 || 0^120 (in little-endian)
283    let mut r = [zero_byte; 16];
284    r[0][0] = one_bool;
285    r[0][5] = one_bool;
286    r[0][6] = one_bool;
287    r[0][7] = one_bool;
288
289    let mut z = [zero_byte; 16];
290    let mut v = y;
291    for i in 0..128 {
292        let byte_index = i / 8;
293        let bit_index = 7 - (i % 8);
294        let xi = x[byte_index][bit_index];
295
296        // set z = if xi==1: z^v, else: z
297        for b in 0..16 {
298            for k in 0..8 {
299                let z_xor_v = xor(builder, z[b][k], v[b][k]);
300                z[b][k] =
301                    BoolTarget::new_unsafe(builder.select(xi, z_xor_v.target, z[b][k].target));
302            }
303        }
304
305        let lsb = v[15][0]; // (little-endian)
306        v = right_shift_one_target(builder, &v);
307
308        // if lsb==1: v=v^R, else: v
309        for b in 0..16 {
310            for k in 0..8 {
311                let v_xor_r = xor(builder, v[b][k], r[b][k]);
312                v[b][k] =
313                    BoolTarget::new_unsafe(builder.select(lsb, v_xor_r.target, v[b][k].target));
314            }
315        }
316    }
317    z
318}
319pub fn right_shift_one_target(
320    builder: &mut CircuitBuilder<F, D>,
321    v: &[ByteTarget; 16],
322) -> [ByteTarget; 16] {
323    let mut r: [ByteTarget; 16] = *v;
324
325    let mut carry = builder._false();
326    for i in 0..16 {
327        let current = v[i];
328
329        let next_carry = current[0];
330
331        let mut shifted = [builder._false(); 8];
332        shifted[..7].copy_from_slice(&current[1..(7 + 1)]);
333        shifted[7] = carry;
334        r[i] = shifted;
335        carry = next_carry;
336    }
337    r
338}
339
340pub fn inc32_target(
341    builder: &mut CircuitBuilder<F, D>,
342    block: [ByteTarget; 16],
343) -> [ByteTarget; 16] {
344    let mut r = block;
345
346    let mut carry = BoolTarget::new_unsafe(builder.one());
347    for byte_index in (12..16).rev() {
348        for bit_index in 0..8 {
349            let a = block[byte_index][bit_index];
350            // builder.assert_bool(a); // Note: we can assume it's valid bool, skip check
351
352            let sum = xor(builder, a, carry);
353            let carry_out = builder.and(a, carry);
354            r[byte_index][bit_index] = sum;
355            carry = carry_out;
356        }
357    }
358    r
359}
360
361fn xor_byte(builder: &mut CircuitBuilder<F, D>, b1: ByteTarget, b2: ByteTarget) -> ByteTarget {
362    array::from_fn(|i| xor(builder, b1[i], b2[i]))
363}
364
365fn xor_blocks(
366    builder: &mut CircuitBuilder<F, D>,
367    b1: [ByteTarget; 16],
368    b2: [ByteTarget; 16],
369) -> [ByteTarget; 16] {
370    array::from_fn(|i| xor_byte(builder, b1[i], b2[i]))
371}
372
373pub fn msb_t_target(
374    builder: &mut CircuitBuilder<F, D>,
375    t_bytes: usize,
376    block: &[ByteTarget],
377) -> Vec<ByteTarget> {
378    let t = t_bytes * 8;
379
380    let full_bytes = t / 8;
381    let rem_bits = t % 8;
382
383    let mut out: Vec<ByteTarget> = Vec::new();
384    for block_i in block.iter().take(full_bytes) {
385        out.push(*block_i);
386    }
387
388    let zero_bool = BoolTarget::new_unsafe(builder.zero());
389    if rem_bits != 0 {
390        let mut partial = [zero_bool; 8];
391
392        for bit in 0..8 {
393            if bit < rem_bits {
394                partial[bit] = block[full_bytes][bit];
395            } else {
396                partial[bit] = zero_bool;
397            }
398        }
399        out.push(partial);
400    }
401    out
402}
403
404#[cfg(test)]
405mod tests {
406    use std::array;
407
408    use anyhow::Result;
409    use plonky2::{
410        field::goldilocks_field::GoldilocksField as F,
411        iop::witness::PartialWitness,
412        plonk::{
413            circuit_builder::CircuitBuilder, circuit_data::CircuitConfig,
414            config::PoseidonGoldilocksConfig,
415        },
416    };
417
418    use super::*;
419    use crate::{
420        circuit_aes::PartialWitnessByteArray,
421        native_aes::key_expansion,
422        native_gcm::{gctr, gf_2_128_mul, ghash, right_shift_one},
423    };
424
425    use super::D;
426
427    #[test]
428    fn test_gctr() -> Result<()> {
429        // AES-GCM-128
430        test_gctr_opt::<4, 4, 10, 13>()?;
431        test_gctr_opt::<4, 4, 10, 17>()?;
432
433        // AES-GCM-256
434        test_gctr_opt::<8, 4, 14, 13>()?;
435
436        Ok(())
437    }
438    fn test_gctr_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>()
439    -> Result<()>
440    where
441        [(); 4 * (NR + 1)]:,
442        [(); NK * NB]:,
443    {
444        let key: &[u8; NK * NB] = &[42; NK * NB];
445        let expanded_key = key_expansion::<NK, NB, NR>(key);
446        let nonce: &[u8; 12] = &[111; 12];
447        let icb: &[u8; 16] = &[222; 16];
448        let pt: &[u8; L] = &[42u8; L];
449
450        let expected = gctr(expanded_key, icb, pt);
451
452        // Circuit declaration
453        let config = CircuitConfig::standard_recursion_config();
454        let mut builder = CircuitBuilder::<F, D>::new(config);
455
456        let key_target: [ByteTarget; NK * NB] =
457            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
458        let nonce_target: [ByteTarget; 12] =
459            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
460        let icb_target: [ByteTarget; 16] =
461            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
462        let pt_target: [ByteTarget; L] =
463            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
464        let sbox_lut = sbox_lut(&mut builder);
465        let mix_matrix = state_mix_matrix_bits(&mut builder);
466
467        let key_expanded_target = builder.key_expansion::<NK, NB, NR>(sbox_lut, key_target);
468        let gctr_out_target = gctr_target::<NR, L>(
469            &mut builder,
470            sbox_lut,
471            mix_matrix,
472            key_expanded_target,
473            &icb_target,
474            &pt_target,
475        );
476
477        println!(
478            "gctr (NK:{}, NB:{}, NR:{}, L:{}) num_gates: {}",
479            NK,
480            NB,
481            NR,
482            L,
483            builder.num_gates()
484        );
485        let data = builder.build::<PoseidonGoldilocksConfig>();
486
487        // set values to circuit
488        let mut pw = PartialWitness::<F>::new();
489        std::iter::zip(key_target, key).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
490        std::iter::zip(nonce_target, nonce)
491            .try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
492        std::iter::zip(icb_target, icb).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
493        std::iter::zip(pt_target, pt).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
494
495        std::iter::zip(gctr_out_target, expected)
496            .try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
497
498        let proof = data.prove(pw)?;
499        data.verify(proof)
500    }
501
502    #[test]
503    fn test_right_shift_one() -> Result<()> {
504        let x: [u8; 16] = [111; 16];
505
506        let mut expected = x;
507        right_shift_one(&mut expected);
508
509        // Circuit declaration
510        let config = CircuitConfig::standard_recursion_config();
511        let mut builder = CircuitBuilder::<F, D>::new(config);
512
513        let x_target: [ByteTarget; 16] =
514            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
515
516        let out_target: [ByteTarget; 16] = right_shift_one_target(&mut builder, &x_target);
517
518        println!("right_shift_one num_gates: {}", builder.num_gates());
519        let data = builder.build::<PoseidonGoldilocksConfig>();
520
521        // set values to circuit
522        let mut pw = PartialWitness::<F>::new();
523        std::iter::zip(x_target, x).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
524
525        std::iter::zip(out_target, expected)
526            .try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
527
528        let proof = data.prove(pw)?;
529        data.verify(proof)?;
530        Ok(())
531    }
532
533    #[test]
534    fn test_gf_mul() -> Result<()> {
535        // AES-GCM-128
536        test_gf_mul_opt::<4, 4, 10, 16>()?;
537        test_gf_mul_opt::<4, 4, 10, 32>()?;
538
539        // AES-GCM-256
540        test_gf_mul_opt::<8, 4, 14, 16>()?;
541
542        Ok(())
543    }
544    fn test_gf_mul_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>()
545    -> Result<()>
546    where
547        [(); 4 * (NR + 1)]:,
548        [(); NK * NB]:,
549    {
550        let x: [u8; 16] = [111; 16];
551        let y: [u8; 16] = [222; 16];
552
553        let expected = gf_2_128_mul(x, y);
554        let x_bits: [[bool; 8]; 16] =
555            array::from_fn(|b| crate::circuit_aes::le_bits_from_byte(x[b]));
556        let y_bits: [[bool; 8]; 16] =
557            array::from_fn(|b| crate::circuit_aes::le_bits_from_byte(y[b]));
558
559        // sanity check
560        let expected2 = crate::native_gcm::gf_2_128_mul_circuit_version(x_bits, y_bits);
561        let expected_bits: [[bool; 8]; 16] =
562            array::from_fn(|b| crate::circuit_aes::le_bits_from_byte(expected[b]));
563        assert_eq!(expected2, expected_bits);
564
565        // Circuit declaration
566        let config = CircuitConfig::standard_recursion_config();
567        let mut builder = CircuitBuilder::<F, D>::new(config);
568
569        let x_target: [ByteTarget; 16] =
570            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
571        let y_target: [ByteTarget; 16] =
572            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
573
574        let out_target = gf_2_128_mul_target(&mut builder, x_target, y_target);
575
576        println!("gf_2_128_mul (L:{}) num_gates: {}", L, builder.num_gates());
577        let data = builder.build::<PoseidonGoldilocksConfig>();
578
579        // set values to circuit
580        let mut pw = PartialWitness::<F>::new();
581        std::iter::zip(x_target, x).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
582        std::iter::zip(y_target, y).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
583
584        std::iter::zip(out_target, expected)
585            .try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
586
587        let proof = data.prove(pw)?;
588        data.verify(proof)
589    }
590
591    #[test]
592    fn test_ghash() -> Result<()> {
593        // AES-GCM-128
594        test_ghash_opt::<4, 4, 10, 16>()?;
595        test_ghash_opt::<4, 4, 10, 32>()?;
596
597        // AES-GCM-256
598        test_ghash_opt::<8, 4, 14, 16>()?;
599
600        Ok(())
601    }
602    fn test_ghash_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>()
603    -> Result<()>
604    where
605        [(); 4 * (NR + 1)]:,
606        [(); NK * NB]:,
607    {
608        let h: [u8; 16] = [222; 16];
609        let x: &[u8; L] = &[42u8; L];
610
611        let expected = ghash(h, x);
612
613        // Circuit declaration
614        let config = CircuitConfig::standard_recursion_config();
615        let mut builder = CircuitBuilder::<F, D>::new(config);
616
617        let h_target: [ByteTarget; 16] =
618            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
619        let x_target: [ByteTarget; L] =
620            array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe()));
621
622        // let ghash_out_target = ghash_target::<L>(&mut builder, h_target, &x_target);
623        let ghash_out_target = ghash_target(&mut builder, h_target, x_target.to_vec());
624
625        println!("ghash (L:{}) num_gates: {}", L, builder.num_gates());
626        let data = builder.build::<PoseidonGoldilocksConfig>();
627
628        // set values to circuit
629        let mut pw = PartialWitness::<F>::new();
630        std::iter::zip(h_target, h).try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
631        std::iter::zip(x_target, x).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;
632
633        std::iter::zip(ghash_out_target, expected)
634            .try_for_each(|(t, v)| pw.set_byte_array_target(t, v))?;
635
636        let proof = data.prove(pw)?;
637        data.verify(proof)
638    }
639
640    #[test]
641    fn test_encrypt() -> Result<()> {
642        // AES-GCM-128
643        test_encrypt_opt::<4, 4, 10, 13, false>(false)?;
644        test_encrypt_opt::<4, 4, 10, 13, true>(false)?; // with tag
645        test_encrypt_opt::<4, 4, 10, 17, false>(false)?;
646
647        // AES-GCM-256
648        test_encrypt_opt::<8, 4, 14, 13, false>(false)?;
649
650        Ok(())
651    }
652    // run it with: cargo test -- --ignored
653    #[ignore]
654    #[test]
655    fn test_encrypt_report_sizes() -> Result<()> {
656        // AES-GCM-128
657        test_encrypt_opt::<4, 4, 10, 16, false>(true)?;
658        test_encrypt_opt::<4, 4, 10, 17, false>(true)?;
659        test_encrypt_opt::<4, 4, 10, 32, false>(true)?;
660        test_encrypt_opt::<4, 4, 10, 33, false>(true)?;
661        test_encrypt_opt::<4, 4, 10, 64, false>(true)?;
662        test_encrypt_opt::<4, 4, 10, 128, false>(true)?;
663        test_encrypt_opt::<4, 4, 10, 256, false>(true)?;
664        test_encrypt_opt::<4, 4, 10, 512, false>(true)?;
665        test_encrypt_opt::<4, 4, 10, 1024, false>(true)?;
666        test_encrypt_opt::<4, 4, 10, 2048, false>(true)?;
667
668        // AES-GCM-256
669        test_encrypt_opt::<8, 4, 14, 16, false>(true)?;
670        test_encrypt_opt::<8, 4, 14, 17, false>(true)?;
671        test_encrypt_opt::<8, 4, 14, 32, false>(true)?;
672        test_encrypt_opt::<8, 4, 14, 33, false>(true)?;
673        test_encrypt_opt::<8, 4, 14, 64, false>(true)?;
674        test_encrypt_opt::<8, 4, 14, 128, false>(true)?;
675        test_encrypt_opt::<8, 4, 14, 256, false>(true)?;
676        test_encrypt_opt::<8, 4, 14, 512, false>(true)?;
677        test_encrypt_opt::<8, 4, 14, 1024, false>(true)?;
678        test_encrypt_opt::<8, 4, 14, 2048, false>(true)?;
679
680        Ok(())
681    }
682    fn test_encrypt_opt<
683        const NK: usize,
684        const NB: usize,
685        const NR: usize,
686        const L: usize,
687        const TAG: bool,
688    >(
689        only_build: bool,
690    ) -> Result<()>
691    where
692        [(); 4 * (NR + 1)]:,
693        [(); NK * NB]:,
694    {
695        let key: &[u8; NK * NB] = &[42; NK * NB];
696        let nonce: &[u8; 12] = &[111; 12];
697        let pt: &[u8; L] = &[42u8; L];
698
699        let (ct, tag) = crate::native_gcm::encrypt::<NK, NB, NR>(key, nonce, pt);
700
701        // Circuit declaration
702        let config = CircuitConfig::standard_recursion_config();
703        let mut builder = CircuitBuilder::<F, D>::new(config);
704
705        let aes_targets = AesGcmTarget::<NK, NB, NR, L, TAG>::new_virtual(&mut builder);
706        aes_targets.build_circuit(&mut builder);
707
708        println!(
709            "encrypt (NK:{}, NB:{}, NR:{}, L:{}, TAG:{}) num_gates: {}",
710            NK,
711            NB,
712            NR,
713            L,
714            TAG,
715            builder.num_gates()
716        );
717        let data = builder.build::<PoseidonGoldilocksConfig>();
718
719        if only_build {
720            return Ok(());
721        }
722
723        // set values to circuit
724        let mut pw = PartialWitness::<F>::new();
725        aes_targets.set_targets(&mut pw, key, nonce, pt, &ct, &tag)?;
726
727        let proof = data.prove(pw)?;
728        data.verify(proof)
729    }
730}