-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathelembytes.go
49 lines (41 loc) · 1.14 KB
/
elembytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package merkletree
import (
"encoding/hex"
"fmt"
"math/big"
)
const (
// ElemBytesLen is the length of the Hash byte array
ElemBytesLen = 32
)
// ElemBytes is the basic type used to store data in the MT. ElemBytes
// corresponds to the serialization of an element from mimc7.
type ElemBytes [ElemBytesLen]byte
func NewElemBytesFromBigInt(v *big.Int) (e ElemBytes) {
bs := SwapEndianness(v.Bytes())
copy(e[:], bs)
return e
}
func (e *ElemBytes) BigInt() *big.Int {
return new(big.Int).SetBytes(SwapEndianness(e[:]))
}
// String returns the first 4 bytes of ElemBytes in hex.
func (e *ElemBytes) String() string {
return fmt.Sprintf("%v...", hex.EncodeToString(e[:4]))
}
// ElemBytesToBytes serializes an array of ElemBytes to []byte.
func ElemBytesToBytes(es []ElemBytes) []byte {
bs := make([]byte, len(es)*ElemBytesLen)
for i := 0; i < len(es); i++ {
copy(bs[i*ElemBytesLen:(i+1)*ElemBytesLen], es[i][:])
}
return bs
}
// ElemBytesToBigInts serializes an array of ElemBytes to []byte.
func ElemBytesToBigInts(es []ElemBytes) []*big.Int {
bs := make([]*big.Int, len(es))
for i := 0; i < len(es); i++ {
bs[i] = es[i].BigInt()
}
return bs
}