Skip to content

Commit 25ff1f3

Browse files
committed
Fix poseidon hashing when absorbing empty slice
1 parent 9aabaa2 commit 25ff1f3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ledger/src/poseidon/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,23 @@ impl<F: Field, SC: SpongeConstants> Sponge<F, F> for ArithmeticSponge<F, SC> {
18721872

18731873
#[inline(never)]
18741874
fn absorb(&mut self, x: &[F]) {
1875+
if x.is_empty() {
1876+
// Same as the loop below but doesn't add `x`
1877+
match self.sponge_state {
1878+
SpongeState::Absorbed(n) => {
1879+
if n == self.rate {
1880+
self.poseidon_block_cipher();
1881+
self.sponge_state = SpongeState::Absorbed(1);
1882+
} else {
1883+
self.sponge_state = SpongeState::Absorbed(n + 1);
1884+
}
1885+
}
1886+
SpongeState::Squeezed(_n) => {
1887+
self.sponge_state = SpongeState::Absorbed(1);
1888+
}
1889+
}
1890+
return;
1891+
}
18751892
for x in x.iter() {
18761893
match self.sponge_state {
18771894
SpongeState::Absorbed(n) => {

ledger/src/scan_state/transaction_logic.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,9 @@ pub mod zkapp_command {
945945
pub struct Event(pub Vec<Fp>);
946946

947947
impl Event {
948+
pub fn empty() -> Self {
949+
Self(Vec::new())
950+
}
948951
pub fn hash(&self) -> Fp {
949952
hash_with_kimchi("MinaZkappEvent", &self.0[..])
950953
}
@@ -8395,6 +8398,16 @@ mod tests {
83958398
.into_compressed()
83968399
}
83978400

8401+
#[test]
8402+
fn test_hash_empty_event() {
8403+
// Same value than OCaml
8404+
const EXPECTED: &str =
8405+
"6963060754718463299978089777716994949151371320681588566338620419071140958308";
8406+
8407+
let event = zkapp_command::Event::empty();
8408+
assert_eq!(event.hash(), Fp::from_str(EXPECTED).unwrap());
8409+
}
8410+
83988411
/// Test using same values as here:
83998412
/// https://github.com/MinaProtocol/mina/blob/3a78f0e0c1343d14e2729c8b00205baa2ec70c93/src/lib/mina_base/receipt.ml#L136
84008413
#[test]

0 commit comments

Comments
 (0)