plonky2-crypto-gadgets/examples/aes_gcm_256.rs
examples/aes_gcm_256.rsRaw
1//! Run in real mode: `cargo run --release --example aes_gcm_256`
2#![allow(incomplete_features)]
3#![feature(generic_const_exprs)]
4
5use anyhow::Result;
6use plonky2::{
7 field::goldilocks_field::GoldilocksField as F,
8 iop::witness::PartialWitness,
9 plonk::{
10 circuit_builder::CircuitBuilder, circuit_data::CircuitConfig,
11 config::PoseidonGoldilocksConfig,
12 },
13};
14
15use plonky2_aes::{AesGcm256Target, D, KEY_LEN_256};
16
17fn main() -> Result<()> {
18 const L: usize = 42; // size (bytes) of plaintext to encrypt
19
20 let key: &[u8; KEY_LEN_256] = &[42; KEY_LEN_256];
21 let nonce: &[u8; 12] = &[111; 12];
22 let pt: &[u8; L] = &[42u8; L];
23
24 let (ct, tag) = plonky2_aes::native_gcm::encrypt::<8, 4, 14>(key, nonce, pt);
25
26 // Circuit declaration
27 let config = CircuitConfig::standard_recursion_config();
28 let mut builder = CircuitBuilder::<F, D>::new(config);
29 let aes_targets = AesGcm256Target::<L>::new_virtual(&mut builder);
30 aes_targets.build_circuit(&mut builder);
31
32 println!(
33 "AES-GCM-256 circuit (L:{}) num_gates: {}",
34 L,
35 builder.num_gates()
36 );
37
38 let data = builder.build::<PoseidonGoldilocksConfig>();
39
40 // set values to circuit
41 let mut pw = PartialWitness::<F>::new();
42 aes_targets.set_targets(&mut pw, key, nonce, pt, &ct, &tag)?;
43
44 let proof = data.prove(pw)?;
45 data.verify(proof)
46}