forked from bitbandi/go-x11
-
Notifications
You must be signed in to change notification settings - Fork 1
/
x17.go
144 lines (112 loc) · 3.03 KB
/
x17.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
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package x17
import (
"crypto/sha512"
"encoding/binary"
"log"
"github.com/marpme/go-x17/blake"
"github.com/marpme/go-x17/bmw"
"github.com/marpme/go-x17/cubed"
"github.com/marpme/go-x17/echo"
"github.com/marpme/go-x17/fugue"
"github.com/marpme/go-x17/groest"
"github.com/marpme/go-x17/hamsi"
"github.com/marpme/go-x17/hash"
"github.com/marpme/go-x17/haval"
"github.com/marpme/go-x17/jhash"
"github.com/marpme/go-x17/keccak"
"github.com/marpme/go-x17/luffa"
"github.com/marpme/go-x17/shabal"
"github.com/marpme/go-x17/shavite"
"github.com/marpme/go-x17/simd"
"github.com/marpme/go-x17/skein"
"github.com/marpme/go-x17/whirlpool_x17"
)
////////////////
// Hash contains the state objects
// required to perform the x17.Hash.
type Hash struct {
tha [64]byte
thb [64]byte
le [4]uint64
blake hash.Digest
bmw hash.Digest
cubed hash.Digest
echo hash.Digest
groest hash.Digest
jhash hash.Digest
keccak hash.Digest
luffa hash.Digest
shavite hash.Digest
simd hash.Digest
skein hash.Digest
}
// New returns a new object to compute a x17 hash.
func New() *Hash {
ref := &Hash{}
ref.blake = blake.New()
ref.bmw = bmw.New()
ref.cubed = cubed.New()
ref.echo = echo.New()
ref.groest = groest.New()
ref.jhash = jhash.New()
ref.keccak = keccak.New()
ref.luffa = luffa.New()
ref.shavite = shavite.New()
ref.simd = simd.New()
ref.skein = skein.New()
return ref
}
// Hash computes the hash from the src bytes and stores the result in dst.
func (ref *Hash) Hash(src []byte, dst []byte) {
ta := ref.tha[:]
tb := ref.thb[:]
ref.blake.Write(src)
ref.blake.Close(tb, 0, 0)
ref.bmw.Write(tb)
ref.bmw.Close(ta, 0, 0)
ref.groest.Write(ta)
ref.groest.Close(tb, 0, 0)
ref.skein.Write(tb)
ref.skein.Close(ta, 0, 0)
ref.jhash.Write(ta)
ref.jhash.Close(tb, 0, 0)
ref.keccak.Write(tb)
ref.keccak.Close(ta, 0, 0)
ref.luffa.Write(ta)
ref.luffa.Close(tb, 0, 0)
ref.cubed.Write(tb)
ref.cubed.Close(ta, 0, 0)
ref.shavite.Write(ta)
ref.shavite.Close(tb, 0, 0)
ref.simd.Write(tb)
ref.simd.Close(ta, 0, 0)
ref.echo.Write(ta)
ref.echo.Close(tb, 0, 0)
hamsi.SumBig(tb, ta[:])
fugue.SumBig(ta, tb[:])
shabal.SumBig(tb, ta[:])
whirlpool := whirlpool_x17.New()
whirlpool.Write(ta)
tb = whirlpool.Sum(nil)
sha512Hash := sha512.Sum512(tb)
ta = sha512Hash[:]
haval256Hash := haval.New()
haval256Hash.Update(ta, 0, len(ta))
tb = haval256Hash.Digest()
ref.convert32BytesToBE(tb)
copy(dst, tb)
}
func (ref *Hash) convert32BytesToBE(hashedBytes []byte) {
if len(hashedBytes) < 32 {
log.Fatal("Expected at least 32 bytes to be converted into big endian.")
}
ref.le[0] = binary.LittleEndian.Uint64(hashedBytes[0:8])
ref.le[1] = binary.LittleEndian.Uint64(hashedBytes[8:16])
ref.le[2] = binary.LittleEndian.Uint64(hashedBytes[16:24])
ref.le[3] = binary.LittleEndian.Uint64(hashedBytes[24:32])
for i := 0; i < len(ref.le); i++ {
binary.BigEndian.PutUint64(hashedBytes[i*8:(i+1)*8], ref.le[len(ref.le)-1-i])
}
}