-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashout.go
122 lines (106 loc) · 3.25 KB
/
hashout.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package pgoldilocks
import (
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
)
type HashOut256 [4]uint64
var (
// HashOut256Zero is used at Empty nodes
HashOut256Zero = HashOut256{0, 0, 0, 0}
)
func (h HashOut256) Bytes() []byte {
bytes := make([]byte, 32)
binary.BigEndian.PutUint64(bytes[0:8], h[3])
binary.BigEndian.PutUint64(bytes[8:16], h[2])
binary.BigEndian.PutUint64(bytes[16:24], h[1])
binary.BigEndian.PutUint64(bytes[24:32], h[0])
return bytes
}
// MarshalText implements the marshaler for the HashOut256 type
func (h HashOut256) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h.Bytes())), nil
}
func (h HashOut256) String() string {
return hex.EncodeToString(h.Bytes())
}
func (h HashOut256) ElementString() string {
return fmt.Sprintf("[%d,%d,%d,%d]", h[0], h[1], h[2], h[3])
}
func (h HashOut256) Hex() string {
return hex.EncodeToString(h.Bytes())
}
func (h *HashOut256) Copy(x *HashOut256) {
h[0] = x[0]
h[1] = x[1]
h[2] = x[2]
h[3] = x[3]
}
func (h HashOut256) IsZeroHash() bool {
return h[0] == 0 && h[1] == 0 && h[2] == 0 && h[3] == 0
}
func (h *HashOut256) UnmarshalText(b []byte) error {
ha, err := NewHashOut256FromString(string(b))
if err != nil {
return err
}
copy(h[:], ha[:])
return nil
}
func (h *HashOut256) IsInGoldilocksField() bool {
return h[0] < 18446744069414584321 && h[1] < 18446744069414584321 && h[2] < 18446744069414584321 && h[3] < 18446744069414584321
}
func (h *HashOut256) Equals(h2 *HashOut256) bool {
return h[0] == h2[0] && h[1] == h2[1] && h[2] == h2[2] && h[3] == h2[3]
}
func NewHashOut256FromBytes(b []byte) (*HashOut256, error) {
if len(b) != 32 {
return nil, fmt.Errorf("NewHashOut256FromBytes: input bytes must be of length 32")
}
h := &HashOut256{binary.BigEndian.Uint64(b[24:32]), binary.BigEndian.Uint64(b[16:24]), binary.BigEndian.Uint64(b[8:16]), binary.BigEndian.Uint64(b[0:8])}
if !h.IsInGoldilocksField() {
return nil, fmt.Errorf("NewHashOut256FromBytes: contains element not in goldilocks field")
}
return h, nil
}
func NewHashOut256FromBytes32(b [32]byte) HashOut256 {
return HashOut256{binary.BigEndian.Uint64(b[24:32]), binary.BigEndian.Uint64(b[16:24]), binary.BigEndian.Uint64(b[8:16]), binary.BigEndian.Uint64(b[0:8])}
}
func NewHashOut256FromKnownSize(b []byte) HashOut256 {
return HashOut256{binary.BigEndian.Uint64(b[24:32]), binary.BigEndian.Uint64(b[16:24]), binary.BigEndian.Uint64(b[8:16]), binary.BigEndian.Uint64(b[0:8])}
}
func NewHashOut256FromString(s string) (*HashOut256, error) {
if strings.HasPrefix(s, "0x") {
d, err := hex.DecodeString(s[2:])
if err != nil {
return nil, err
}
return NewHashOut256FromBytes(d)
} else {
d, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return NewHashOut256FromBytes(d)
}
}
func NewHashOut256FromStringNoError(s string) *HashOut256 {
r, err := NewHashOut256FromString(s)
if err != nil {
panic(err)
}
return r
}
func NewHashOut256FromUint64Array(d [4]uint64) HashOut256 {
return HashOut256{d[0], d[1], d[2], d[3]}
}
func NewHashOut256FromUint64(x uint64) HashOut256 {
return HashOut256{x, 0, 0, 0}
}
func NewHashOut256PtrFromUint64(x uint64) *HashOut256 {
return &HashOut256{x, 0, 0, 0}
}
func NewHashOut256PtrFromUint64Last(x uint64) *HashOut256 {
return &HashOut256{0, 0, 0, x}
}