-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathfors.go
173 lines (141 loc) · 4.08 KB
/
fors.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
package slhdsa
// See FIPS 205 -- Section 8
// Forest of Random Subsets (FORS) is a few-time signature scheme that is
// used to sign the digests of the actual messages.
type (
forsPublicKey []byte // n bytes
forsPrivateKey []byte // n bytes
forsSignature []forsPair // k*forsPairSize() bytes
forsPair struct {
sk forsPrivateKey // forsSkSize() bytes
auth [][]byte // a*n bytes
} // forsSkSize() + a*n bytes
)
func (p *params) forsMsgSize() uint32 { return (p.k*p.a + 7) / 8 }
func (p *params) forsPkSize() uint32 { return p.n }
func (p *params) forsSkSize() uint32 { return p.n }
func (p *params) forsSigSize() uint32 { return p.k * p.forsPairSize() }
func (p *params) forsPairSize() uint32 { return p.forsSkSize() + p.a*p.n }
func (fs *forsSignature) fromBytes(p *params, c *cursor) {
*fs = make([]forsPair, p.k)
for i := range *fs {
(*fs)[i].fromBytes(p, c)
}
}
func (fp *forsPair) fromBytes(p *params, c *cursor) {
fp.sk = c.Next(p.forsSkSize())
fp.auth = make([][]byte, p.a)
for i := range fp.auth {
fp.auth[i] = c.Next(p.n)
}
}
// See FIPS 205 -- Section 8.1 -- Algorithm 14.
func (s *statePriv) forsSkGen(addr address, idx uint32) forsPrivateKey {
s.PRF.address.Set(addr)
s.PRF.address.SetTypeAndClear(addressForsPrf)
s.PRF.address.SetKeyPairAddress(addr.GetKeyPairAddress())
s.PRF.address.SetTreeIndex(idx)
return s.PRF.Final()
}
// See FIPS 205 -- Section 8.2 -- Algorithm 15 -- Iterative version.
//
// This is a stack-based implementation that computes the tree leaves
// in order (from the left to the right).
// Its recursive version can be found at fors_test.go file.
func (s *statePriv) forsNodeIter(
stack stackNode, root []byte, i, z uint32, addr address,
) {
if !(z <= s.a && i < s.k<<(s.a-z)) {
panic(ErrTree)
}
s.F.address.Set(addr)
s.F.address.SetTreeHeight(0)
s.H.address.Set(addr)
twoZ := uint32(1) << z
iTwoZ := i << z
for k := range twoZ {
li := iTwoZ + k
lz := uint32(0)
sk := s.forsSkGen(addr, li)
s.F.address.SetTreeIndex(li)
s.F.SetMessage(sk)
node := s.F.Final()
for !stack.isEmpty() && stack.top().z == lz {
left := stack.pop()
li = (li - 1) >> 1
lz = lz + 1
s.H.address.SetTreeHeight(lz)
s.H.address.SetTreeIndex(li)
s.H.SetMsgs(left.node, node)
node = s.H.Final()
}
stack.push(item{node, lz})
}
copy(root, stack.pop().node)
}
// See FIPS 205 -- Section 8.3 -- Algorithm 16.
func (s *statePriv) forsSign(sig forsSignature, digest []byte, addr address) {
stack := s.NewStack(s.a - 1)
defer stack.Clear()
in, bits, total := 0, uint32(0), uint32(0)
maskA := (uint32(1) << s.a) - 1
for i := range s.k {
for bits < s.a {
total = (total << 8) + uint32(digest[in])
in++
bits += 8
}
bits -= s.a
indicesI := (total >> bits) & maskA
treeIdx := (i << s.a) + indicesI
forsSk := s.forsSkGen(addr, treeIdx)
copy(sig[i].sk, forsSk)
for j := range s.a {
shift := (indicesI >> j) ^ 1
s.forsNodeIter(stack, sig[i].auth[j], (i<<(s.a-j))+shift, j, addr)
}
}
}
// See FIPS 205 -- Section 8.4 -- Algorithm 17.
func (s *state) forsPkFromSig(
sig forsSignature, digest []byte, addr address,
) (pk forsPublicKey) {
pk = make([]byte, s.forsPkSize())
s.F.address.Set(addr)
s.F.address.SetTreeHeight(0)
s.H.address.Set(addr)
s.T.address.Set(addr)
s.T.address.SetTypeAndClear(addressForsRoots)
s.T.address.SetKeyPairAddress(addr.GetKeyPairAddress())
s.T.Reset()
in, bits, total := 0, uint32(0), uint32(0)
maskA := (uint32(1) << s.a) - 1
for i := range s.k {
for bits < s.a {
total = (total << 8) + uint32(digest[in])
in++
bits += 8
}
bits -= s.a
indicesI := (total >> bits) & maskA
treeIdx := (i << s.a) + indicesI
s.F.address.SetTreeIndex(treeIdx)
s.F.SetMessage(sig[i].sk)
node := s.F.Final()
for j := range s.a {
if (indicesI>>j)&0x1 == 0 {
treeIdx = treeIdx >> 1
s.H.SetMsgs(node, sig[i].auth[j])
} else {
treeIdx = (treeIdx - 1) >> 1
s.H.SetMsgs(sig[i].auth[j], node)
}
s.H.address.SetTreeHeight(j + 1)
s.H.address.SetTreeIndex(treeIdx)
node = s.H.Final()
}
s.T.WriteMessage(node)
}
copy(pk, s.T.Final())
return pk
}