-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
179 lines (145 loc) · 3.83 KB
/
utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package haval
import (
"math/bits"
"encoding/binary"
)
// Endianness option
const littleEndian bool = true
func getu32(ptr []byte) uint32 {
if littleEndian {
return binary.LittleEndian.Uint32(ptr)
} else {
return binary.BigEndian.Uint32(ptr)
}
}
func putu32(ptr []byte, a uint32) {
if littleEndian {
binary.LittleEndian.PutUint32(ptr, a)
} else {
binary.BigEndian.PutUint32(ptr, a)
}
}
func getu64(ptr []byte) uint64 {
if littleEndian {
return binary.LittleEndian.Uint64(ptr)
} else {
return binary.BigEndian.Uint64(ptr)
}
}
func putu64(ptr []byte, a uint64) {
if littleEndian {
binary.LittleEndian.PutUint64(ptr, a)
} else {
binary.BigEndian.PutUint64(ptr, a)
}
}
func bytesToUint32s(b []byte) []uint32 {
size := len(b) / 4
dst := make([]uint32, size)
for i := 0; i < size; i++ {
j := i * 4
if littleEndian {
dst[i] = binary.LittleEndian.Uint32(b[j:])
} else {
dst[i] = binary.BigEndian.Uint32(b[j:])
}
}
return dst
}
func uint32sToBytes(w []uint32) []byte {
size := len(w) * 4
dst := make([]byte, size)
for i := 0; i < len(w); i++ {
j := i * 4
if littleEndian {
binary.LittleEndian.PutUint32(dst[j:], w[i])
} else {
binary.BigEndian.PutUint32(dst[j:], w[i])
}
}
return dst
}
func circularLeft(x uint32, n int) uint32 {
return bits.RotateLeft32(x, n)
}
func circularRight(x uint32, n int) uint32 {
return circularLeft(x, 32 - n)
}
func F1(x6, x5, x4, x3, x2, x1, x0 uint32) uint32 {
return (x1 & x4) ^ (x2 & x5) ^ (x3 & x6) ^ (x0 & x1) ^ x0
}
func F2(x6, x5, x4, x3, x2, x1, x0 uint32) uint32 {
return (x2 & ((x1 & ^x3) ^ (x4 & x5) ^ x6 ^ x0)) ^
(x4 & (x1 ^ x5)) ^ ((x3 & x5) ^ x0)
}
func F3(x6, x5, x4, x3, x2, x1, x0 uint32) uint32 {
return (x3 & ((x1 & x2) ^ x6 ^ x0)) ^
(x1 & x4) ^ (x2 & x5) ^ x0
}
func F4(x6, x5, x4, x3, x2, x1, x0 uint32) uint32 {
return (x3 & ((x1 & x2) ^ (x4 | x6) ^ x5)) ^
(x4 & ((^x2 & x5) ^ x1 ^ x6 ^ x0)) ^ (x2 & x6) ^ x0
}
func F5(x6, x5, x4, x3, x2, x1, x0 uint32) uint32 {
return (x0 & ^((x1 & x2 & x3) ^ x5)) ^
(x1 & x4) ^ (x2 & x5) ^ (x3 & x6)
}
func mix128(a0, a1, a2, a3 uint32, n int) uint32 {
tmp := (a0 & 0x000000FF) |
(a1 & 0x0000FF00) |
(a2 & 0x00FF0000) |
(a3 & 0xFF000000)
if n > 0 {
tmp = circularLeft(tmp, n)
}
return tmp
}
func mix160_0(x5, x6, x7 uint32) uint32 {
return circularLeft(
(x5 & 0x01F80000) |
(x6 & 0xFE000000) |
(x7 & 0x0000003F),
13,
)
}
func mix160_1(x5, x6, x7 uint32) uint32 {
return circularLeft(
(x5 & 0xFE000000) |
(x6 & 0x0000003F) |
(x7 & 0x00000FC0),
7,
)
}
func mix160_2(x5, x6, x7 uint32) uint32 {
return (x5 & 0x0000003F) |
(x6 & 0x00000FC0) |
(x7 & 0x0007F000)
}
func mix160_3(x5, x6, x7 uint32) uint32 {
return ((x5 & 0x00000FC0) |
(x6 & 0x0007F000) |
(x7 & 0x01F80000)) >> 6
}
func mix160_4(x5, x6, x7 uint32) uint32 {
return ((x5 & 0x0007F000) |
(x6 & 0x01F80000) |
(x7 & 0xFE000000)) >> 12
}
func mix192_0(x6, x7 uint32) uint32 {
return circularLeft((x6 & 0xFC000000) | (x7 & 0x0000001F), 6)
}
func mix192_1(x6, x7 uint32) uint32 {
return (x6 & 0x0000001F) | (x7 & 0x000003E0)
}
func mix192_2(x6, x7 uint32) uint32 {
return ((x6 & 0x000003E0) | (x7 & 0x0000FC00)) >> 5
}
func mix192_3(x6, x7 uint32) uint32 {
return ((x6 & 0x0000FC00) | (x7 & 0x001F0000)) >> 10
}
func mix192_4(x6, x7 uint32) uint32 {
return ((x6 & 0x001F0000) | (x7 & 0x03E00000)) >> 16
}
func mix192_5(x6, x7 uint32) uint32 {
return ((x6 & 0x03E00000) | (x7 & 0xFC000000)) >> 21
}