-
Notifications
You must be signed in to change notification settings - Fork 34
/
Summa.ts
495 lines (436 loc) · 17.7 KB
/
Summa.ts
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { expect } from "chai";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { ethers } from "hardhat";
import { Summa, Halo2VerifyingKey, Verifier, GrandSumVerifier, InclusionVerifier } from "../typechain-types";
import { BigNumber } from "ethers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import * as fs from "fs";
import * as path from "path";
describe("Summa Contract", () => {
async function deploySummaFixture() {
// Contracts are deployed using the first signer/account by default
const [owner, addr1, addr2, addr3]: SignerWithAddress[] =
await ethers.getSigners();
const verifyingKey = await ethers.deployContract(
"src/VerifyingKey.sol:Halo2VerifyingKey"
);
await verifyingKey.deployed();
const snarkVerifier = await ethers.deployContract(
"src/SnarkVerifier.sol:Verifier"
);
await snarkVerifier.deployed();
const grandSumVerifier = await ethers.deployContract(
"src/GrandSumVerifier.sol:GrandSumVerifier"
) as GrandSumVerifier;
await grandSumVerifier.deployed();
const inclusionVerifier = await ethers.deployContract(
"src/InclusionVerifier.sol:InclusionVerifier"
) as InclusionVerifier;
await inclusionVerifier.deployed();
const summa = await ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", "BTC"],
8, // The number of bytes used to represent the balance of a cryptocurrency in the polynomials
]);
await summa.deployed();
return {
summa: summa as Summa,
owner,
addr1,
addr2,
addr3,
};
}
describe("deployment tests", () => {
let verifyingKey: Halo2VerifyingKey;
let snarkVerifier: Verifier;
let grandSumVerifier: GrandSumVerifier;
let inclusionVerifier: InclusionVerifier;
beforeEach(async () => {
// Deploy the verifying key and verifiers
verifyingKey = await ethers.deployContract(
"src/VerifyingKey.sol:Halo2VerifyingKey"
) as Halo2VerifyingKey;
await verifyingKey.deployed();
snarkVerifier = await ethers.deployContract(
"src/SnarkVerifier.sol:Verifier"
) as Verifier;
await snarkVerifier.deployed();
grandSumVerifier = await ethers.deployContract(
"src/GrandSumVerifier.sol:GrandSumVerifier"
) as GrandSumVerifier;
await grandSumVerifier.deployed();
inclusionVerifier = await ethers.deployContract(
"src/InclusionVerifier.sol:InclusionVerifier"
) as InclusionVerifier;
await inclusionVerifier.deployed();
});
it("should not deploy with invalid currencies", async () => {
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
["", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith("Invalid cryptocurrency");
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", ""],
8,
])
).to.be.revertedWith("Invalid cryptocurrency");
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
[],
["ETH", ""],
8,
])
).to.be.revertedWith("Cryptocurrency names and chains number mismatch");
});
it("should not deploy with invalid byte range", async () => {
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", "BTC"],
0, // Invalid byte range
])
).to.be.revertedWith(
"The config parameters do not correspond to the verifying key"
);
});
it("should not deploy with invalid verification key", async () => {
await expect(
ethers.deployContract("Summa", [
ethers.constants.AddressZero,
snarkVerifier.address,
grandSumVerifier.address,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith("Invalid verifying key address");
});
it("should not deploy with invalid snark verifier", async () => {
const verifyingKey = await ethers.deployContract(
"src/VerifyingKey.sol:Halo2VerifyingKey"
);
await verifyingKey.deployed();
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
ethers.constants.AddressZero,
grandSumVerifier.address,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith("Invalid polynomial interpolation verifier address");
});
it("should not deploy with invalid grand sum verifier", async () => {
const verifyingKey = await ethers.deployContract(
"src/VerifyingKey.sol:Halo2VerifyingKey"
);
await verifyingKey.deployed();
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
ethers.constants.AddressZero,
inclusionVerifier.address,
["ETH", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith("Invalid grand sum verifier address");
});
it("should not deploy with invalid inclusion verifier", async () => {
await expect(
ethers.deployContract("Summa", [
verifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
ethers.constants.AddressZero,
["ETH", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith("Invalid inclusion verifier address");
});
it("should not deploy if the number of cryptocurrencies is not matching the verification key ", async () => {
const dummyVerifyingKey = await ethers.deployContract(
"src/DummyVerifyingKey.sol:Halo2VerifyingKey"
);
await dummyVerifyingKey.deployed();
await expect(
ethers.deployContract("Summa", [
dummyVerifyingKey.address,
snarkVerifier.address,
grandSumVerifier.address,
ethers.constants.AddressZero,
["ETH", "BTC"],
["ETH", "BTC"],
8,
])
).to.be.revertedWith(
"The config parameters do not correspond to the verifying key"
);
});
});
describe("verify address ownership", () => {
let summa: Summa;
let account1: SignerWithAddress;
let account2: SignerWithAddress;
let account3: SignerWithAddress;
let ownedAddresses: Summa.AddressOwnershipProofStruct[];
const message = ethers.utils.defaultAbiCoder.encode(
["string"],
["Summa proof of solvency for CryptoExchange"]
);
beforeEach(async () => {
const deploymentInfo = await loadFixture(deploySummaFixture);
summa = deploymentInfo.summa as Summa;
account1 = deploymentInfo.addr1;
account2 = deploymentInfo.addr2;
account3 = deploymentInfo.addr3;
//Reference signing procedure for ETH:
// const message = ethers.utils.defaultAbiCoder.encode(
// ["string"],
// ["Summa proof of solvency for CryptoExchange"]
// );
// const hashedMessage = ethers.utils.solidityKeccak256(
// ["bytes"],
// [message]
// );
// const signature = await deploymentInfo.addr3.signMessage(
// ethers.utils.arrayify(hashedMessage)
// );
// console.log("signature", signature);
ownedAddresses = [
{
chain: "ETH",
cexAddress: account1.address.toString(),
signature:
"0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b",
message: message,
},
{
chain: "ETH",
cexAddress: account2.address.toString(),
signature:
"0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c",
message: message,
},
];
});
it("should verify the address ownership and store the addresses", async () => {
await expect(summa.submitProofOfAddressOwnership(ownedAddresses))
.to.emit(summa, "AddressOwnershipProofSubmitted")
.withArgs((ownedAddresses: any) => {
return (
ownedAddresses[0].chain == "ETH" &&
ownedAddresses[0].cexAddress == account1.address &&
ownedAddresses[0].signature ==
"0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b" &&
ownedAddresses[0].message == message &&
ownedAddresses[1].chain == "ETH" &&
ownedAddresses[1].cexAddress == account2.address &&
ownedAddresses[1].signature ==
"0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c" &&
ownedAddresses[1].message == message
);
});
const addr1Hash = ethers.utils.solidityKeccak256(
["string"],
[account1.address]
);
let proofOfAddressOwnership1 = await summa.getAddressOwnershipProof(
addr1Hash
);
expect(proofOfAddressOwnership1.chain).to.be.equal("ETH");
expect(proofOfAddressOwnership1.cexAddress).to.be.equal(account1.address);
expect(proofOfAddressOwnership1.signature).to.be.equal(
"0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b"
);
expect(proofOfAddressOwnership1.message).to.be.equal(message);
const addr2Hash = ethers.utils.solidityKeccak256(
["string"],
[account2.address]
);
let proofOfAddressOwnership2 = await summa.getAddressOwnershipProof(
addr2Hash
);
expect(proofOfAddressOwnership2.chain).to.be.equal("ETH");
expect(proofOfAddressOwnership2.cexAddress).to.be.equal(account2.address);
expect(proofOfAddressOwnership2.signature).to.be.equal(
"0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c"
);
expect(proofOfAddressOwnership2.message).to.be.equal(message);
});
it("should revert if the caller is not the owner", async () => {
await expect(
summa.connect(account3).submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Ownable: caller is not the owner");
});
it("should revert if the address ownership has already been verified", async () => {
await summa.submitProofOfAddressOwnership(ownedAddresses);
await expect(
summa.submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Address already verified");
});
it("should revert if the proof of address ownership has invalid address", async () => {
ownedAddresses[0].cexAddress = "";
await expect(
summa.submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Invalid proof of address ownership");
});
it("should revert if the proof of address ownership has invalid chain type", async () => {
ownedAddresses[0].chain = "";
await expect(
summa.submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Invalid proof of address ownership");
});
it("should revert if the proof of address ownership has invalid signature", async () => {
ownedAddresses[0].signature = ethers.utils.toUtf8Bytes("");
await expect(
summa.submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Invalid proof of address ownership");
});
it("should revert if the proof of address ownership has invalid message", async () => {
ownedAddresses[0].message = ethers.utils.toUtf8Bytes("");
await expect(
summa.submitProofOfAddressOwnership(ownedAddresses)
).to.be.revertedWith("Invalid proof of address ownership");
});
it("should revert if requesting proof for unverified address", async () => {
const addr1Hash = ethers.utils.solidityKeccak256(
["string"],
[account1.address]
);
await expect(
summa.getAddressOwnershipProof(addr1Hash)
).to.be.revertedWith("Address not verified");
});
});
describe("submit commitment", () => {
let rangeCheckSnarkProof: string;
let grandSumProof: string;
let totalBalances: BigNumber[];
let summa: Summa;
beforeEach(async () => {
const deploymentInfo = await loadFixture(deploySummaFixture);
summa = deploymentInfo.summa as Summa;
const commitmentCalldataJson = fs.readFileSync(
path.resolve(
__dirname,
"../../prover/bin/commitment_solidity_calldata.json"
),
"utf-8"
);
const commitmentCalldata: any = JSON.parse(commitmentCalldataJson);
rangeCheckSnarkProof = commitmentCalldata.range_check_snark_proof;
grandSumProof = commitmentCalldata.grand_sums_batch_proof;
totalBalances = commitmentCalldata.total_balances;
});
it("should submit a valid commitment", async () => {
let expect_commitment_on_contract = rangeCheckSnarkProof.slice(0, grandSumProof.length + 128);
expect(await summa.commitments(1)).to.be.equal("0x");
await summa.submitCommitment(rangeCheckSnarkProof, grandSumProof, totalBalances, 1);
expect(await summa.commitments(1)).to.be.equal(expect_commitment_on_contract);
});
it("should revert when the grand sum proof length mismatches with the total balances", async () => {
let wrong_total_balance = totalBalances.slice(0, totalBalances.length - 1);
await expect(
summa.submitCommitment(rangeCheckSnarkProof, grandSumProof, wrong_total_balance, 1)
).to.be.revertedWith("Invalid grand sum proof length");
let wrong_grand_sum_proof = grandSumProof.slice(0, grandSumProof.length - 64);
await expect(
summa.submitCommitment(rangeCheckSnarkProof, wrong_grand_sum_proof, totalBalances, 1)
).to.be.revertedWith("Invalid grand sum proof length");
});
it("should revert a snark proof if its length is less than the grand sum proof", async () => {
let wrong_range_check_snark_proof = rangeCheckSnarkProof.slice(0, grandSumProof.length - 64);
await expect(
summa.submitCommitment(wrong_range_check_snark_proof, grandSumProof, totalBalances, 1)
).to.be.revertedWith("Invalid snark proof length");
});
it("should revert due to an invalid snark proof", async () => {
let wrong_range_check_snark_proof = rangeCheckSnarkProof.replace("1", "2");
await expect(
summa.submitCommitment(wrong_range_check_snark_proof, grandSumProof, totalBalances, 1)
).to.be.reverted;
});
});
describe("verify inclusion proof", () => {
let rangeCheckSnarkProof: string;
let inclusionProof: string;
let challenges: BigNumber[];
let values: BigNumber[];
let summa: Summa;
beforeEach(async () => {
const deploymentInfo = await loadFixture(deploySummaFixture);
summa = deploymentInfo.summa as Summa;
const commitmentCalldataJson = fs.readFileSync(
path.resolve(
__dirname,
"../../prover/bin/commitment_solidity_calldata.json"
),
"utf-8"
);
const commitmentCalldata: any = JSON.parse(commitmentCalldataJson);
rangeCheckSnarkProof = commitmentCalldata.range_check_snark_proof;
const grandSumProof = commitmentCalldata.grand_sums_batch_proof;
const totalBalances = commitmentCalldata.total_balances;
const inclusionCalldataJson = fs.readFileSync(
path.resolve(
__dirname,
"../../prover/bin/inclusion_proof_solidity_calldata.json"
),
"utf-8"
);
const inclusionCalldata: any = JSON.parse(inclusionCalldataJson);
await summa.submitCommitment(rangeCheckSnarkProof, grandSumProof, totalBalances, 1);
inclusionProof = inclusionCalldata.proof;
challenges = inclusionCalldata.challenges;
values = inclusionCalldata.user_values;
});
// Testing verifyInclusionProof function
it("should verify inclusion proof with `verifyInclusionProof` function", async () => {
expect(await summa.verifyInclusionProof(1, inclusionProof, challenges, values)).to.be.true;
});
it("should not verify inclusion proof with wrong snark proof", async () => {
// No commitment is submitted at timestamp 2
await expect(summa.verifyInclusionProof(2, inclusionProof, challenges, values)).to.be.reverted;
});
it("should not verify inclusion proof with wrong challenge points", async () => {
let wrongChallenges = challenges.slice(0, challenges.length - 1);
await expect(summa.verifyInclusionProof(1, inclusionProof, wrongChallenges, values)).to.be.revertedWith("Invalid challenges length");
});
it("should not verify inclusion proof with value length mismatches with config", async () => {
let wrongValues = values.slice(0, values.length - 1);
await expect(summa.verifyInclusionProof(1, inclusionProof, challenges, wrongValues)).to.be.revertedWith("Values length mismatch with config");
});
});
});