@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
@@ -15,408 +15,264 @@
15
15
  // limitations under the License.
16
16
  pragma solidity ^0.8.27;
17
17
 
18
- import {PoseidonUnit5L} from "@iden3/contracts/contracts/lib/Poseidon.sol";
19
- import {IZeto} from "./lib/interfaces/izeto.sol";
20
- import {MAX_BATCH} from "./lib/interfaces/izeto.sol";
21
- import {Groth16Verifier_Deposit} from "./verifiers/verifier_deposit.sol";
22
- import {Groth16Verifier_WithdrawNullifier} from "./verifiers/verifier_withdraw_nullifier.sol";
23
- import {Groth16Verifier_WithdrawNullifierBatch} from "./verifiers/verifier_withdraw_nullifier_batch.sol";
24
- import {Groth16Verifier_AnonNullifierQurrencyTransfer} from "./verifiers/verifier_anon_nullifier_qurrency_transfer.sol";
25
- // import {Groth16Verifier_AnonNullifierTransferLocked} from "./verifiers/verifier_anon_nullifier_transferLocked.sol";
26
- import {Groth16Verifier_AnonNullifierQurrencyTransferBatch} from "./verifiers/verifier_anon_nullifier_qurrency_transfer_batch.sol";
27
- // import {Groth16Verifier_AnonNullifierTransferLockedBatch} from "./verifiers/verifier_anon_nullifier_transferLocked_batch.sol";
28
- import {ZetoNullifier} from "./lib/zeto_nullifier.sol";
29
- import {ZetoFungibleWithdrawWithNullifiers} from "./lib/zeto_fungible_withdraw_nullifier.sol";
30
- import {Commonlib} from "./lib/common.sol";
31
- import {IZetoInitializable} from "./lib/interfaces/izeto_initializable.sol";
32
- import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
33
- import {console} from "hardhat/console.sol";
18
+ import {Commonlib} from "./lib/common/common.sol";
19
+ import {IZetoInitializable} from "./lib/interfaces/IZetoInitializable.sol";
20
+ import {Zeto_AnonNullifier} from "./zeto_anon_nullifier.sol";
34
21
 
35
- uint256 constant INPUT_SIZE = 9;
36
- // uint256 constant INPUT_SIZE_LOCKED = 8;
37
- uint256 constant BATCH_INPUT_SIZE = 33;
38
-
39
- // uint256 constant BATCH_INPUT_SIZE_LOCKED = 32;
40
-
41
- /// @title A sample implementation of a Zeto based fungible token with anonymity and history masking
22
+ /// @title A sample implementation of a Zeto based fungible token with
23
+ /// anonymity, history masking via nullifiers, and post-quantum
24
+ /// ML-KEM encryption envelopes for transferred values.
42
25
  /// @author Kaleido, Inc.
43
- /// @dev The proof has the following statements:
44
- /// - each value in the output commitments must be a positive number in the range 0 ~ (2\*\*40 - 1)
45
- /// - the sum of the nullified values match the sum of output values
46
- /// - the hashes in the input and output match the hash(value, salt, owner public key) formula
47
- /// - the sender possesses the private BabyJubjub key, whose public key is part of the pre-image of the input commitment hashes, which match the corresponding nullifiers
48
- /// - the nullifiers represent input commitments that are included in a Sparse Merkle Tree represented by the root hash
49
- contract Zeto_AnonNullifierQurrency is
50
- IZeto,
51
- IZetoInitializable,
52
- ZetoNullifier,
53
- ZetoFungibleWithdrawWithNullifiers,
54
- UUPSUpgradeable
55
- {
56
- Groth16Verifier_AnonNullifierQurrencyTransfer internal _verifier;
57
- Groth16Verifier_AnonNullifierQurrencyTransferBatch internal _batchVerifier;
26
+ /// @notice Decimals: this token uses **4** decimals, inherited from
27
+ /// {ZetoCommon.decimals}. Indexers and UIs reading this contract
28
+ /// directly should treat balances accordingly.
29
+ /// @dev Replaces the BabyJubjub-based ECDH envelope used by
30
+ /// {Zeto_AnonEncNullifier} with an ML-KEM (post-quantum)
31
+ /// key-encapsulation envelope. The proof carries the
32
+ /// `encapsulatedSharedSecret` (25 field elements -- the encoded
33
+ /// KEM ciphertext) alongside the encrypted output values; the
34
+ /// circuit asserts that the encrypted values were produced under
35
+ /// the same shared secret derived from that ciphertext.
36
+ ///
37
+ /// Locked-input circuit binding: this contract intentionally does
38
+ /// NOT include `msg.sender` in the public-input vector for the
39
+ /// locked-input flavour, even though `main` did. Lock-spender
40
+ /// authorization is now enforced at the Zeto contract layer (via
41
+ /// `ZetoFungible._locks[lockId].spender == msg.sender`) rather than
42
+ /// inside the circuit. This matches how {Zeto_AnonNullifier}
43
+ /// organises its locked-input proof; deployments that still want a
44
+ /// circuit-level binding to the spender can override
45
+ /// {constructPublicInputs} and append it.
46
+ contract Zeto_AnonNullifierQurrency is Zeto_AnonNullifier {
47
+ /// @dev Proof decode + public-input assembly scratch lives in
48
+ /// {ZetoAnonNullifierQurrencyStorage} (ERC-7201).
58
49
 
59
- // Groth16Verifier_AnonNullifierTransferLocked internal _lockVerifier;
60
- // Groth16Verifier_AnonNullifierTransferLockedBatch
61
- // internal _batchLockVerifier;
50
+ /// @dev Lock the implementation contract on construction. Restated at
51
+ /// every leaf in the inheritance graph for H-2 robustness.
52
+ /// @custom:oz-upgrades-unsafe-allow constructor
53
+ constructor() {
54
+ _disableInitializers();
55
+ }
62
56
 
63
57
  function initialize(
64
- string memory name,
65
- string memory symbol,
58
+ string calldata name,
59
+ string calldata symbol,
66
60
  address initialOwner,
67
61
  IZetoInitializable.VerifiersInfo calldata verifiers
68
- ) public initializer {
69
- __ZetoNullifier_init(name, symbol, initialOwner);
70
- __ZetoFungibleWithdrawWithNullifiers_init(
71
- (Groth16Verifier_Deposit)(verifiers.depositVerifier),
72
- (Groth16Verifier_WithdrawNullifier)(verifiers.withdrawVerifier),
73
- (Groth16Verifier_WithdrawNullifierBatch)(
74
- verifiers.batchWithdrawVerifier
75
- )
76
- );
77
- _verifier = (Groth16Verifier_AnonNullifierQurrencyTransfer)(
78
- verifiers.verifier
79
- );
80
- // _lockVerifier = (Groth16Verifier_AnonNullifierTransferLocked)(
81
- // verifiers.lockVerifier
82
- // );
83
- _batchVerifier = (Groth16Verifier_AnonNullifierQurrencyTransferBatch)(
84
- verifiers.batchVerifier
85
- );
86
- // _batchLockVerifier = (Groth16Verifier_AnonNullifierTransferLockedBatch)(
87
- // verifiers.batchLockVerifier
88
- // );
62
+ ) public override initializer {
63
+ __ZetoAnonNullifier_init(name, symbol, initialOwner, verifiers);
89
64
  }
90
65
 
91
- function _authorizeUpgrade(address) internal override onlyOwner {}
92
-
93
- function constructPublicInputs(
94
- uint[2] memory computed_hashes,
66
+ function emitTransferEvent(
95
67
  uint256[] memory nullifiers,
96
68
  uint256[] memory outputs,
97
- uint256 root,
98
- uint256 size,
99
- bool locked
100
- ) internal view returns (uint256[] memory publicInputs) {
101
- publicInputs = new uint256[](size);
102
- // copy computed hashes
103
- publicInputs[0] = computed_hashes[0];
104
- publicInputs[1] = computed_hashes[1];
105
- uint256 piIndex = 2;
106
- // copy input commitments
107
- for (uint256 i = 0; i < nullifiers.length; i++) {
108
- publicInputs[piIndex++] = nullifiers[i];
109
- }
110
- // when verifying locked transfers, additional public input
111
- // for the lock delegate
112
- if (locked) {
113
- publicInputs[piIndex++] = uint256(uint160(msg.sender));
114
- }
115
- // copy root
116
- publicInputs[piIndex++] = root;
117
-
118
- // populate enables
119
- for (uint256 i = 0; i < nullifiers.length; i++) {
120
- publicInputs[piIndex++] = (nullifiers[i] == 0) ? 0 : 1;
121
- }
122
-
123
- // copy output commitments
124
- for (uint256 i = 0; i < outputs.length; i++) {
125
- publicInputs[piIndex++] = outputs[i];
126
- }
127
-
128
- return publicInputs;
69
+ bytes memory proof,
70
+ bytes memory data
71
+ ) internal override {
72
+ ZetoAnonNullifierQurrencyStorage.DecodedProofQurrency memory dp;
73
+ (dp, ) = decodeProof_Qurrency(proof);
74
+ emit UTXOTransferWithMlkemEncryptedValues(
75
+ nullifiers,
76
+ outputs,
77
+ dp.encryptionNonce,
78
+ dp.encapsulatedSharedSecret,
79
+ dp.encryptedValues,
80
+ msg.sender,
81
+ data
82
+ );
129
83
  }
130
84
 
131
85
  /**
132
- * @dev the main function of the contract.
86
+ * @dev Layout of `proof` (ABI-encoded tuple):
87
+ * (uint256 root,
88
+ * uint256 encryptionNonce,
89
+ * uint256[] encryptedValues,
90
+ * uint256[25] encapsulatedSharedSecret,
91
+ * Commonlib.Proof groth16Proof)
92
+ *
93
+ * Public-inputs layout (unlocked):
94
+ * encapsulatedSharedSecret[25]
95
+ * ++ encryptedValues
96
+ * ++ nullifiers
97
+ * ++ root
98
+ * ++ enabled-flags
99
+ * ++ outputs
133
100
  *
134
- * @param nullifiers Array of nullifiers that are secretly bound to UTXOs to be spent by the transaction.
135
- * @param outputs Array of new UTXOs to generate, for future transactions to spend.
136
- * @param root The root hash of the Sparse Merkle Tree that contains the nullifiers.
137
- * @param proof A zero knowledge proof that the submitter is authorized to spend the inputs, and
138
- * that the outputs are valid in terms of obeying mass conservation rules.
101
+ * Locked variant drops the `root`/`enabled-flags` block (locked
102
+ * inputs are raw UTXOs, not nullifiers, so the SMT-membership
103
+ * block is not part of the witness) but keeps the encryption
104
+ * commitments so the receiver can still decrypt the new outputs.
139
105
  *
140
- * Emits a {UTXOTransfer} event.
106
+ * Root validation is performed here for the unlocked path so the
107
+ * proof bytes are decoded exactly once per transition.
141
108
  */
142
- function transfer(
109
+ function constructPublicInputs(
143
110
  uint256[] memory nullifiers,
144
111
  uint256[] memory outputs,
145
- uint256 root,
146
- bytes memory ciphertext,
147
- Commonlib.Proof calldata proof,
148
- bytes calldata data
149
- ) public returns (bool) {
150
- nullifiers = checkAndPadCommitments(nullifiers);
151
- outputs = checkAndPadCommitments(outputs);
152
- validateTransactionProposal(nullifiers, outputs, root, false);
153
- uint[2] memory computed_pubSignals = calculateHash(ciphertext);
154
- verifyProof(computed_pubSignals, nullifiers, outputs, root, proof);
155
- uint256[] memory empty;
156
- processInputsAndOutputs(nullifiers, outputs, empty, address(0));
112
+ bytes memory proof,
113
+ bool inputsLocked
114
+ )
115
+ internal
116
+ virtual
117
+ override
118
+ returns (uint256[] memory, Commonlib.Proof memory)
119
+ {
120
+ (
121
+ ZetoAnonNullifierQurrencyStorage.DecodedProofQurrency memory dp,
122
+ Commonlib.Proof memory proofStruct
123
+ ) = decodeProof_Qurrency(proof);
157
124
 
158
- uint256[] memory nullifierArray = new uint256[](nullifiers.length);
159
- uint256[] memory outputArray = new uint256[](outputs.length);
160
- for (uint256 i = 0; i < nullifiers.length; ++i) {
161
- nullifierArray[i] = nullifiers[i];
162
- outputArray[i] = outputs[i];
125
+ if (!inputsLocked) {
126
+ validateRoot(dp.root);
163
127
  }
164
- emit UTXOTransfer(nullifierArray, outputArray, msg.sender, data);
165
- return true;
166
- }
167
128
 
168
- // function transferLocked(
169
- // uint256[] memory nullifiers,
170
- // uint256[] memory outputs,
171
- // uint256 root,
172
- // Commonlib.Proof calldata proof,
173
- // bytes calldata data
174
- // ) public returns (bool) {
175
- // nullifiers = checkAndPadCommitments(nullifiers);
176
- // outputs = checkAndPadCommitments(outputs);
177
- // validateTransactionProposal(nullifiers, outputs, root, true);
178
- // verifyProofLocked(nullifiers, outputs, root, proof);
179
- // uint256[] memory empty;
180
- // processInputsAndOutputs(nullifiers, outputs, empty, address(0));
129
+ ZetoAnonNullifierQurrencyStorage.layout().dpq = dp;
181
130
 
182
- // uint256[] memory nullifierArray = new uint256[](nullifiers.length);
183
- // uint256[] memory outputArray = new uint256[](outputs.length);
184
- // for (uint256 i = 0; i < nullifiers.length; ++i) {
185
- // nullifierArray[i] = nullifiers[i];
186
- // outputArray[i] = outputs[i];
187
- // }
188
- // emit UTXOTransfer(nullifierArray, outputArray, msg.sender, data);
189
- // return true;
190
- // }
131
+ uint256 size = _calcSize_Qurrency(nullifiers, outputs, inputsLocked);
132
+ _fillPublicInputs_Qurrency(size, nullifiers, outputs, inputsLocked);
191
133
 
192
- function deposit(
193
- uint256 amount,
194
- uint256[] memory outputs,
195
- Commonlib.Proof calldata proof,
196
- bytes calldata data
197
- ) public {
198
- _deposit(amount, outputs, proof);
199
- _mint(outputs, data);
134
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
135
+ .layout();
136
+ uint256[] memory out = new uint256[]($.publicInputsBuf.length);
137
+ for (uint256 i = 0; i < out.length; ++i) {
138
+ out[i] = $.publicInputsBuf[i];
139
+ }
140
+ return (out, proofStruct);
200
141
  }
201
142
 
202
- function withdraw(
203
- uint256 amount,
204
- uint256[] memory nullifiers,
205
- uint256 output,
206
- uint256 root,
207
- Commonlib.Proof calldata proof,
208
- bytes calldata data
209
- ) public {
210
- uint256[] memory outputs = new uint256[](nullifiers.length);
211
- outputs[0] = output;
212
- // Check and pad inputs and outputs based on the max size
213
- nullifiers = checkAndPadCommitments(nullifiers);
214
- outputs = checkAndPadCommitments(outputs);
215
- validateTransactionProposal(nullifiers, outputs, root, false);
216
- _withdrawWithNullifiers(amount, nullifiers, output, root, proof);
217
- uint256[] memory empty;
218
- processInputsAndOutputs(nullifiers, outputs, empty, address(0));
219
- emit UTXOWithdraw(amount, nullifiers, output, msg.sender, data);
143
+ function decodeProof_Qurrency(
144
+ bytes memory proof
145
+ )
146
+ internal
147
+ pure
148
+ returns (
149
+ ZetoAnonNullifierQurrencyStorage.DecodedProofQurrency memory dp,
150
+ Commonlib.Proof memory proofStruct
151
+ )
152
+ {
153
+ (
154
+ dp.root,
155
+ dp.encryptionNonce,
156
+ dp.encryptedValues,
157
+ dp.encapsulatedSharedSecret,
158
+ proofStruct
159
+ ) = abi.decode(
160
+ proof,
161
+ (uint256, uint256, uint256[], uint256[25], Commonlib.Proof)
162
+ );
163
+ return (dp, proofStruct);
220
164
  }
221
165
 
222
- function mint(
223
- uint256[] memory utxos,
224
- bytes calldata data
225
- ) public onlyOwner {
226
- _mint(utxos, data);
166
+ function _calcSize_Qurrency(
167
+ uint256[] memory nullifiers,
168
+ uint256[] memory outputs,
169
+ bool inputsLocked
170
+ ) internal view returns (uint256) {
171
+ ZetoAnonNullifierQurrencyStorage.DecodedProofQurrency storage dq = ZetoAnonNullifierQurrencyStorage
172
+ .layout()
173
+ .dpq;
174
+ if (inputsLocked) {
175
+ return
176
+ dq.encapsulatedSharedSecret.length +
177
+ dq.encryptedValues.length +
178
+ nullifiers.length +
179
+ outputs.length;
180
+ }
181
+ return
182
+ dq.encapsulatedSharedSecret.length +
183
+ dq.encryptedValues.length +
184
+ (nullifiers.length * 2) + // nullifiers + enabled flags
185
+ 1 + // root
186
+ outputs.length;
227
187
  }
228
188
 
229
- // function lock(
230
- // uint256[] memory nullifiers,
231
- // uint256[] memory outputs,
232
- // uint256[] memory lockedOutputs,
233
- // uint256 root,
234
- // Commonlib.Proof calldata proof,
235
- // address delegate,
236
- // bytes calldata data
237
- // ) public {
238
- // // merge the outputs and lockedOutputs and do a regular transfer
239
- // uint256[] memory allOutputs = new uint256[](
240
- // outputs.length + lockedOutputs.length
241
- // );
242
- // for (uint256 i = 0; i < outputs.length; i++) {
243
- // allOutputs[i] = outputs[i];
244
- // }
245
- // for (uint256 i = 0; i < lockedOutputs.length; i++) {
246
- // allOutputs[outputs.length + i] = lockedOutputs[i];
247
- // }
248
- // nullifiers = checkAndPadCommitments(nullifiers);
249
- // allOutputs = checkAndPadCommitments(allOutputs);
250
- // validateTransactionProposal(nullifiers, allOutputs, root, false);
251
- // verifyProof(nullifiers, allOutputs, root, proof);
252
-
253
- // spendNullifiers(nullifiers);
254
-
255
- // // lock the intended outputs
256
- // _lock(nullifiers, outputs, lockedOutputs, delegate, data);
257
- // }
258
-
259
- // function unlock(
260
- // uint256[] memory nullifiers,
261
- // uint256[] memory outputs,
262
- // uint256 root,
263
- // Commonlib.Proof calldata proof,
264
- // bytes calldata data
265
- // ) public {
266
- // transferLocked(nullifiers, outputs, root, proof, data);
267
- // }
268
-
269
- function verifyProof(
270
- uint[2] memory computed_hashes,
189
+ function _fillPublicInputs_Qurrency(
190
+ uint256 size,
271
191
  uint256[] memory nullifiers,
272
192
  uint256[] memory outputs,
273
- uint256 root,
274
- Commonlib.Proof calldata proof
275
- ) public view returns (bool) {
276
- if (nullifiers.length > 2 || outputs.length > 2) {
277
- uint256[] memory publicInputs = constructPublicInputs(
278
- computed_hashes,
279
- nullifiers,
280
- outputs,
281
- root,
282
- BATCH_INPUT_SIZE,
283
- false
284
- );
285
- // construct the public inputs for verifier
286
- uint256[BATCH_INPUT_SIZE] memory fixedSizeInputs;
287
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
288
- fixedSizeInputs[i] = publicInputs[i];
289
- }
290
- // Check the proof using batchVerifier
291
- require(
292
- _batchVerifier.verifyProof(
293
- proof.pA,
294
- proof.pB,
295
- proof.pC,
296
- fixedSizeInputs
297
- ),
298
- "Invalid proof (batch)"
299
- );
300
- } else {
301
- uint256[] memory publicInputs = constructPublicInputs(
302
- computed_hashes,
303
- nullifiers,
304
- outputs,
305
- root,
306
- INPUT_SIZE,
307
- false
308
- );
309
- // construct the public inputs for verifier
310
- uint256[INPUT_SIZE] memory fixedSizeInputs;
311
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
312
- fixedSizeInputs[i] = publicInputs[i];
313
- }
314
- // Check the proof
315
- require(
316
- _verifier.verifyProof(
317
- proof.pA,
318
- proof.pB,
319
- proof.pC,
320
- fixedSizeInputs
321
- ),
322
- "Invalid proof"
323
- );
193
+ bool inputsLocked
194
+ ) internal {
195
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
196
+ .layout();
197
+ $.publicInputsBuf = new uint256[](size);
198
+ $.piIndex = 0;
199
+
200
+ _fillEncapsulatedSharedSecret_Q();
201
+ _fillEncryptedValues_Q();
202
+ _fillInputs_Q(nullifiers);
203
+ if (!inputsLocked) {
204
+ _fillRootAndEnables_Q(nullifiers);
324
205
  }
325
- return true;
206
+ _fillOutputs_Q(outputs);
326
207
  }
327
208
 
328
- // function verifyProofLocked(
329
- // uint256[] memory nullifiers,
330
- // uint256[] memory outputs,
331
- // uint256 root,
332
- // Commonlib.Proof calldata proof
333
- // ) public view returns (bool) {
334
- // if (nullifiers.length > 2 || outputs.length > 2) {
335
- // uint256[] memory publicInputs = constructPublicInputs(
336
- // nullifiers,
337
- // outputs,
338
- // root,
339
- // BATCH_INPUT_SIZE_LOCKED,
340
- // true
341
- // );
342
- // // construct the public inputs for batchVerifier
343
- // uint256[BATCH_INPUT_SIZE_LOCKED] memory fixedSizeInputs;
344
- // for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
345
- // fixedSizeInputs[i] = publicInputs[i];
346
- // }
209
+ function _fillEncapsulatedSharedSecret_Q() internal {
210
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
211
+ .layout();
212
+ for (
213
+ uint256 i = 0;
214
+ i < $.dpq.encapsulatedSharedSecret.length;
215
+ ++i
216
+ ) {
217
+ $.publicInputsBuf[$.piIndex++] = $.dpq.encapsulatedSharedSecret[i];
218
+ }
219
+ }
347
220
 
348
- // // Check the proof using batchVerifier
349
- // require(
350
- // _batchLockVerifier.verifyProof(
351
- // proof.pA,
352
- // proof.pB,
353
- // proof.pC,
354
- // fixedSizeInputs
355
- // ),
356
- // "Invalid proof"
357
- // );
358
- // } else {
359
- // uint256[] memory publicInputs = constructPublicInputs(
360
- // nullifiers,
361
- // outputs,
362
- // root,
363
- // INPUT_SIZE_LOCKED,
364
- // true
365
- // );
366
- // // construct the public inputs for verifier
367
- // uint256[INPUT_SIZE_LOCKED] memory fixedSizeInputs;
368
- // for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
369
- // fixedSizeInputs[i] = publicInputs[i];
370
- // }
371
- // // Check the proof
372
- // require(
373
- // _lockVerifier.verifyProof(
374
- // proof.pA,
375
- // proof.pB,
376
- // proof.pC,
377
- // fixedSizeInputs
378
- // ),
379
- // "Invalid proof"
380
- // );
381
- // }
382
- // return true;
383
- // }
221
+ function _fillEncryptedValues_Q() internal {
222
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
223
+ .layout();
224
+ for (uint256 i = 0; i < $.dpq.encryptedValues.length; ++i) {
225
+ $.publicInputsBuf[$.piIndex++] = $.dpq.encryptedValues[i];
226
+ }
227
+ }
384
228
 
385
- function calculateHash(
386
- bytes memory ciphertext
387
- ) internal view returns (uint[2] memory) {
388
- bytes memory ret = new bytes(32); // return is a simple 0 or 1
389
- bool ok;
390
- uint256 result;
391
- bytes memory ct = new bytes(768);
392
- for (uint i = 0; i < ct.length; i++) {
393
- ct[i] = ciphertext[i];
229
+ function _fillInputs_Q(uint256[] memory nullifiers) internal {
230
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
231
+ .layout();
232
+ for (uint256 i = 0; i < nullifiers.length; i++) {
233
+ $.publicInputsBuf[$.piIndex++] = nullifiers[i];
394
234
  }
395
- assembly {
396
- ok := staticcall(
397
- gas(),
398
- 0x02,
399
- add(ct, 0x20),
400
- 768,
401
- add(ret, 0x20),
402
- 0x20
403
- )
404
- result := mload(add(ret, 0x20))
235
+ }
236
+
237
+ function _fillRootAndEnables_Q(uint256[] memory nullifiers) internal {
238
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
239
+ .layout();
240
+ $.publicInputsBuf[$.piIndex++] = $.dpq.root;
241
+ for (uint256 i = 0; i < nullifiers.length; i++) {
242
+ $.publicInputsBuf[$.piIndex++] = (nullifiers[i] == 0) ? 0 : 1;
405
243
  }
406
- require(ok, "hash failed");
244
+ }
407
245
 
408
- bytes32 hash = bytes32(result);
409
- uint[2] memory computed_pubSignals;
410
- // Calculate h0: sum of the first 16 bytes
411
- for (uint i = 0; i < 16; i++) {
412
- computed_pubSignals[0] += uint256(uint8(hash[i])) * (1 << (8 * i));
246
+ function _fillOutputs_Q(uint256[] memory outputs) internal {
247
+ ZetoAnonNullifierQurrencyStorage.Layout storage $ = ZetoAnonNullifierQurrencyStorage
248
+ .layout();
249
+ for (uint256 i = 0; i < outputs.length; i++) {
250
+ $.publicInputsBuf[$.piIndex++] = outputs[i];
413
251
  }
414
- // Calculate h1: sum of the next 16 bytes
415
- for (uint i = 16; i < 32; i++) {
416
- computed_pubSignals[1] +=
417
- uint256(uint8(hash[i])) *
418
- (1 << (8 * (i - 16)));
252
+ }
253
+ }
254
+
255
+ /// @dev ERC-7201 (`erc7201:zeto.storage.Zeto_AnonNullifierQurrency`).
256
+ library ZetoAnonNullifierQurrencyStorage {
257
+ struct DecodedProofQurrency {
258
+ uint256 root;
259
+ uint256 encryptionNonce;
260
+ uint256[] encryptedValues;
261
+ uint256[25] encapsulatedSharedSecret;
262
+ }
263
+
264
+ struct Layout {
265
+ DecodedProofQurrency dpq;
266
+ uint256[] publicInputsBuf;
267
+ uint256 piIndex;
268
+ }
269
+
270
+ bytes32 private constant STORAGE_LOCATION =
271
+ 0x61ea13f87e8d672de1c36df013aa52a47bae99190f6e222ce275d312c118d800;
272
+
273
+ function layout() internal pure returns (Layout storage $) {
274
+ assembly {
275
+ $.slot := STORAGE_LOCATION
419
276
  }
420
- return computed_pubSignals;
421
277
  }
422
278
  }