clippy & cargofmt pass
@@ -1,4 +1,5 @@
//! Run in real mode: `cargo run --release --example aes_gcm_128` +#![allow(incomplete_features)] #![feature(generic_const_exprs)] use anyhow::Result;
@@ -1,4 +1,5 @@
//! Run in real mode: `cargo run --release --example aes_gcm_256` +#![allow(incomplete_features)] #![feature(generic_const_exprs)] use anyhow::Result;
@@ -13,9 +13,9 @@ plonk::circuit_builder::CircuitBuilder,
}; use crate::{ - constants::{RCON, SBOX}, - native_aes::{rot_word, shift_rows, State}, D, + constants::{RCON, SBOX}, + native_aes::{State, rot_word, shift_rows}, }; #[derive(Debug, Copy, Clone)]@@ -62,7 +62,7 @@ ) -> StateTarget;
/// AddRoundKey fn state_add_round_key(&mut self, round_key: &[[ByteTarget; 4]], s: StateTarget) - -> StateTarget; + -> StateTarget; /// KeyExpansion fn key_expansion<const NK: usize, const NB: usize, const NR: usize>(@@ -411,10 +411,10 @@ };
use rand::RngExt; use super::{ - sbox_lut, state_mix_matrix_bits, ByteTarget, CircuitBuilderAESState, - PartialWitnessAESState, PartialWitnessByteArray, + ByteTarget, CircuitBuilderAESState, PartialWitnessAESState, PartialWitnessByteArray, + sbox_lut, state_mix_matrix_bits, }; - use crate::native_aes::{encrypt_block, key_expansion, mix_columns, sub_bytes, State}; + use crate::native_aes::{State, encrypt_block, key_expansion, mix_columns, sub_bytes}; use super::D;
@@ -11,12 +11,12 @@ plonk::circuit_builder::CircuitBuilder,
}; use crate::{ + D, circuit_aes::{ - le_bits_from_byte, sbox_lut, state_mix_matrix_bits, xor, ByteTarget, - CircuitBuilderAESState, PartialWitnessByteArray, StateTarget, + ByteTarget, CircuitBuilderAESState, PartialWitnessByteArray, StateTarget, + le_bits_from_byte, sbox_lut, state_mix_matrix_bits, xor, }, constants::TAG_LEN, - D, }; pub struct AesGcmTarget<@@ -28,7 +28,7 @@ const L: usize,
const TAG: bool, > where [(); NK * NB]:, - [(); 4 * (NR + 1)]:, // TODO rm + [(); 4 * (NR + 1)]:, { key: [ByteTarget; NK * NB], nonce: [ByteTarget; 12],@@ -42,7 +42,7 @@ impl<const NK: usize, const NB: usize, const NR: usize, const L: usize, const TAG: bool>
AesGcmTarget<NK, NB, NR, L, TAG> where [(); NK * NB]:, - [(); 4 * (NR + 1)]:, // TODO rm + [(); 4 * (NR + 1)]:, { pub fn new_virtual(builder: &mut CircuitBuilder<F, D>) -> Self { let key: [ByteTarget; NK * NB] =@@ -66,11 +66,7 @@ tag,
} } - pub fn build_circuit(&self, builder: &mut CircuitBuilder<F, D>) - // where - // [(); NK * NB]:, - // [(); 4 * (NR + 1)]:, - { + pub fn build_circuit(&self, builder: &mut CircuitBuilder<F, D>) { let sbox_lut = sbox_lut(builder); let mix_matrix = state_mix_matrix_bits(builder);@@ -117,6 +113,7 @@ &self.pt,
); // connect the ct value to the external ct value + #[allow(clippy::needless_range_loop)] for byte in 0..L { for bit in 0..8 { builder.connect(self.ct[byte][bit].target, c[byte][bit].target);@@ -152,7 +149,7 @@ BoolTarget::new_unsafe(builder.constant(bit_goldilocks))
}); byte }); - let ghash_input_vec = vec![ + let ghash_input_vec = [ a.to_vec(), vec![zero_byte; v], c.to_vec(),@@ -170,6 +167,7 @@ let mut t: [ByteTarget; TAG_LEN / 8] = [zero_byte; TAG_LEN / 8];
t.copy_from_slice(&t_vec); // connect the computed tag value to the external tag value + #[allow(clippy::needless_range_loop)] for byte in 0..TAG_LEN / 8 { for bit in 0..8 { builder.connect(self.tag[byte][bit].target, t[byte][bit].target);@@ -192,11 +190,11 @@ assert!(tag.len() <= TAG_LEN / 8);
// extend to expected sizes let mut pt_arr: [u8; L] = [0u8; L]; - pt_arr.copy_from_slice(&pt); + pt_arr.copy_from_slice(pt); let mut ct_arr: [u8; L] = [0u8; L]; - ct_arr.copy_from_slice(&ct); + ct_arr.copy_from_slice(ct); let mut tag_arr: [u8; TAG_LEN / 8] = [0u8; TAG_LEN / 8]; - tag_arr.copy_from_slice(&tag); + tag_arr.copy_from_slice(tag); std::iter::zip(self.key, key).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?; std::iter::zip(self.nonce, nonce).try_for_each(|(t, v)| pw.set_byte_array_target(t, *v))?;@@ -219,7 +217,7 @@ x: &[ByteTarget; L],
) -> [ByteTarget; L] { let n = ((L * 8) as f64 / 128_f64).ceil() as usize; - let mut y: [ByteTarget; L] = x.clone(); + let mut y: [ByteTarget; L] = *x; let mut cb_i = *icb; let zero_byte = builder.zero_byte();@@ -304,7 +302,7 @@ BoolTarget::new_unsafe(builder.select(xi, z_xor_v.target, z[b][k].target));
} } - let lsb = v[15][0].clone(); // (little-endian) + let lsb = v[15][0]; // (little-endian) v = right_shift_one_target(builder, &v); // if lsb==1: v=v^R, else: v@@ -322,7 +320,7 @@ pub fn right_shift_one_target(
builder: &mut CircuitBuilder<F, D>, v: &[ByteTarget; 16], ) -> [ByteTarget; 16] { - let mut r: [ByteTarget; 16] = v.clone(); + let mut r: [ByteTarget; 16] = *v; let mut carry = builder._false(); for i in 0..16 {@@ -331,9 +329,7 @@
let next_carry = current[0]; let mut shifted = [builder._false(); 8]; - for j in 0..7 { - shifted[j] = current[j + 1]; - } + shifted[..7].copy_from_slice(¤t[1..(7 + 1)]); shifted[7] = carry; r[i] = shifted; carry = next_carry;@@ -351,7 +347,7 @@ let mut carry = BoolTarget::new_unsafe(builder.one());
for byte_index in (12..16).rev() { for bit_index in 0..8 { let a = block[byte_index][bit_index]; - // builder.assert_bool(a); // TODO we can assume it's valid bool, skip check + // builder.assert_bool(a); // Note: we can assume it's valid bool, skip check let sum = xor(builder, a, carry); let carry_out = builder.and(a, carry);@@ -385,8 +381,8 @@ let full_bytes = t / 8;
let rem_bits = t % 8; let mut out: Vec<ByteTarget> = Vec::new(); - for i in 0..full_bytes { - out.push(block[i]); + for block_i in block.iter().take(full_bytes) { + out.push(*block_i); } let zero_bool = BoolTarget::new_unsafe(builder.zero());@@ -439,8 +435,8 @@ test_gctr_opt::<8, 4, 14, 13>()?;
Ok(()) } - fn test_gctr_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>( - ) -> Result<()> + fn test_gctr_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>() + -> Result<()> where [(); 4 * (NR + 1)]:, [(); NK * NB]:,@@ -451,7 +447,7 @@ let nonce: &[u8; 12] = &[111; 12];
let icb: &[u8; 16] = &[222; 16]; let pt: &[u8; L] = &[42u8; L]; - let expected = gctr(expanded_key, &icb, pt); + let expected = gctr(expanded_key, icb, pt); // Circuit declaration let config = CircuitConfig::standard_recursion_config();@@ -507,7 +503,7 @@ #[test]
fn test_right_shift_one() -> Result<()> { let x: [u8; 16] = [111; 16]; - let mut expected = x.clone(); + let mut expected = x; right_shift_one(&mut expected); // Circuit declaration@@ -545,8 +541,8 @@ test_gf_mul_opt::<8, 4, 14, 16>()?;
Ok(()) } - fn test_gf_mul_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>( - ) -> Result<()> + fn test_gf_mul_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>() + -> Result<()> where [(); 4 * (NR + 1)]:, [(); NK * NB]:,@@ -603,8 +599,8 @@ test_ghash_opt::<8, 4, 14, 16>()?;
Ok(()) } - fn test_ghash_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>( - ) -> Result<()> + fn test_ghash_opt<const NK: usize, const NB: usize, const NR: usize, const L: usize>() + -> Result<()> where [(); 4 * (NR + 1)]:, [(); NK * NB]:,
@@ -33,7 +33,6 @@
let mut s: State = [[0; 4]; 4]; for i in 0..4 { for j in 0..4 { - // TODO s[i][j] = input[i + 4 * j]; } }@@ -225,7 +224,7 @@ assert_eq!(flatten_state(c), expected);
} use aes::{ - cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit}, + cipher::{BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, generic_array::GenericArray}, {Aes128, Aes192, Aes256}, }; use rand::RngExt;
@@ -21,7 +21,7 @@ ) -> (Vec<u8>, Vec<u8>)
where [(); 4 * (NR + 1)]:, { - let a: &[u8] = &[]; // additional authenticated data // TODO maybe as input + let a: &[u8] = &[]; // additional authenticated data let expanded_key: [[u8; 4]; 4 * (NR + 1)] = key_expansion::<NK, NB, NR>(key); // 1. CIPH_K@@ -186,8 +186,7 @@ z[b][k] = if xi { z_xor_v } else { z[b][k] };
} } - // let lsb = v[15][7].clone(); - let lsb = v[15][0].clone(); // (little-endian) + let lsb = v[15][0]; // (little-endian) v = right_shift_one_circuit_version(&v); // if lsb==1: v=v^R, else: v@@ -207,15 +206,13 @@ .enumerate()
.fold(0u8, |acc, (i, &bit)| if bit { acc | (1 << i) } else { acc }) } fn right_shift_one_circuit_version(v: &[[bool; 8]; 16]) -> [[bool; 8]; 16] { - let mut r: [[bool; 8]; 16] = v.clone(); + let mut r: [[bool; 8]; 16] = *v; let mut carry = false; for i in 0..16 { let current = v[i]; let next_carry = current[0]; let mut shifted = [false; 8]; - for j in 0..7 { - shifted[j] = current[j + 1]; - } + shifted[..7].copy_from_slice(¤t[1..(7 + 1)]); shifted[7] = carry; r[i] = shifted; carry = next_carry;@@ -272,8 +269,8 @@ mod tests {
use super::*; use aes_gcm::{ - aead::{Aead, KeyInit}, Aes128Gcm, Aes256Gcm, Nonce, + aead::{Aead, KeyInit}, }; use rand::Rng;@@ -338,8 +335,8 @@ fn test_with_external_lib() -> anyhow::Result<()> {
test_with_external_lib_op::<4, 4, 10, Aes128Gcm>()?; test_with_external_lib_op::<8, 4, 14, Aes256Gcm>() } - fn test_with_external_lib_op<const NK: usize, const NB: usize, const NR: usize, C>( - ) -> anyhow::Result<()> + fn test_with_external_lib_op<const NK: usize, const NB: usize, const NR: usize, C>() + -> anyhow::Result<()> where [(); NK * NB]:, [(); 4 * (NR + 1)]:,