From 0cbd019c40ffbdd5107bf4c3051a9baff4687958 Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Tue, 29 Aug 2023 13:48:50 -0300 Subject: [PATCH] fix hashing of symbols (#636) * fix hashing of symbols * add test for the correctness of hashing symbols --- src/eval/tests/mod.rs | 2 +- src/store.rs | 25 +++++++++++++++++++++++++ src/z_data/z_store.rs | 2 +- tests/lurk-cli-tests.rs | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/eval/tests/mod.rs b/src/eval/tests/mod.rs index 6ca9f680f3..57f3cc01de 100644 --- a/src/eval/tests/mod.rs +++ b/src/eval/tests/mod.rs @@ -1597,7 +1597,7 @@ fn commitment_value() { fn commit_nil() { let s = &mut Store::::default(); let x = s - .read("0x239b15d97a9a69b3db1c9130601ec2a1f8ac2ed6033633e4fb5232d85c622250") + .read("0x1f7f3e554ed27c104d79bb69346996d61a735d5bbedc2da7da2935036d9c4fad") .unwrap(); let expr = "(num (commit nil))"; diff --git a/src/store.rs b/src/store.rs index 043b93c344..1da380992d 100644 --- a/src/store.rs +++ b/src/store.rs @@ -2244,6 +2244,31 @@ pub mod test { store.cdr(&opaque_cons).unwrap(); } + #[test] + fn symbol_hashing() { + let s = &mut Store::::default(); + let foo_ptr = s.intern_string("foo"); + let bar_ptr = s.intern_string("bar"); + let foo_bar_ptr = s.intern_symbol(&Symbol::sym_from_vec(vec!["foo".into(), "bar".into()])); + + let foo_z_ptr = s.hash_expr(&foo_ptr).unwrap(); + let bar_z_ptr = s.hash_expr(&bar_ptr).unwrap(); + let foo_bar_hash = s.hash_expr(&foo_bar_ptr).unwrap().1; + + let foo_bar_hash_manual = s.poseidon_cache.hash4(&[ + bar_z_ptr.0.to_field(), + bar_z_ptr.1, + ExprTag::Sym.to_field(), + s.poseidon_cache.hash4(&[ + foo_z_ptr.0.to_field(), + foo_z_ptr.1, + ExprTag::Sym.to_field(), + Fr::ZERO, + ]), + ]); + assert_eq!(foo_bar_hash, foo_bar_hash_manual); + } + #[test] fn sym_and_key_hashes() { let s = &mut Store::::default(); diff --git a/src/z_data/z_store.rs b/src/z_data/z_store.rs index 828b09b2ac..0aab5e4650 100644 --- a/src/z_data/z_store.rs +++ b/src/z_data/z_store.rs @@ -168,7 +168,7 @@ impl ZStore { ) -> (ZExprPtr, ZExpr) { let mut expr = ZExpr::RootSym; let mut ptr = expr.z_ptr(poseidon_cache); - for s in sym.path().iter().rev() { + for s in sym.path() { let (str_ptr, _) = self.put_string(s, poseidon_cache); expr = ZExpr::Sym(str_ptr, ptr); ptr = expr.z_ptr(poseidon_cache); diff --git a/tests/lurk-cli-tests.rs b/tests/lurk-cli-tests.rs index ef51e1fa54..125198ee0f 100644 --- a/tests/lurk-cli-tests.rs +++ b/tests/lurk-cli-tests.rs @@ -91,7 +91,7 @@ fn test_prove_and_verify() { let mut file = File::create(lurk_file.clone()).unwrap(); file.write_all(b"!(prove (+ 1 1))\n").unwrap(); - file.write_all(b"!(verify \"Nova_Pallas_10_0d723f6dd68729d7d119a13386f81d04daf6e1715f9ad53fb1ea54646771108a\")\n").unwrap(); + file.write_all(b"!(verify \"Nova_Pallas_10_3f2526abf20fc9006dd93c0d3ff49954ef070ef52d2e88426974de42cc27bdb2\")\n").unwrap(); let mut cmd = lurk_cmd(); cmd.arg("load");