@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
@@ -1,293 +0,0 @@
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 {IZeto} from "./interfaces/izeto.sol";
19
- import {IZetoLockable} from "./interfaces/izeto_lockable.sol";
20
- import {Commonlib} from "./common.sol";
21
- import {ZetoCommon} from "./zeto_common.sol";
22
-
23
- /// @title A sample base implementation of a Zeto based token contract
24
- /// without using nullifiers. Each UTXO's spending status is explicitly tracked.
25
- /// @author Kaleido, Inc.
26
- /// @dev Implements common functionalities of Zeto based tokens without nullifiers
27
- abstract contract ZetoBase is IZeto, IZetoLockable, ZetoCommon {
28
- enum UTXOStatus {
29
- UNKNOWN, // default value for the empty UTXO slots
30
- UNSPENT,
31
- SPENT
32
- }
33
-
34
- // tracks all the regular (unlocked) UTXOs
35
- mapping(uint256 => UTXOStatus) internal _utxos;
36
- // used for tracking locked UTXOs. multi-step transaction flows that require counterparties
37
- // to upload proofs. To protect the party that uploads their proof first,
38
- // and prevent any other party from utilizing the uploaded proof to execute
39
- // a transaction, the input UTXOs or nullifiers can be locked and only usable
40
- // by the same party that did the locking.
41
- mapping(uint256 => UTXOStatus) internal _lockedUtxos;
42
- mapping(uint256 => address) internal delegates;
43
-
44
- function __ZetoBase_init(
45
- string memory name_,
46
- string memory symbol_,
47
- address initialOwner
48
- ) internal onlyInitializing {
49
- __ZetoCommon_init(name_, symbol_, initialOwner);
50
- }
51
-
52
- /// @dev query whether a UTXO is currently spent
53
- /// @return bool whether the UTXO is spent
54
- function spent(uint256 txo) public view returns (bool) {
55
- return
56
- _utxos[txo] == UTXOStatus.SPENT ||
57
- _lockedUtxos[txo] == UTXOStatus.SPENT;
58
- }
59
-
60
- function validateTransactionProposal(
61
- uint256[] memory inputs,
62
- uint256[] memory outputs,
63
- uint256[] memory lockedOutputs,
64
- bool inputsLocked
65
- ) internal view returns (bool) {
66
- uint256[] memory allOutputs = new uint256[](
67
- outputs.length + lockedOutputs.length
68
- );
69
- for (uint256 i = 0; i < outputs.length; i++) {
70
- allOutputs[i] = outputs[i];
71
- }
72
- for (uint256 i = 0; i < lockedOutputs.length; i++) {
73
- allOutputs[outputs.length + i] = lockedOutputs[i];
74
- }
75
- validateInputs(inputs, inputsLocked);
76
- validateOutputs(allOutputs);
77
-
78
- return true;
79
- }
80
-
81
- function validateInputs(
82
- uint256[] memory inputs,
83
- bool inputsLocked
84
- ) internal view {
85
- // sort the inputs to detect duplicates
86
- uint256[] memory sortedInputs = sortCommitments(inputs);
87
- // Check the inputs are all unspent
88
- for (uint256 i = 0; i < sortedInputs.length; ++i) {
89
- if (sortedInputs[i] == 0) {
90
- // skip the zero inputs
91
- continue;
92
- }
93
- if (i > 0 && sortedInputs[i] == sortedInputs[i - 1]) {
94
- revert UTXODuplicate(sortedInputs[i]);
95
- }
96
- if (
97
- _lockedUtxos[sortedInputs[i]] == UTXOStatus.UNKNOWN &&
98
- _utxos[sortedInputs[i]] == UTXOStatus.UNKNOWN
99
- ) {
100
- revert UTXONotMinted(sortedInputs[i]);
101
- }
102
- if (
103
- _lockedUtxos[sortedInputs[i]] == UTXOStatus.SPENT ||
104
- _utxos[sortedInputs[i]] == UTXOStatus.SPENT
105
- ) {
106
- revert UTXOAlreadySpent(sortedInputs[i]);
107
- }
108
- if (
109
- !inputsLocked &&
110
- _lockedUtxos[sortedInputs[i]] == UTXOStatus.UNSPENT
111
- ) {
112
- revert UTXOAlreadyLocked(sortedInputs[i]);
113
- }
114
- if (
115
- inputsLocked &&
116
- delegates[sortedInputs[i]] != msg.sender &&
117
- delegates[sortedInputs[i]] != address(0)
118
- ) {
119
- revert NotLockDelegate(
120
- sortedInputs[i],
121
- delegates[sortedInputs[i]],
122
- msg.sender
123
- );
124
- }
125
- }
126
- }
127
-
128
- function validateOutputs(uint256[] memory outputs) internal view {
129
- // sort the outputs to detect duplicates
130
- uint256[] memory sortedOutputs = sortCommitments(outputs);
131
-
132
- // Check for duplicate outputs
133
- for (uint256 i = 0; i < sortedOutputs.length; ++i) {
134
- if (sortedOutputs[i] == 0) {
135
- // skip the zero outputs
136
- continue;
137
- }
138
- if (i > 0 && sortedOutputs[i] == sortedOutputs[i - 1]) {
139
- revert UTXODuplicate(sortedOutputs[i]);
140
- }
141
- }
142
-
143
- // check the outputs are all new - looking in the locked and unlocked UTXOs
144
- for (uint256 i = 0; i < outputs.length; ++i) {
145
- if (
146
- _utxos[outputs[i]] == UTXOStatus.SPENT ||
147
- _lockedUtxos[outputs[i]] == UTXOStatus.SPENT
148
- ) {
149
- revert UTXOAlreadySpent(outputs[i]);
150
- } else if (
151
- _utxos[outputs[i]] == UTXOStatus.UNSPENT ||
152
- _lockedUtxos[outputs[i]] == UTXOStatus.UNSPENT
153
- ) {
154
- revert UTXOAlreadyOwned(outputs[i]);
155
- }
156
- }
157
- }
158
-
159
- function processInputsAndOutputs(
160
- uint256[] memory inputs,
161
- uint256[] memory outputs,
162
- uint256[] memory lockedOutputs,
163
- bool inputsLocked
164
- ) internal {
165
- processInputs(inputs, inputsLocked);
166
- processOutputs(outputs);
167
- processLockedOutputs(lockedOutputs);
168
- }
169
-
170
- function processInputs(
171
- uint256[] memory inputs,
172
- bool inputsLocked
173
- ) internal {
174
- mapping(uint256 => UTXOStatus) storage utxos = inputsLocked
175
- ? _lockedUtxos
176
- : _utxos;
177
- // consume the input UTXOs
178
- for (uint256 i = 0; i < inputs.length; ++i) {
179
- if (inputs[i] != 0) {
180
- utxos[inputs[i]] = UTXOStatus.SPENT;
181
- }
182
- }
183
- }
184
-
185
- function processOutputs(uint256[] memory outputs) internal {
186
- for (uint256 i = 0; i < outputs.length; ++i) {
187
- if (outputs[i] != 0) {
188
- _utxos[outputs[i]] = UTXOStatus.UNSPENT;
189
- }
190
- }
191
- }
192
-
193
- function processLockedOutputs(uint256[] memory lockedOutputs) internal {
194
- for (uint256 i = 0; i < lockedOutputs.length; ++i) {
195
- if (lockedOutputs[i] != 0) {
196
- _lockedUtxos[lockedOutputs[i]] = UTXOStatus.UNSPENT;
197
- }
198
- }
199
- }
200
-
201
- // This function is used to mint new UTXOs, as an example implementation,
202
- // which is only callable by the owner.
203
- function _mint(
204
- uint256[] memory utxos,
205
- bytes calldata data
206
- ) internal virtual {
207
- validateOutputs(utxos);
208
- processOutputs(utxos);
209
- emit UTXOMint(utxos, msg.sender, data);
210
- }
211
-
212
- // The caller function must perform the proof verification
213
- function _burn(
214
- uint256[] memory inputs,
215
- uint256 output,
216
- bytes calldata data
217
- ) internal virtual {
218
- validateInputs(inputs, false);
219
- uint256[] memory outputStates = new uint256[](1);
220
- outputStates[0] = output;
221
- validateOutputs(outputStates);
222
- processInputs(inputs, false);
223
- processOutputs(outputStates);
224
- emit UTXOBurn(inputs, output, msg.sender, data);
225
- }
226
-
227
- // Locks the UTXOs so that they can only be spent by submitting the appropriate
228
- // proof from the Eth account designated as the "delegate". This function
229
- // should be called by a participant, to designate an escrow contract as the delegate,
230
- // which can use uploaded proofs to execute transactions.
231
- function _lock(
232
- uint256[] memory inputs,
233
- uint256[] memory outputs,
234
- uint256[] memory lockedOutputs,
235
- address delegate,
236
- bytes calldata data
237
- ) internal {
238
- for (uint256 i = 0; i < lockedOutputs.length; ++i) {
239
- if (lockedOutputs[i] == 0) {
240
- continue;
241
- }
242
- if (
243
- delegates[lockedOutputs[i]] != address(0) &&
244
- delegates[lockedOutputs[i]] != msg.sender
245
- ) {
246
- revert NotLockDelegate(
247
- lockedOutputs[i],
248
- delegates[lockedOutputs[i]],
249
- msg.sender
250
- );
251
- }
252
- delegates[lockedOutputs[i]] = delegate;
253
- }
254
-
255
- emit UTXOsLocked(
256
- inputs,
257
- outputs,
258
- lockedOutputs,
259
- delegate,
260
- msg.sender,
261
- data
262
- );
263
- }
264
-
265
- // move the ability to spend the locked UTXOs to the delegate account.
266
- // The sender must be the current delegate.
267
- //
268
- // Setting the delegate to address(0) will unlock the UTXOs.
269
- function delegateLock(
270
- uint256[] memory utxos,
271
- address delegate,
272
- bytes calldata data
273
- ) public {
274
- for (uint256 i = 0; i < utxos.length; ++i) {
275
- if (utxos[i] == 0) {
276
- continue;
277
- }
278
- if (delegates[utxos[i]] != msg.sender) {
279
- revert NotLockDelegate(utxos[i], delegate, msg.sender);
280
- }
281
- delegates[utxos[i]] = delegate;
282
- }
283
-
284
- emit LockDelegateChanged(utxos, msg.sender, delegate, data);
285
- }
286
-
287
- function locked(uint256 utxo) public view returns (bool, address) {
288
- if (_lockedUtxos[utxo] == UTXOStatus.UNSPENT) {
289
- return (true, delegates[utxo]);
290
- }
291
- return (false, address(0));
292
- }
293
- }
@@ -1,132 +0,0 @@
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 {Groth16Verifier_Deposit} from "../verifiers/verifier_deposit.sol";
19
- import {Groth16Verifier_Withdraw} from "../verifiers/verifier_withdraw.sol";
20
- import {Groth16Verifier_WithdrawBatch} from "../verifiers/verifier_withdraw_batch.sol";
21
- import {ZetoFungible} from "./zeto_fungible.sol";
22
- import {Commonlib} from "./common.sol";
23
-
24
- uint256 constant WITHDRAW_INPUT_SIZE = 4;
25
- uint256 constant BATCH_WITHDRAW_INPUT_SIZE = 12;
26
-
27
- /// @title A sample implementation of a base Zeto fungible token contract
28
- /// @author Kaleido, Inc.
29
- /// @dev Defines the verifier library for checking UTXOs against a claimed value.
30
- abstract contract ZetoFungibleWithdraw is ZetoFungible {
31
- // nullifierVerifier library for checking nullifiers against a claimed value.
32
- // this can be used in the optional withdraw calls to verify that the nullifiers
33
- // match the withdrawn value
34
- Groth16Verifier_Withdraw internal _withdrawVerifier;
35
- Groth16Verifier_WithdrawBatch internal _batchWithdrawVerifier;
36
-
37
- function __ZetoFungibleWithdraw_init(
38
- Groth16Verifier_Deposit depositVerifier,
39
- Groth16Verifier_Withdraw withdrawVerifier,
40
- Groth16Verifier_WithdrawBatch batchWithdrawVerifier
41
- ) public onlyInitializing {
42
- __ZetoFungible_init(depositVerifier);
43
- _withdrawVerifier = withdrawVerifier;
44
- _batchWithdrawVerifier = batchWithdrawVerifier;
45
- }
46
-
47
- function constructPublicInputs(
48
- uint256 amount,
49
- uint256[] memory inputs,
50
- uint256 output,
51
- uint256 size
52
- ) internal pure returns (uint256[] memory publicInputs) {
53
- publicInputs = new uint256[](size);
54
- uint256 piIndex = 0;
55
-
56
- // copy output amount
57
- publicInputs[piIndex++] = amount;
58
-
59
- // copy input commitments
60
- for (uint256 i = 0; i < inputs.length; i++) {
61
- publicInputs[piIndex++] = inputs[i];
62
- }
63
-
64
- // copy output commitment
65
- publicInputs[piIndex++] = output;
66
-
67
- return publicInputs;
68
- }
69
-
70
- function _withdraw(
71
- uint256 amount,
72
- uint256[] memory inputs,
73
- uint256 output,
74
- Commonlib.Proof calldata proof
75
- ) public virtual {
76
- // Check the proof
77
- if (inputs.length > 2) {
78
- // Check if inputs or outputs exceed batchMax and revert with custom error if necessary
79
- if (inputs.length > BATCH_WITHDRAW_INPUT_SIZE) {
80
- revert WithdrawArrayTooLarge(BATCH_WITHDRAW_INPUT_SIZE);
81
- }
82
- uint256[] memory publicInputs = constructPublicInputs(
83
- amount,
84
- inputs,
85
- output,
86
- BATCH_WITHDRAW_INPUT_SIZE
87
- );
88
- // construct the public inputs for verifier
89
- uint256[BATCH_WITHDRAW_INPUT_SIZE] memory fixedSizeInputs;
90
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
91
- fixedSizeInputs[i] = publicInputs[i];
92
- }
93
- // Check the proof
94
- require(
95
- _batchWithdrawVerifier.verifyProof(
96
- proof.pA,
97
- proof.pB,
98
- proof.pC,
99
- fixedSizeInputs
100
- ),
101
- "Invalid proof"
102
- );
103
- } else {
104
- uint256[] memory publicInputs = constructPublicInputs(
105
- amount,
106
- inputs,
107
- output,
108
- WITHDRAW_INPUT_SIZE
109
- );
110
- // construct the public inputs for verifier
111
- uint256[WITHDRAW_INPUT_SIZE] memory fixedSizeInputs;
112
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
113
- fixedSizeInputs[i] = publicInputs[i];
114
- }
115
- // Check the proof
116
- require(
117
- _withdrawVerifier.verifyProof(
118
- proof.pA,
119
- proof.pB,
120
- proof.pC,
121
- fixedSizeInputs
122
- ),
123
- "Invalid proof"
124
- );
125
- }
126
-
127
- require(
128
- _erc20.transfer(msg.sender, amount),
129
- "Failed to transfer ERC20 tokens"
130
- );
131
- }
132
- }
@@ -1,144 +0,0 @@
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 {Groth16Verifier_Deposit} from "../verifiers/verifier_deposit.sol";
19
- import {Groth16Verifier_WithdrawNullifier} from "../verifiers/verifier_withdraw_nullifier.sol";
20
- import {Groth16Verifier_WithdrawNullifierBatch} from "../verifiers/verifier_withdraw_nullifier_batch.sol";
21
- import {ZetoFungible} from "./zeto_fungible.sol";
22
- import {Commonlib} from "./common.sol";
23
-
24
- uint256 constant WITHDRAW_INPUT_SIZE = 7;
25
- uint256 constant BATCH_WITHDRAW_INPUT_SIZE = 23;
26
-
27
- /// @title A sample implementation of a base Zeto fungible token contract
28
- /// @author Kaleido, Inc.
29
- /// @dev Defines the verifier library for checking UTXOs against a claimed value.
30
- abstract contract ZetoFungibleWithdrawWithNullifiers is ZetoFungible {
31
- // nullifierVerifier library for checking nullifiers against a claimed value.
32
- // this can be used in the optional withdraw calls to verify that the nullifiers
33
- // match the withdrawn value
34
- Groth16Verifier_WithdrawNullifier internal _withdrawVerifier;
35
- Groth16Verifier_WithdrawNullifierBatch internal _batchWithdrawVerifier;
36
-
37
- function __ZetoFungibleWithdrawWithNullifiers_init(
38
- Groth16Verifier_Deposit depositVerifier,
39
- Groth16Verifier_WithdrawNullifier withdrawVerifier,
40
- Groth16Verifier_WithdrawNullifierBatch batchWithdrawVerifier
41
- ) internal onlyInitializing {
42
- __ZetoFungible_init(depositVerifier);
43
- _withdrawVerifier = withdrawVerifier;
44
- _batchWithdrawVerifier = batchWithdrawVerifier;
45
- }
46
-
47
- function constructPublicInputs(
48
- uint256 amount,
49
- uint256[] memory nullifiers,
50
- uint256 output,
51
- uint256 root,
52
- uint256 size
53
- ) internal pure returns (uint256[] memory publicInputs) {
54
- publicInputs = new uint256[](size);
55
- uint256 piIndex = 0;
56
-
57
- // copy output amount
58
- publicInputs[piIndex++] = amount;
59
-
60
- // copy input commitments
61
- for (uint256 i = 0; i < nullifiers.length; i++) {
62
- publicInputs[piIndex++] = nullifiers[i];
63
- }
64
-
65
- // copy root
66
- publicInputs[piIndex++] = root;
67
-
68
- // populate enables
69
- for (uint256 i = 0; i < nullifiers.length; i++) {
70
- publicInputs[piIndex++] = (nullifiers[i] == 0) ? 0 : 1;
71
- }
72
-
73
- // copy output commitment
74
- publicInputs[piIndex++] = output;
75
-
76
- return publicInputs;
77
- }
78
-
79
- function _withdrawWithNullifiers(
80
- uint256 amount,
81
- uint256[] memory nullifiers,
82
- uint256 output,
83
- uint256 root,
84
- Commonlib.Proof calldata proof
85
- ) public virtual {
86
- // Check the proof
87
- if (nullifiers.length > 2) {
88
- // Check if inputs or outputs exceed batchMax and revert with custom error if necessary
89
- if (nullifiers.length > BATCH_WITHDRAW_INPUT_SIZE) {
90
- revert WithdrawArrayTooLarge(BATCH_WITHDRAW_INPUT_SIZE);
91
- }
92
- uint256[] memory publicInputs = constructPublicInputs(
93
- amount,
94
- nullifiers,
95
- output,
96
- root,
97
- BATCH_WITHDRAW_INPUT_SIZE
98
- );
99
- // construct the public inputs for verifier
100
- uint256[BATCH_WITHDRAW_INPUT_SIZE] memory fixedSizeInputs;
101
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
102
- fixedSizeInputs[i] = publicInputs[i];
103
- }
104
- // Check the proof
105
- require(
106
- _batchWithdrawVerifier.verifyProof(
107
- proof.pA,
108
- proof.pB,
109
- proof.pC,
110
- fixedSizeInputs
111
- ),
112
- "Invalid proof"
113
- );
114
- } else {
115
- uint256[] memory publicInputs = constructPublicInputs(
116
- amount,
117
- nullifiers,
118
- output,
119
- root,
120
- WITHDRAW_INPUT_SIZE
121
- );
122
- // construct the public inputs for verifier
123
- uint256[WITHDRAW_INPUT_SIZE] memory fixedSizeInputs;
124
- for (uint256 i = 0; i < fixedSizeInputs.length; i++) {
125
- fixedSizeInputs[i] = publicInputs[i];
126
- }
127
- // Check the proof
128
- require(
129
- _withdrawVerifier.verifyProof(
130
- proof.pA,
131
- proof.pB,
132
- proof.pC,
133
- fixedSizeInputs
134
- ),
135
- "Invalid proof"
136
- );
137
- }
138
-
139
- require(
140
- _erc20.transfer(msg.sender, amount),
141
- "Failed to transfer ERC20 tokens"
142
- );
143
- }
144
- }