@lfdecentralizedtrust/zeto-contracts 0.2.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +70 -162
  2. package/contracts/factory.sol +30 -34
  3. package/contracts/factory_upgradeable.sol +11 -7
  4. package/contracts/lib/common/util.sol +40 -0
  5. package/contracts/lib/interfaces/ILockableCapability.sol +318 -0
  6. package/contracts/lib/interfaces/{izeto.sol → IZeto.sol} +13 -1
  7. package/contracts/lib/interfaces/{izeto_initializable.sol → IZetoInitializable.sol} +14 -12
  8. package/contracts/lib/interfaces/{izeto_kyc.sol → IZetoKyc.sol} +3 -3
  9. package/contracts/lib/interfaces/IZetoLockableCapability.sol +237 -0
  10. package/contracts/lib/interfaces/IZetoStorage.sol +106 -0
  11. package/contracts/lib/interfaces/{izeto_lockable.sol → IZetoVerifier.sol} +8 -21
  12. package/contracts/lib/registry.sol +74 -26
  13. package/contracts/lib/storage/base.sol +210 -0
  14. package/contracts/lib/storage/nullifier.sol +166 -0
  15. package/contracts/lib/zeto_common.sol +277 -28
  16. package/contracts/lib/zeto_fungible.sol +451 -33
  17. package/contracts/lib/zeto_fungible_base.sol +69 -0
  18. package/contracts/lib/zeto_fungible_burn.sol +83 -53
  19. package/contracts/lib/zeto_fungible_burn_nullifier.sol +115 -72
  20. package/contracts/lib/zeto_fungible_nullifier.sol +167 -0
  21. package/contracts/lib/zeto_lockable.sol +613 -0
  22. package/contracts/lib/zeto_non_fungible.sol +236 -0
  23. package/contracts/lib/zeto_non_fungible_base.sol +61 -0
  24. package/contracts/lib/zeto_non_fungible_nullifier.sol +61 -0
  25. package/contracts/test/escrow1.sol +90 -34
  26. package/contracts/test/escrow2.sol +64 -29
  27. package/contracts/test/qurrency.sol +10 -2
  28. package/contracts/test/smt.sol +6 -1
  29. package/contracts/test/tendecimals.sol +14 -12
  30. package/contracts/verifiers/impl/anon.sol +189 -0
  31. package/contracts/verifiers/impl/anon_batch.sol +301 -0
  32. package/contracts/verifiers/impl/anon_enc.sol +266 -0
  33. package/contracts/verifiers/impl/anon_enc_batch.sol +602 -0
  34. package/contracts/verifiers/impl/anon_enc_nullifier.sol +287 -0
  35. package/contracts/verifiers/impl/anon_enc_nullifier_batch.sol +679 -0
  36. package/contracts/verifiers/impl/anon_enc_nullifier_kyc.sol +294 -0
  37. package/contracts/verifiers/impl/anon_enc_nullifier_kyc_batch.sol +686 -0
  38. package/contracts/verifiers/impl/anon_enc_nullifier_non_repudiation.sol +413 -0
  39. package/contracts/verifiers/impl/anon_enc_nullifier_non_repudiation_batch.sol +1141 -0
  40. package/contracts/verifiers/impl/anon_nullifier_kyc_transfer.sol +217 -0
  41. package/contracts/verifiers/impl/anon_nullifier_kyc_transferLocked.sol +196 -0
  42. package/contracts/verifiers/impl/anon_nullifier_kyc_transferLocked_batch.sol +308 -0
  43. package/contracts/verifiers/impl/anon_nullifier_kyc_transfer_batch.sol +385 -0
  44. package/contracts/verifiers/impl/anon_nullifier_qurrency_transfer.sol +497 -0
  45. package/contracts/verifiers/impl/anon_nullifier_qurrency_transfer_batch.sol +1001 -0
  46. package/contracts/verifiers/{verifier_anon_nullifier_transferLocked.sol → impl/anon_nullifier_transfer.sol} +12 -19
  47. package/contracts/verifiers/{verifier_anon_nullifier_transferLocked_batch.sol → impl/anon_nullifier_transfer_batch.sol} +44 -51
  48. package/contracts/verifiers/impl/burn.sol +182 -0
  49. package/contracts/verifiers/impl/burn_batch.sol +238 -0
  50. package/contracts/verifiers/impl/burn_nullifier.sol +203 -0
  51. package/contracts/verifiers/impl/burn_nullifier_batch.sol +315 -0
  52. package/contracts/verifiers/impl/deposit.sol +182 -0
  53. package/contracts/verifiers/impl/deposit_kyc.sol +189 -0
  54. package/contracts/verifiers/impl/nf_anon.sol +175 -0
  55. package/contracts/verifiers/{verifier_nf_anon_nullifier_transferLocked.sol → impl/nf_anon_nullifier_transfer.sol} +6 -13
  56. package/contracts/verifiers/impl/withdraw.sol +189 -0
  57. package/contracts/verifiers/impl/withdraw_batch.sol +245 -0
  58. package/contracts/verifiers/impl/withdraw_nullifier.sol +210 -0
  59. package/contracts/verifiers/impl/withdraw_nullifier_batch.sol +322 -0
  60. package/contracts/verifiers/verifier_anon.sol +31 -186
  61. package/contracts/verifiers/verifier_anon_batch.sol +31 -298
  62. package/contracts/verifiers/verifier_anon_enc.sol +31 -263
  63. package/contracts/verifiers/verifier_anon_enc_batch.sol +31 -599
  64. package/contracts/verifiers/verifier_anon_enc_nullifier.sol +31 -284
  65. package/contracts/verifiers/verifier_anon_enc_nullifier_batch.sol +33 -676
  66. package/contracts/verifiers/verifier_anon_enc_nullifier_kyc.sol +31 -291
  67. package/contracts/verifiers/verifier_anon_enc_nullifier_kyc_batch.sol +33 -683
  68. package/contracts/verifiers/verifier_anon_enc_nullifier_non_repudiation.sol +33 -410
  69. package/contracts/verifiers/verifier_anon_enc_nullifier_non_repudiation_batch.sol +33 -1138
  70. package/contracts/verifiers/verifier_anon_nullifier_kyc_transfer.sol +33 -214
  71. package/contracts/verifiers/verifier_anon_nullifier_kyc_transferLocked.sol +33 -221
  72. package/contracts/verifiers/verifier_anon_nullifier_kyc_transferLocked_batch.sol +33 -389
  73. package/contracts/verifiers/verifier_anon_nullifier_kyc_transfer_batch.sol +33 -382
  74. package/contracts/verifiers/verifier_anon_nullifier_qurrency_transfer.sol +33 -221
  75. package/contracts/verifiers/verifier_anon_nullifier_qurrency_transfer_batch.sol +33 -389
  76. package/contracts/verifiers/verifier_anon_nullifier_transfer.sol +33 -207
  77. package/contracts/verifiers/verifier_anon_nullifier_transfer_batch.sol +33 -375
  78. package/contracts/verifiers/verifier_burn.sol +31 -179
  79. package/contracts/verifiers/verifier_burn_batch.sol +31 -235
  80. package/contracts/verifiers/verifier_burn_nullifier.sol +31 -200
  81. package/contracts/verifiers/verifier_burn_nullifier_batch.sol +31 -312
  82. package/contracts/verifiers/verifier_deposit.sol +31 -179
  83. package/contracts/verifiers/verifier_deposit_kyc.sol +34 -0
  84. package/contracts/verifiers/verifier_nf_anon.sol +31 -172
  85. package/contracts/verifiers/verifier_nf_anon_nullifier_transfer.sol +33 -179
  86. package/contracts/verifiers/verifier_withdraw.sol +31 -186
  87. package/contracts/verifiers/verifier_withdraw_batch.sol +31 -242
  88. package/contracts/verifiers/verifier_withdraw_nullifier.sol +31 -207
  89. package/contracts/verifiers/verifier_withdraw_nullifier_batch.sol +33 -319
  90. package/contracts/zeto_anon.sol +77 -231
  91. package/contracts/zeto_anon_burnable.sol +56 -12
  92. package/contracts/zeto_anon_enc.sol +93 -190
  93. package/contracts/zeto_anon_enc_nullifier.sol +249 -195
  94. package/contracts/zeto_anon_enc_nullifier_kyc.sol +51 -232
  95. package/contracts/zeto_anon_enc_nullifier_non_repudiation.sol +231 -238
  96. package/contracts/zeto_anon_nullifier.sol +164 -298
  97. package/contracts/zeto_anon_nullifier_burnable.sol +68 -18
  98. package/contracts/zeto_anon_nullifier_kyc.sol +40 -345
  99. package/contracts/zeto_anon_nullifier_qurrency.sol +217 -361
  100. package/contracts/zeto_nf_anon.sol +55 -130
  101. package/contracts/zeto_nf_anon_nullifier.sol +90 -152
  102. package/hardhat.config.ts +9 -3
  103. package/ignition/modules/lib/deps.ts +8 -0
  104. package/ignition/modules/zeto_anon_burnable.ts +8 -7
  105. package/ignition/modules/zeto_anon_enc_nullifier.ts +31 -0
  106. package/ignition/modules/zeto_anon_enc_nullifier_kyc.ts +2 -2
  107. package/ignition/modules/zeto_anon_nullifier.ts +28 -4
  108. package/ignition/modules/zeto_anon_nullifier_burnable.ts +50 -16
  109. package/ignition/modules/zeto_anon_nullifier_kyc.ts +27 -12
  110. package/ignition/modules/zeto_nf_anon_nullifier.ts +14 -10
  111. package/package.json +4 -2
  112. package/scripts/deploy_cloneable.ts +19 -10
  113. package/scripts/deploy_upgradeable.ts +9 -5
  114. package/scripts/tokens/Zeto_Anon.ts +3 -3
  115. package/scripts/tokens/Zeto_AnonBurnable.ts +1 -1
  116. package/scripts/tokens/Zeto_AnonEnc.ts +12 -3
  117. package/scripts/tokens/Zeto_AnonEncNullifier.ts +5 -3
  118. package/scripts/tokens/Zeto_AnonEncNullifierKyc.ts +1 -1
  119. package/scripts/tokens/Zeto_AnonEncNullifierNonRepudiation.ts +1 -1
  120. package/scripts/tokens/Zeto_AnonNullifier.ts +1 -1
  121. package/scripts/tokens/Zeto_AnonNullifierBurnable.ts +1 -1
  122. package/scripts/tokens/Zeto_AnonNullifierKyc.ts +1 -1
  123. package/scripts/tokens/Zeto_AnonNullifierQurrency.ts +1 -1
  124. package/scripts/tokens/Zeto_NfAnon.ts +2 -2
  125. package/scripts/tokens/Zeto_NfAnonNullifier.ts +1 -1
  126. package/test/factory.ts +46 -73
  127. package/test/lib/anon_nullifier_helpers.ts +89 -0
  128. package/test/lib/anon_zeto_helpers.ts +76 -0
  129. package/test/lib/deploy.ts +3 -2
  130. package/test/lib/utils.ts +74 -35
  131. package/test/test/escrow1.ts +185 -58
  132. package/test/test/escrow2.ts +200 -107
  133. package/test/test/qurrency.ts +13 -33
  134. package/test/usdc-shielding.ts +39 -26
  135. package/test/utils.ts +144 -21
  136. package/test/zeto_anon.ts +956 -465
  137. package/test/zeto_anon_enc.ts +881 -142
  138. package/test/zeto_anon_enc_nullifier.ts +847 -38
  139. package/test/zeto_anon_enc_nullifier_kyc.ts +120 -181
  140. package/test/zeto_anon_enc_nullifier_non_repudiation.ts +77 -46
  141. package/test/zeto_anon_nullifier.ts +1212 -953
  142. package/test/zeto_anon_nullifier_kyc.ts +674 -655
  143. package/test/zeto_anon_nullifier_qurrency.ts +452 -306
  144. package/test/zeto_nf_anon.ts +737 -137
  145. package/test/zeto_nf_anon_nullifier.ts +876 -246
  146. package/contracts/lib/zeto_base.sol +0 -293
  147. package/contracts/lib/zeto_fungible_withdraw.sol +0 -132
  148. package/contracts/lib/zeto_fungible_withdraw_nullifier.sol +0 -144
  149. package/contracts/lib/zeto_nullifier.sol +0 -340
  150. package/contracts/zkDvP.sol_ +0 -273
  151. package/test/zkDvP.ts_ +0 -455
  152. /package/contracts/lib/{common.sol → common/common.sol} +0 -0
package/test/utils.ts CHANGED
@@ -16,12 +16,10 @@
16
16
 
17
17
  import { readFileSync } from "fs";
18
18
  import * as path from "path";
19
- import crypto from "crypto";
20
- import { BigNumberish } from "ethers";
19
+ import { AbiCoder, BigNumberish, keccak256, toUtf8Bytes } from "ethers";
21
20
  import { groth16 } from "snarkjs";
22
- import { loadCircuit, encodeProof, tokenUriHash } from "zeto-js";
23
- import { User, UTXO } from "./lib/utils";
24
- import { formatPrivKeyForBabyJub, stringifyBigInts } from "maci-crypto";
21
+ import { loadCircuit, encodeProof } from "zeto-js";
22
+ import { User, UTXO, ZERO_UTXO, logger } from "./lib/utils";
25
23
 
26
24
  function provingKeysRoot() {
27
25
  const PROVING_KEYS_ROOT = process.env.PROVING_KEYS_ROOT;
@@ -44,6 +42,24 @@ export function loadProvingKeys(type: string) {
44
42
  };
45
43
  }
46
44
 
45
+ export function inflateUtxos(outputUtxos: UTXO[]) {
46
+ const desiredLength = outputUtxos.length > 2 ? 10 : 2;
47
+ const inflatedOutputUtxos = [...outputUtxos];
48
+ for (let i = 0; i < desiredLength - outputUtxos.length; i++) {
49
+ inflatedOutputUtxos.push(ZERO_UTXO);
50
+ }
51
+ return inflatedOutputUtxos;
52
+ }
53
+
54
+ export function inflateOwners(owners: User[]) {
55
+ const desiredLength = owners.length > 2 ? 10 : 2;
56
+ const inflatedOwners = [...owners];
57
+ for (let i = 0; i < desiredLength - owners.length; i++) {
58
+ inflatedOwners.push(owners[0]); // use a random owner for the extra utxos
59
+ }
60
+ return inflatedOwners;
61
+ }
62
+
47
63
  export async function prepareDepositProof(signer: User, outputs: [UTXO, UTXO]) {
48
64
  const outputCommitments: [BigNumberish, BigNumberish] = [
49
65
  outputs[0].hash,
@@ -60,10 +76,10 @@ export async function prepareDepositProof(signer: User, outputs: [UTXO, UTXO]) {
60
76
  const outputOwnerPublicKeys: [
61
77
  [BigNumberish, BigNumberish],
62
78
  [BigNumberish, BigNumberish],
63
- ] = [signer.babyJubPublicKey, outputs[1].hash ? signer.babyJubPublicKey : [0n, 0n]] as [
64
- [BigNumberish, BigNumberish],
65
- [BigNumberish, BigNumberish],
66
- ];
79
+ ] = [
80
+ signer.babyJubPublicKey,
81
+ outputs[1].hash ? signer.babyJubPublicKey : [0n, 0n],
82
+ ] as [[BigNumberish, BigNumberish], [BigNumberish, BigNumberish]];
67
83
 
68
84
  const inputObj = {
69
85
  outputCommitments,
@@ -86,7 +102,67 @@ export async function prepareDepositProof(signer: User, outputs: [UTXO, UTXO]) {
86
102
  )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
87
103
  const timeProofGeneration = Date.now() - startProofGeneration;
88
104
 
89
- console.log(
105
+ logger.debug(
106
+ `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
107
+ );
108
+
109
+ const encodedProof = encodeProof(proof);
110
+ return {
111
+ outputCommitments,
112
+ encodedProof,
113
+ };
114
+ }
115
+
116
+ export async function prepareDepositKycProof(
117
+ signer: User,
118
+ outputs: [UTXO, UTXO],
119
+ identitiesRoot: BigInt,
120
+ identitiesMerkleProof: BigInt[][],
121
+ ) {
122
+ const outputCommitments: [BigNumberish, BigNumberish] = [
123
+ outputs[0].hash,
124
+ outputs[1].hash,
125
+ ] as [BigNumberish, BigNumberish];
126
+ const outputValues = [
127
+ BigInt(outputs[0].value || 0),
128
+ BigInt(outputs[1].value || 0),
129
+ ];
130
+ const outputSalts = [
131
+ BigInt(outputs[0].salt || 0n),
132
+ BigInt(outputs[1].salt || 0n),
133
+ ];
134
+ const outputOwnerPublicKeys: [
135
+ [BigNumberish, BigNumberish],
136
+ [BigNumberish, BigNumberish],
137
+ ] = [
138
+ signer.babyJubPublicKey,
139
+ outputs[1].hash ? signer.babyJubPublicKey : [0n, 0n],
140
+ ] as [[BigNumberish, BigNumberish], [BigNumberish, BigNumberish]];
141
+
142
+ const inputObj = {
143
+ outputCommitments,
144
+ outputValues,
145
+ outputSalts,
146
+ outputOwnerPublicKeys,
147
+ identitiesRoot,
148
+ identitiesMerkleProof,
149
+ };
150
+
151
+ const circuit = await loadCircuit("deposit_kyc");
152
+ const { provingKeyFile } = loadProvingKeys("deposit_kyc");
153
+
154
+ const startWitnessCalculation = Date.now();
155
+ const witness = await circuit.calculateWTNSBin(inputObj, true);
156
+ const timeWithnessCalculation = Date.now() - startWitnessCalculation;
157
+
158
+ const startProofGeneration = Date.now();
159
+ const { proof, publicSignals } = (await groth16.prove(
160
+ provingKeyFile,
161
+ witness,
162
+ )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
163
+ const timeProofGeneration = Date.now() - startProofGeneration;
164
+
165
+ logger.debug(
90
166
  `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
91
167
  );
92
168
 
@@ -142,7 +218,7 @@ export async function prepareWithdrawProof(
142
218
  )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
143
219
  const timeProofGeneration = Date.now() - startProofGeneration;
144
220
 
145
- console.log(
221
+ logger.debug(
146
222
  `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
147
223
  );
148
224
 
@@ -208,7 +284,7 @@ export async function prepareNullifierWithdrawProof(
208
284
  )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
209
285
  const timeProofGeneration = Date.now() - startProofGeneration;
210
286
 
211
- console.log(
287
+ logger.debug(
212
288
  `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
213
289
  );
214
290
 
@@ -259,7 +335,7 @@ export async function prepareBurnProof(
259
335
  )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
260
336
  const timeProofGeneration = Date.now() - startProofGeneration;
261
337
 
262
- console.log(
338
+ logger.debug(
263
339
  `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
264
340
  );
265
341
 
@@ -320,7 +396,7 @@ export async function prepareNullifierBurnProof(
320
396
  )) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
321
397
  const timeProofGeneration = Date.now() - startProofGeneration;
322
398
 
323
- console.log(
399
+ logger.debug(
324
400
  `Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`,
325
401
  );
326
402
 
@@ -333,11 +409,58 @@ export async function prepareNullifierBurnProof(
333
409
  };
334
410
  }
335
411
 
336
- export function randomBytesAsDigitArray(length: number) {
337
- const bytes = crypto.randomBytes(length);
338
- let s = "";
339
- for (let i = 0; i < bytes.length; i++) {
340
- s += bytes[i].toString(2).padStart(8, "0");
341
- }
342
- return s.split("").map((b) => parseInt(b));
412
+ export function encodeToBytesForDeposit(proof: any) {
413
+ return new AbiCoder().encode(
414
+ ["tuple(uint256[2] pA, uint256[2][2] pB, uint256[2] pC)"],
415
+ [proof],
416
+ );
417
+ }
418
+
419
+ export function encodeToBytesForWithdraw(root: any, proof: any) {
420
+ return new AbiCoder().encode(
421
+ ["uint256 root", "tuple(uint256[2] pA, uint256[2][2] pB, uint256[2] pC)"],
422
+ [root, proof],
423
+ );
343
424
  }
425
+
426
+ // Domain-separation tags mirroring `_SPEND_HASH_DOMAIN` /
427
+ // `_CANCEL_HASH_DOMAIN` in `lib/zeto_fungible.sol`. Keep these strings
428
+ // in sync with the on-chain constants; otherwise computed commitments
429
+ // will not match what the contract expects.
430
+ const SPEND_HASH_DOMAIN = keccak256(toUtf8Bytes("Zeto.spendCommitment.v1"));
431
+ const CANCEL_HASH_DOMAIN = keccak256(toUtf8Bytes("Zeto.cancelCommitment.v1"));
432
+
433
+ function buildUnlockHash(
434
+ lockedInputs: UTXO[],
435
+ lockedOutputs: UTXO[],
436
+ outputs: UTXO[],
437
+ data: any,
438
+ domain: string,
439
+ ) {
440
+ const abiCoder = new AbiCoder();
441
+ // Hash each dynamic component with `abi.encode` rather than
442
+ // `abi.encodePacked` so dynamic-length arrays cannot collide
443
+ // (mirrors `_buildUnlockHash` in zeto_fungible.sol).
444
+ return keccak256(
445
+ abiCoder.encode(
446
+ ["bytes32", "bytes32", "bytes32", "bytes32", "bytes32"],
447
+ [
448
+ domain,
449
+ keccak256(abiCoder.encode(["uint256[]"], [lockedInputs.map((utxo) => utxo.hash)])),
450
+ keccak256(abiCoder.encode(["uint256[]"], [lockedOutputs.map((utxo) => utxo.hash)])),
451
+ keccak256(abiCoder.encode(["uint256[]"], [outputs.map((utxo) => utxo.hash)])),
452
+ keccak256(data),
453
+ ],
454
+ ),
455
+ );
456
+ }
457
+
458
+ /// Mirror of `IZetoLockableCapability.computeSpendHash`.
459
+ export function calculateSpendHash(lockedInputs: UTXO[], lockedOutputs: UTXO[], outputs: UTXO[], data: any) {
460
+ return buildUnlockHash(lockedInputs, lockedOutputs, outputs, data, SPEND_HASH_DOMAIN);
461
+ }
462
+
463
+ /// Mirror of `IZetoLockableCapability.computeCancelHash`.
464
+ export function calculateCancelHash(lockedInputs: UTXO[], lockedOutputs: UTXO[], outputs: UTXO[], data: any) {
465
+ return buildUnlockHash(lockedInputs, lockedOutputs, outputs, data, CANCEL_HASH_DOMAIN);
466
+ }