-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathwotsp.go
142 lines (114 loc) · 3.55 KB
/
wotsp.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
package slhdsa
// See FIPS 205 -- Section 5
// Winternitz One-Time Signature Plus Scheme
const (
wotsW uint32 = 16 // wotsW is w = 2^lg_w, where lg_w = 4.
wotsLen2 uint32 = 3 // wotsLen2 is len_2 fixed to 3.
)
type (
wotsPublicKey []byte // n bytes
wotsSignature []byte // wotsLen()*n bytes
)
func (p *params) wotsSigSize() uint32 { return p.wotsLen() * p.n }
func (p *params) wotsLen() uint32 { return p.wotsLen1() + wotsLen2 }
func (p *params) wotsLen1() uint32 { return 2 * p.n }
func (ws *wotsSignature) fromBytes(p *params, c *cursor) {
*ws = c.Next(p.wotsSigSize())
}
// See FIPS 205 -- Section 5 -- Algorithm 5.
func (s *state) chain(
x []byte, index, steps uint32, addr address,
) (out []byte) {
out = x
s.F.address.Set(addr)
for j := index; j < index+steps; j++ {
s.F.address.SetHashAddress(j)
s.F.SetMessage(out)
out = s.F.Final()
}
return
}
// See FIPS 205 -- Section 5.1 -- Algorithm 6.
func (s *statePriv) wotsPkGen(addr address) wotsPublicKey {
s.PRF.address.Set(addr)
s.PRF.address.SetTypeAndClear(addressWotsPrf)
s.PRF.address.SetKeyPairAddress(addr.GetKeyPairAddress())
s.T.address.Set(addr)
s.T.address.SetTypeAndClear(addressWotsPk)
s.T.address.SetKeyPairAddress(addr.GetKeyPairAddress())
s.T.Reset()
wotsLen := s.wotsLen()
for i := range wotsLen {
s.PRF.address.SetChainAddress(i)
sk := s.PRF.Final()
addr.SetChainAddress(i)
tmpi := s.chain(sk, 0, wotsW-1, addr)
s.T.WriteMessage(tmpi)
}
return s.T.Final()
}
// See FIPS 205 -- Section 5.2 -- Algorithm 7.
func (s *statePriv) wotsSign(sig wotsSignature, msg []byte, addr address) {
if len(msg) != int(s.wotsLen1()/2) {
panic(ErrMsgLen)
}
curSig := cursor(sig)
wotsLen1 := s.wotsLen1()
csum := wotsLen1 * (wotsW - 1)
s.PRF.address.Set(addr)
s.PRF.address.SetTypeAndClear(addressWotsPrf)
s.PRF.address.SetKeyPairAddress(addr.GetKeyPairAddress())
// Signs every nibble of the message and computes the checksum.
for i := range wotsLen1 {
s.PRF.address.SetChainAddress(i)
sk := s.PRF.Final()
addr.SetChainAddress(i)
msgi := uint32((msg[i/2] >> ((1 - (i & 1)) << 2)) & 0xF)
sigi := s.chain(sk, 0, msgi, addr)
copy(curSig.Next(s.n), sigi)
csum -= msgi
}
// Lastly, every nibble of the checksum is also signed.
for i := range wotsLen2 {
s.PRF.address.SetChainAddress(wotsLen1 + i)
sk := s.PRF.Final()
addr.SetChainAddress(wotsLen1 + i)
csumi := (csum >> (8 - 4*i)) & 0xF
sigi := s.chain(sk, 0, csumi, addr)
copy(curSig.Next(s.n), sigi)
}
}
// See FIPS 205 -- Section 5.3 -- Algorithm 8.
func (s *state) wotsPkFromSig(
sig wotsSignature, msg []byte, addr address,
) wotsPublicKey {
if len(msg) != int(s.wotsLen1()/2) {
panic(ErrMsgLen)
}
wotsLen1 := s.wotsLen1()
csum := wotsLen1 * (wotsW - 1)
s.T.address.Set(addr)
s.T.address.SetTypeAndClear(addressWotsPk)
s.T.address.SetKeyPairAddress(addr.GetKeyPairAddress())
s.T.Reset()
curSig := cursor(sig)
// Signs every nibble of the message, computes the checksum, and
// feeds each signature to the T function.
for i := range wotsLen1 {
addr.SetChainAddress(i)
msgi := uint32((msg[i/2] >> ((1 - (i & 1)) << 2)) & 0xF)
sigi := s.chain(curSig.Next(s.n), msgi, wotsW-1-msgi, addr)
s.T.WriteMessage(sigi)
csum -= msgi
}
// Every nibble of the checksum is also signed feeding the signature
// to the T function.
for i := range wotsLen2 {
addr.SetChainAddress(wotsLen1 + i)
csumi := (csum >> (8 - 4*i)) & 0xF
sigi := s.chain(curSig.Next(s.n), csumi, wotsW-1-csumi, addr)
s.T.WriteMessage(sigi)
}
// Generates the public key as the output of the T function.
return s.T.Final()
}