Skip to content

Commit

Permalink
crypto: poseidon2: Clean the stack in Poseidon utils (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Jan 27, 2025
1 parent 59a8817 commit d88e5b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/crypto/poseidon2/roundUtils.huff
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
ADD_RC(<RC1>) // [a + RC1, (b + RC2)^5, (c + RC3)^5, a, b, c]
PUSH_PRIME() swap1 // [a + RC1, PRIME, (b + RC2)^5, (c + RC3)^5, a, b, c]
SBOX() // [(a + RC1)^5, (b + RC2)^5, (c + RC3)^5, a, b, c]

// Pop the old state off the stack
swap3 pop swap3 pop swap3 pop // [a', b', c']

// Multiply the intermediate state by the external round MDS matrix
EXTERNAL_MDS()
Expand Down Expand Up @@ -104,10 +107,15 @@
PUSH_PRIME() dup2 PUSH_PRIME() // [PRIME, sum, PRIME, sum, state[0], state[1], state[2]]
dup7 dup1 addmod // [state[2] * 2, sum, PRIME, sum, state[0], state[1], state[2]]
addmod // [state'[2], sum, state[0], state[1], state[2]]
PUSH_PRIME() dup3 // [sum, PRIME, state'[2], state[0], state[1], state[2]]
dup6 addmod // [state'[1], state'[2], sum, state[0], state[1], state[2]]
PUSH_PRIME() dup4 // [sum, PRIME, state'[1], state'[2], sum, state[0], state[1], state[2]]
dup6 addmod // [state'[0], state'[1], state'[2], sum, state[0], state[1], state[2]]
swap4 pop // [sum, state[0], state[1], state'[2]]

PUSH_PRIME() dup2 // [sum, PRIME, sum, state[0], state[1], state'[2]]
dup5 addmod // [state'[1], sum, state[0], state[1], state'[2]]
swap3 pop // [sum, state[0], state'[1], state'[2]]

PUSH_PRIME() dup2 // [sum, PRIME, sum, state[0], state'[1], state'[2]]
dup4 addmod // [state'[0], sum, state[0], state'[1], state'[2]]
swap2 pop pop // [state'[0], state'[1], state'[2]]
}

/// @dev Apply the external MDS matrix to the sponge state
Expand Down
7 changes: 2 additions & 5 deletions test/poseidon-reference-implementation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# renegade-constants = { package = "constants", git = "https://github.com/renegade-fi/renegade.git", default-features = false }
# renegade-crypto = { git = "https://github.com/renegade-fi/renegade.git" }

renegade-constants = { package = "constants", path = "/Users/joeykraut/work/renegade/constants", default-features = false }
renegade-crypto = { path = "/Users/joeykraut/work/renegade/renegade-crypto" }
renegade-constants = { package = "constants", git = "https://github.com/renegade-fi/renegade.git", default-features = false }
renegade-crypto = { git = "https://github.com/renegade-fi/renegade.git" }

num-bigint = "0.4"

0 comments on commit d88e5b8

Please sign in to comment.