Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/padding.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn pad(input: [u1; INPUT_SIZE], input_length: u64) -> [u1; BLOCK_SIZE] {
// We require 2 bits of space after the message in order to include the padding bits.
// constrain input_length < BLOCK_SIZE - 2;

let mut padded_input: [u1; BLOCK_SIZE] = [0 as u1; 10];
let mut padded_input: [u1; BLOCK_SIZE] = [0 as u1; BLOCK_SIZE];
for i in 0..BLOCK_SIZE {
if (i as u64) < input_length {
// Copy input into padded array.
Expand Down
5 changes: 3 additions & 2 deletions lib/src/permutations/rhoPi.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global STATE_SIZE: Field = 1600;
global LANE_LENGTH: Field = 64;
global NUM_LANES: Field = 25;

// This function is a combination of the rho and pi functions as defined in the Keccak256 specification.
// We merge these two functions as rho consists of a rotation of the bits within each lane and pi is a remapping of
Expand All @@ -18,14 +19,14 @@ fn rhoPi(state: [u1; STATE_SIZE]) -> [u1; STATE_SIZE] {
// This definition adds an additional modulo compared to the spec but makes it easier to calculate correct offsets.
let T: [comptime Field; 24] = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44];

let mut new_state: [u1; STATE_SIZE] = [0 as u1; 1600];
let mut new_state: [u1; STATE_SIZE] = [0 as u1; STATE_SIZE];
// The center lane is unaffected by the rho and pi functions so we write it directly into the new state.
for i in 0..LANE_LENGTH {
new_state[i] = state[i];
};

// Now for the remaining lanes we write the rotated lane outputted from rho into the remapped lane specified by pi.
for i in 0..24 {
for i in 0..NUM_LANES - 1 {
for z in 0..LANE_LENGTH {
// This is equivalent to `(z - T[i]) % LANE_LENGTH`
let shifted_z_position = if z >= T[i] { z - T[i] } else { LANE_LENGTH - T[i] + z };
Expand Down
20 changes: 10 additions & 10 deletions lib/src/permutations/theta.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ fn theta(state: [u1; STATE_SIZE]) -> [u1; STATE_SIZE] {
// The theta function works by calculating the parity of each of the columns in the state array. We store these
// in the C[x, z] arrays.
// C[x, z] = A[x, 0, z] ^ A[x, 1, z] ^ A[x, 2, z] ^ A[x, 3, z] ^ A[x, 4, z]
let mut c0: [u1; LANE_LENGTH] = [0 as u1; 64];
let mut c1: [u1; LANE_LENGTH] = [0 as u1; 64];
let mut c2: [u1; LANE_LENGTH] = [0 as u1; 64];
let mut c3: [u1; LANE_LENGTH] = [0 as u1; 64];
let mut c4: [u1; LANE_LENGTH] = [0 as u1; 64];
let mut c0: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH];
let mut c1: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH];
let mut c2: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH];
let mut c3: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH];
let mut c4: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH];
for i in 0..LANE_LENGTH {
c0[i] = state[0 * LANE_LENGTH + i] ^ state[5 * LANE_LENGTH + i] ^ state[10 * LANE_LENGTH + i] ^ state[15 * LANE_LENGTH + i] ^ state[20 * LANE_LENGTH + i];
c1[i] = state[1 * LANE_LENGTH + i] ^ state[6 * LANE_LENGTH + i] ^ state[11 * LANE_LENGTH + i] ^ state[16 * LANE_LENGTH + i] ^ state[21 * LANE_LENGTH + i];
Expand All @@ -19,11 +19,11 @@ fn theta(state: [u1; STATE_SIZE]) -> [u1; STATE_SIZE] {
};

// D[x, z] = C[(x - 1) mod 5, z] ^ C[(x + 1) mod 5, (z - 1) mod LANE_LENGTH]
let mut d0: [u1; LANE_LENGTH] = [0 as u1; 64]; // D[0, Z] = C[4, z] ^ C[1, (z-1) mod LANE_LENGTH]
let mut d1: [u1; LANE_LENGTH] = [0 as u1; 64]; // D[1, Z] = C[0, z] ^ C[2, (z-1) mod LANE_LENGTH]
let mut d2: [u1; LANE_LENGTH] = [0 as u1; 64]; // D[2, Z] = C[1, z] ^ C[3, (z-1) mod LANE_LENGTH]
let mut d3: [u1; LANE_LENGTH] = [0 as u1; 64]; // D[3, Z] = C[2, z] ^ C[4, (z-1) mod LANE_LENGTH]
let mut d4: [u1; LANE_LENGTH] = [0 as u1; 64]; // D[4, Z] = C[3, z] ^ C[0, (z-1) mod LANE_LENGTH]
let mut d0: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH]; // D[0, Z] = C[4, z] ^ C[1, (z-1) mod LANE_LENGTH]
let mut d1: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH]; // D[1, Z] = C[0, z] ^ C[2, (z-1) mod LANE_LENGTH]
let mut d2: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH]; // D[2, Z] = C[1, z] ^ C[3, (z-1) mod LANE_LENGTH]
let mut d3: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH]; // D[3, Z] = C[2, z] ^ C[4, (z-1) mod LANE_LENGTH]
let mut d4: [u1; LANE_LENGTH] = [0 as u1; LANE_LENGTH]; // D[4, Z] = C[3, z] ^ C[0, (z-1) mod LANE_LENGTH]
// The modulus only affects the first cell in the lane so we handle this outside of the for-loop.
d0[0] = c4[0] ^ c1[LANE_LENGTH - 1];
d1[0] = c0[0] ^ c2[LANE_LENGTH - 1];
Expand Down