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