From 196df267f5e1ef94b1906a0014c414d73f1d66c7 Mon Sep 17 00:00:00 2001 From: Oleg Babin Date: Wed, 19 Jun 2024 18:17:15 +0400 Subject: [PATCH] fix invalid poseidon hash usage We need to pad high-order bytes with zeros in case if result is less than 32 bytes. --- hasher.go | 7 +++---- tests/codetrie_test.go | 9 +++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/hasher.go b/hasher.go index 4b4881f..5686437 100755 --- a/hasher.go +++ b/hasher.go @@ -42,11 +42,10 @@ func init() { } func poseidonSum(input []byte) []byte { + output := make([]byte, 32) res := poseidon.Sum(input) - if rest := len(res) % 32; rest != 0 { - res = append(res, zeroBytes[:32-rest]...) - } - return res + copy(output[32-len(res):], res) + return output } // HashWithDefaultHasher hashes a HashRoot object with a Hasher from diff --git a/tests/codetrie_test.go b/tests/codetrie_test.go index 2b529dc..7ff20bb 100644 --- a/tests/codetrie_test.go +++ b/tests/codetrie_test.go @@ -11,14 +11,11 @@ import ( "github.com/iden3/go-iden3-crypto/poseidon" ) -var zeroBytes = make([]byte, 32) - func poseidonSum(input []byte) []byte { + output := make([]byte, 32) res := poseidon.Sum(input) - if len(res) != 32 { - res = append(res, zeroBytes[:32-len(res)]...) - } - return res + copy(output[32-len(res):], res) + return output } func TestVerifyMetadataProof(t *testing.T) {