arnaucube
arnaucube/plonky2-crypto-gadgetsPublic Self-hosted repository
rename ByteArrayTarget -> ByteTarget
arnaucube git@arnaucube.com
Mon, 16 Feb 2026 12:18:08 +0000
commit

d9f3077be719854e6585b6bf9e1c31c1f1c9cc1c

parent

77f152567f4ff6c1536e747508d1a1e0ede5d443

3 files changed, 36 insertions(+), 48 deletions(-)

jump to
M src/circuit_aes.rs → src/circuit_aes.rs

@@ -19,20 +19,19 @@ D,

}; #[derive(Debug, Copy, Clone)] -pub struct StateTarget(pub [[ByteArrayTarget; 4]; 4]); +pub struct StateTarget(pub [[ByteTarget; 4]; 4]); impl StateTarget { - pub(crate) fn flatten(self) -> [ByteArrayTarget; 16] { + pub(crate) fn flatten(self) -> [ByteTarget; 16] { array::from_fn(|i| self.0[i % 4][i / 4]) } - pub(crate) fn from_flat(b: [ByteArrayTarget; 16]) -> Self { + pub(crate) fn from_flat(b: [ByteTarget; 16]) -> Self { StateTarget(array::from_fn(|i| array::from_fn(|j| b[j * 4 + i]))) } } -// TODO: Normalise terminology! maybe ByteArray-->Byte? -pub type ByteArrayTarget = [BoolTarget; 8]; +pub type ByteTarget = [BoolTarget; 8]; pub trait CircuitBuilderAESState<F: RichField + Extendable<D>, const D: usize> { /// Adds state target.

@@ -42,42 +41,35 @@ /// AES cipher as in spec.

fn encrypt_block<const NR: usize>( &mut self, sbox_lut_idx: usize, - mix_matrix: [[ByteArrayTarget; 4]; 4], + mix_matrix: [[ByteTarget; 4]; 4], s: StateTarget, - w: [[ByteArrayTarget; 4]; 4 * (NR + 1)], + w: [[ByteTarget; 4]; 4 * (NR + 1)], ) -> StateTarget; /// Applies sub_bytes routine to state. fn state_sub_bytes(&mut self, sbox_lut_idx: usize, s: StateTarget) -> StateTarget; /// SubWord - fn state_sub_word( - &mut self, - sbox_lut_idx: usize, - word: [ByteArrayTarget; 4], - ) -> [ByteArrayTarget; 4]; + fn state_sub_word(&mut self, sbox_lut_idx: usize, word: [ByteTarget; 4]) -> [ByteTarget; 4]; /// MixColumns fn state_mix_columns( &mut self, - mix_matrix: [[ByteArrayTarget; 4]; 4], + mix_matrix: [[ByteTarget; 4]; 4], s: StateTarget, ) -> StateTarget; /// AddRoundKey - fn state_add_round_key( - &mut self, - round_key: &[[ByteArrayTarget; 4]], - s: StateTarget, - ) -> StateTarget; + fn state_add_round_key(&mut self, round_key: &[[ByteTarget; 4]], s: StateTarget) + -> StateTarget; /// KeyExpansion fn key_expansion<const NK: usize, const NB: usize, const NR: usize>( &mut self, sbox_lut_idx: usize, - key: [ByteArrayTarget; NK * NB], - ) -> [[ByteArrayTarget; 4]; 4 * (NR + 1)]; + key: [ByteTarget; NK * NB], + ) -> [[ByteTarget; 4]; 4 * (NR + 1)]; /// GF(2^8) addition fn gf_2_8_add(&mut self, x: Target, y: Target) -> Target;

@@ -105,12 +97,12 @@

/// Bytearray matrix application. fn bytearray_matrix_apply_bits<const M: usize, const N: usize>( &mut self, - a: [[ByteArrayTarget; N]; M], - x: [ByteArrayTarget; N], - ) -> [ByteArrayTarget; M]; + a: [[ByteTarget; N]; M], + x: [ByteTarget; N], + ) -> [ByteTarget; M]; /// returns a 0u8 in the shape of a ByteArrayTarget. - fn zero_byte(&mut self) -> ByteArrayTarget; + fn zero_byte(&mut self) -> ByteTarget; fn empty_state(&mut self) -> StateTarget; }

@@ -125,9 +117,9 @@

fn encrypt_block<const NR: usize>( &mut self, sbox_lut_idx: usize, - mix_matrix: [[ByteArrayTarget; 4]; 4], // constant mix_matrix + mix_matrix: [[ByteTarget; 4]; 4], // constant mix_matrix s: StateTarget, - w: [[ByteArrayTarget; 4]; 4 * (NR + 1)], // expanded key + w: [[ByteTarget; 4]; 4 * (NR + 1)], // expanded key ) -> StateTarget { let mut s = s; s = self.state_add_round_key(&w[0..4], s);

@@ -148,11 +140,7 @@ self.state_sub_word(sbox_lut_idx, s.0[i])

})) } - fn state_sub_word( - &mut self, - sbox_lut_idx: usize, - word: [ByteArrayTarget; 4], - ) -> [ByteArrayTarget; 4] { + fn state_sub_word(&mut self, sbox_lut_idx: usize, word: [ByteTarget; 4]) -> [ByteTarget; 4] { array::from_fn(|i| { let byte_target = target_from_bitarray(self, &word[i]); let out_target = self.add_lookup_from_index(byte_target, sbox_lut_idx);

@@ -162,7 +150,7 @@ }

fn state_mix_columns( &mut self, - mix_matrix: [[ByteArrayTarget; 4]; 4], + mix_matrix: [[ByteTarget; 4]; 4], s: StateTarget, ) -> StateTarget { let cols: [_; 4] = array::from_fn(|i| array::from_fn(|j| s.0[j][i]));

@@ -173,7 +161,7 @@ }

fn state_add_round_key( &mut self, - round_key: &[[ByteArrayTarget; 4]], + round_key: &[[ByteTarget; 4]], s: StateTarget, ) -> StateTarget { StateTarget(array::from_fn(|i| {

@@ -184,9 +172,9 @@

fn key_expansion<const NK: usize, const NB: usize, const NR: usize>( &mut self, sbox_lut_idx: usize, - key: [ByteArrayTarget; NK * NB], - ) -> [[ByteArrayTarget; 4]; 4 * (NR + 1)] { - let rcon: [ByteArrayTarget; 11] = array::from_fn(|i| { + key: [ByteTarget; NK * NB], + ) -> [[ByteTarget; 4]; 4 * (NR + 1)] { + let rcon: [ByteTarget; 11] = array::from_fn(|i| { let rcon_bits = le_bits_from_byte(RCON[i]); array::from_fn(|j| self.constant_bool(rcon_bits[j])) });

@@ -309,13 +297,13 @@ }

fn bytearray_matrix_apply_bits<const M: usize, const N: usize>( &mut self, - a: [[ByteArrayTarget; N]; M], - x: [ByteArrayTarget; N], - ) -> [ByteArrayTarget; M] { + a: [[ByteTarget; N]; M], + x: [ByteTarget; N], + ) -> [ByteTarget; M] { std::array::from_fn(|i| self.bytearray_ip_bits(a[i], x)) } - fn zero_byte(&mut self) -> ByteArrayTarget { + fn zero_byte(&mut self) -> ByteTarget { array::from_fn(|_| self._false()) } fn empty_state(&mut self) -> StateTarget {

@@ -325,11 +313,11 @@ }

} pub trait PartialWitnessByteArray { - fn set_byte_array_target(&mut self, target: ByteArrayTarget, value: u8) -> anyhow::Result<()>; + fn set_byte_array_target(&mut self, target: ByteTarget, value: u8) -> anyhow::Result<()>; } impl<F: Field> PartialWitnessByteArray for PartialWitness<F> { - fn set_byte_array_target(&mut self, target: ByteArrayTarget, value: u8) -> anyhow::Result<()> { + fn set_byte_array_target(&mut self, target: ByteTarget, value: u8) -> anyhow::Result<()> { let value_bits = le_bits_from_byte(value); std::iter::zip(target, value_bits).try_for_each(|(t, v)| self.set_bool_target(t, v)) }

@@ -423,7 +411,7 @@ };

use rand::RngExt; use super::{ - sbox_lut, state_mix_matrix_bits, ByteArrayTarget, CircuitBuilderAESState, + sbox_lut, state_mix_matrix_bits, ByteTarget, CircuitBuilderAESState, PartialWitnessAESState, PartialWitnessByteArray, }; use crate::native_aes::{encrypt_block, key_expansion, mix_columns, sub_bytes, State};

@@ -570,7 +558,7 @@ // Circuit declaration

let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::<F, D>::new(config); - let key_target: [ByteArrayTarget; NK * NB] = + let key_target: [ByteTarget; NK * NB] = array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe())); let sbox_lut = sbox_lut(&mut builder); let expanded_key_target = builder.key_expansion::<NK, NB, NR>(sbox_lut, key_target);

@@ -652,11 +640,11 @@ // Circuit declaration

let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::<F, D>::new(config); - let key_target: [ByteArrayTarget; NK * NB] = + let key_target: [ByteTarget; NK * NB] = array::from_fn(|_| array::from_fn(|_| builder.add_virtual_bool_target_safe())); let sbox_lut = sbox_lut(&mut builder); let mix_matrix = state_mix_matrix_bits(&mut builder); - let expanded_key_target: [[ByteArrayTarget; 4]; 4 * (NR + 1)] = + let expanded_key_target: [[ByteTarget; 4]; 4 * (NR + 1)] = builder.key_expansion::<NK, NB, NR>(sbox_lut, key_target); let input_state_target = builder.add_virtual_state();
M src/circuit_gcm.rs → src/circuit_gcm.rs

@@ -10,7 +10,7 @@ };

use crate::{ circuit_aes::{ - le_bits_from_byte, sbox_lut, state_mix_matrix_bits, xor, ByteArrayTarget as ByteTarget, + le_bits_from_byte, sbox_lut, state_mix_matrix_bits, xor, ByteTarget, CircuitBuilderAESState, StateTarget, }, native_gcm::TAG_LEN,
M src/lib.rs → src/lib.rs

@@ -12,7 +12,7 @@

/// D defines the extension degree of the field used in the Plonky2 proofs (quadratic extension). pub const D: usize = 2; -use circuit_aes::ByteArrayTarget as ByteTarget; +use circuit_aes::ByteTarget; use circuit_gcm::encrypt_target; use native_gcm::TAG_LEN; use plonky2::{