support hash interface for poseidon; support golang crypto interfaces for bjj

This commit is contained in:
Ilya
2023-05-09 13:21:59 +03:00
parent bd5255dea7
commit b015806983
4 changed files with 336 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package poseidon
import (
"bytes"
"hash"
)
type digest struct {
buf *bytes.Buffer
}
// NewPoseidon returns the Poseidon hash of the input bytes.
func NewPoseidon(b []byte) []byte {
h := New()
h.Write(b)
return h.Sum(nil)
}
// New returns a new hash.Hash computing the Poseidon hash.
func New() hash.Hash {
return &digest{
buf: bytes.NewBuffer([]byte{}),
}
}
// Write (via the embedded io.Writer interface) adds more data to the running hash.
func (d *digest) Write(p []byte) (n int, err error) {
return d.buf.Write(p)
}
// Sum returns the Poseidon checksum of the data.
func (d *digest) Sum(b []byte) []byte {
hahs, err := HashBytes(d.buf.Bytes())
if err != nil {
panic(err)
}
return append(b, hahs.Bytes()...)
}
// Reset resets the Hash to its initial state.
func (d *digest) Reset() {
d.buf.Reset()
}
// Size returns the number of bytes Sum will return.
func (d *digest) Size() int {
return 32
}
// BlockSize returns the hash block size.
func (d *digest) BlockSize() int {
return spongeChunkSize
}

File diff suppressed because one or more lines are too long