Skip to content

Commit 3d33b6d

Browse files
committed
Update poseidon in witness generation
Fix absorbing empty slice
1 parent 25ff1f3 commit 3d33b6d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

ledger/src/proofs/transaction.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,28 @@ pub mod poseidon {
16871687
Self::new_with_state([F::zero(); 3], F::get_params2())
16881688
}
16891689

1690+
fn absorb_empty(&mut self, w: &mut Witness<F>) {
1691+
match self.sponge_state {
1692+
SpongeState::Absorbed(n) => {
1693+
if n == C::SPONGE_RATE {
1694+
self.poseidon_block_cipher(true, w);
1695+
self.sponge_state = SpongeState::Absorbed(1);
1696+
} else {
1697+
self.sponge_state = SpongeState::Absorbed(n + 1);
1698+
}
1699+
}
1700+
SpongeState::Squeezed(_n) => {
1701+
self.sponge_state = SpongeState::Absorbed(1);
1702+
}
1703+
}
1704+
}
1705+
16901706
pub fn absorb(&mut self, x: &[F], w: &mut Witness<F>) {
1707+
if x.is_empty() {
1708+
self.absorb_empty(w);
1709+
return;
1710+
}
1711+
16911712
// Hack to know when to ignore witness
16921713
// That should be removed once we use `cvar`
16931714
let mut first = true;
@@ -1719,6 +1740,11 @@ pub mod poseidon {
17191740
}
17201741

17211742
pub fn absorb2(&mut self, x: &[F], w: &mut Witness<F>) {
1743+
if x.is_empty() {
1744+
self.absorb_empty(w);
1745+
return;
1746+
}
1747+
17221748
// Hack to know when to ignore witness
17231749
// That should be removed once we use `cvar`
17241750
let mut first = true;
@@ -1754,6 +1780,11 @@ pub mod poseidon {
17541780
}
17551781

17561782
pub fn absorb3(&mut self, x: &[F], w: &mut Witness<F>) {
1783+
if x.is_empty() {
1784+
self.absorb_empty(w);
1785+
return;
1786+
}
1787+
17571788
// Hack to know when to ignore witness
17581789
// That should be removed once we use `cvar`
17591790
let mut first = true;
@@ -4429,6 +4460,21 @@ mod tests {
44294460
dbg!(sum);
44304461
}
44314462

4463+
#[test]
4464+
fn test_hash_empty_event_checked() {
4465+
// Same value than OCaml
4466+
const EXPECTED: &str =
4467+
"6963060754718463299978089777716994949151371320681588566338620419071140958308";
4468+
4469+
let mut w = Witness::empty();
4470+
let hash = transaction_snark::checked_hash("MinaZkappEvent", &[], &mut w);
4471+
assert_eq!(hash, Fp::from_str(EXPECTED).unwrap());
4472+
4473+
let mut w = Witness::empty();
4474+
let hash = transaction_snark::checked_hash3("MinaZkappEvent", &[], &mut w);
4475+
assert_eq!(hash, Fp::from_str(EXPECTED).unwrap());
4476+
}
4477+
44324478
/// Print requests types
44334479
#[allow(unused)]
44344480
#[test]

0 commit comments

Comments
 (0)