|
|
@ -2,25 +2,32 @@ package poseidon |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"errors" |
|
|
|
"hash" |
|
|
|
) |
|
|
|
|
|
|
|
type digest struct { |
|
|
|
buf *bytes.Buffer |
|
|
|
buf *bytes.Buffer |
|
|
|
frameSize int |
|
|
|
} |
|
|
|
|
|
|
|
// NewPoseidon returns the Poseidon hash of the input bytes.
|
|
|
|
// use frame size of 16 inputs by default
|
|
|
|
func NewPoseidon(b []byte) []byte { |
|
|
|
h := New() |
|
|
|
h, _ := New(16) |
|
|
|
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{}), |
|
|
|
func New(frameSize int) (hash.Hash, error) { |
|
|
|
if frameSize < 2 || frameSize > 16 { |
|
|
|
return nil, errors.New("incorrect frame size") |
|
|
|
} |
|
|
|
return &digest{ |
|
|
|
buf: bytes.NewBuffer([]byte{}), |
|
|
|
frameSize: frameSize, |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|
|
|
|
// Write (via the embedded io.Writer interface) adds more data to the running hash.
|
|
|
@ -30,7 +37,7 @@ func (d *digest) Write(p []byte) (n int, err error) { |
|
|
|
|
|
|
|
// Sum returns the Poseidon checksum of the data.
|
|
|
|
func (d *digest) Sum(b []byte) []byte { |
|
|
|
hahs, err := HashBytes(d.buf.Bytes()) |
|
|
|
hahs, err := HashBytesX(d.buf.Bytes(), d.frameSize) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|