@inco/lightning 0.9.0-devnet-test-10 → 0.10.0-devnet-2

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 (89) hide show
  1. package/manifest.yaml +150 -0
  2. package/package.json +8 -8
  3. package/src/CreateXHelper.sol +1 -1
  4. package/src/DeployUtils.sol +22 -28
  5. package/src/IncoLightning.sol +27 -18
  6. package/src/IncoVerifier.sol +1 -1
  7. package/src/Lib.alphanet.sol +294 -13
  8. package/src/Lib.demonet.sol +294 -13
  9. package/src/Lib.devnet.sol +294 -13
  10. package/src/Lib.sol +294 -13
  11. package/src/Lib.template.sol +357 -13
  12. package/src/Lib.testnet.sol +294 -13
  13. package/src/Types.sol +44 -0
  14. package/src/interfaces/IIncoLightning.sol +8 -12
  15. package/src/interfaces/automata-interfaces/BELE.sol +1 -1
  16. package/src/interfaces/automata-interfaces/IPCCSRouter.sol +1 -1
  17. package/src/interfaces/automata-interfaces/IPcsDao.sol +1 -1
  18. package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +1 -1
  19. package/src/interfaces/automata-interfaces/Types.sol +1 -1
  20. package/src/libs/incoLightning_alphanet_v0_297966649.sol +294 -13
  21. package/src/libs/incoLightning_alphanet_v1_725458969.sol +294 -13
  22. package/src/libs/incoLightning_alphanet_v2_976644394.sol +294 -13
  23. package/src/libs/incoLightning_demonet_v0_863421733.sol +294 -13
  24. package/src/libs/incoLightning_demonet_v2_467437523.sol +294 -13
  25. package/src/libs/incoLightning_devnet_v0_340846814.sol +294 -13
  26. package/src/libs/incoLightning_devnet_v10_266391127.sol +1223 -0
  27. package/src/libs/incoLightning_devnet_v1_904635675.sol +294 -13
  28. package/src/libs/incoLightning_devnet_v2_295237520.sol +294 -13
  29. package/src/libs/incoLightning_devnet_v3_976859633.sol +294 -13
  30. package/src/libs/incoLightning_devnet_v4_409204766.sol +294 -13
  31. package/src/libs/incoLightning_devnet_v5_203964628.sol +1223 -0
  32. package/src/libs/incoLightning_devnet_v6_281949651.sol +1223 -0
  33. package/src/libs/incoLightning_devnet_v7_24560427.sol +1223 -0
  34. package/src/libs/incoLightning_devnet_v8_985328058.sol +1223 -0
  35. package/src/libs/incoLightning_devnet_v9_269218568.sol +1223 -0
  36. package/src/libs/incoLightning_testnet_v0_183408998.sol +294 -13
  37. package/src/libs/incoLightning_testnet_v2_889158349.sol +294 -13
  38. package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +8 -3
  39. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +0 -16
  40. package/src/lightning-parts/AccessControl/interfaces/IAdvancedAccessControl.sol +1 -1
  41. package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +3 -2
  42. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +18 -1
  43. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +0 -43
  44. package/src/lightning-parts/DecryptionAttester.sol +124 -2
  45. package/src/lightning-parts/DecryptionAttester.types.sol +20 -0
  46. package/src/lightning-parts/EList.sol +397 -0
  47. package/src/lightning-parts/EncryptedInput.sol +78 -8
  48. package/src/lightning-parts/EncryptedOperations.sol +40 -34
  49. package/src/lightning-parts/Fee.sol +29 -0
  50. package/src/lightning-parts/TEELifecycle.sol +38 -30
  51. package/src/lightning-parts/TEELifecycle.types.sol +1 -1
  52. package/src/lightning-parts/TrivialEncryption.sol +1 -2
  53. package/src/lightning-parts/interfaces/IDecryptionAttester.sol +16 -1
  54. package/src/lightning-parts/interfaces/IEList.sol +38 -0
  55. package/src/lightning-parts/interfaces/IEncryptedInput.sol +9 -1
  56. package/src/lightning-parts/interfaces/IEncryptedOperations.sol +3 -2
  57. package/src/lightning-parts/interfaces/ITEELifecycle.sol +1 -1
  58. package/src/lightning-parts/interfaces/ITrivialEncryption.sol +3 -1
  59. package/src/lightning-parts/primitives/EListHandleGeneration.sol +66 -0
  60. package/src/lightning-parts/primitives/EListHandleMetadata.sol +67 -0
  61. package/src/lightning-parts/primitives/HandleGeneration.sol +31 -8
  62. package/src/lightning-parts/primitives/HandleMetadata.sol +10 -3
  63. package/src/lightning-parts/primitives/interfaces/IEListHandleMetadata.sol +8 -0
  64. package/src/lightning-parts/primitives/test/SignatureVerifier.t.sol +1 -1
  65. package/src/lightning-parts/test/Elist.t.sol +218 -0
  66. package/src/lightning-parts/test/HandleMetadata.t.sol +66 -23
  67. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +41 -13
  68. package/src/misc/ABIHelper.sol +15 -0
  69. package/src/pasted-dependencies/ICreateX.sol +1 -1
  70. package/src/periphery/IncoUtils.sol +1 -1
  71. package/src/periphery/SessionVerifier.sol +4 -4
  72. package/src/shared/IOwnable.sol +1 -1
  73. package/src/shared/IUUPSUpgradable.sol +1 -1
  74. package/src/shared/TestUtils.sol +8 -1
  75. package/src/test/EListTester.sol +171 -0
  76. package/src/test/FakeIncoInfra/FakeComputeServer.sol +2 -2
  77. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +3 -3
  78. package/src/test/FakeIncoInfra/MockOpHandler.sol +7 -9
  79. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +2 -1
  80. package/src/test/FakeIncoInfra/getOpForSelector.sol +0 -2
  81. package/src/test/IncoTest.sol +17 -5
  82. package/src/test/OpsTest.sol +3 -2
  83. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +85 -57
  84. package/src/test/TestDeploy.t.sol +73 -1
  85. package/src/test/TestFakeInfra.t.sol +32 -6
  86. package/src/test/TestLib.t.sol +986 -19
  87. package/src/test/TestReceive.t.sol +42 -0
  88. package/src/test/TestUpgrade.t.sol +34 -63
  89. package/src/version/IncoLightningConfig.sol +1 -1
@@ -0,0 +1,1223 @@
1
+ // AUTOGENERATED FILE. DO NOT EDIT.
2
+ // This file was generated by the IncoLightning library generator.
3
+ // The original template is located at Lib.template.sol
4
+
5
+ /// SPDX-License-Identifier: No License
6
+ pragma solidity ^0.8;
7
+
8
+ import { IncoLightning } from "../IncoLightning.sol";
9
+ import { ebool, euint256, eaddress, ETypes, elist, IndexOutOfRange, InvalidRange, SliceOutOfRange, UnsupportedListType, HandleMismatch, InvalidTEEAttestation, UnexpectedDecryptedValue } from "../Types.sol";
10
+ import { asBool } from "../shared/TypeUtils.sol";
11
+ import { DecryptionAttestation, ElementAttestationWithProof } from "../lightning-parts/DecryptionAttester.types.sol";
12
+
13
+ IncoLightning constant inco = IncoLightning(payable(0x43119Ad1F673E998CE4f3BB305bB92Bd5ab97E3B));
14
+ address constant deployedBy = 0x8202D2D747784Cb7D48868E44C42C4bf162a70BC;
15
+
16
+ /// @notice Returns the ETypes enum value encoded in a handle
17
+ /// @param handle The handle to decode
18
+ /// @return The ETypes value
19
+ function typeOf(bytes32 handle) pure returns (ETypes) {
20
+ return ETypes(uint8(uint256(handle) >> 8));
21
+ }
22
+
23
+ library e {
24
+ error CallFailedAfterFeeRefresh();
25
+
26
+ /// @dev slot to store the fee for inco operations
27
+ bytes32 private constant FEE_SLOT = keccak256("inco.fee");
28
+
29
+ /// @notice Returns a sanitized euint256, replacing zero with asEuint256(0)
30
+ /// @param a The euint256 to sanitize
31
+ /// @return The sanitized euint256
32
+ function sanitize(euint256 a) internal returns (euint256) {
33
+ if (euint256.unwrap(a) == bytes32(0)) {
34
+ return asEuint256(0);
35
+ }
36
+ return a;
37
+ }
38
+
39
+ /// @notice Returns a sanitized ebool, replacing zero with asEbool(false)
40
+ /// @param a The ebool to sanitize
41
+ /// @return The sanitized ebool
42
+ function sanitize(ebool a) internal returns (ebool) {
43
+ if (ebool.unwrap(a) == bytes32(0)) {
44
+ return asEbool(false);
45
+ }
46
+ return a;
47
+ }
48
+
49
+ /// @notice Returns a sanitized eaddress, replacing zero with asEaddress(address(0))
50
+ /// @param a The eaddress to sanitize
51
+ /// @return The sanitized eaddress
52
+ function sanitize(eaddress a) internal returns (eaddress) {
53
+ if (eaddress.unwrap(a) == bytes32(0)) {
54
+ return asEaddress(address(0));
55
+ }
56
+ return a;
57
+ }
58
+
59
+ /// @notice Alias for sanitize(euint256)
60
+ /// @param a The euint256 to sanitize
61
+ /// @return The sanitized euint256
62
+ function s(euint256 a) internal returns (euint256) {
63
+ return sanitize(a);
64
+ }
65
+
66
+ /// @notice Alias for sanitize(ebool)
67
+ /// @param a The ebool to sanitize
68
+ /// @return The sanitized ebool
69
+ function s(ebool a) internal returns (ebool) {
70
+ return sanitize(a);
71
+ }
72
+
73
+ /// @notice Alias for sanitize(eaddress)
74
+ /// @param a The eaddress to sanitize
75
+ /// @return The sanitized eaddress
76
+ function s(eaddress a) internal returns (eaddress) {
77
+ return sanitize(a);
78
+ }
79
+
80
+ /// @notice Adds two encrypted uint256 values
81
+ /// @param a First operand
82
+ /// @param b Second operand
83
+ /// @return The encrypted sum
84
+ function add(euint256 a, euint256 b) internal returns (euint256) {
85
+ return inco.eAdd(s(a), s(b));
86
+ }
87
+
88
+ /// @notice Adds an encrypted uint256 and a plaintext uint256
89
+ /// @param a Encrypted operand
90
+ /// @param b Plaintext operand
91
+ /// @return The encrypted sum
92
+ function add(euint256 a, uint256 b) internal returns (euint256) {
93
+ return inco.eAdd(s(a), asEuint256(b));
94
+ }
95
+
96
+ /// @notice Adds a plaintext uint256 and an encrypted uint256
97
+ /// @param a Plaintext operand
98
+ /// @param b Encrypted operand
99
+ /// @return The encrypted sum
100
+ function add(uint256 a, euint256 b) internal returns (euint256) {
101
+ return inco.eAdd(asEuint256(a), s(b));
102
+ }
103
+
104
+ /// @notice Subtracts two encrypted uint256 values
105
+ /// @param a Encrypted minuend
106
+ /// @param b Encrypted subtrahend
107
+ /// @return The encrypted difference
108
+ function sub(euint256 a, euint256 b) internal returns (euint256) {
109
+ return inco.eSub(s(a), s(b));
110
+ }
111
+
112
+ /// @notice Subtracts a plaintext uint256 from an encrypted uint256
113
+ /// @param a Encrypted minuend
114
+ /// @param b Plaintext subtrahend
115
+ /// @return The encrypted difference
116
+ function sub(euint256 a, uint256 b) internal returns (euint256) {
117
+ return inco.eSub(s(a), asEuint256(b));
118
+ }
119
+
120
+ /// @notice Subtracts an encrypted uint256 from a plaintext uint256
121
+ /// @param a Plaintext minuend
122
+ /// @param b Encrypted subtrahend
123
+ /// @return The encrypted difference
124
+ function sub(uint256 a, euint256 b) internal returns (euint256) {
125
+ return inco.eSub(asEuint256(a), s(b));
126
+ }
127
+
128
+ /// @notice Multiplies two encrypted uint256 values
129
+ /// @param a Encrypted operand
130
+ /// @param b Encrypted operand
131
+ /// @return The encrypted product
132
+ function mul(euint256 a, euint256 b) internal returns (euint256) {
133
+ return inco.eMul(s(a), s(b));
134
+ }
135
+
136
+ /// @notice Multiplies an encrypted uint256 and a plaintext uint256
137
+ /// @param a Encrypted operand
138
+ /// @param b Plaintext operand
139
+ /// @return The encrypted product
140
+ function mul(euint256 a, uint256 b) internal returns (euint256) {
141
+ return inco.eMul(s(a), asEuint256(b));
142
+ }
143
+
144
+ /// @notice Multiplies a plaintext uint256 and an encrypted uint256
145
+ /// @param a Plaintext operand
146
+ /// @param b Encrypted operand
147
+ /// @return The encrypted product
148
+ function mul(uint256 a, euint256 b) internal returns (euint256) {
149
+ return inco.eMul(asEuint256(a), s(b));
150
+ }
151
+
152
+ /// @notice Divides two encrypted uint256 values
153
+ /// @param a Encrypted Dividend
154
+ /// @param b Encrypted Divisor
155
+ /// @return The encrypted quotient
156
+ function div(euint256 a, euint256 b) internal returns (euint256) {
157
+ return inco.eDiv(s(a), s(b));
158
+ }
159
+
160
+ /// @notice Divides an encrypted uint256 by a plaintext uint256
161
+ /// @param a Encrypted dividend
162
+ /// @param b Plaintext divisor
163
+ /// @return The encrypted quotient
164
+ function div(euint256 a, uint256 b) internal returns (euint256) {
165
+ return inco.eDiv(s(a), asEuint256(b));
166
+ }
167
+
168
+ /// @notice Divides a plaintext uint256 by an encrypted uint256
169
+ /// @param a Plaintext dividend
170
+ /// @param b Encrypted divisor
171
+ /// @return The encrypted quotient
172
+ function div(uint256 a, euint256 b) internal returns (euint256) {
173
+ return inco.eDiv(asEuint256(a), s(b));
174
+ }
175
+
176
+ /// @notice Remainder of two encrypted uint256 values
177
+ /// @param a Encrypted dividend
178
+ /// @param b Encrypted divisor
179
+ /// @return The encrypted remainder
180
+ function rem(euint256 a, euint256 b) internal returns (euint256) {
181
+ return inco.eRem(s(a), s(b));
182
+ }
183
+
184
+ /// @notice Remainder of an encrypted uint256 and a plaintext uint256
185
+ /// @param a Encrypted dividend
186
+ /// @param b Plaintext divisor
187
+ /// @return The encrypted remainder
188
+ function rem(euint256 a, uint256 b) internal returns (euint256) {
189
+ return inco.eRem(s(a), asEuint256(b));
190
+ }
191
+
192
+ /// @notice Remainder of a plaintext uint256 and an encrypted uint256
193
+ /// @param a Plaintext dividend
194
+ /// @param b Encrypted divisor
195
+ /// @return The encrypted remainder
196
+ function rem(uint256 a, euint256 b) internal returns (euint256) {
197
+ return inco.eRem(asEuint256(a), s(b));
198
+ }
199
+
200
+ /// @notice Bitwise AND of two encrypted uint256 values
201
+ /// @param a Encrypted operand
202
+ /// @param b Encrypted operand
203
+ /// @return The encrypted result
204
+ function and(euint256 a, euint256 b) internal returns (euint256) {
205
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
206
+ }
207
+
208
+ /// @notice Bitwise AND of an encrypted uint256 and a plaintext uint256
209
+ /// @param a Encrypted operand
210
+ /// @param b Plaintext operand
211
+ /// @return The encrypted result
212
+ function and(euint256 a, uint256 b) internal returns (euint256) {
213
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
214
+ }
215
+
216
+ /// @notice Bitwise AND of a plaintext uint256 and an encrypted uint256
217
+ /// @param a Plaintext operand
218
+ /// @param b Encrypted operand
219
+ /// @return The encrypted result
220
+ function and(uint256 a, euint256 b) internal returns (euint256) {
221
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
222
+ }
223
+
224
+ /// @notice Bitwise AND of two encrypted bool values
225
+ /// @param a Encrypted operand
226
+ /// @param b Encrypted operand
227
+ /// @return The encrypted result
228
+ function and(ebool a, ebool b) internal returns (ebool) {
229
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
230
+ }
231
+
232
+ /// @notice Bitwise AND of an encrypted bool and a plaintext bool
233
+ /// @param a Encrypted operand
234
+ /// @param b Plaintext operand
235
+ /// @return The encrypted result
236
+ function and(ebool a, bool b) internal returns (ebool) {
237
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
238
+ }
239
+
240
+ /// @notice Bitwise AND of a plaintext bool and an encrypted bool
241
+ /// @param a Plaintext operand
242
+ /// @param b Encrypted operand
243
+ /// @return The encrypted result
244
+ function and(bool a, ebool b) internal returns (ebool) {
245
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
246
+ }
247
+
248
+ /// @notice Bitwise OR of two encrypted uint256 values
249
+ /// @param a Encrypted operand
250
+ /// @param b Encrypted operand
251
+ /// @return The encrypted result
252
+ function or(euint256 a, euint256 b) internal returns (euint256) {
253
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
254
+ }
255
+
256
+ /// @notice Bitwise OR of an encrypted uint256 and a plaintext uint256
257
+ /// @param a Encrypted operand
258
+ /// @param b Plaintext operand
259
+ /// @return The encrypted result
260
+ function or(euint256 a, uint256 b) internal returns (euint256) {
261
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
262
+ }
263
+
264
+ /// @notice Bitwise OR of a plaintext uint256 and an encrypted uint256
265
+ /// @param a Plaintext operand
266
+ /// @param b Encrypted operand
267
+ /// @return The encrypted result
268
+ function or(uint256 a, euint256 b) internal returns (euint256) {
269
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
270
+ }
271
+
272
+ /// @notice Bitwise OR of two encrypted bool values
273
+ /// @param a Encrypted operand
274
+ /// @param b Encrypted operand
275
+ /// @return The encrypted result
276
+ function or(ebool a, ebool b) internal returns (ebool) {
277
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
278
+ }
279
+
280
+ /// @notice Bitwise OR of an encrypted bool and a plaintext bool
281
+ /// @param a Encrypted operand
282
+ /// @param b Plaintext operand
283
+ /// @return The encrypted result
284
+ function or(ebool a, bool b) internal returns (ebool) {
285
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
286
+ }
287
+
288
+ /// @notice Bitwise OR of a plaintext bool and an encrypted bool
289
+ /// @param a Plaintext operand
290
+ /// @param b Encrypted operand
291
+ /// @return The encrypted result
292
+ function or(bool a, ebool b) internal returns (ebool) {
293
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
294
+ }
295
+
296
+ /// @notice Bitwise XOR of two encrypted uint256 values
297
+ /// @param a Encrypted operand
298
+ /// @param b Encrypted operand
299
+ /// @return The encrypted result
300
+ function xor(euint256 a, euint256 b) internal returns (euint256) {
301
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
302
+ }
303
+
304
+ /// @notice Bitwise XOR of an encrypted uint256 and a plaintext uint256
305
+ /// @param a Encrypted operand
306
+ /// @param b Plaintext operand
307
+ /// @return The encrypted result
308
+ function xor(euint256 a, uint256 b) internal returns (euint256) {
309
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
310
+ }
311
+
312
+ /// @notice Bitwise XOR of a plaintext uint256 and an encrypted uint256
313
+ /// @param a Plaintext operand
314
+ /// @param b Encrypted operand
315
+ /// @return The encrypted result
316
+ function xor(uint256 a, euint256 b) internal returns (euint256) {
317
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
318
+ }
319
+
320
+ /// @notice Bitwise XOR of two encrypted bool values
321
+ /// @param a First operand
322
+ /// @param b Second operand
323
+ /// @return The encrypted result
324
+ function xor(ebool a, ebool b) internal returns (ebool) {
325
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
326
+ }
327
+
328
+ /// @notice Bitwise XOR of an encrypted bool and a plaintext bool
329
+ /// @param a Encrypted operand
330
+ /// @param b Plaintext operand
331
+ /// @return The encrypted result
332
+ function xor(ebool a, bool b) internal returns (ebool) {
333
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
334
+ }
335
+
336
+ /// @notice Bitwise XOR of a plaintext bool and an encrypted bool
337
+ /// @param a Plaintext operand
338
+ /// @param b Encrypted operand
339
+ /// @return The encrypted result
340
+ function xor(bool a, ebool b) internal returns (ebool) {
341
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
342
+ }
343
+
344
+ /// @notice Left shift of two encrypted uint256 values
345
+ /// @param a Encrypted value to shift
346
+ /// @param b Encrypted shift amount
347
+ /// @return The encrypted result
348
+ function shl(euint256 a, euint256 b) internal returns (euint256) {
349
+ return inco.eShl(s(a), s(b));
350
+ }
351
+
352
+ /// @notice Left shift of an encrypted uint256 by a plaintext uint256
353
+ /// @param a Encrypted value to shift
354
+ /// @param b Plaintext shift amount
355
+ /// @return The encrypted result
356
+ function shl(euint256 a, uint256 b) internal returns (euint256) {
357
+ return inco.eShl(s(a), asEuint256(b));
358
+ }
359
+
360
+ /// @notice Left shift of a plaintext uint256 by an encrypted uint256
361
+ /// @param a Plaintext value to shift
362
+ /// @param b Encrypted shift amount
363
+ /// @return The encrypted result
364
+ function shl(uint256 a, euint256 b) internal returns (euint256) {
365
+ return inco.eShl(asEuint256(a), s(b));
366
+ }
367
+
368
+ /// @notice Right shift of two encrypted uint256 values
369
+ /// @param a Encrypted value to shift
370
+ /// @param b Encrypted shift amount
371
+ /// @return The encrypted result
372
+ function shr(euint256 a, euint256 b) internal returns (euint256) {
373
+ return inco.eShr(s(a), s(b));
374
+ }
375
+
376
+ /// @notice Right shift of an encrypted uint256 by a plaintext uint256
377
+ /// @param a Encrypted value to shift
378
+ /// @param b Plaintext shift amount
379
+ /// @return The encrypted result
380
+ function shr(euint256 a, uint256 b) internal returns (euint256) {
381
+ return inco.eShr(s(a), asEuint256(b));
382
+ }
383
+
384
+ /// @notice Right shift of a plaintext uint256 by an encrypted uint256
385
+ /// @param a Plaintext value to shift
386
+ /// @param b Encrypted shift amount
387
+ /// @return The encrypted result
388
+ function shr(uint256 a, euint256 b) internal returns (euint256) {
389
+ return inco.eShr(asEuint256(a), s(b));
390
+ }
391
+
392
+ /// @notice Left rotate of two encrypted uint256 values
393
+ /// @param a Encrypted value to rotate
394
+ /// @param b Encrypted rotate amount
395
+ /// @return The encrypted result
396
+ function rotl(euint256 a, euint256 b) internal returns (euint256) {
397
+ return inco.eRotl(s(a), s(b));
398
+ }
399
+
400
+ /// @notice Left rotate of an encrypted uint256 by a plaintext uint256
401
+ /// @param a Encrypted value to rotate
402
+ /// @param b Plaintext rotate amount
403
+ /// @return The encrypted result
404
+ function rotl(euint256 a, uint256 b) internal returns (euint256) {
405
+ return inco.eRotl(s(a), asEuint256(b));
406
+ }
407
+
408
+ /// @notice Left rotate of a plaintext uint256 by an encrypted uint256
409
+ /// @param a Plaintext value to rotate
410
+ /// @param b Encrypted rotate amount
411
+ /// @return The encrypted result
412
+ function rotl(uint256 a, euint256 b) internal returns (euint256) {
413
+ return inco.eRotl(asEuint256(a), s(b));
414
+ }
415
+
416
+ /// @notice Right rotate of two encrypted uint256 values
417
+ /// @param a Encrypted value to rotate
418
+ /// @param b Encrypted rotate amount
419
+ /// @return The encrypted result
420
+ function rotr(euint256 a, euint256 b) internal returns (euint256) {
421
+ return inco.eRotr(s(a), s(b));
422
+ }
423
+
424
+ /// @notice Right rotate of an encrypted uint256 by a plaintext uint256
425
+ /// @param a Encrypted value to rotate
426
+ /// @param b Plaintext rotate amount
427
+ /// @return The encrypted result
428
+ function rotr(euint256 a, uint256 b) internal returns (euint256) {
429
+ return inco.eRotr(s(a), asEuint256(b));
430
+ }
431
+
432
+ /// @notice Right rotate of a plaintext uint256 by an encrypted uint256
433
+ /// @param a Plaintext value to rotate
434
+ /// @param b Encrypted rotate amount
435
+ /// @return The encrypted result
436
+ function rotr(uint256 a, euint256 b) internal returns (euint256) {
437
+ return inco.eRotr(asEuint256(a), s(b));
438
+ }
439
+
440
+ /// @notice Checks equality of two encrypted uint256 values
441
+ /// @param a Encrypted operand
442
+ /// @param b Encrypted operand
443
+ /// @return The encrypted bool result
444
+ function eq(euint256 a, euint256 b) internal returns (ebool) {
445
+ return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(s(b)));
446
+ }
447
+
448
+ /// @notice Checks equality of an encrypted uint256 and a plaintext uint256
449
+ /// @param a Encrypted operand
450
+ /// @param b Plaintext operand
451
+ /// @return The encrypted bool result
452
+ function eq(euint256 a, uint256 b) internal returns (ebool) {
453
+ return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)));
454
+ }
455
+
456
+ /// @notice Checks equality of a plaintext uint256 and an encrypted uint256
457
+ /// @param a Plaintext operand
458
+ /// @param b Encrypted operand
459
+ /// @return The encrypted bool result
460
+ function eq(uint256 a, euint256 b) internal returns (ebool) {
461
+ return inco.eEq(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)));
462
+ }
463
+
464
+ /// @notice Checks equality of an encrypted address and a plaintext address
465
+ /// @param a Encrypted operand
466
+ /// @param b Plaintext operand
467
+ /// @return The encrypted bool result
468
+ function eq(eaddress a, address b) internal returns (ebool) {
469
+ return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b)));
470
+ }
471
+
472
+ /// @notice Checks equality of two encrypted addresses
473
+ /// @param a Encrypted operand
474
+ /// @param b Encrypted operand
475
+ /// @return The encrypted bool result
476
+ function eq(eaddress a, eaddress b) internal returns (ebool) {
477
+ return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(s(b)));
478
+ }
479
+
480
+ /// @notice Checks equality of a plaintext address and an encrypted address
481
+ /// @param a Plaintext operand
482
+ /// @param b Encrypted operand
483
+ /// @return The encrypted bool result
484
+ function eq(address a, eaddress b) internal returns (ebool) {
485
+ return inco.eEq(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b)));
486
+ }
487
+
488
+ /// @notice Checks inequality of two encrypted uint256 values
489
+ /// @param a Encrypted operand
490
+ /// @param b Encrypted operand
491
+ /// @return The encrypted bool result
492
+ function ne(euint256 a, euint256 b) internal returns (ebool) {
493
+ return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(s(b)));
494
+ }
495
+
496
+ /// @notice Checks inequality of an encrypted uint256 and a plaintext uint256
497
+ /// @param a Encrypted operand
498
+ /// @param b Plaintext operand
499
+ /// @return The encrypted bool result
500
+ function ne(euint256 a, uint256 b) internal returns (ebool) {
501
+ return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)));
502
+ }
503
+
504
+ /// @notice Checks inequality of a plaintext uint256 and an encrypted uint256
505
+ /// @param a Plaintext operand
506
+ /// @param b Encrypted operand
507
+ /// @return The encrypted bool result
508
+ function ne(uint256 a, euint256 b) internal returns (ebool) {
509
+ return inco.eNe(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)));
510
+ }
511
+
512
+ /// @notice Checks inequality of two encrypted addresses
513
+ /// @param a Encrypted operand
514
+ /// @param b Encrypted operand
515
+ /// @return The encrypted bool result
516
+ function ne(eaddress a, eaddress b) internal returns (ebool) {
517
+ return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(s(b)));
518
+ }
519
+
520
+ /// @notice Checks inequality of an encrypted address and a plaintext address
521
+ /// @param a Encrypted operand
522
+ /// @param b Plaintext operand
523
+ /// @return The encrypted bool result
524
+ function ne(eaddress a, address b) internal returns (ebool) {
525
+ return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b)));
526
+ }
527
+
528
+ /// @notice Checks inequality of a plaintext address and an encrypted address
529
+ /// @param a Plaintext operand
530
+ /// @param b Encrypted operand
531
+ /// @return The encrypted bool result
532
+ function ne(address a, eaddress b) internal returns (ebool) {
533
+ return inco.eNe(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b)));
534
+ }
535
+
536
+ /// @notice Checks if one encrypted uint256 is greater than or equal to another
537
+ /// @param a Encrypted operand
538
+ /// @param b Encrypted operand
539
+ /// @return The encrypted bool result
540
+ function ge(euint256 a, euint256 b) internal returns (ebool) {
541
+ return inco.eGe(s(a), s(b));
542
+ }
543
+
544
+ /// @notice Checks if an encrypted uint256 is greater than or equal to a plaintext uint256
545
+ /// @param a Encrypted operand
546
+ /// @param b Plaintext operand
547
+ /// @return The encrypted bool result
548
+ function ge(euint256 a, uint256 b) internal returns (ebool) {
549
+ return inco.eGe(s(a), asEuint256(b));
550
+ }
551
+
552
+ /// @notice Checks if a plaintext uint256 is greater than or equal to an encrypted uint256
553
+ /// @param a Plaintext operand
554
+ /// @param b Encrypted operand
555
+ /// @return The encrypted bool result
556
+ function ge(uint256 a, euint256 b) internal returns (ebool) {
557
+ return inco.eGe(asEuint256(a), s(b));
558
+ }
559
+
560
+ /// @notice Checks if one encrypted uint256 is greater than another
561
+ /// @param a Encrypted operand
562
+ /// @param b Encrypted operand
563
+ /// @return The encrypted bool result
564
+ function gt(euint256 a, euint256 b) internal returns (ebool) {
565
+ return inco.eGt(s(a), s(b));
566
+ }
567
+
568
+ /// @notice Checks if an encrypted uint256 is greater than a plaintext uint256
569
+ /// @param a Encrypted operand
570
+ /// @param b Plaintext operand
571
+ /// @return The encrypted bool result
572
+ function gt(euint256 a, uint256 b) internal returns (ebool) {
573
+ return inco.eGt(s(a), asEuint256(b));
574
+ }
575
+
576
+ /// @notice Checks if a plaintext uint256 is greater than an encrypted uint256
577
+ /// @param a Plaintext operand
578
+ /// @param b Encrypted operand
579
+ /// @return The encrypted bool result
580
+ function gt(uint256 a, euint256 b) internal returns (ebool) {
581
+ return inco.eGt(asEuint256(a), s(b));
582
+ }
583
+
584
+ /// @notice Checks if one encrypted uint256 is less than or equal to another
585
+ /// @param a Encrypted operand
586
+ /// @param b Encrypted operand
587
+ /// @return The encrypted bool result
588
+ function le(euint256 a, euint256 b) internal returns (ebool) {
589
+ return inco.eLe(s(a), s(b));
590
+ }
591
+
592
+ /// @notice Checks if an encrypted uint256 is less than or equal to a plaintext uint256
593
+ /// @param a Encrypted operand
594
+ /// @param b Plaintext operand
595
+ /// @return The encrypted bool result
596
+ function le(euint256 a, uint256 b) internal returns (ebool) {
597
+ return inco.eLe(s(a), asEuint256(b));
598
+ }
599
+
600
+ /// @notice Checks if a plaintext uint256 is less than or equal to an encrypted uint256
601
+ /// @param a Plaintext operand
602
+ /// @param b Encrypted operand
603
+ /// @return The encrypted bool result
604
+ function le(uint256 a, euint256 b) internal returns (ebool) {
605
+ return inco.eLe(asEuint256(a), s(b));
606
+ }
607
+
608
+ /// @notice Checks if one encrypted uint256 is less than another
609
+ /// @param a Encrypted operand
610
+ /// @param b Encrypted operand
611
+ /// @return The encrypted bool result
612
+ function lt(euint256 a, euint256 b) internal returns (ebool) {
613
+ return inco.eLt(s(a), s(b));
614
+ }
615
+
616
+ /// @notice Checks if an encrypted uint256 is less than a plaintext uint256
617
+ /// @param a Encrypted operand
618
+ /// @param b Plaintext operand
619
+ /// @return The encrypted bool result
620
+ function lt(euint256 a, uint256 b) internal returns (ebool) {
621
+ return inco.eLt(s(a), asEuint256(b));
622
+ }
623
+
624
+ /// @notice Checks if a plaintext uint256 is less than an encrypted uint256
625
+ /// @param a Plaintext operand
626
+ /// @param b Encrypted operand
627
+ /// @return The encrypted bool result
628
+ function lt(uint256 a, euint256 b) internal returns (ebool) {
629
+ return inco.eLt(asEuint256(a), s(b));
630
+ }
631
+
632
+ /// @notice Returns the minimum of two encrypted uint256 values
633
+ /// @param a Encrypted operand
634
+ /// @param b Encrypted operand
635
+ /// @return The encrypted minimum
636
+ function min(euint256 a, euint256 b) internal returns (euint256) {
637
+ return inco.eMin(s(a), s(b));
638
+ }
639
+
640
+ /// @notice Returns the minimum of an encrypted uint256 and a plaintext uint256
641
+ /// @param a Encrypted operand
642
+ /// @param b Plaintext operand
643
+ /// @return The encrypted minimum
644
+ function min(euint256 a, uint256 b) internal returns (euint256) {
645
+ return inco.eMin(s(a), asEuint256(b));
646
+ }
647
+
648
+ /// @notice Returns the minimum of a plaintext uint256 and an encrypted uint256
649
+ /// @param a Plaintext operand
650
+ /// @param b Encrypted operand
651
+ /// @return The encrypted minimum
652
+ function min(uint256 a, euint256 b) internal returns (euint256) {
653
+ return inco.eMin(asEuint256(a), s(b));
654
+ }
655
+
656
+ /// @notice Returns the maximum of two encrypted uint256 values
657
+ /// @param a Encrypted operand
658
+ /// @param b Encrypted operand
659
+ /// @return The encrypted maximum
660
+ function max(euint256 a, euint256 b) internal returns (euint256) {
661
+ return inco.eMax(s(a), s(b));
662
+ }
663
+
664
+ /// @notice Returns the maximum of an encrypted uint256 and a plaintext uint256
665
+ /// @param a Encrypted operand
666
+ /// @param b Plaintext operand
667
+ /// @return The encrypted maximum
668
+ function max(euint256 a, uint256 b) internal returns (euint256) {
669
+ return inco.eMax(s(a), asEuint256(b));
670
+ }
671
+
672
+ /// @notice Returns the maximum of a plaintext uint256 and an encrypted uint256
673
+ /// @param a Plaintext operand
674
+ /// @param b Encrypted operand
675
+ /// @return The encrypted maximum
676
+ function max(uint256 a, euint256 b) internal returns (euint256) {
677
+ return inco.eMax(asEuint256(a), s(b));
678
+ }
679
+
680
+ /// @notice Bitwise NOT of an encrypted bool
681
+ /// @param a Encrypted operand
682
+ /// @return The encrypted result
683
+ function not(ebool a) internal returns (ebool) {
684
+ return inco.eNot(s(a));
685
+ }
686
+
687
+ /// @notice Generates a random encrypted uint256 (costs the inco fee)
688
+ /// @dev costs the inco fee
689
+ /// @return The encrypted random value
690
+ function rand() internal returns (euint256) {
691
+ bytes32 boundHandle = euint256.unwrap(asEuint256(0));
692
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
693
+ return euint256.wrap(result);
694
+ }
695
+
696
+ /// @notice Generates a random encrypted uint256 less than a plaintext upper bound (costs the inco fee)
697
+ /// @dev costs the inco fee
698
+ /// @param upperBound The plaintext upper bound (exclusive). If the upper bound is 0, the whole 256bit range is sampled, equivalent to providing type(uint256).max as upper bound.
699
+ /// @return The encrypted random value
700
+ function randBounded(uint256 upperBound) internal returns (euint256) {
701
+ bytes32 boundHandle = euint256.unwrap(asEuint256(upperBound));
702
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
703
+ return euint256.wrap(result);
704
+ }
705
+
706
+ /// @notice Generates a random encrypted uint256 less than an encrypted upper bound (costs the inco fee)
707
+ /// @dev costs the inco fee
708
+ /// @param upperBound The encrypted upper bound (exclusive). If the upper bound is e(0), the whole 256bit range is sampled, equivalent to providing e(type(uint256).max) as upper bound.
709
+ /// @return The encrypted random value
710
+ function randBounded(euint256 upperBound) internal returns (euint256) {
711
+ bytes32 boundHandle = euint256.unwrap(s(upperBound));
712
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
713
+ return euint256.wrap(result);
714
+ }
715
+
716
+ /// @notice Casts a plaintext uint256 to an encrypted uint256
717
+ /// @param a The plaintext value
718
+ /// @return The encrypted value
719
+ function asEuint256(uint256 a) internal returns (euint256) {
720
+ return inco.asEuint256(a);
721
+ }
722
+
723
+ /// @notice Casts a plaintext bool to an encrypted bool
724
+ /// @param a The plaintext value
725
+ /// @return The encrypted value
726
+ function asEbool(bool a) internal returns (ebool) {
727
+ return inco.asEbool(a);
728
+ }
729
+
730
+ /// @notice Casts a plaintext address to an encrypted address
731
+ /// @param a The plaintext value
732
+ /// @return The encrypted value
733
+ function asEaddress(address a) internal returns (eaddress) {
734
+ return inco.asEaddress(a);
735
+ }
736
+
737
+ /// @notice Casts an encrypted bool to an encrypted uint256
738
+ /// @param a The encrypted bool value
739
+ /// @return The encrypted uint256 value
740
+ function asEuint256(ebool a) internal returns (euint256) {
741
+ return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
742
+ }
743
+
744
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
745
+ /// @dev costs the inco fee
746
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
747
+ return newEuint256(ciphertext, msg.sender);
748
+ }
749
+
750
+ /// @notice Creates a new encrypted uint256 for the given user.
751
+ /// @dev costs the inco fee
752
+ function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
753
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEuint256.selector, ciphertext, user));
754
+ return euint256.wrap(result);
755
+ }
756
+
757
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
758
+ /// @dev costs the inco fee
759
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
760
+ return newEbool(ciphertext, msg.sender);
761
+ }
762
+
763
+ /// @notice Creates a new encrypted bool for the given user.
764
+ /// @dev costs the inco fee
765
+ function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
766
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEbool.selector, ciphertext, user));
767
+ return ebool.wrap(result);
768
+ }
769
+
770
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
771
+ /// @dev costs the inco fee
772
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
773
+ return newEaddress(ciphertext, msg.sender);
774
+ }
775
+
776
+ /// @notice Creates a new encrypted address for the given user.
777
+ /// @dev costs the inco fee
778
+ function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
779
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEaddress.selector, ciphertext, user));
780
+ return eaddress.wrap(result);
781
+ }
782
+
783
+ /// @notice Allows an address to access an encrypted uint256
784
+ /// @param a The encrypted uint256
785
+ /// @param to The address to allow
786
+ function allow(euint256 a, address to) internal {
787
+ inco.allow(euint256.unwrap(a), to);
788
+ }
789
+
790
+ /// @notice Allows an address to access an encrypted bool
791
+ /// @param a The encrypted bool
792
+ /// @param to The address to allow
793
+ function allow(ebool a, address to) internal {
794
+ inco.allow(ebool.unwrap(a), to);
795
+ }
796
+
797
+ /// @notice Allows an address to access an encrypted address
798
+ /// @param a The encrypted address
799
+ /// @param to The address to allow
800
+ function allow(eaddress a, address to) internal {
801
+ inco.allow(eaddress.unwrap(a), to);
802
+ }
803
+
804
+ /// @notice Reveals an encrypted uint256
805
+ /// @param a The encrypted uint256
806
+ function reveal(euint256 a) internal {
807
+ inco.reveal(euint256.unwrap(a));
808
+ }
809
+
810
+ /// @notice Reveals an encrypted bool
811
+ /// @param a The encrypted bool
812
+ function reveal(ebool a) internal {
813
+ inco.reveal(ebool.unwrap(a));
814
+ }
815
+
816
+ /// @notice Reveals an encrypted address
817
+ /// @param a The encrypted address
818
+ function reveal(eaddress a) internal {
819
+ inco.reveal(eaddress.unwrap(a));
820
+ }
821
+
822
+ /// @notice Verifies a decryption attestation for a euint256
823
+ /// @param handle The encrypted handle
824
+ /// @param value The claimed decrypted value
825
+ /// @param signatures The covalidator signatures
826
+ /// @return True if the attestation is valid
827
+ function verifyDecryption(euint256 handle, uint256 value, bytes[] memory signatures) internal view returns (bool) {
828
+ DecryptionAttestation memory attestation = DecryptionAttestation({handle: euint256.unwrap(handle), value: bytes32(value)});
829
+ return inco.incoVerifier().isValidDecryptionAttestation(attestation, signatures);
830
+ }
831
+
832
+ /// @notice Verifies a decryption attestation for an ebool
833
+ /// @param handle The encrypted handle
834
+ /// @param value The claimed decrypted value
835
+ /// @param signatures The covalidator signatures
836
+ /// @return True if the attestation is valid
837
+ function verifyDecryption(ebool handle, bool value, bytes[] memory signatures) internal view returns (bool) {
838
+ DecryptionAttestation memory attestation = DecryptionAttestation({handle: ebool.unwrap(handle), value: value ? bytes32(uint256(1)) : bytes32(0)});
839
+ return inco.incoVerifier().isValidDecryptionAttestation(attestation, signatures);
840
+ }
841
+
842
+ /// @notice Allows this contract to access an encrypted uint256
843
+ /// @param a The encrypted uint256
844
+ function allowThis(euint256 a) internal {
845
+ allow(a, address(this));
846
+ }
847
+
848
+ /// @notice Allows this contract to access an encrypted bool
849
+ /// @param a The encrypted bool
850
+ function allowThis(ebool a) internal {
851
+ allow(a, address(this));
852
+ }
853
+
854
+ /// @notice Allows this contract to access an encrypted address
855
+ /// @param a The encrypted address
856
+ function allowThis(eaddress a) internal {
857
+ allow(a, address(this));
858
+ }
859
+
860
+ /// @notice Checks if a user is allowed to access an encrypted uint256
861
+ /// @param user The address to check
862
+ /// @param a The encrypted uint256
863
+ /// @return True if allowed, false otherwise
864
+ function isAllowed(address user, euint256 a) internal view returns (bool) {
865
+ return inco.isAllowed(euint256.unwrap(a), user);
866
+ }
867
+
868
+ /// @notice Selects between two encrypted uint256 values based on an encrypted bool
869
+ /// @param control The encrypted bool condition
870
+ /// @param ifTrue Value if control is true
871
+ /// @param ifFalse Value if control is false
872
+ /// @return The selected encrypted uint256
873
+ function select(ebool control, euint256 ifTrue, euint256 ifFalse) internal returns (euint256) {
874
+ return euint256.wrap(inco.eIfThenElse(s(control), euint256.unwrap(s(ifTrue)), euint256.unwrap(s(ifFalse))));
875
+ }
876
+
877
+ /// @notice Selects between two encrypted bool values based on an encrypted bool
878
+ /// @param control The encrypted bool condition
879
+ /// @param ifTrue Value if control is true
880
+ /// @param ifFalse Value if control is false
881
+ /// @return The selected encrypted bool
882
+ function select(ebool control, ebool ifTrue, ebool ifFalse) internal returns (ebool) {
883
+ return ebool.wrap(inco.eIfThenElse(s(control), ebool.unwrap(s(ifTrue)), ebool.unwrap(s(ifFalse))));
884
+ }
885
+
886
+ /// @notice Selects between two encrypted addresses based on an encrypted bool
887
+ /// @param control The encrypted bool condition
888
+ /// @param ifTrue Value if control is true
889
+ /// @param ifFalse Value if control is false
890
+ /// @return The selected encrypted address
891
+ function select(ebool control, eaddress ifTrue, eaddress ifFalse) internal returns (eaddress) {
892
+ return eaddress.wrap(inco.eIfThenElse(s(control), eaddress.unwrap(s(ifTrue)), eaddress.unwrap(s(ifFalse))));
893
+ }
894
+
895
+ /// @dev Store fee in the custom slot
896
+ /// @param _fee The fee to store
897
+ function _setFee(uint256 _fee) private {
898
+ bytes32 slot = FEE_SLOT;
899
+ assembly {
900
+ sstore(slot, _fee)
901
+ }
902
+ }
903
+
904
+ /// @dev Retrieve fee from the custom slot
905
+ /// @return fee The stored fee
906
+ function _getFee() private view returns (uint256 fee) {
907
+ bytes32 slot = FEE_SLOT;
908
+ assembly {
909
+ fee := sload(slot)
910
+ }
911
+ }
912
+
913
+ /// @dev Get current fee with fallback to inco.getFee() if not cached
914
+ function getCurrentFee() private returns (uint256) {
915
+ uint256 cachedFee = _getFee();
916
+ if (cachedFee == 0) {
917
+ cachedFee = inco.getFee();
918
+ _setFee(cachedFee);
919
+ }
920
+ return cachedFee;
921
+ }
922
+
923
+ /// @dev Execute a call to inco with fee, retrying with fresh fee if it fails
924
+ /// @param callData The encoded function call (use abi.encodeWithSelector)
925
+ /// @return result The bytes32 result from the call
926
+ function _callWithFeeRetry(bytes memory callData) private returns (bytes32) {
927
+ uint256 fee = getCurrentFee();
928
+ (bool success, bytes memory result) = address(inco).call{value: fee}(callData);
929
+ if (!success) {
930
+ fee = inco.getFee();
931
+ _setFee(fee);
932
+ (success, result) = address(inco).call{value: fee}(callData);
933
+ require(success, CallFailedAfterFeeRefresh());
934
+ }
935
+ return abi.decode(result, (bytes32));
936
+ }
937
+
938
+ /// @notice Returns the type of the elements contained within the list.
939
+ /// @param handle EList handle to read container type from.
940
+ /// @return ret Type of each element in the list.
941
+ function listTypeOf(elist handle) internal pure returns (ETypes ret) {
942
+ return ETypes(uint8(uint256(elist.unwrap(handle)) >> 16));
943
+ }
944
+
945
+ /// @notice Creates a new empty list. Type must be specified ahead of time and can not be changed.
946
+ /// @param listType Type of each element in the list.
947
+ /// @return ret A new elist handle
948
+ function newEList(ETypes listType) internal returns (elist ret) {
949
+ return inco.newEList(new bytes32[](0), listType);
950
+ }
951
+
952
+ /// @notice Creates a new list from existing array of handles.
953
+ /// @param handles An array of handles to create a new list from
954
+ /// @param listType Type of each element in the list.
955
+ /// @return ret A new elist handle
956
+ function newEList(bytes32[] memory handles, ETypes listType) internal returns (elist ret) {
957
+ return inco.newEList{value: inco.getEListFee(uint16(handles.length), listType)}(handles, listType);
958
+ }
959
+
960
+ /// @notice Takes an array of user encrypted ciphertexts and returns a new elist handle.
961
+ /// @param ciphertexts An array of encrypted user inputs
962
+ /// @param listType Expected type of each element in the list.
963
+ /// @param user Address of the user encrypting the ciphertexts.
964
+ /// @return ret A new elist handle
965
+ /// @dev each ciphertext costs the inco fee
966
+ function newEList(bytes[] memory ciphertexts, ETypes listType, address user) internal returns (elist ret) {
967
+ return inco.newEList{value: inco.getEListFee(uint16(ciphertexts.length), listType)}(ciphertexts, listType, user);
968
+ }
969
+
970
+ /// @notice Appends an ebool element at the end of a list.
971
+ /// @param a An elist handle to append to.
972
+ /// @param b Element value to be appended.
973
+ /// @return ret A new elist handle with length len(a)+1.
974
+ function append(elist a, ebool b) internal returns (elist ret) {
975
+ return inco.listAppend{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, ebool.unwrap(s(b)));
976
+ }
977
+
978
+ /// @notice Appends an euint256 element at the end of a list.
979
+ /// @param a An elist handle to append to.
980
+ /// @param b Element value to be appended.
981
+ /// @return ret A new elist handle with length len(a)+1.
982
+ function append(elist a, euint256 b) internal returns (elist ret) {
983
+ return inco.listAppend{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(b)));
984
+ }
985
+
986
+ /// @notice Replaces an element at some hidden index "i" (ebool variant).
987
+ /// @dev If the encrypted index is out of bounds, the operation is silently ignored
988
+ /// and the returned list is identical to the input. This avoids leaking index validity.
989
+ /// @param a EList handle to modify element in.
990
+ /// @param i Hidden index position of an element to modify.
991
+ /// @param b Element value to be changed.
992
+ /// @return ret A new elist handle
993
+ function set(elist a, euint256 i, ebool b) internal returns (elist ret) {
994
+ return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
995
+ }
996
+
997
+ /// @notice Replaces an element at some hidden index "i" (euint256 variant).
998
+ /// @dev If the encrypted index is out of bounds, the operation is silently ignored
999
+ /// and the returned list is identical to the input. This avoids leaking index validity.
1000
+ /// @param a EList handle to modify element in.
1001
+ /// @param i Hidden index position of an element to modify.
1002
+ /// @param b Element value to be changed.
1003
+ /// @return ret A new elist handle
1004
+ function set(elist a, euint256 i, euint256 b) internal returns (elist ret) {
1005
+ return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
1006
+ }
1007
+
1008
+ /// @notice Replaces an element at plaintext index "i" (ebool variant). Reverts if out of range.
1009
+ /// @param a EList handle to modify element in.
1010
+ /// @param i Index position of element to modify.
1011
+ /// @param b Element value to be changed.
1012
+ /// @return ret A new elist handle
1013
+ function set(elist a, uint16 i, ebool b) internal returns (elist ret) {
1014
+ require(i < length(a), IndexOutOfRange(i, length(a)));
1015
+ return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
1016
+ }
1017
+
1018
+ /// @notice Replaces an element at plaintext index "i" (euint256 variant). Reverts if out of range.
1019
+ /// @param a EList handle to modify element in.
1020
+ /// @param i Index position of element to modify.
1021
+ /// @param b Element value to be changed.
1022
+ /// @return ret A new elist handle
1023
+ function set(elist a, uint16 i, euint256 b) internal returns (elist ret) {
1024
+ require(i < length(a), IndexOutOfRange(i, length(a)));
1025
+ return inco.listSet{value: inco.getEListFee(length(a), listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
1026
+ }
1027
+
1028
+ /// @notice Return hidden element at some hidden position (ebool variant).
1029
+ /// @param a EList handle to get element from.
1030
+ /// @param i Hidden index position to get element at.
1031
+ /// @param defaultValue A default element value returned if index is out of range.
1032
+ /// @return ret A new handle for the element or the defaultValue
1033
+ function getOr(elist a, euint256 i, ebool defaultValue) internal returns (ebool ret) {
1034
+ return ebool.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), ebool.unwrap(s(defaultValue))));
1035
+ }
1036
+
1037
+ /// @notice Return hidden element at some hidden position (euint256 variant).
1038
+ /// @param a EList handle to get element from.
1039
+ /// @param i Hidden index position to get element at.
1040
+ /// @param defaultValue A default element value returned if index is out of range.
1041
+ /// @return ret A new handle for the element or the defaultValue
1042
+ function getOr(elist a, euint256 i, euint256 defaultValue) internal returns (euint256 ret) {
1043
+ return euint256.wrap(inco.listGetOr(a, euint256.unwrap(s(i)), euint256.unwrap(s(defaultValue))));
1044
+ }
1045
+
1046
+ /// @notice Return hidden element at a known position as ebool.
1047
+ /// @param a EList handle to get element from.
1048
+ /// @param i Index position to get element at.
1049
+ /// @return ret Returns a handle for the element.
1050
+ function getEbool(elist a, uint16 i) internal returns (ebool ret) {
1051
+ return ebool.wrap(inco.listGet(a, i));
1052
+ }
1053
+
1054
+ /// @notice Return hidden element at a known position as euint256.
1055
+ /// @param a EList handle to get element from.
1056
+ /// @param i Index position to get element at.
1057
+ /// @return ret Returns a handle for the element.
1058
+ function getEuint256(elist a, uint16 i) internal returns (euint256 ret) {
1059
+ return euint256.wrap(inco.listGet(a, i));
1060
+ }
1061
+
1062
+ /// @notice Inserts a hidden element at a desired hidden position (ebool, hidden index).
1063
+ /// @param a An elist handle to insert into.
1064
+ /// @param i Hidden index position to insert at.
1065
+ /// @param b Element value to be inserted.
1066
+ /// @return ret A new elist handle with length len(a)+1.
1067
+ function insert(elist a, euint256 i, ebool b) internal returns (elist ret) {
1068
+ return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(i)), ebool.unwrap(s(b)));
1069
+ }
1070
+
1071
+ /// @notice Inserts an ebool element at a known position. Reverts if out of range.
1072
+ /// @param a An elist handle to insert into.
1073
+ /// @param i Index position to insert at.
1074
+ /// @param b Element value to be inserted.
1075
+ /// @return ret A new elist handle with length len(a)+1.
1076
+ function insert(elist a, uint16 i, ebool b) internal returns (elist ret) {
1077
+ require(i < length(a), IndexOutOfRange(i, length(a)));
1078
+ return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), ebool.unwrap(s(b)));
1079
+ }
1080
+
1081
+ /// @notice Inserts a hidden element at a desired hidden position (euint256, hidden index).
1082
+ /// @param a An elist handle to insert into.
1083
+ /// @param i Hidden index position to insert at.
1084
+ /// @param b Element value to be inserted.
1085
+ /// @return ret A new elist handle with length len(a)+1.
1086
+ function insert(elist a, euint256 i, euint256 b) internal returns (elist ret) {
1087
+ return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(s(i)), euint256.unwrap(s(b)));
1088
+ }
1089
+
1090
+ /// @notice Inserts a euint256 element at a known position. Reverts if out of range.
1091
+ /// @param a An elist handle to insert into.
1092
+ /// @param i Index position to insert at.
1093
+ /// @param b Element value to be inserted.
1094
+ /// @return ret A new elist handle with length len(a)+1.
1095
+ function insert(elist a, uint16 i, euint256 b) internal returns (elist ret) {
1096
+ require(i < length(a), IndexOutOfRange(i, length(a)));
1097
+ return inco.listInsert{value: inco.getEListFee(length(a) + 1, listTypeOf(a))}(a, euint256.unwrap(asEuint256(uint256(i))), euint256.unwrap(s(b)));
1098
+ }
1099
+
1100
+ /// @notice Concatenates two elists into one.
1101
+ /// @param a EList handle to be prepended
1102
+ /// @param b EList handle to be appended
1103
+ /// @return ret A new elist handle with length len(a)+len(b).
1104
+ function concat(elist a, elist b) internal returns (elist ret) {
1105
+ return inco.listConcat{value: inco.getEListFee(length(a) + length(b), listTypeOf(a))}(a, b);
1106
+ }
1107
+
1108
+ /// @notice Slices a list from plaintext start to plaintext end.
1109
+ /// @param a EList handle to be sliced
1110
+ /// @param start Starting index (inclusive).
1111
+ /// @param end End index (exclusive). Must be >= start and within bounds.
1112
+ /// @return ret A new sliced list with length "end-start".
1113
+ function slice(elist a, uint16 start, uint16 end) internal returns (elist ret) {
1114
+ require(end >= start, InvalidRange(start, end));
1115
+ require(end <= length(a), SliceOutOfRange(start, end, length(a)));
1116
+ require(start < length(a), SliceOutOfRange(start, end, length(a)));
1117
+ bytes32 defaultValue;
1118
+ if (ETypes.Uint256 == listTypeOf(a)) {
1119
+ defaultValue = euint256.unwrap(asEuint256(0));
1120
+ } else if (ETypes.Bool == listTypeOf(a)) {
1121
+ defaultValue = ebool.unwrap(asEbool(false));
1122
+ } else {
1123
+ revert UnsupportedListType(listTypeOf(a));
1124
+ }
1125
+ return inco.listSlice{value: inco.getEListFee(end - start, listTypeOf(a))}(a, euint256.unwrap(asEuint256(start)), end - start, defaultValue);
1126
+ }
1127
+
1128
+ /// @notice Slices at a hidden index with a specified length (euint256 default).
1129
+ /// @param a EList handle to be sliced.
1130
+ /// @param start Hidden starting index.
1131
+ /// @param len Length of the desired slice.
1132
+ /// @param defaultValue Default value if out of range.
1133
+ /// @return ret A new sliced list with length "len".
1134
+ function sliceLen(elist a, euint256 start, uint16 len, euint256 defaultValue) internal returns (elist ret) {
1135
+ return inco.listSlice{value: inco.getEListFee(len, listTypeOf(a))}(a, euint256.unwrap(s(start)), len, euint256.unwrap(s(defaultValue)));
1136
+ }
1137
+
1138
+ /// @notice Slices at a hidden index with a specified length (ebool default).
1139
+ /// @param a EList handle to be sliced.
1140
+ /// @param start Hidden starting index.
1141
+ /// @param len Length of the desired slice.
1142
+ /// @param defaultValue Default value if out of range.
1143
+ /// @return ret A new sliced list with length "len".
1144
+ function sliceLen(elist a, euint256 start, uint16 len, ebool defaultValue) internal returns (elist ret) {
1145
+ return inco.listSlice{value: inco.getEListFee(len, listTypeOf(a))}(a, euint256.unwrap(s(start)), len, ebool.unwrap(s(defaultValue)));
1146
+ }
1147
+
1148
+ /// @notice Creates a new list populated with ordered values from within range.
1149
+ /// @param start Start value of the range, inclusive.
1150
+ /// @param end End of the range, exclusive. Must be >= start.
1151
+ /// @return ret A new elist handle with length "end-start"
1152
+ function range(uint16 start, uint16 end, ETypes listType) internal returns (elist ret) {
1153
+ return inco.listRange{value: inco.getEListFee(end - start, listType)}(start, end, listType);
1154
+ }
1155
+
1156
+ /// @notice Deterministically shuffles elements within a list.
1157
+ /// @dev costs the inco fee
1158
+ /// @param a elist handle to be shuffled
1159
+ /// @return ret A new elist handle with elements shuffled
1160
+ function shuffle(elist a) internal returns (elist ret) {
1161
+ return inco.listShuffle{value: inco.getEListFee(length(a), listTypeOf(a))}(a);
1162
+ }
1163
+
1164
+ /// @notice A convenience function equivalent to range() followed by shuffle().
1165
+ /// @dev costs the inco fee
1166
+ /// @param start Start value of the range, inclusive.
1167
+ /// @param end End of the range, exclusive. Must be >= start.
1168
+ /// @return ret A new elist handle in random order with length "end-start".
1169
+ function shuffledRange(uint16 start, uint16 end, ETypes listType) internal returns (elist ret) {
1170
+ uint256 fee = inco.getEListFee(end - start, listType);
1171
+ elist rangeList = inco.listRange{value: fee}(start, end, listType);
1172
+ return inco.listShuffle{value: fee}(rangeList);
1173
+ }
1174
+
1175
+ /// @notice Reverses the order of elements in a list.
1176
+ /// @param a Elist handle to be reversed
1177
+ /// @return ret A new elist handle with elements in reverse order
1178
+ function reverse(elist a) internal returns (elist ret) {
1179
+ return inco.listReverse{value: inco.getEListFee(length(a), listTypeOf(a))}(a);
1180
+ }
1181
+
1182
+ /// @notice Returns the length of the list in plaintext. Pure function, no gas cost.
1183
+ /// @param a EList handle to read the length from
1184
+ /// @return len The length of the list
1185
+ function length(elist a) internal pure returns (uint16 len) {
1186
+ return inco.lengthOf(elist.unwrap(a));
1187
+ }
1188
+
1189
+ /// @notice verifyEListDecryption verifies elist decryption attestation and commitment proof. Each element can be either a pre-computed hash or a commitment-value pair.
1190
+ /// @param elistHandle The elist handle that was decrypted
1191
+ /// @param proofElements Array of proof elements. Each can provide either pairHash directly, or commitment+value to compute the pairHash
1192
+ /// @param proof The expected proof that should match keccak256 of all concatenated pair hashes
1193
+ /// @param signatures Array of signatures from attesters
1194
+ /// @return bool True if verification succeeds
1195
+ /// @dev For privacy, publish only pairHash. For transparency, publish commitment+value. Can mix both in the same array.
1196
+ function verifyEListDecryption(elist elistHandle, ElementAttestationWithProof[] memory proofElements, bytes32 proof, bytes[] memory signatures) internal view returns (bool) {
1197
+ return inco.incoVerifier().isValidEListDecryptionAttestation(elist.unwrap(elistHandle), proofElements, proof, signatures);
1198
+ }
1199
+
1200
+ /// @notice Checks a decryption attestation for an encrypted bool against an expected plaintext value
1201
+ /// @param handle The encrypted handle
1202
+ /// @param expected The expected plaintext value
1203
+ /// @param decryption The decryption attestation to verify
1204
+ /// @param signatures The covalidator signatures for the attestation
1205
+ /// @dev Reverts if the handle doesn't match, the decrypted value doesn't match the expected value, or the attestation is invalid
1206
+ function requireEqual(ebool handle, bool expected, DecryptionAttestation memory decryption, bytes[] memory signatures) internal view {
1207
+ require(ebool.unwrap(handle) == decryption.handle, HandleMismatch());
1208
+ require(asBool(decryption.value) == expected, UnexpectedDecryptedValue());
1209
+ require(inco.incoVerifier().isValidDecryptionAttestation(decryption, signatures), InvalidTEEAttestation());
1210
+ }
1211
+
1212
+ /// @notice Checks a decryption attestation for an encrypted uint256 against an expected plaintext value
1213
+ /// @param handle The encrypted handle
1214
+ /// @param expected The expected plaintext value
1215
+ /// @param decryption The decryption attestation to verify
1216
+ /// @param signatures The covalidator signatures for the attestation
1217
+ /// @dev Reverts if the handle doesn't match, the decrypted value doesn't match the expected value, or the attestation is invalid
1218
+ function requireEqual(euint256 handle, uint256 expected, DecryptionAttestation memory decryption, bytes[] memory signatures) internal view {
1219
+ require(euint256.unwrap(handle) == decryption.handle, HandleMismatch());
1220
+ require(uint256(decryption.value) == expected, UnexpectedDecryptedValue());
1221
+ require(inco.incoVerifier().isValidDecryptionAttestation(decryption, signatures), InvalidTEEAttestation());
1222
+ }
1223
+ }