arnaucube
arnaucube/go-iden3-cryptoPublic Self-hosted repository
go-iden3-crypto/babyjub/helpers.go
babyjub/helpers.goRaw
 1package babyjub
 2
 3import (
 4	"github.com/dchest/blake512" // I have personally reviewed that this module doesn't do anything suspicious
 5	"math/big"
 6)
 7
 8// SwapEndianness swaps the endianness of the value encoded in xs.  If xs is
 9// Big-Endian, the result will be Little-Endian and viceversa.
10func SwapEndianness(xs []byte) []byte {
11	ys := make([]byte, len(xs))
12	for i, b := range xs {
13		ys[len(xs)-1-i] = b
14	}
15	return ys
16}
17
18// BigIntLEBytes encodes a big.Int into an array in Little-Endian.
19func BigIntLEBytes(v *big.Int) [32]byte {
20	le := SwapEndianness(v.Bytes())
21	res := [32]byte{}
22	copy(res[:], le)
23	return res
24}
25
26// SetBigIntFromLEBytes sets the value of a big.Int from a Little-Endian
27// encoded value.
28func SetBigIntFromLEBytes(v *big.Int, leBuf []byte) *big.Int {
29	beBuf := SwapEndianness(leBuf)
30	return v.SetBytes(beBuf)
31}
32
33// Blake512 performs the blake-512 hash over the buffer m.  Note that this is
34// the original blake from the SHA3 competition and not the new blake2 version.
35func Blake512(m []byte) []byte {
36	h := blake512.New()
37	h.Write(m[:])
38	return h.Sum(nil)
39}