Skip to content

Commit

Permalink
fix invalid poseidon hash usage
Browse files Browse the repository at this point in the history
We need to pad high-order bytes with zeros in case if result is less than 32 bytes.
  • Loading branch information
olegrok committed Jun 19, 2024
1 parent 93b9cf0 commit 196df26
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
7 changes: 3 additions & 4 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions tests/codetrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 196df26

Please sign in to comment.