-
Notifications
You must be signed in to change notification settings - Fork 2
/
wots.go
115 lines (103 loc) · 2.75 KB
/
wots.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
package SPHINCS_golang
func expandSeed(outseeds []byte, inseed []byte) {
prg(outseeds[0:WOTS_L* HASHSIZE], inseed[0:SEED_BYTES])
}
func genChain(out, seed []byte, masks []byte, chainlen int) {
copy(out[0:HASHSIZE], seed[0:HASHSIZE])
for i := 0; i < chainlen && i < WOTS_W; i++ {
mask := masks[i* HASHSIZE:]
Hash_n_n_mask(out[:], out[:], mask)
}
}
func pkgen(pk []byte, sk []byte, masks []byte) {
expandSeed(pk, sk)
for i := 0; i < WOTS_L; i++ {
genChain(pk[i* HASHSIZE:], pk[i* HASHSIZE:], masks, WOTS_W-1)
}
}
func Sign(sig []byte, msg *[HASHSIZE]byte, sk *[SEED_BYTES]byte, masks []byte) {
// sig = sig[:L*SIZE]
// masks = masks[:(W-1)*SIZE]
var basew [WOTS_L]int
var c, i int
switch WOTS_W {
case 16:
for i = 0; i < WOTS_L1; i += 2 {
basew[i] = int(msg[i/2] & 0xf)
basew[i+1] = int(msg[i/2] >> 4)
c += WOTS_W - 1 - basew[i]
c += WOTS_W - 1 - basew[i+1]
}
for ; i < WOTS_L; i++ {
basew[i] = c & 0xf
c >>= 4
}
expandSeed(sig, sk[:])
for i = 0; i < WOTS_L; i++ {
genChain(sig[i* HASHSIZE:], sig[i* HASHSIZE:], masks, basew[i])
}
case 4:
for i = 0; i < WOTS_L1; i += 4 {
basew[i] = int(msg[i/4] & 0x3)
basew[i+1] = int((msg[i/4] >> 2) & 0x3)
basew[i+2] = int((msg[i/4] >> 4) & 0x3)
basew[i+3] = int((msg[i/4] >> 6) & 0x3)
c += WOTS_W - 1 - basew[i]
c += WOTS_W - 1 - basew[i+1]
c += WOTS_W - 1 - basew[i+2]
c += WOTS_W - 1 - basew[i+3]
}
for ; i < WOTS_L; i++ {
basew[i] = c & 0xf
c >>= 4
}
expandSeed(sig, sk[:])
for i = 0; i < WOTS_L; i++ {
genChain(sig[i* HASHSIZE:], sig[i* HASHSIZE:], masks, basew[i])
}
default:
panic("not yet implemented")
}
}
func verify(pk *[WOTS_L * HASHSIZE]byte, sig []byte, msg *[HASHSIZE]byte, masks []byte) {
// sig = sig[:L*SIZE]
// masks = masks[:(W-1)*SIZE]
var basew [WOTS_L]int
var c, i int
switch WOTS_W {
case 16:
for i = 0; i < WOTS_L1; i += 2 {
basew[i] = int(msg[i/2] & 0xf)
basew[i+1] = int(msg[i/2] >> 4)
c += WOTS_W - 1 - basew[i]
c += WOTS_W - 1 - basew[i+1]
}
for ; i < WOTS_L; i++ {
basew[i] = c & 0xf
c >>= 4
}
for i = 0; i < WOTS_L; i++ {
genChain(pk[i* HASHSIZE:], sig[i* HASHSIZE:], masks[basew[i]* HASHSIZE:], WOTS_W-1-basew[i])
}
case 4:
for i = 0; i < WOTS_L1; i += 4 {
basew[i] = int(msg[i/4] & 0x3)
basew[i+1] = int((msg[i/4] >> 2) & 0x3)
basew[i+2] = int((msg[i/4] >> 4) & 0x3)
basew[i+3] = int((msg[i/4] >> 6) & 0x3)
c += WOTS_W - 1 - basew[i]
c += WOTS_W - 1 - basew[i+1]
c += WOTS_W - 1 - basew[i+2]
c += WOTS_W - 1 - basew[i+3]
}
for ; i < WOTS_L; i++ {
basew[i] = c & 0xf
c >>= 4
}
for i = 0; i < WOTS_L; i++ {
genChain(pk[i* HASHSIZE:], sig[i* HASHSIZE:], masks[basew[i]* HASHSIZE:], WOTS_W-1-basew[i])
}
default:
panic("not yet implemented")
}
}