-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimelock.go
249 lines (221 loc) · 6.35 KB
/
timelock.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package timelock
import (
"bytes"
"errors"
"io"
"math/big"
"slices"
"strconv"
tz "github.com/ecadlabs/gotez/v2"
"golang.org/x/crypto/blake2b"
)
var rsaModulus *big.Int
func init() {
rsaModulus, _ = new(big.Int).SetString("25195908475657893494027183240048398571429282126204032027777137836043662020707595556264018525880784406918290641249515082189298559149176184502808489120072844992687392807287776735971418347270261896375014971824691165077613379859095700097330459748808428401797429100642458691817195118746121515172654632282216869987549182422433637259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133844143603833904414952634432190114657544454178424020924616515723350778707749817125772467962926386356373289912154831438167899885040445364023527381951378636564391212010397122822120720357", 10)
}
var (
two = big.NewInt(2)
one = big.NewInt(1)
)
type Timelock struct {
LockedValue tz.BigUint `json:"locked_value"`
UnlockedValue tz.BigUint `json:"unlocked_value"`
VDFProof tz.BigUint `json:"vdf_proof"`
}
type TimelockProof struct {
VDFTuple Timelock `json:"vdf_tuple"`
Nonce tz.BigUint `json:"nonce"`
}
func nextPrime(n *big.Int) *big.Int {
p := new(big.Int)
if n.Cmp(two) < 0 {
return p.Set(two)
}
p.Add(n, one)
limit := new(big.Int).Mul(n, two)
for p.Cmp(limit) < 0 {
// use 25 bases like in GMP mpz_nextprime and thus in Tezos
if p.ProbablyPrime(25) {
break
}
p.Add(p, one)
}
return p
}
func randomInt(r io.Reader, bitLen int) (*big.Int, error) {
sz := bitLen / 8
bytes := make([]byte, sz)
if _, err := io.ReadFull(r, bytes); err != nil {
return nil, err
}
return new(big.Int).SetBytes(bytes), nil
}
func generate(r io.Reader, mod *big.Int) (*big.Int, error) {
x, err := randomInt(r, mod.BitLen())
if err != nil {
return nil, err
}
m := new(big.Int).Sub(mod, two)
x.Mod(x, m)
x.Add(x, two)
return x, nil
}
func intBytes(x *big.Int) []byte {
b := x.Bytes()
slices.Reverse(b)
return b
}
var hashSeparator = []byte{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
func hashToPrime(time int, value, key, mod *big.Int) *big.Int {
var b bytes.Buffer
b.Write([]byte(strconv.FormatInt(int64(time), 10)))
b.Write(hashSeparator)
b.Write(intBytes(mod))
b.Write(hashSeparator)
b.Write(intBytes(value))
b.Write(hashSeparator)
b.Write(intBytes(key))
h, _ := blake2b.New(32, []byte{32})
h.Write(b.Bytes())
sum := h.Sum(nil)
slices.Reverse(sum)
val := new(big.Int).SetBytes(sum)
return nextPrime(val)
}
func proveWesolowski(time int, locked, unlocked, mod *big.Int) *big.Int {
l := hashToPrime(time, locked, unlocked, mod)
pi, r := big.NewInt(1), big.NewInt(1)
for ; time > 0; time -= 1 {
var rr big.Int
rr.Lsh(r, 1)
r.Mod(&rr, l)
var pi2 big.Int
pi2.Mul(pi, pi)
pi2.Mod(&pi2, mod)
if rr.Cmp(l) >= 0 {
pi.Mul(&pi2, locked)
} else {
pi.Set(&pi2)
}
}
return pi.Mod(pi, mod)
}
func mustNewBigUint(x *big.Int) tz.BigUint {
out, err := tz.NewBigUint(x)
if err != nil {
panic(err.Error())
}
return out
}
func newBigUintUnsafe(x *big.Int) tz.BigUint {
out, _ := tz.NewBigUint(x)
return out
}
func Prove(time int, locked, unlocked, mod *big.Int) *TimelockProof {
if mod == nil {
mod = rsaModulus
}
return &TimelockProof{
VDFTuple: Timelock{
LockedValue: mustNewBigUint(locked),
UnlockedValue: mustNewBigUint(unlocked),
VDFProof: newBigUintUnsafe(proveWesolowski(time, locked, unlocked, mod)),
},
Nonce: tz.NewBigUint64(1),
}
}
func unlockTimelock(time int, locked, mod *big.Int) *big.Int {
x := new(big.Int).Set(locked)
if locked.Cmp(one) <= 0 {
return x
}
for ; time > 0; time -= 1 {
x.Mul(x, x)
x.Mod(x, mod)
}
return x
}
func UnlockAndProve(time int, locked, mod *big.Int) *TimelockProof {
if mod == nil {
mod = rsaModulus
}
unlocked := unlockTimelock(time, locked, mod)
return Prove(time, locked, unlocked, mod)
}
func PrecomputeTimelock(random io.Reader, time int, mod *big.Int) (*Timelock, error) {
if mod == nil {
mod = rsaModulus
}
locked, err := generate(random, mod)
if err != nil {
return nil, err
}
unlocked := unlockTimelock(time, locked, mod)
return &Timelock{
LockedValue: newBigUintUnsafe(locked),
UnlockedValue: newBigUintUnsafe(unlocked),
VDFProof: newBigUintUnsafe(proveWesolowski(time, locked, unlocked, mod)),
}, nil
}
func (vdfTuple *Timelock) verifyWesolowski(time int, mod *big.Int) bool {
lockedValue := vdfTuple.LockedValue.Int()
unlockedValue := vdfTuple.UnlockedValue.Int()
l := hashToPrime(time, lockedValue, unlockedValue, mod)
r := new(big.Int).Exp(big.NewInt(2), big.NewInt(int64(time)), l)
ll := new(big.Int).Exp(vdfTuple.VDFProof.Int(), l, mod)
rr := new(big.Int).Exp(lockedValue, r, mod)
unlocked := new(big.Int).Mul(ll, rr)
unlocked.Mod(unlocked, mod)
return unlocked.Cmp(unlockedValue) == 0
}
func Verify(locked *big.Int, proof *TimelockProof, time int, mod *big.Int) bool {
if mod == nil {
mod = rsaModulus
}
randomizedChallenge := new(big.Int).Exp(proof.VDFTuple.LockedValue.Int(), proof.Nonce.Int(), mod)
return randomizedChallenge.Cmp(locked) == 0 && proof.VDFTuple.verifyWesolowski(time, mod)
}
var (
ErrInvalidArgument = errors.New("invalid argument")
ErrVerification = errors.New("timelock tuple verification failed")
)
func (v *Timelock) NewProof(random io.Reader, time int, mod *big.Int) (locked *big.Int, proof *TimelockProof, err error) {
if mod == nil {
mod = rsaModulus
}
lockedValue := v.LockedValue.Int()
unlockedValue := v.UnlockedValue.Int()
vdfProof := v.VDFProof.Int()
if lockedValue.Cmp(one) < 1 ||
unlockedValue.Sign() < 1 ||
vdfProof.Sign() < 1 ||
lockedValue.Cmp(mod) > 0 ||
unlockedValue.Cmp(mod) > 0 ||
vdfProof.Cmp(mod) >= 0 {
return nil, nil, ErrInvalidArgument
}
if !v.verifyWesolowski(time, mod) {
return nil, nil, ErrVerification
}
nonce, err := randomInt(random, 16*8)
if err != nil {
return nil, nil, err
}
randomizedLockedValue := new(big.Int).Exp(lockedValue, nonce, mod)
return randomizedLockedValue, &TimelockProof{
VDFTuple: *v,
Nonce: newBigUintUnsafe(nonce),
}, nil
}
var kdfKey = []byte("Tezoskdftimelockv1")
func (p *TimelockProof) SymmetricKey(mod *big.Int) [32]byte {
if mod == nil {
mod = rsaModulus
}
updated := new(big.Int).Exp(p.VDFTuple.UnlockedValue.Int(), p.Nonce.Int(), mod)
h, _ := blake2b.New(32, kdfKey)
h.Write([]byte(updated.String()))
var out [32]byte
copy(out[:], h.Sum(nil))
return out
}