@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,61 +15,471 @@
15
15
  // limitations under the License.
16
16
  pragma solidity ^0.8.27;
17
17
 
18
- import {Groth16Verifier_Deposit} from "../verifiers/verifier_deposit.sol";
19
- import {Commonlib} from "./common.sol";
18
+ import {IGroth16Verifier} from "./interfaces/IZetoVerifier.sol";
19
+ import {IZetoInitializable} from "./interfaces/IZetoInitializable.sol";
20
+ import {IZetoLockableCapability} from "./interfaces/IZetoLockableCapability.sol";
21
+ import {Commonlib} from "./common/common.sol";
20
22
  import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
21
- import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
23
+ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
24
+ import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
25
+ import {ZetoLockable} from "./zeto_lockable.sol";
26
+ import {IZetoStorage} from "./interfaces/IZetoStorage.sol";
22
27
 
23
28
  /// @title A sample implementation of a base Zeto fungible token contract
24
29
  /// @author Kaleido, Inc.
25
- /// @dev Defines the verifier library for checking UTXOs against a claimed value.
26
- abstract contract ZetoFungible is OwnableUpgradeable {
27
- // _depositVerifier library for checking UTXOs against a claimed value.
28
- // this can be used in the optional deposit calls to verify that
29
- // the UTXOs match the deposited value
30
- Groth16Verifier_Deposit internal _depositVerifier;
30
+ /// @dev Adds the ERC20-backed deposit/withdraw lifecycle and a multi-input
31
+ /// {transfer} flow on top of the lock-aware {ZetoLockable} base.
32
+ ///
33
+ /// The lock-lifecycle plumbing (state, external entry points, views)
34
+ /// lives in the linked external {ZetoLockableLib}; this contract
35
+ /// supplies only the two ZK-circuit-shaped hooks
36
+ /// ({_doLockTransition} and {_transferLocked}) that pad
37
+ /// inputs/outputs to the next supported batch size before
38
+ /// {constructPublicInputs}.
39
+ ///
40
+ /// Inherits {ReentrancyGuardUpgradeable} so that {deposit} and
41
+ /// {withdraw}, which perform external ERC20 transfers, are
42
+ /// protected from reentrant calls (defense-in-depth on top of the
43
+ /// checks-effects-interactions ordering enforced inside those
44
+ /// functions).
45
+ abstract contract ZetoFungible is ZetoLockable, ReentrancyGuardUpgradeable {
46
+ using SafeERC20 for IERC20;
31
47
 
32
- error WithdrawArrayTooLarge(uint256 maxAllowed);
48
+ /// @dev Emitted when {setERC20} successfully binds the backing
49
+ /// ERC20. Indexed by the new token address so subscribers can
50
+ /// filter.
51
+ event ERC20Set(address indexed erc20);
33
52
 
34
- IERC20 internal _erc20;
53
+ /// @dev Thrown when {setERC20} is called with the zero address.
54
+ error ZeroERC20Address();
35
55
 
56
+ /// @dev Thrown when {setERC20} is called after the backing ERC20
57
+ /// has already been bound. {setERC20} is one-shot: once a
58
+ /// Zeto contract is paired with an ERC20, that pairing is
59
+ /// permanent for the lifetime of the proxy. This protects
60
+ /// depositors from having their backing token rug-pulled out
61
+ /// from under existing UTXOs.
62
+ error ERC20AlreadySet(address current);
63
+
64
+ /// @dev Deposit / withdraw verifiers and ERC20 pairing live in
65
+ /// {ZetoFungibleStorage} (ERC-7201 namespace `zeto.storage.ZetoFungible`).
66
+
67
+ /// @dev Initializer should only ever be called from a derived
68
+ /// contract's own `initializer`-guarded entrypoint, so the
69
+ /// `internal` visibility prevents external poking of the
70
+ /// lifecycle on a partially-initialized proxy.
36
71
  function __ZetoFungible_init(
37
- Groth16Verifier_Deposit depositVerifier
38
- ) public onlyInitializing {
39
- _depositVerifier = depositVerifier;
72
+ string calldata name_,
73
+ string calldata symbol_,
74
+ address initialOwner,
75
+ IZetoInitializable.VerifiersInfo calldata verifiers,
76
+ IZetoStorage storage_
77
+ ) internal onlyInitializing {
78
+ __ZetoCommon_init(name_, symbol_, initialOwner, verifiers, storage_);
79
+ __ReentrancyGuard_init();
80
+ ZetoFungibleStorage.Layout storage $ = ZetoFungibleStorage.layout();
81
+ $.depositVerifier = verifiers.depositVerifier;
82
+ $.withdrawVerifier = verifiers.withdrawVerifier;
83
+ $.batchWithdrawVerifier = verifiers.batchWithdrawVerifier;
40
84
  }
41
85
 
86
+ /**
87
+ * @dev Bind the ERC20 token that this Zeto contract will interact
88
+ * with for {deposit} and {withdraw}. One-shot: a subsequent
89
+ * call reverts with {ERC20AlreadySet}.
90
+ *
91
+ * Rationale: depositors trust that the asset backing their
92
+ * UTXOs is stable for the lifetime of the contract. Letting
93
+ * the owner swap the backing ERC20 after deposits exist would
94
+ * let them silently change what `withdraw` pays out. Making
95
+ * this one-shot removes that vector entirely while still
96
+ * allowing the operator to wire up the pairing post-deployment.
97
+ *
98
+ * @param erc20 The ERC20 token to be used. Must be non-zero.
99
+ *
100
+ * Emits {ERC20Set}.
101
+ */
42
102
  function setERC20(IERC20 erc20) public onlyOwner {
43
- _erc20 = erc20;
103
+ if (address(erc20) == address(0)) {
104
+ revert ZeroERC20Address();
105
+ }
106
+ ZetoFungibleStorage.Layout storage $ = ZetoFungibleStorage.layout();
107
+ if (address($.erc20Token) != address(0)) {
108
+ revert ERC20AlreadySet(address($.erc20Token));
109
+ }
110
+ $.erc20Token = erc20;
111
+ emit ERC20Set(address(erc20));
44
112
  }
45
113
 
46
- function _deposit(
47
- uint256 amount,
48
- uint256[] memory outputs,
49
- Commonlib.Proof calldata proof
114
+ /**
115
+ * @dev the main function of the contract, which transfers values from one account (represented by Babyjubjub public keys)
116
+ * to one or more receiver accounts (also represented by Babyjubjub public keys). One of the two nullifiers may be zero
117
+ * if the transaction only needs one UTXO to be spent. Equally one of the two outputs may be zero if the transaction
118
+ * only needs to create one new UTXO.
119
+ *
120
+ * @param inputs Array of nullifiers that are secretly bound to UTXOs to be spent by the transaction.
121
+ * @param outputs Array of new UTXOs to generate, for future transactions to spend.
122
+ * @param proof A zero knowledge proof that the submitter is authorized to spend the inputs, and
123
+ * that the outputs are valid in terms of obeying mass conservation rules.
124
+ *
125
+ * Emits a {UTXOTransfer} event.
126
+ */
127
+ function transfer(
128
+ uint256[] calldata inputs,
129
+ uint256[] calldata outputs,
130
+ bytes calldata proof,
131
+ bytes calldata data
50
132
  ) public virtual {
133
+ uint256[] memory lockedOutputs;
134
+ validateTransactionProposal(
135
+ inputs,
136
+ outputs,
137
+ lockedOutputs,
138
+ proof,
139
+ false
140
+ );
141
+ // Check and pad commitments
142
+ (
143
+ uint256[] memory paddedInputs,
144
+ uint256[] memory paddedOutputs
145
+ ) = checkAndPadCommitments(inputs, outputs);
146
+ // construct the public inputs for the proof verification
147
+ (
148
+ uint256[] memory publicInputs,
149
+ Commonlib.Proof memory proofStruct
150
+ ) = constructPublicInputs(paddedInputs, paddedOutputs, proof, false);
151
+ bool isBatch = (inputs.length > 2 || outputs.length > 2);
152
+ verifyProof(proofStruct, publicInputs, isBatch, false);
153
+ processInputsAndOutputs(paddedInputs, paddedOutputs, false);
154
+
155
+ emitTransferEvent(inputs, outputs, proof, data);
156
+ }
157
+
158
+ // ------------------------------------------------------------------
159
+ // ZetoLockable hook overrides — fungible flavour pads inputs and
160
+ // outputs to the next supported batch size before constructing the
161
+ // public-inputs array.
162
+ // ------------------------------------------------------------------
163
+
164
+ function _doLockTransition(
165
+ ZetoCreateLockArgs calldata args
166
+ ) internal override {
167
+ validateTransactionProposal(
168
+ args.inputs,
169
+ args.outputs,
170
+ args.lockedOutputs,
171
+ args.proof,
172
+ false
173
+ );
174
+
175
+ // Combine the locked outputs and the regular outputs because the
176
+ // circuits do not differentiate them.
177
+ uint256[] memory allOutputs = new uint256[](
178
+ args.lockedOutputs.length + args.outputs.length
179
+ );
180
+ for (uint256 i = 0; i < args.lockedOutputs.length; i++) {
181
+ allOutputs[i] = args.lockedOutputs[i];
182
+ }
183
+ for (uint256 i = 0; i < args.outputs.length; i++) {
184
+ allOutputs[args.lockedOutputs.length + i] = args.outputs[i];
185
+ }
186
+
187
+ (
188
+ uint256[] memory paddedInputs,
189
+ uint256[] memory paddedOutputs
190
+ ) = checkAndPadCommitments(args.inputs, allOutputs);
191
+
192
+ (
193
+ uint256[] memory publicInputs,
194
+ Commonlib.Proof memory proofStruct
195
+ ) = constructPublicInputs(
196
+ paddedInputs,
197
+ paddedOutputs,
198
+ args.proof,
199
+ false
200
+ );
201
+
202
+ bool isBatch = (paddedInputs.length > 2 ||
203
+ args.outputs.length > 2 ||
204
+ args.lockedOutputs.length > 2);
205
+ verifyProof(proofStruct, publicInputs, isBatch, false);
206
+
207
+ processInputsAndOutputs(paddedInputs, args.outputs, false);
208
+ processLockedOutputs(args.lockedOutputs);
209
+ }
210
+
211
+ function _transferLocked(
212
+ bytes32 /* lockId */,
213
+ uint256[] calldata lockedInputs,
214
+ uint256[] calldata lockedOutputs,
215
+ uint256[] calldata outputs,
216
+ bytes calldata proof,
217
+ bytes calldata /* data */
218
+ ) internal override {
219
+ validateTransactionProposal(
220
+ lockedInputs,
221
+ outputs,
222
+ lockedOutputs,
223
+ proof,
224
+ true
225
+ );
226
+ // combine the locked outputs and the outputs, because the
227
+ // circuits do not care about the difference between locked and
228
+ // unlocked outputs
229
+ uint256[] memory allOutputs = new uint256[](
230
+ lockedOutputs.length + outputs.length
231
+ );
232
+ for (uint256 i = 0; i < lockedOutputs.length; i++) {
233
+ allOutputs[i] = lockedOutputs[i];
234
+ }
235
+ for (uint256 i = 0; i < outputs.length; i++) {
236
+ allOutputs[lockedOutputs.length + i] = outputs[i];
237
+ }
238
+ // Check and pad inputs and outputs based on the max size
239
+ (
240
+ uint256[] memory paddedInputs,
241
+ uint256[] memory paddedOutputs
242
+ ) = checkAndPadCommitments(lockedInputs, allOutputs);
243
+ // construct the public inputs for the proof verification
244
+ (
245
+ uint256[] memory publicInputs,
246
+ Commonlib.Proof memory proofStruct
247
+ ) = constructPublicInputs(paddedInputs, paddedOutputs, proof, true);
248
+ bool isBatch = (lockedInputs.length > 2 || allOutputs.length > 2);
249
+ verifyProof(proofStruct, publicInputs, isBatch, true);
250
+ processInputsAndOutputs(paddedInputs, paddedOutputs, true);
251
+ processLockedOutputs(lockedOutputs);
252
+ }
253
+
254
+ /**
255
+ * @dev Deposit ERC20 tokens into the Zeto contract.
256
+ *
257
+ * @param amount The amount of ERC20 tokens to be deposited.
258
+ * @param outputs The UTXOs to be minted.
259
+ * @param proof The proof of the deposit.
260
+ * @param data Additional data to be passed to the deposit function.
261
+ *
262
+ * Emits a {UTXOMint} event.
263
+ */
264
+ function deposit(
265
+ uint256 amount,
266
+ uint256[] calldata outputs,
267
+ bytes calldata proof,
268
+ bytes calldata data
269
+ ) public nonReentrant {
270
+ // ---- Checks ----
271
+ validateOutputs(outputs);
272
+
51
273
  // verifies that the output UTXOs match the claimed value
52
274
  // to be deposited
275
+ (
276
+ uint256[] memory publicInputs,
277
+ Commonlib.Proof memory proofStruct
278
+ ) = constructPublicInputsForDeposit(amount, outputs, proof);
279
+ if (
280
+ !ZetoFungibleStorage.layout().depositVerifier.verify(
281
+ proofStruct.pA,
282
+ proofStruct.pB,
283
+ proofStruct.pC,
284
+ publicInputs
285
+ )
286
+ ) {
287
+ revert InvalidProof();
288
+ }
289
+
290
+ // ---- Effects ----
291
+ // Mint the UTXOs (commits the new outputs to storage and emits
292
+ // {UTXOMint}) before the external ERC20 call. This ensures that
293
+ // any reentrant call into this contract triggered by the ERC20
294
+ // transfer observes the new outputs as already-committed and
295
+ // cannot replay them.
296
+ _mint(outputs, data);
297
+
298
+ // ---- Interactions ----
299
+ // SafeERC20 handles non-standard tokens that return no value on
300
+ // success and reverts cleanly when the underlying call fails or
301
+ // returns false.
302
+ ZetoFungibleStorage.layout().erc20Token.safeTransferFrom(
303
+ msg.sender,
304
+ address(this),
305
+ amount
306
+ );
307
+ }
308
+
309
+ /**
310
+ * @dev Withdraw ERC20 tokens from the Zeto contract.
311
+ *
312
+ * @param amount The amount of ERC20 tokens to be withdrawn.
313
+ * @param inputs The UTXOs to be spent.
314
+ * @param output The UTXO to be minted.
315
+ * @param proof The proof of the withdrawal.
316
+ * @param data Additional data to be passed to the withdrawal
317
+ * function.
318
+ *
319
+ * Emits a {UTXOWithdraw} event.
320
+ */
321
+ function withdraw(
322
+ uint256 amount,
323
+ uint256[] calldata inputs,
324
+ uint256 output,
325
+ bytes calldata proof,
326
+ bytes calldata data
327
+ ) public nonReentrant {
328
+ uint256[] memory outputs = new uint256[](1);
329
+ outputs[0] = output;
330
+ uint256[] memory lockedOutputs;
331
+
332
+ // ---- Checks ----
333
+ validateTransactionProposal(
334
+ inputs,
335
+ outputs,
336
+ lockedOutputs,
337
+ proof,
338
+ false
339
+ );
340
+ // Check and pad inputs and outputs based on the max size
341
+ (
342
+ uint256[] memory paddedInputs,
343
+ uint256[] memory paddedOutputs
344
+ ) = checkAndPadCommitments(inputs, outputs);
345
+ (
346
+ uint256[] memory publicInputs,
347
+ Commonlib.Proof memory proofStruct
348
+ ) = constructPublicInputsForWithdraw(
349
+ amount,
350
+ paddedInputs,
351
+ output,
352
+ proof
353
+ );
354
+ ZetoFungibleStorage.Layout storage $ = ZetoFungibleStorage.layout();
355
+ IGroth16Verifier verifier = (inputs.length > 2)
356
+ ? $.batchWithdrawVerifier
357
+ : $.withdrawVerifier;
358
+ if (
359
+ !verifier.verify(
360
+ proofStruct.pA,
361
+ proofStruct.pB,
362
+ proofStruct.pC,
363
+ publicInputs
364
+ )
365
+ ) {
366
+ revert InvalidProof();
367
+ }
368
+
369
+ // ---- Effects ----
370
+ // Mark the input nullifiers as spent and commit the
371
+ // change-output before performing the external ERC20 transfer.
372
+ // Following checks-effects-interactions ensures a callback-style
373
+ // ERC20 cannot re-enter and double-spend the same nullifiers.
374
+ processInputsAndOutputs(paddedInputs, paddedOutputs, false);
375
+
376
+ // ---- Interactions ----
377
+ // SafeERC20 handles non-standard tokens that return no value on
378
+ // success and reverts cleanly when the underlying call fails or
379
+ // returns false.
380
+ ZetoFungibleStorage.layout().erc20Token.safeTransfer(
381
+ msg.sender,
382
+ amount
383
+ );
384
+
385
+ // Emitted after the transfer so that the on-chain event order
386
+ // remains ERC20.Transfer → UTXOWithdraw, matching listeners and
387
+ // tests built before the CEI reorder. Event emission is a pure
388
+ // log and does not affect security; the nullifier state was
389
+ // already committed above.
390
+ emit UTXOWithdraw(amount, inputs, output, msg.sender, data);
391
+ }
392
+
393
+ function emitTransferEvent(
394
+ uint256[] memory inputs,
395
+ uint256[] memory outputs,
396
+ bytes memory proof,
397
+ bytes memory data
398
+ ) internal virtual {
399
+ emit UTXOTransfer(inputs, outputs, msg.sender, data);
400
+ }
401
+
402
+ // this is a utility function that constructs the public inputs for a proof of a deposit() call.
403
+ // specific implementations of this function may be overridden by each token implementation
404
+ function constructPublicInputsForDeposit(
405
+ uint256 amount,
406
+ uint256[] memory outputs,
407
+ bytes memory proof
408
+ ) public virtual returns (uint256[] memory, Commonlib.Proof memory) {
409
+ Commonlib.Proof memory proofStruct = abi.decode(
410
+ proof,
411
+ (Commonlib.Proof)
412
+ );
53
413
  // construct the public inputs
54
- uint256[3] memory publicInputs;
414
+ uint256[] memory extra = extraInputsForDeposit();
415
+ uint256[] memory publicInputs = new uint256[](3 + extra.length);
55
416
  publicInputs[0] = amount;
56
417
  publicInputs[1] = outputs[0];
57
418
  publicInputs[2] = outputs[1];
419
+ for (uint256 i = 0; i < extra.length; i++) {
420
+ publicInputs[3 + i] = extra[i];
421
+ }
58
422
 
59
- // Check the proof
60
- require(
61
- _depositVerifier.verifyProof(
62
- proof.pA,
63
- proof.pB,
64
- proof.pC,
65
- publicInputs
66
- ),
67
- "Invalid proof"
68
- );
423
+ return (publicInputs, proofStruct);
424
+ }
425
+
426
+ function extraInputsForDeposit()
427
+ internal
428
+ view
429
+ virtual
430
+ returns (uint256[] memory)
431
+ {
432
+ return new uint256[](0);
433
+ }
69
434
 
70
- require(
71
- _erc20.transferFrom(msg.sender, address(this), amount),
72
- "Failed to transfer ERC20 tokens"
435
+ // this is a utility function that constructs the public inputs for a proof of a withdraw() call.
436
+ // specific implementations of this function may be overridden by each token implementation
437
+ function constructPublicInputsForWithdraw(
438
+ uint256 amount,
439
+ uint256[] memory inputs,
440
+ uint256 output,
441
+ bytes memory proof
442
+ ) internal virtual returns (uint256[] memory, Commonlib.Proof memory) {
443
+ Commonlib.Proof memory proofStruct = abi.decode(
444
+ proof,
445
+ (Commonlib.Proof)
73
446
  );
447
+ uint256 size = (inputs.length + 1 + 1); // inputs, output, and amount
448
+
449
+ uint256[] memory publicInputs = new uint256[](size);
450
+ uint256 piIndex = 0;
451
+
452
+ // copy output amount
453
+ publicInputs[piIndex++] = amount;
454
+
455
+ // copy input commitments
456
+ for (uint256 i = 0; i < inputs.length; i++) {
457
+ publicInputs[piIndex++] = inputs[i];
458
+ }
459
+
460
+ // copy output commitment
461
+ publicInputs[piIndex++] = output;
462
+
463
+ return (publicInputs, proofStruct);
464
+ }
465
+ }
466
+
467
+ /// @dev ERC-7201 (`erc7201:zeto.storage.ZetoFungible`): deposit/withdraw verifiers
468
+ /// and ERC20 pairing for this abstract contract.
469
+ library ZetoFungibleStorage {
470
+ struct Layout {
471
+ IGroth16Verifier depositVerifier;
472
+ IGroth16Verifier withdrawVerifier;
473
+ IGroth16Verifier batchWithdrawVerifier;
474
+ IERC20 erc20Token;
475
+ }
476
+
477
+ bytes32 private constant STORAGE_LOCATION =
478
+ 0x502fd05a4212f6b786ff372127438db29bf1c676e6268e59ca1feb2fae998d00;
479
+
480
+ function layout() internal pure returns (Layout storage $) {
481
+ assembly {
482
+ $.slot := STORAGE_LOCATION
483
+ }
74
484
  }
75
485
  }
@@ -0,0 +1,69 @@
1
+ // Copyright © 2024 Kaleido, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+ pragma solidity ^0.8.27;
17
+
18
+ import {IZetoInitializable} from "./interfaces/IZetoInitializable.sol";
19
+ import {ZetoFungible} from "./zeto_fungible.sol";
20
+ import {IZetoStorage} from "./interfaces/IZetoStorage.sol";
21
+ import {BaseStorage} from "./storage/base.sol";
22
+
23
+ /// @title A sample base implementation of a Zeto based fungible token contract
24
+ /// WITHOUT nullifiers. Each UTXO's spending status is explicitly tracked
25
+ /// in the {BaseStorage} ledger.
26
+ /// @author Kaleido, Inc.
27
+ /// @dev Sibling of {ZetoFungibleNullifier}. The two differ only in the storage
28
+ /// backend wired into {ZetoFungible}: this contract uses {BaseStorage},
29
+ /// which records `(utxo => UTXOStatus)` for both regular and locked UTXOs;
30
+ /// the nullifier sibling uses {NullifierStorage}, which records spent
31
+ /// nullifiers in a Sparse Merkle Tree.
32
+ ///
33
+ /// The locking lifecycle (createLock / updateLock / delegateLock /
34
+ /// spendLock / cancelLock) is inherited unchanged from {ZetoFungible}
35
+ /// and is therefore identical across both backends. The only contract
36
+ /// that differs between the two siblings is the leaf token (e.g.
37
+ /// Zeto_Anon vs Zeto_AnonNullifier), which supplies the
38
+ /// circuit-specific `constructPublicInputs` implementation.
39
+ ///
40
+ /// Future non-nullifier-only state belongs in {ZetoFungibleBaseStorage}
41
+ /// below — append fields there on upgrade instead of `__gap`.
42
+ abstract contract ZetoFungibleBase is ZetoFungible {
43
+ function __ZetoFungibleBase_init(
44
+ string calldata name_,
45
+ string calldata symbol_,
46
+ address initialOwner,
47
+ IZetoInitializable.VerifiersInfo calldata verifiers
48
+ ) internal onlyInitializing {
49
+ IZetoStorage storage_ = new BaseStorage();
50
+ __ZetoFungible_init(name_, symbol_, initialOwner, verifiers, storage_);
51
+ }
52
+ }
53
+
54
+ /// @dev ERC-7201 (`erc7201:zeto.storage.ZetoFungibleBase`): reserved for
55
+ /// non-nullifier fungible leaf extensions.
56
+ library ZetoFungibleBaseStorage {
57
+ struct Layout {
58
+ uint256 __reserved;
59
+ }
60
+
61
+ bytes32 private constant STORAGE_LOCATION =
62
+ 0x06a87037c58e5288ef9ee2ed0d7efb7c68b30748ac0ed4d5eb8700eb60966f00;
63
+
64
+ function layout() internal pure returns (Layout storage $) {
65
+ assembly {
66
+ $.slot := STORAGE_LOCATION
67
+ }
68
+ }
69
+ }