-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRand.ts
More file actions
317 lines (295 loc) · 11.1 KB
/
Rand.ts
File metadata and controls
317 lines (295 loc) · 11.1 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { expect } from 'chai';
import { ethers, network } from 'hardhat';
import {
createInstances,
decrypt8,
decrypt16,
decrypt32,
decrypt64,
decrypt128,
decrypt256,
decryptBool,
} from '../instance';
import { getSigners, initSigners } from '../signers';
import { deployRandFixture } from './Rand.fixture';
describe('Rand', function () {
before(async function () {
await initSigners(1);
this.signers = await getSigners();
});
beforeEach(async function () {
const contract = await deployRandFixture();
this.contractAddress = await contract.getAddress();
this.rand = contract;
this.instances = await createInstances(this.signers);
});
it('ebool generate and decrypt', async function () {
const values: boolean[] = [];
for (let i = 0; i < 15; i++) {
const txn = await this.rand.generateBool();
await txn.wait();
const valueHandle = await this.rand.valueb();
const value = await decryptBool(valueHandle);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('8 bits generate and decrypt', async function () {
const values: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate8();
await txn.wait();
const valueHandle = await this.rand.value8();
const value = await decrypt8(valueHandle);
expect(value).to.be.lessThanOrEqual(0xff);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('8 bits generate with upper bound and decrypt', async function () {
const values: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate8UpperBound(128);
await txn.wait();
const valueHandle = await this.rand.value8();
const value = await decrypt8(valueHandle);
expect(value).to.be.lessThanOrEqual(127);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('16 bits generate and decrypt', async function () {
const values: number[] = [];
let has16bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate16();
await txn.wait();
const valueHandle = await this.rand.value16();
const value = await decrypt16(valueHandle);
expect(value).to.be.lessThanOrEqual(0xffff);
if (value > 0xff) {
has16bit = true;
}
values.push(value);
}
// Make sure we actually generate 16 bit integers.
expect(has16bit).to.be.true;
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('16 bits generate with upper bound and decrypt', async function () {
const values: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate16UpperBound(8192);
await txn.wait();
const valueHandle = await this.rand.value16();
const value = await decrypt16(valueHandle);
expect(value).to.be.lessThanOrEqual(8191);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('32 bits generate and decrypt', async function () {
const values: number[] = [];
let has32bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate32();
await txn.wait();
const valueHandle = await this.rand.value32();
const value = await decrypt32(valueHandle);
expect(value).to.be.lessThanOrEqual(0xffffffff);
if (value > 0xffff) {
has32bit = true;
}
values.push(value);
}
// Make sure we actually generate 32 bit integers.
expect(has32bit).to.be.true;
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('32 bits generate with upper bound and decrypt', async function () {
const values: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate32UpperBound(262144);
await txn.wait();
const valueHandle = await this.rand.value32();
const value = await decrypt32(valueHandle);
expect(value).to.be.lessThanOrEqual(262141);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('64 bits generate and decrypt', async function () {
const values: bigint[] = [];
let has64bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate64();
await txn.wait();
const valueHandle = await this.rand.value64();
const value = await decrypt64(valueHandle);
expect(value).to.be.lessThanOrEqual(BigInt('0xffffffffffffffff'));
if (value > BigInt('0xffffffff')) {
has64bit = true;
}
// Make sure we actually generate 64 bit integers.
expect(has64bit).to.be.true;
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('64 bits generate with upper bound and decrypt', async function () {
const values: bigint[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate64UpperBound(262144);
await txn.wait();
const valueHandle = await this.rand.value64();
const value = await decrypt64(valueHandle);
expect(value).to.be.lessThanOrEqual(262141);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
});
it('128 bits generate and decrypt', async function () {
const values: bigint[] = [];
let has128bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate128();
await txn.wait();
const valueHandle = await this.rand.value128();
const value = await decrypt128(valueHandle);
expect(value).to.be.lessThanOrEqual(BigInt('0xffffffffffffffffffffffffffffffff'));
if (value > BigInt('0xffffffffffffffff')) {
has128bit = true;
}
values.push(value);
// Make sure we actually generate 128 bit integers.
expect(has128bit).to.be.true;
}
// Expect at least 4 different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(4);
});
it('128 bits generate with upper bound and decrypt', async function () {
const values: bigint[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate128UpperBound(2n ** 100n);
await txn.wait();
const valueHandle = await this.rand.value128();
const value = await decrypt128(valueHandle);
expect(value).to.be.lessThanOrEqual(2n ** 100n);
values.push(value);
}
// Expect at least 4 different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(4);
});
it('256 bits generate and decrypt', async function () {
const values: bigint[] = [];
let has256bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate256();
await txn.wait();
const valueHandle = await this.rand.value256();
const value = await decrypt256(valueHandle);
expect(value).to.be.lessThanOrEqual(BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'));
if (value > BigInt('0xffffffffffffffffffffffffffffffff')) {
has256bit = true;
}
values.push(value);
// Make sure we actually generate 256 bit integers.
expect(has256bit).to.be.true;
}
// Expect at least 5 different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(5);
});
it('256 bits generate with upper bound and decrypt', async function () {
const values: bigint[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate256UpperBound(2n ** 200n);
await txn.wait();
const valueHandle = await this.rand.value256();
const value = await decrypt256(valueHandle);
expect(value).to.be.lessThanOrEqual(2n ** 200n);
values.push(value);
}
// Expect at least 5 different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(5);
});
it('8 and 16 bits generate and decrypt with hardhat snapshots [skip-on-coverage]', async function () {
if (network.name === 'hardhat') {
// snapshots are only possible in hardhat node, i.e in mocked mode
this.snapshotId = await ethers.provider.send('evm_snapshot');
const values: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate8();
await txn.wait();
const valueHandle = await this.rand.value8();
const value = await decrypt8(valueHandle);
expect(value).to.be.lessThanOrEqual(0xff);
values.push(value);
}
// Expect at least two different generated values.
const unique = new Set(values);
expect(unique.size).to.be.greaterThanOrEqual(2);
await ethers.provider.send('evm_revert', [this.snapshotId]);
this.snapshotId = await ethers.provider.send('evm_snapshot');
const values2: number[] = [];
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate8();
await txn.wait();
const valueHandle = await this.rand.value8();
const value = await decrypt8(valueHandle);
expect(value).to.be.lessThanOrEqual(0xff);
values2.push(value);
}
// Expect at least two different generated values.
const unique2 = new Set(values2);
expect(unique2.size).to.be.greaterThanOrEqual(2);
await ethers.provider.send('evm_revert', [this.snapshotId]);
const values3: number[] = [];
let has16bit: boolean = false;
for (let i = 0; i < 5; i++) {
const txn = await this.rand.generate16();
await txn.wait();
const valueHandle = await this.rand.value16();
const value = await decrypt16(valueHandle);
expect(value).to.be.lessThanOrEqual(0xffff);
if (value > 0xff) {
has16bit = true;
}
values3.push(value);
}
// Make sure we actually generate 16 bit integers.
expect(has16bit).to.be.true;
// Expect at least two different generated values.
const unique3 = new Set(values3);
expect(unique3.size).to.be.greaterThanOrEqual(2);
}
});
it('generating rand in reverting sub-call', async function () {
const txn = await this.rand.generate64Reverting();
await txn.wait();
const valueHandle = await this.rand.value64Bounded();
const value = await decrypt16(valueHandle);
expect(value).to.be.lessThan(1024);
});
});