Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poseidon hash type #16

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/wealdtech/go-merkletree/v2/blake2b"
"github.com/wealdtech/go-merkletree/v2/keccak256"
"github.com/wealdtech/go-merkletree/v2/poseidon"
"github.com/wealdtech/go-merkletree/v2/sha3"
)

Expand Down Expand Up @@ -49,6 +50,8 @@ func (t *MerkleTree) UnmarshalJSON(data []byte) error {
aux.Hash = blake2b.New()
case "keccak256":
aux.Hash = keccak256.New()
case "poseidon":
aux.Hash = poseidon.New()
default:
return errors.New("cannot parse hash type")
}
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ go 1.20

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.8.2
golang.org/x/crypto v0.18.0
)

require gopkg.in/yaml.v3 v3.0.1 // indirect

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/iden3/go-iden3-crypto v0.0.16
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.16.0 // indirect
)
19 changes: 16 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iden3/go-iden3-crypto v0.0.16 h1:zN867xiz6HgErXVIV/6WyteGcOukE9gybYTorBMEdsk=
github.com/iden3/go-iden3-crypto v0.0.16/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
61 changes: 61 additions & 0 deletions poseidon/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package poseidon provides hashing using the Poseidon system.
package poseidon

import (
"github.com/iden3/go-iden3-crypto/poseidon"
)

const _hashlength = 32

// Poseidon is the Poseidon hashing method.
type Poseidon struct{}

// New creates a new Poseidon hashing method with `frameSize` set to 16.
func New() *Poseidon {
return &Poseidon{}
}

// Hash generates a Poseidon hash from a byte array.
func (ph *Poseidon) Hash(data ...[]byte) []byte {
var hash []byte
if len(data) == 1 {
hash = poseidon.Sum(data[0])
} else {
concatDataLen := 0
for _, d := range data {
concatDataLen += len(d)
}
concatData := make([]byte, concatDataLen)
curOffset := 0
for _, d := range data {
copy(concatData[curOffset:], d)
curOffset += len(d)
}
hash = poseidon.Sum(concatData)
}

return hash
}

// HashLength returns the length of hashes generated by Hash() in bytes.
func (ph *Poseidon) HashLength() int {
return _hashlength
}

// HashName returns the name of this hash.
func (ph *Poseidon) HashName() string {
return "poseidon"
}
24 changes: 24 additions & 0 deletions poseidon/hash_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2023 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package poseidon

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHashLength(t *testing.T) {
assert.Equal(t, _hashlength, New().HashLength(), "incorrect hash length reported")
}
75 changes: 75 additions & 0 deletions poseidon/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2018, 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package poseidon_test

import (
"encoding/hex"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/wealdtech/go-merkletree/v2/poseidon"
)

// _byteArray is a helper to turn a string in to a byte array
func _byteArray(input string) []byte {
x, err := hex.DecodeString(input)
if err != nil {
panic(err)
}
return x
}

func TestHash(t *testing.T) {
tests := []struct {
data []byte
output []byte
}{
{
data: _byteArray("e9e0083e456539e9f6336164cd98700e668178f98af147ef750eb90afcf2f637"),
output: _byteArray("05c26e4fd0b4cbf27aa04a2f7934afc086bfb20e9d431f7c50b7e5cc5ea07a02"),
},
}

hash := poseidon.New()
assert.Equal(t, "poseidon", hash.HashName())
for i, test := range tests {
output := hash.Hash(test.data)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}

func TestMultiHash(t *testing.T) {
tests := []struct {
data1 []byte
data2 []byte
data3 []byte
data4 []byte
output []byte
}{
{
data1: _byteArray("e9e0083e456539e9"),
data2: _byteArray("f6336164cd98700e"),
data3: _byteArray("668178f98af147ef"),
data4: _byteArray("750eb90afcf2f637"),
output: _byteArray("05c26e4fd0b4cbf27aa04a2f7934afc086bfb20e9d431f7c50b7e5cc5ea07a02"),
},
}

hash := poseidon.New()
for i, test := range tests {
output := hash.Hash(test.data1, test.data2, test.data3, test.data4)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}
Loading