From 80d5e5375e18a7ea449168cecd7bfc31a46ff44e Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Fri, 3 Jun 2022 11:32:19 -0700 Subject: [PATCH] fixes, cleanup, and correctness test --- evm/Cargo.toml | 1 + evm/src/keccak/keccak_stark.rs | 71 +++++++++++++++++++++++----------- evm/src/keccak/registers.rs | 14 +++---- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/evm/Cargo.toml b/evm/Cargo.toml index d65fb30c..e282583e 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -14,3 +14,4 @@ log = "0.4.14" rayon = "1.5.1" rand = "0.8.5" rand_chacha = "0.3.1" +keccak-rust = { git = "https://github.com/npwardberkeley/keccak-rust" } \ No newline at end of file diff --git a/evm/src/keccak/keccak_stark.rs b/evm/src/keccak/keccak_stark.rs index 147d393c..87787e31 100644 --- a/evm/src/keccak/keccak_stark.rs +++ b/evm/src/keccak/keccak_stark.rs @@ -130,7 +130,7 @@ impl, const D: usize> KeccakStark { row[reg_a_prime(x, y, z)] = xor([ row[reg_a(x, y, z)], row[reg_c((x + 4) % 5, z)], - row[reg_c((x + 1) % 5, (z + 1) % 64)], + row[reg_c((x + 1) % 5, (z + 64 - 1) % 64)], ]); } } @@ -358,12 +358,8 @@ impl, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark, const D: usize> Stark for KeccakStark usize { @@ -528,9 +507,13 @@ impl, const D: usize> Stark for KeccakStark(stark) } + + #[test] + fn keccak_correctness_test() -> Result<()> { + let mut input: Vec = Vec::new(); + let mut rng = rand::thread_rng(); + for _ in 0..INPUT_LIMBS { + input.push(rng.gen()); + } + + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + type S = KeccakStark; + + let stark = S { + f: Default::default(), + }; + + let rows = stark.generate_trace_rows(vec![input.clone().try_into().unwrap()]); + let last_row = rows[23]; + let mut output = Vec::new(); + let base = F::from_canonical_u64(1 << 32); + for x in 0..5 { + for y in 0..5 { + output.push(last_row[reg_a_prime_prime_prime(x, y)] + base * last_row[reg_a_prime_prime_prime(x, y) + 1]); + } + } + + let mut expected: [[u64; 5]; 5] = [input[0..5].try_into().unwrap(), + input[5..10].try_into().unwrap(), + input[10..15].try_into().unwrap(), + input[15..20].try_into().unwrap(), + input[20..25].try_into().unwrap()]; + + let keccak = KeccakF::new(StateBitsWidth::F1600); + keccak.permutations(&mut expected); + let expected_flattened: Vec<_> = expected.iter().flatten().map(|&x| F::from_canonical_u64(x)).collect(); + + assert_eq!(output, expected_flattened); + + Ok(()) + } } diff --git a/evm/src/keccak/registers.rs b/evm/src/keccak/registers.rs index f3e847b1..9a851eec 100644 --- a/evm/src/keccak/registers.rs +++ b/evm/src/keccak/registers.rs @@ -7,11 +7,11 @@ pub(crate) const fn reg_step(i: usize) -> usize { } const R: [[u8; 5]; 5] = [ - [0, 18, 41, 3, 36], - [1, 2, 45, 10, 44], - [62, 61, 15, 43, 6], - [28, 56, 21, 25, 55], - [27, 14, 8, 39, 20], + [0, 36, 3, 41, 18], + [1, 44, 10, 45, 2], + [62, 6, 43, 15, 61], + [28, 55, 25, 21, 56], + [27, 20, 39, 8, 14], ]; const RC: [u64; 24] = [ @@ -218,11 +218,10 @@ pub(crate) const fn reg_b(x: usize, y: usize, z: usize) -> usize { let a = (x + 3 * y) % 5; let b = x; let rot = R[a][b] as usize; - reg_a_prime(a, b, (z + rot) % 64) + reg_a_prime(a, b, (z + 64 - rot) % 64) } // A''[x, y] = xor(B[x, y], andn(B[x + 1, y], B[x + 2, y])). -// A''[0, 0] is additionally xor'd with RC. const START_A_PRIME_PRIME: usize = START_A_PRIME + 5 * 5 * 64; pub(crate) const fn reg_a_prime_prime(x: usize, y: usize) -> usize { debug_assert!(x < 5); @@ -239,6 +238,7 @@ pub(crate) const fn reg_a_prime_prime_0_0_bit(i: usize) -> usize { const REG_A_PRIME_PRIME_PRIME_0_0_LO: usize = START_A_PRIME_PRIME_0_0_BITS + 64; const REG_A_PRIME_PRIME_PRIME_0_0_HI: usize = REG_A_PRIME_PRIME_PRIME_0_0_LO + 1; +// A'''[0, 0] is additionally xor'd with RC. pub(crate) const fn reg_a_prime_prime_prime(x: usize, y: usize) -> usize { debug_assert!(x < 5); debug_assert!(y < 5);