@lfdecentralizedtrust/zeto-contracts 0.2.2 → 0.5.1

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 (164) hide show
  1. package/README.md +76 -162
  2. package/config/eip170.ts +30 -0
  3. package/contracts/factory.sol +30 -34
  4. package/contracts/factory_upgradeable.sol +11 -7
  5. package/contracts/lib/common/util.sol +40 -0
  6. package/contracts/lib/interfaces/ILockableCapability.sol +318 -0
  7. package/contracts/lib/interfaces/{izeto.sol → IZeto.sol} +13 -1
  8. package/contracts/lib/interfaces/{izeto_initializable.sol → IZetoInitializable.sol} +14 -12
  9. package/contracts/lib/interfaces/{izeto_kyc.sol → IZetoKyc.sol} +3 -3
  10. package/contracts/lib/interfaces/IZetoLockHooks.sol +42 -0
  11. package/contracts/lib/interfaces/IZetoLockableCapability.sol +237 -0
  12. package/contracts/lib/interfaces/IZetoStorage.sol +106 -0
  13. package/contracts/lib/interfaces/{izeto_lockable.sol → IZetoVerifier.sol} +8 -21
  14. package/contracts/lib/registry.sol +74 -26
  15. package/contracts/lib/storage/base.sol +210 -0
  16. package/contracts/lib/storage/nullifier.sol +166 -0
  17. package/contracts/lib/zeto_common.sol +277 -28
  18. package/contracts/lib/zeto_fungible.sol +443 -33
  19. package/contracts/lib/zeto_fungible_base.sol +69 -0
  20. package/contracts/lib/zeto_fungible_burn.sol +83 -53
  21. package/contracts/lib/zeto_fungible_burn_nullifier.sol +115 -72
  22. package/contracts/lib/zeto_fungible_nullifier.sol +167 -0
  23. package/contracts/lib/zeto_lockable.sol +219 -0
  24. package/contracts/lib/zeto_lockable_lib.sol +460 -0
  25. package/contracts/lib/zeto_lockable_storage.sol +43 -0
  26. package/contracts/lib/zeto_non_fungible.sol +228 -0
  27. package/contracts/lib/zeto_non_fungible_base.sol +61 -0
  28. package/contracts/lib/zeto_non_fungible_nullifier.sol +61 -0
  29. package/contracts/test/escrow1.sol +90 -34
  30. package/contracts/test/escrow2.sol +64 -29
  31. package/contracts/test/qurrency.sol +10 -2
  32. package/contracts/test/smt.sol +6 -1
  33. package/contracts/test/tendecimals.sol +14 -12
  34. package/contracts/verifiers/impl/anon.sol +189 -0
  35. package/contracts/verifiers/impl/anon_batch.sol +301 -0
  36. package/contracts/verifiers/impl/anon_enc.sol +266 -0
  37. package/contracts/verifiers/impl/anon_enc_batch.sol +602 -0
  38. package/contracts/verifiers/impl/anon_enc_nullifier.sol +287 -0
  39. package/contracts/verifiers/impl/anon_enc_nullifier_batch.sol +679 -0
  40. package/contracts/verifiers/impl/anon_enc_nullifier_kyc.sol +294 -0
  41. package/contracts/verifiers/impl/anon_enc_nullifier_kyc_batch.sol +686 -0
  42. package/contracts/verifiers/impl/anon_enc_nullifier_non_repudiation.sol +413 -0
  43. package/contracts/verifiers/impl/anon_enc_nullifier_non_repudiation_batch.sol +1141 -0
  44. package/contracts/verifiers/impl/anon_nullifier_kyc_transfer.sol +217 -0
  45. package/contracts/verifiers/impl/anon_nullifier_kyc_transferLocked.sol +196 -0
  46. package/contracts/verifiers/impl/anon_nullifier_kyc_transferLocked_batch.sol +308 -0
  47. package/contracts/verifiers/impl/anon_nullifier_kyc_transfer_batch.sol +385 -0
  48. package/contracts/verifiers/impl/anon_nullifier_qurrency_transfer.sol +497 -0
  49. package/contracts/verifiers/impl/anon_nullifier_qurrency_transfer_batch.sol +1001 -0
  50. package/contracts/verifiers/{verifier_anon_nullifier_transferLocked.sol → impl/anon_nullifier_transfer.sol} +12 -19
  51. package/contracts/verifiers/{verifier_anon_nullifier_transferLocked_batch.sol → impl/anon_nullifier_transfer_batch.sol} +44 -51
  52. package/contracts/verifiers/impl/burn.sol +182 -0
  53. package/contracts/verifiers/impl/burn_batch.sol +238 -0
  54. package/contracts/verifiers/impl/burn_nullifier.sol +203 -0
  55. package/contracts/verifiers/impl/burn_nullifier_batch.sol +315 -0
  56. package/contracts/verifiers/impl/deposit.sol +182 -0
  57. package/contracts/verifiers/impl/deposit_kyc.sol +189 -0
  58. package/contracts/verifiers/impl/nf_anon.sol +175 -0
  59. package/contracts/verifiers/{verifier_nf_anon_nullifier_transferLocked.sol → impl/nf_anon_nullifier_transfer.sol} +6 -13
  60. package/contracts/verifiers/impl/withdraw.sol +189 -0
  61. package/contracts/verifiers/impl/withdraw_batch.sol +245 -0
  62. package/contracts/verifiers/impl/withdraw_nullifier.sol +210 -0
  63. package/contracts/verifiers/impl/withdraw_nullifier_batch.sol +322 -0
  64. package/contracts/verifiers/verifier_anon.sol +31 -186
  65. package/contracts/verifiers/verifier_anon_batch.sol +31 -298
  66. package/contracts/verifiers/verifier_anon_enc.sol +31 -263
  67. package/contracts/verifiers/verifier_anon_enc_batch.sol +31 -599
  68. package/contracts/verifiers/verifier_anon_enc_nullifier.sol +31 -284
  69. package/contracts/verifiers/verifier_anon_enc_nullifier_batch.sol +33 -676
  70. package/contracts/verifiers/verifier_anon_enc_nullifier_kyc.sol +31 -291
  71. package/contracts/verifiers/verifier_anon_enc_nullifier_kyc_batch.sol +33 -683
  72. package/contracts/verifiers/verifier_anon_enc_nullifier_non_repudiation.sol +33 -410
  73. package/contracts/verifiers/verifier_anon_enc_nullifier_non_repudiation_batch.sol +33 -1138
  74. package/contracts/verifiers/verifier_anon_nullifier_kyc_transfer.sol +33 -214
  75. package/contracts/verifiers/verifier_anon_nullifier_kyc_transferLocked.sol +33 -221
  76. package/contracts/verifiers/verifier_anon_nullifier_kyc_transferLocked_batch.sol +33 -389
  77. package/contracts/verifiers/verifier_anon_nullifier_kyc_transfer_batch.sol +33 -382
  78. package/contracts/verifiers/verifier_anon_nullifier_qurrency_transfer.sol +33 -221
  79. package/contracts/verifiers/verifier_anon_nullifier_qurrency_transfer_batch.sol +33 -389
  80. package/contracts/verifiers/verifier_anon_nullifier_transfer.sol +33 -207
  81. package/contracts/verifiers/verifier_anon_nullifier_transfer_batch.sol +33 -375
  82. package/contracts/verifiers/verifier_burn.sol +31 -179
  83. package/contracts/verifiers/verifier_burn_batch.sol +31 -235
  84. package/contracts/verifiers/verifier_burn_nullifier.sol +31 -200
  85. package/contracts/verifiers/verifier_burn_nullifier_batch.sol +31 -312
  86. package/contracts/verifiers/verifier_deposit.sol +31 -179
  87. package/contracts/verifiers/verifier_deposit_kyc.sol +34 -0
  88. package/contracts/verifiers/verifier_nf_anon.sol +31 -172
  89. package/contracts/verifiers/verifier_nf_anon_nullifier_transfer.sol +33 -179
  90. package/contracts/verifiers/verifier_withdraw.sol +31 -186
  91. package/contracts/verifiers/verifier_withdraw_batch.sol +31 -242
  92. package/contracts/verifiers/verifier_withdraw_nullifier.sol +31 -207
  93. package/contracts/verifiers/verifier_withdraw_nullifier_batch.sol +33 -319
  94. package/contracts/zeto_anon.sol +77 -231
  95. package/contracts/zeto_anon_burnable.sol +56 -12
  96. package/contracts/zeto_anon_enc.sol +93 -190
  97. package/contracts/zeto_anon_enc_nullifier.sol +249 -195
  98. package/contracts/zeto_anon_enc_nullifier_kyc.sol +51 -232
  99. package/contracts/zeto_anon_enc_nullifier_non_repudiation.sol +231 -238
  100. package/contracts/zeto_anon_nullifier.sol +164 -298
  101. package/contracts/zeto_anon_nullifier_burnable.sol +68 -18
  102. package/contracts/zeto_anon_nullifier_kyc.sol +40 -345
  103. package/contracts/zeto_anon_nullifier_qurrency.sol +217 -361
  104. package/contracts/zeto_nf_anon.sol +55 -130
  105. package/contracts/zeto_nf_anon_nullifier.sol +90 -152
  106. package/hardhat.config.ts +18 -3
  107. package/ignition/modules/lib/deps.ts +13 -0
  108. package/ignition/modules/test/tendecimals.ts +7 -1
  109. package/ignition/modules/zeto_anon.ts +3 -0
  110. package/ignition/modules/zeto_anon_burnable.ts +11 -7
  111. package/ignition/modules/zeto_anon_enc.ts +3 -0
  112. package/ignition/modules/zeto_anon_enc_nullifier.ts +34 -0
  113. package/ignition/modules/zeto_anon_enc_nullifier_kyc.ts +5 -2
  114. package/ignition/modules/zeto_anon_enc_nullifier_non_repudiation.ts +3 -0
  115. package/ignition/modules/zeto_anon_nullifier.ts +31 -4
  116. package/ignition/modules/zeto_anon_nullifier_burnable.ts +53 -16
  117. package/ignition/modules/zeto_anon_nullifier_kyc.ts +30 -12
  118. package/ignition/modules/zeto_anon_nullifier_qurrency.ts +3 -0
  119. package/ignition/modules/zeto_nf_anon.ts +3 -1
  120. package/ignition/modules/zeto_nf_anon_nullifier.ts +17 -12
  121. package/package.json +7 -3
  122. package/scripts/deploy_cloneable.ts +19 -10
  123. package/scripts/deploy_upgradeable.ts +9 -5
  124. package/scripts/lib/zeto_libraries.ts +47 -0
  125. package/scripts/tokens/Zeto_Anon.ts +6 -3
  126. package/scripts/tokens/Zeto_AnonBurnable.ts +4 -1
  127. package/scripts/tokens/Zeto_AnonEnc.ts +15 -3
  128. package/scripts/tokens/Zeto_AnonEncNullifier.ts +11 -8
  129. package/scripts/tokens/Zeto_AnonEncNullifierKyc.ts +7 -6
  130. package/scripts/tokens/Zeto_AnonEncNullifierNonRepudiation.ts +7 -6
  131. package/scripts/tokens/Zeto_AnonNullifier.ts +7 -6
  132. package/scripts/tokens/Zeto_AnonNullifierBurnable.ts +7 -6
  133. package/scripts/tokens/Zeto_AnonNullifierKyc.ts +7 -6
  134. package/scripts/tokens/Zeto_AnonNullifierQurrency.ts +7 -8
  135. package/scripts/tokens/Zeto_NfAnon.ts +5 -3
  136. package/scripts/tokens/Zeto_NfAnonNullifier.ts +7 -7
  137. package/test/factory.ts +55 -73
  138. package/test/lib/anon_nullifier_helpers.ts +89 -0
  139. package/test/lib/anon_zeto_helpers.ts +76 -0
  140. package/test/lib/deploy.ts +5 -2
  141. package/test/lib/eip170.ts +23 -0
  142. package/test/lib/utils.ts +74 -35
  143. package/test/test/escrow1.ts +185 -58
  144. package/test/test/escrow2.ts +200 -107
  145. package/test/test/qurrency.ts +13 -33
  146. package/test/usdc-shielding.ts +39 -27
  147. package/test/utils.ts +144 -21
  148. package/test/zeto_anon.ts +956 -465
  149. package/test/zeto_anon_enc.ts +881 -143
  150. package/test/zeto_anon_enc_nullifier.ts +850 -39
  151. package/test/zeto_anon_enc_nullifier_kyc.ts +123 -182
  152. package/test/zeto_anon_enc_nullifier_non_repudiation.ts +80 -47
  153. package/test/zeto_anon_nullifier.ts +1212 -954
  154. package/test/zeto_anon_nullifier_kyc.ts +677 -656
  155. package/test/zeto_anon_nullifier_qurrency.ts +455 -307
  156. package/test/zeto_nf_anon.ts +737 -138
  157. package/test/zeto_nf_anon_nullifier.ts +876 -247
  158. package/contracts/lib/zeto_base.sol +0 -293
  159. package/contracts/lib/zeto_fungible_withdraw.sol +0 -132
  160. package/contracts/lib/zeto_fungible_withdraw_nullifier.sol +0 -144
  161. package/contracts/lib/zeto_nullifier.sol +0 -340
  162. package/contracts/zkDvP.sol_ +0 -273
  163. package/test/zkDvP.ts_ +0 -455
  164. /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
+ }