-
Notifications
You must be signed in to change notification settings - Fork 2
/
uint512.go
42 lines (33 loc) · 922 Bytes
/
uint512.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
package hash
import (
"math/big"
)
const (
uint512PrimeHexString = "100000000000000000000000000000000000000000000000000000000000000000000000000000000000157"
uint512OffsetHexString = "b86db0b1171f4416dca1e50f309990acac87d059c90000000000000000000d21e948f68a34c192f62ea79bc942dbe7ce182036415f56e34bac982aac4afe9fd9"
)
var (
uint512Mod = new(big.Int)
uint512Prime = new(big.Int)
uint512Offset = new(big.Int)
)
func init() {
uint512Mod.Lsh(bigOne, 512)
uint512Prime.SetString(uint512PrimeHexString, 16)
uint512Offset.SetString(uint512OffsetHexString, 16)
}
func Uint512(data []byte) (hash *big.Int) {
hash = new(big.Int)
hash.Set(uint512Offset)
datum := new(big.Int)
for _, d := range data {
hash.Xor(hash, datum.SetUint64(uint64(d)))
hash.Mul(hash, uint512Prime)
hash.Mod(hash, uint512Mod)
}
return
}
func Uint512String(data string) (hash *big.Int) {
hash = Uint512([]byte(data))
return
}