-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimelock_test.go
232 lines (209 loc) · 12.8 KB
/
timelock_test.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
//go:build !integration
package timelock
import (
"bytes"
"crypto/rand"
"fmt"
"testing"
tz "github.com/ecadlabs/gotez/v2"
"github.com/ecadlabs/gotez/v2/encoding"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testVec struct {
time int
lockedValue string
unlockedValue string
vdfProof string
chest []byte
chestKey []byte
payload []byte
}
var vectors = []testVec{
{
time: 1000,
lockedValue: "2392904408470554760978250157158565090653637952136762921161516100509051391049416831457462039932478640291652726351080198671859996140057339372380086535932400522958974310026438424102153990384089784500653318477293530420962843692218892918988205538426381343721312110433044911192744107280690893836339960802523237242109451591185561929544955070103238361377069046942990731495583419847302422591133819009495854067369923583339851832790544852946059231130220372498428526776543884005067088796386197792313123561410301169124358027932110230067066409155285281742333049171462004625241119322045521575481655311353929124539622186101076748864",
unlockedValue: "2620972056309502203350073650286301739478009901639656349428468442971368260860477920384801161519151141389207879000892630445006989547563981436487162339355921946227902313634922964215766663403683793609398070897667988504581607415039042627366320254931453779573188223657161830442999359840625214212642535766265507463635541531567265680913752408808425465277792763747892692935124714495179890358018114052290444465906147090900820680419938650632928673837841665922987908733196112708220574557164138684507614494552362771514352185377561360506882371267250444601184913675624217636022274571032731821624141565427113520961050261027701688579",
vdfProof: "17564152955066873573078931876493836375713147141819560696099981987732703425301624756504144477263265055448296370066610053953664521505535116779715535774901492163469089418127511595654002366955659454781408285380869060985209048734331958859280218887187813830308319969150202876601909280741396658356471150494649070974594793369873095513104056582373661221026608714012658841380635106791509386405115117913742298627838781577718351301131533095762448398059913682568857261754088746553923070547372162291917940310315395401427416906841838921771042291599477497237869723233636578707888291424040994801642075836403802610478032477054755135500",
chest: []byte{
0xf4, 0x8e, 0xf8, 0xb2, 0xdb, 0x95, 0xfd, 0x9f, 0x9c, 0xb7, 0xb2, 0xdd, 0xc1, 0xd4, 0xce, 0x90,
0xe7, 0x98, 0x9b, 0xd9, 0xda, 0xfa, 0x8e, 0xb6, 0xa5, 0x80, 0xbc, 0xc1, 0x97, 0xb2, 0xf5, 0xeb,
0xdf, 0xfc, 0x9e, 0xcc, 0xf8, 0xa2, 0xf5, 0xf0, 0xbc, 0xf7, 0x81, 0xcd, 0xf1, 0xb5, 0xed, 0x83,
0xee, 0x93, 0x83, 0xcf, 0xde, 0xba, 0xf9, 0xf4, 0xb5, 0x89, 0x8b, 0xd2, 0xf5, 0x98, 0x9c, 0xe7,
0xe9, 0x81, 0x82, 0xcb, 0xa3, 0x92, 0xc7, 0xb4, 0xf4, 0x98, 0xcf, 0xa1, 0xdd, 0xc9, 0xd4, 0xe6,
0xaa, 0x9b, 0x8f, 0xea, 0xc3, 0xc2, 0xb2, 0xbf, 0xdc, 0x87, 0x9d, 0xd2, 0x9e, 0x85, 0x9e, 0xa8,
0x99, 0xfe, 0xdf, 0xf9, 0x9a, 0xbc, 0x90, 0xec, 0xbf, 0xca, 0x8c, 0x89, 0xdf, 0xbd, 0xcf, 0xc8,
0xba, 0x8d, 0xb8, 0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 0xe1, 0x80, 0x97, 0xd1, 0xf9,
0x88, 0x9b, 0xd2, 0xb9, 0xde, 0x9f, 0x8e, 0xc0, 0xfb, 0xb3, 0xdd, 0xd8, 0xd6, 0xe9, 0xe1, 0x98,
0xe8, 0xe4, 0xb9, 0x8e, 0xca, 0xe1, 0xed, 0xc2, 0xc3, 0xed, 0xcb, 0x90, 0x8a, 0xd9, 0x08, 0x80,
0xde, 0xb7, 0x88, 0xb4, 0xf6, 0xd7, 0xd3, 0xec, 0xa7, 0xa7, 0xb7, 0xd9, 0x82, 0xe9, 0xd9, 0x9c,
0x5e,
},
payload: []byte("payload"),
},
}
func TestVec(t *testing.T) {
for _, v := range vectors {
lockedValue, _ := tz.ParseBigUint(v.lockedValue, 10)
unlockedValue, _ := tz.ParseBigUint(v.unlockedValue, 10)
vdfProof, _ := tz.ParseBigUint(v.vdfProof, 10)
tuple := Timelock{
LockedValue: lockedValue,
UnlockedValue: unlockedValue,
VDFProof: vdfProof,
}
locked, proof, err := tuple.NewProof(rand.Reader, v.time, nil)
require.NoError(t, err)
assert.True(t, Verify(locked, proof, v.time, nil))
var (
chest Chest
chestKey ChestKey
)
_, err = encoding.Decode(v.chest, &chest)
require.NoError(t, err)
_, err = encoding.Decode(v.chestKey, &chestKey)
require.NoError(t, err)
assert.True(t, Verify(chest.LockedValue.Int(), &chestKey, v.time, nil))
pl, ok, err := chest.Open(&chestKey, v.time, nil)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, v.payload, pl)
}
}
func TestLowLevel(t *testing.T) {
time := 10_000
timelockPrecomputedTuple, err := PrecomputeTimelock(rand.Reader, time, nil)
require.NoError(t, err)
locked, proof, err := timelockPrecomputedTuple.NewProof(rand.Reader, time, nil)
require.NoError(t, err)
require.True(t, Verify(locked, proof, time, nil))
proof2 := UnlockAndProve(time, locked, nil)
assert.True(t, Verify(locked, proof2, time, nil))
symKey1 := proof.SymmetricKey(nil)
symKey2 := proof2.SymmetricKey(nil)
assert.Equal(t, symKey1, symKey2)
message := []byte("rzersef")
c, err := Encrypt(rand.Reader, &symKey2, message)
require.NoError(t, err)
d, ok := Decrypt(&symKey2, c)
assert.True(t, ok)
assert.Equal(t, message, d)
}
func TestChest(t *testing.T) {
time := 10_000
payload := []byte("zrethgfdsq")
chest, chestKey1, err := NewChest(rand.Reader, payload, time, nil)
require.NoError(t, err)
chestKey2, err := chest.NewKey(time, nil)
require.NoError(t, err)
op1, ok, err := chest.Open(chestKey1, time, nil)
require.NoError(t, err)
assert.True(t, ok)
op2, ok, err := chest.Open(chestKey2, time, nil)
require.NoError(t, err)
assert.True(t, ok)
assert.Equal(t, payload, op1)
assert.Equal(t, op1, op2)
}
func ExampleNewChest() {
time := 10_000
payload := []byte("message")
chest, key, err := NewChest(rand.Reader, payload, time, nil)
fmt.Println(err)
var chestBytes, keyBytes bytes.Buffer
err = encoding.Encode(&chestBytes, chest)
fmt.Println(err)
err = encoding.Encode(&keyBytes, key)
fmt.Println(err)
// Output:
// <nil>
// <nil>
// <nil>
}
func ExampleNewChestFromTimelock() {
time := 10_000
precomputed, err := PrecomputeTimelock(rand.Reader, time, nil) // can be cached
fmt.Println(err)
payload := []byte("message")
chest, key, err := NewChestFromTimelock(rand.Reader, payload, time, precomputed, nil)
fmt.Println(err)
var chestBytes, keyBytes bytes.Buffer
err = encoding.Encode(&chestBytes, chest)
fmt.Println(err)
err = encoding.Encode(&keyBytes, key)
fmt.Println(err)
// Output:
// <nil>
// <nil>
// <nil>
// <nil>
}