@inco/lightning 0.6.8 → 0.7.0

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 (87) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +29 -2
  3. package/manifest.yaml +35 -42
  4. package/package.json +5 -2
  5. package/src/CreateXHelper.sol +3 -1
  6. package/src/DeployUtils.sol +36 -71
  7. package/src/Errors.sol +1 -1
  8. package/src/IIncoLightning.sol +2 -0
  9. package/src/IncoLightning.sol +5 -17
  10. package/src/IncoVerifier.sol +12 -18
  11. package/src/Lib.alphanet.sol +1 -1
  12. package/src/Lib.sol +1 -1
  13. package/src/Lib.template.sol +35 -153
  14. package/src/Types.sol +231 -97
  15. package/src/interfaces/IIncoLightning.sol +2 -0
  16. package/src/interfaces/IIncoVerifier.sol +6 -12
  17. package/src/interfaces/automata-interfaces/BELE.sol +2 -0
  18. package/src/interfaces/automata-interfaces/IAutomataEnclaveIdentityDao.sol +9 -11
  19. package/src/interfaces/automata-interfaces/IFmspcTcbDao.sol +3 -3
  20. package/src/interfaces/automata-interfaces/IPCCSRouter.sol +13 -47
  21. package/src/interfaces/automata-interfaces/IPCCSRouterExtended.sol +2 -0
  22. package/src/interfaces/automata-interfaces/IPcsDao.sol +6 -11
  23. package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +4 -7
  24. package/src/interfaces/automata-interfaces/Types.sol +7 -6
  25. package/src/libs/incoLightning_alphanet_v2_976644394.sol +478 -0
  26. package/src/libs/incoLightning_devnet_v1_887305889.sol +5 -3
  27. package/src/libs/incoLightning_testnet_v1_938327937.sol +5 -3
  28. package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +39 -72
  29. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +31 -62
  30. package/src/lightning-parts/AccessControl/interfaces/IAdvancedAccessControl.sol +8 -15
  31. package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +5 -12
  32. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +44 -84
  33. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +2 -0
  34. package/src/lightning-parts/DecryptionAttester.sol +14 -28
  35. package/src/lightning-parts/EncryptedInput.sol +23 -52
  36. package/src/lightning-parts/EncryptedOperations.sol +96 -438
  37. package/src/lightning-parts/Fee.sol +3 -1
  38. package/src/lightning-parts/TEELifecycle.sol +94 -223
  39. package/src/lightning-parts/TEELifecycle.types.sol +4 -3
  40. package/src/lightning-parts/TrivialEncryption.sol +6 -20
  41. package/src/lightning-parts/interfaces/IDecryptionAttester.sol +7 -2
  42. package/src/lightning-parts/interfaces/IEncryptedInput.sol +5 -12
  43. package/src/lightning-parts/interfaces/IEncryptedOperations.sol +17 -61
  44. package/src/lightning-parts/interfaces/ITEELifecycle.sol +7 -11
  45. package/src/lightning-parts/interfaces/ITrivialEncryption.sol +2 -0
  46. package/src/lightning-parts/primitives/EventCounter.sol +7 -8
  47. package/src/lightning-parts/primitives/HandleGeneration.sol +20 -32
  48. package/src/lightning-parts/primitives/HandleMetadata.sol +7 -17
  49. package/src/lightning-parts/primitives/LightningAddressGetter.sol +3 -0
  50. package/src/lightning-parts/primitives/SignatureVerifier.sol +91 -27
  51. package/src/lightning-parts/primitives/VerifierAddressGetter.sol +3 -0
  52. package/src/lightning-parts/primitives/interfaces/IEventCounter.sol +2 -0
  53. package/src/lightning-parts/primitives/interfaces/IHandleGeneration.sol +10 -2
  54. package/src/lightning-parts/primitives/interfaces/ISignatureVerifier.sol +4 -2
  55. package/src/lightning-parts/primitives/interfaces/IVerifierAddressGetter.sol +2 -0
  56. package/src/lightning-parts/primitives/test/SignatureVerifier.t.sol +838 -0
  57. package/src/lightning-parts/test/Fee.t.sol +6 -6
  58. package/src/lightning-parts/test/HandleMetadata.t.sol +21 -76
  59. package/src/lightning-parts/test/InputsFee.t.sol +7 -28
  60. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +16 -48
  61. package/src/pasted-dependencies/CreateX.sol +154 -455
  62. package/src/pasted-dependencies/ICreateX.sol +55 -102
  63. package/src/periphery/SessionVerifier.sol +10 -8
  64. package/src/shared/IOwnable.sol +3 -0
  65. package/src/shared/IUUPSUpgradable.sol +5 -1
  66. package/src/shared/JsonUtils.sol +3 -5
  67. package/src/shared/TestUtils.sol +15 -13
  68. package/src/test/AddTwo.sol +9 -7
  69. package/src/test/FakeIncoInfra/FakeComputeServer.sol +11 -53
  70. package/src/test/FakeIncoInfra/FakeDecryptionAttester.sol +35 -119
  71. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +31 -48
  72. package/src/test/FakeIncoInfra/FakeQuoteVerifier.sol +4 -7
  73. package/src/test/FakeIncoInfra/KVStore.sol +2 -0
  74. package/src/test/FakeIncoInfra/MockOpHandler.sol +9 -31
  75. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +50 -21
  76. package/src/test/IncoTest.sol +22 -9
  77. package/src/test/OpsTest.sol +438 -0
  78. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +57 -104
  79. package/src/test/TestAddTwo.t.sol +4 -3
  80. package/src/test/TestDeploy.t.sol +5 -6
  81. package/src/test/TestExtractDataOfEventTooLarge.t.sol +7 -9
  82. package/src/test/TestFakeInfra.t.sol +15 -38
  83. package/src/test/TestUpgrade.t.sol +40 -135
  84. package/src/test/TestVersion.t.sol +6 -5
  85. package/src/version/IncoLightningConfig.sol +2 -2
  86. package/src/version/Version.sol +46 -48
  87. package/src/version/interfaces/IVersion.sol +6 -0
@@ -7,173 +7,53 @@ import {HandleGeneration} from "./primitives/HandleGeneration.sol";
7
7
  import {IEncryptedOperations} from "./interfaces/IEncryptedOperations.sol";
8
8
  import {Fee} from "./Fee.sol";
9
9
 
10
- abstract contract EncryptedOperations is
11
- IEncryptedOperations,
12
- BaseAccessControlList,
13
- HandleGeneration,
14
- Fee
15
- {
10
+ abstract contract EncryptedOperations is IEncryptedOperations, BaseAccessControlList, HandleGeneration, Fee {
11
+
16
12
  error UnexpectedType(ETypes actual, bytes32 expectedTypes);
17
13
  error UnsupportedType(ETypes actual);
18
14
 
19
15
  uint256 internal randCounter;
20
-
21
- bytes32 constant EBOOL = bytes32(1 << uint256(ETypes.Bool));
22
- bytes32 constant EUINT160 = bytes32(1 << uint256(ETypes.AddressOrUint160OrBytes20));
23
- bytes32 constant EUINT256 = bytes32(1 << uint256(ETypes.Uint256));
24
-
25
- bytes32 constant SUPPORTED_TYPES_MASK =
26
- EBOOL |
27
- EUINT160 |
28
- EUINT256;
29
-
30
- event EAdd(
31
- euint256 indexed lhs,
32
- euint256 indexed rhs,
33
- euint256 indexed result,
34
- uint256 eventId
35
- );
36
- event ESub(
37
- euint256 indexed lhs,
38
- euint256 indexed rhs,
39
- euint256 indexed result,
40
- uint256 eventId
41
- );
42
- event EMul(
43
- euint256 indexed lhs,
44
- euint256 indexed rhs,
45
- euint256 indexed result,
46
- uint256 eventId
47
- );
48
- event EDiv(
49
- euint256 indexed lhs,
50
- euint256 indexed rhs,
51
- euint256 indexed result,
52
- uint256 eventId
53
- );
54
- event ERem(
55
- euint256 indexed lhs,
56
- euint256 indexed rhs,
57
- euint256 indexed result,
58
- uint256 eventId
59
- );
60
- event EBitAnd(
61
- bytes32 indexed lhs,
62
- bytes32 indexed rhs,
63
- bytes32 indexed result,
64
- uint256 eventId
65
- );
66
- event EBitOr(
67
- bytes32 indexed lhs,
68
- bytes32 indexed rhs,
69
- bytes32 indexed result,
70
- uint256 eventId
71
- );
72
- event EBitXor(
73
- bytes32 indexed lhs,
74
- bytes32 indexed rhs,
75
- bytes32 indexed result,
76
- uint256 eventId
77
- );
78
- event EShl(
79
- euint256 indexed lhs,
80
- euint256 indexed rhs,
81
- euint256 indexed result,
82
- uint256 eventId
83
- );
84
- event EShr(
85
- euint256 indexed lhs,
86
- euint256 indexed rhs,
87
- euint256 indexed result,
88
- uint256 eventId
89
- );
90
- event ERotl(
91
- euint256 indexed lhs,
92
- euint256 indexed rhs,
93
- euint256 indexed result,
94
- uint256 eventId
95
- );
96
- event ERotr(
97
- euint256 indexed lhs,
98
- euint256 indexed rhs,
99
- euint256 indexed result,
100
- uint256 eventId
101
- );
102
- event EEq(
103
- bytes32 indexed lhs,
104
- bytes32 indexed rhs,
105
- ebool indexed result,
106
- uint256 eventId
107
- );
108
- event ENe(
109
- bytes32 indexed lhs,
110
- bytes32 indexed rhs,
111
- ebool indexed result,
112
- uint256 eventId
113
- );
114
- event EGe(
115
- euint256 indexed lhs,
116
- euint256 indexed rhs,
117
- ebool indexed result,
118
- uint256 eventId
119
- );
120
- event EGt(
121
- euint256 indexed lhs,
122
- euint256 indexed rhs,
123
- ebool indexed result,
124
- uint256 eventId
125
- );
126
- event ELe(
127
- euint256 indexed lhs,
128
- euint256 indexed rhs,
129
- ebool indexed result,
130
- uint256 eventId
131
- );
132
- event ELt(
133
- euint256 indexed lhs,
134
- euint256 indexed rhs,
135
- ebool indexed result,
136
- uint256 eventId
137
- );
138
- event EMin(
139
- euint256 indexed lhs,
140
- euint256 indexed rhs,
141
- euint256 indexed result,
142
- uint256 eventId
143
- );
144
- event EMax(
145
- euint256 indexed lhs,
146
- euint256 indexed rhs,
147
- euint256 indexed result,
148
- uint256 eventId
149
- );
150
- event ERand(
151
- uint256 indexed counter,
152
- ETypes randType,
153
- bytes32 indexed result,
154
- uint256 eventId
155
- );
16
+ uint256 internal constant ONE = 1;
17
+
18
+ bytes32 constant EBOOL = bytes32(ONE << uint256(ETypes.Bool));
19
+ bytes32 constant EUINT160 = bytes32(ONE << uint256(ETypes.AddressOrUint160OrBytes20));
20
+ bytes32 constant EUINT256 = bytes32(ONE << uint256(ETypes.Uint256));
21
+
22
+ bytes32 constant SUPPORTED_TYPES_MASK = EBOOL | EUINT160 | EUINT256;
23
+
24
+ event EAdd(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
25
+ event ESub(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
26
+ event EMul(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
27
+ event EDiv(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
28
+ event ERem(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
29
+ event EBitAnd(bytes32 indexed lhs, bytes32 indexed rhs, bytes32 indexed result, uint256 eventId);
30
+ event EBitOr(bytes32 indexed lhs, bytes32 indexed rhs, bytes32 indexed result, uint256 eventId);
31
+ event EBitXor(bytes32 indexed lhs, bytes32 indexed rhs, bytes32 indexed result, uint256 eventId);
32
+ event EShl(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
33
+ event EShr(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
34
+ event ERotl(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
35
+ event ERotr(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
36
+ event EEq(bytes32 indexed lhs, bytes32 indexed rhs, ebool indexed result, uint256 eventId);
37
+ event ENe(bytes32 indexed lhs, bytes32 indexed rhs, ebool indexed result, uint256 eventId);
38
+ event EGe(euint256 indexed lhs, euint256 indexed rhs, ebool indexed result, uint256 eventId);
39
+ event EGt(euint256 indexed lhs, euint256 indexed rhs, ebool indexed result, uint256 eventId);
40
+ event ELe(euint256 indexed lhs, euint256 indexed rhs, ebool indexed result, uint256 eventId);
41
+ event ELt(euint256 indexed lhs, euint256 indexed rhs, ebool indexed result, uint256 eventId);
42
+ event EMin(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
43
+ event EMax(euint256 indexed lhs, euint256 indexed rhs, euint256 indexed result, uint256 eventId);
44
+ event ERand(uint256 indexed counter, ETypes randType, bytes32 indexed result, uint256 eventId);
156
45
  event ERandBounded(
157
- uint256 indexed counter,
158
- ETypes randType,
159
- bytes32 indexed upperBound,
160
- bytes32 indexed result,
161
- uint256 eventId
46
+ uint256 indexed counter, ETypes randType, bytes32 indexed upperBound, bytes32 indexed result, uint256 eventId
162
47
  );
163
- event EIfThenElse(
164
- ebool control, // can't index >3 fields
48
+ event EIfThenElse( // can't index >3 fields
49
+ ebool control,
165
50
  bytes32 indexed ifTrue,
166
51
  bytes32 indexed ifFalse,
167
52
  bytes32 indexed result,
168
53
  uint256 eventId
169
54
  );
170
55
  event ENot(ebool indexed operand, ebool indexed result, uint256 eventId);
171
- event ECast(
172
- bytes32 indexed ct,
173
- uint8 indexed toType,
174
- bytes32 indexed result,
175
- uint256 eventId
176
- );
56
+ event ECast(bytes32 indexed ct, uint8 indexed toType, bytes32 indexed result, uint256 eventId);
177
57
 
178
58
  modifier checked(euint256 lhs, euint256 rhs) {
179
59
  checkInput(euint256.unwrap(lhs), typeToBitMask(ETypes.Uint256));
@@ -186,123 +66,65 @@ abstract contract EncryptedOperations is
186
66
  _;
187
67
  }
188
68
 
189
-
190
69
  function checkInput(bytes32 input, bytes32 requiredTypes) internal view {
191
- require(
192
- isAllowed(input, msg.sender),
193
- SenderNotAllowedForHandle(input, msg.sender)
194
- );
70
+ require(isAllowed(input, msg.sender), SenderNotAllowedForHandle(input, msg.sender));
195
71
  require(requiredTypes & typeToBitMask(typeOf(input)) != 0, UnexpectedType(typeOf(input), requiredTypes));
196
72
  }
197
73
 
198
- function createResultHandle(
199
- EOps op,
200
- ETypes returnType,
201
- bytes memory packedInputs
202
- ) internal returns (bytes32 result) {
74
+ function createResultHandle(EOps op, ETypes returnType, bytes memory packedInputs)
75
+ internal
76
+ returns (bytes32 result)
77
+ {
203
78
  result = getOpResultHandle(op, returnType, packedInputs);
204
79
  allowTransientInternal(result, msg.sender);
205
80
  }
206
81
 
207
- function eAdd(
208
- euint256 lhs,
209
- euint256 rhs
210
- ) external checked(lhs, rhs) returns (euint256 result) {
82
+ function eAdd(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
211
83
  result = euint256.wrap(
212
- createResultHandle(
213
- EOps.Add,
214
- ETypes.Uint256,
215
- abi.encodePacked(
216
- euint256.unwrap(lhs),
217
- euint256.unwrap(rhs)
218
- )
219
- )
84
+ createResultHandle(EOps.Add, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
220
85
  );
221
86
  uint256 id = getNextEventId();
222
87
  emit EAdd(lhs, rhs, result, id);
223
88
  setDigest(abi.encodePacked(result, id));
224
89
  }
225
90
 
226
- function eSub(
227
- euint256 lhs,
228
- euint256 rhs
229
- ) external checked(lhs, rhs) returns (euint256 result) {
91
+ function eSub(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
230
92
  result = euint256.wrap(
231
- createResultHandle(
232
- EOps.Sub,
233
- ETypes.Uint256,
234
- abi.encodePacked(
235
- euint256.unwrap(lhs),
236
- euint256.unwrap(rhs)
237
- )
238
- )
93
+ createResultHandle(EOps.Sub, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
239
94
  );
240
95
  uint256 id = getNextEventId();
241
96
  emit ESub(lhs, rhs, result, id);
242
97
  setDigest(abi.encodePacked(result, id));
243
98
  }
244
99
 
245
- function eMul(
246
- euint256 lhs,
247
- euint256 rhs
248
- ) external checked(lhs, rhs) returns (euint256 result) {
100
+ function eMul(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
249
101
  result = euint256.wrap(
250
- createResultHandle(
251
- EOps.Mul,
252
- ETypes.Uint256,
253
- abi.encodePacked(
254
- euint256.unwrap(lhs),
255
- euint256.unwrap(rhs)
256
- )
257
- )
102
+ createResultHandle(EOps.Mul, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
258
103
  );
259
104
  uint256 id = getNextEventId();
260
105
  emit EMul(lhs, rhs, result, id);
261
106
  setDigest(abi.encodePacked(result, id));
262
- }
107
+ }
263
108
 
264
- function eDiv(
265
- euint256 lhs,
266
- euint256 rhs
267
- ) external checked(lhs, rhs) returns (euint256 result) {
109
+ function eDiv(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
268
110
  result = euint256.wrap(
269
- createResultHandle(
270
- EOps.Div,
271
- ETypes.Uint256,
272
- abi.encodePacked(
273
- euint256.unwrap(lhs),
274
- euint256.unwrap(rhs)
275
- )
276
- )
111
+ createResultHandle(EOps.Div, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
277
112
  );
278
113
  uint256 id = getNextEventId();
279
114
  emit EDiv(lhs, rhs, result, id);
280
115
  setDigest(abi.encodePacked(result, id));
281
116
  }
282
117
 
283
- function eRem(
284
- euint256 lhs,
285
- euint256 rhs
286
- ) external virtual checked(lhs, rhs) returns (euint256 result) {
118
+ function eRem(euint256 lhs, euint256 rhs) external virtual checked(lhs, rhs) returns (euint256 result) {
287
119
  result = euint256.wrap(
288
- createResultHandle(
289
- EOps.Rem,
290
- ETypes.Uint256,
291
- abi.encodePacked(
292
- euint256.unwrap(lhs),
293
- euint256.unwrap(rhs)
294
- )
295
- )
120
+ createResultHandle(EOps.Rem, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
296
121
  );
297
122
  uint256 id = getNextEventId();
298
123
  emit ERem(lhs, rhs, result, id);
299
124
  setDigest(abi.encodePacked(result, id));
300
125
  }
301
126
 
302
- function eBitAnd(
303
- bytes32 lhs,
304
- bytes32 rhs
305
- ) external returns (bytes32 result) {
127
+ function eBitAnd(bytes32 lhs, bytes32 rhs) external returns (bytes32 result) {
306
128
  ETypes lhsType = typeOf(lhs);
307
129
  ETypes rhsType = typeOf(rhs);
308
130
  checkInput(lhs, typeToBitMask(lhsType));
@@ -314,10 +136,7 @@ abstract contract EncryptedOperations is
314
136
  setDigest(abi.encodePacked(result, id));
315
137
  }
316
138
 
317
- function eBitOr(
318
- bytes32 lhs,
319
- bytes32 rhs
320
- ) external returns (bytes32 result) {
139
+ function eBitOr(bytes32 lhs, bytes32 rhs) external returns (bytes32 result) {
321
140
  ETypes lhsType = typeOf(lhs);
322
141
  ETypes rhsType = typeOf(rhs);
323
142
  checkInput(lhs, typeToBitMask(lhsType));
@@ -329,10 +148,7 @@ abstract contract EncryptedOperations is
329
148
  setDigest(abi.encodePacked(result, id));
330
149
  }
331
150
 
332
- function eBitXor(
333
- bytes32 lhs,
334
- bytes32 rhs
335
- ) external returns (bytes32 result) {
151
+ function eBitXor(bytes32 lhs, bytes32 rhs) external returns (bytes32 result) {
336
152
  ETypes lhsType = typeOf(lhs);
337
153
  ETypes rhsType = typeOf(rhs);
338
154
  checkInput(lhs, typeToBitMask(lhsType));
@@ -344,234 +160,110 @@ abstract contract EncryptedOperations is
344
160
  setDigest(abi.encodePacked(result, id));
345
161
  }
346
162
 
347
- function eShl(
348
- euint256 lhs,
349
- euint256 rhs
350
- ) external checked(lhs, rhs) returns (euint256 result) {
163
+ function eShl(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
351
164
  result = euint256.wrap(
352
- createResultHandle(
353
- EOps.Shl,
354
- ETypes.Uint256,
355
- abi.encodePacked(
356
- euint256.unwrap(lhs),
357
- euint256.unwrap(rhs)
358
- )
359
- )
165
+ createResultHandle(EOps.Shl, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
360
166
  );
361
167
  uint256 id = getNextEventId();
362
168
  emit EShl(lhs, rhs, result, id);
363
169
  setDigest(abi.encodePacked(result, id));
364
170
  }
365
171
 
366
- function eShr(
367
- euint256 lhs,
368
- euint256 rhs
369
- ) external checked(lhs, rhs) returns (euint256 result) {
172
+ function eShr(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
370
173
  result = euint256.wrap(
371
- createResultHandle(
372
- EOps.Shr,
373
- ETypes.Uint256,
374
- abi.encodePacked(
375
- euint256.unwrap(lhs),
376
- euint256.unwrap(rhs)
377
- )
378
- )
174
+ createResultHandle(EOps.Shr, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
379
175
  );
380
176
  uint256 id = getNextEventId();
381
177
  emit EShr(lhs, rhs, result, id);
382
178
  setDigest(abi.encodePacked(result, id));
383
179
  }
384
180
 
385
- function eRotl(
386
- euint256 lhs,
387
- euint256 rhs
388
- ) external checked(lhs, rhs) returns (euint256 result) {
181
+ function eRotl(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
389
182
  result = euint256.wrap(
390
- createResultHandle(
391
- EOps.Rotl,
392
- ETypes.Uint256,
393
- abi.encodePacked(
394
- euint256.unwrap(lhs),
395
- euint256.unwrap(rhs)
396
- )
397
- )
183
+ createResultHandle(EOps.Rotl, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
398
184
  );
399
185
  uint256 id = getNextEventId();
400
186
  emit ERotl(lhs, rhs, result, id);
401
187
  setDigest(abi.encodePacked(result, id));
402
188
  }
403
189
 
404
- function eRotr(
405
- euint256 lhs,
406
- euint256 rhs
407
- ) external checked(lhs, rhs) returns (euint256 result) {
190
+ function eRotr(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
408
191
  result = euint256.wrap(
409
- createResultHandle(
410
- EOps.Rotr,
411
- ETypes.Uint256,
412
- abi.encodePacked(
413
- euint256.unwrap(lhs),
414
- euint256.unwrap(rhs)
415
- )
416
- )
192
+ createResultHandle(EOps.Rotr, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
417
193
  );
418
194
  uint256 id = getNextEventId();
419
195
  emit ERotr(lhs, rhs, result, id);
420
196
  setDigest(abi.encodePacked(result, id));
421
197
  }
422
198
 
423
- function eEq(
424
- bytes32 lhs,
425
- bytes32 rhs
426
- ) external returns (ebool result) {
199
+ function eEq(bytes32 lhs, bytes32 rhs) external returns (ebool result) {
427
200
  checkInput(lhs, SUPPORTED_TYPES_MASK);
428
201
  checkInput(rhs, SUPPORTED_TYPES_MASK);
429
202
 
430
- result = ebool.wrap(
431
- createResultHandle(
432
- EOps.Eq,
433
- ETypes.Bool,
434
- abi.encodePacked(
435
- lhs,
436
- rhs
437
- )
438
- )
439
- );
203
+ result = ebool.wrap(createResultHandle(EOps.Eq, ETypes.Bool, abi.encodePacked(lhs, rhs)));
440
204
  uint256 id = getNextEventId();
441
205
  emit EEq(lhs, rhs, result, id);
442
206
  setDigest(abi.encodePacked(result, id));
443
207
  }
444
208
 
445
- function eNe(
446
- bytes32 lhs,
447
- bytes32 rhs
448
- ) external returns (ebool result) {
209
+ function eNe(bytes32 lhs, bytes32 rhs) external returns (ebool result) {
449
210
  checkInput(lhs, SUPPORTED_TYPES_MASK);
450
211
  checkInput(rhs, SUPPORTED_TYPES_MASK);
451
212
 
452
- result = ebool.wrap(
453
- createResultHandle(
454
- EOps.Ne,
455
- ETypes.Bool,
456
- abi.encodePacked(
457
- lhs,
458
- rhs
459
- )
460
- )
461
- );
213
+ result = ebool.wrap(createResultHandle(EOps.Ne, ETypes.Bool, abi.encodePacked(lhs, rhs)));
462
214
  uint256 id = getNextEventId();
463
215
  emit ENe(lhs, rhs, result, id);
464
216
  setDigest(abi.encodePacked(result, id));
465
217
  }
466
218
 
467
- function eGe(
468
- euint256 lhs,
469
- euint256 rhs
470
- ) external checked(lhs, rhs) returns (ebool result) {
219
+ function eGe(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (ebool result) {
471
220
  result = ebool.wrap(
472
- createResultHandle(
473
- EOps.Ge,
474
- ETypes.Bool,
475
- abi.encodePacked(
476
- euint256.unwrap(lhs),
477
- euint256.unwrap(rhs)
478
- )
479
- )
221
+ createResultHandle(EOps.Ge, ETypes.Bool, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
480
222
  );
481
223
  uint256 id = getNextEventId();
482
224
  emit EGe(lhs, rhs, result, id);
483
225
  setDigest(abi.encodePacked(result, id));
484
226
  }
485
227
 
486
- function eGt(
487
- euint256 lhs,
488
- euint256 rhs
489
- ) external checked(lhs, rhs) returns (ebool result) {
228
+ function eGt(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (ebool result) {
490
229
  result = ebool.wrap(
491
- createResultHandle(
492
- EOps.Gt,
493
- ETypes.Bool,
494
- abi.encodePacked(
495
- euint256.unwrap(lhs),
496
- euint256.unwrap(rhs)
497
- )
498
- )
230
+ createResultHandle(EOps.Gt, ETypes.Bool, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
499
231
  );
500
232
  uint256 id = getNextEventId();
501
233
  emit EGt(lhs, rhs, result, id);
502
234
  setDigest(abi.encodePacked(result, id));
503
235
  }
504
236
 
505
- function eLe(
506
- euint256 lhs,
507
- euint256 rhs
508
- ) external checked(lhs, rhs) returns (ebool result) {
237
+ function eLe(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (ebool result) {
509
238
  result = ebool.wrap(
510
- createResultHandle(
511
- EOps.Le,
512
- ETypes.Bool,
513
- abi.encodePacked(
514
- euint256.unwrap(lhs),
515
- euint256.unwrap(rhs)
516
- )
517
- )
239
+ createResultHandle(EOps.Le, ETypes.Bool, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
518
240
  );
519
241
  uint256 id = getNextEventId();
520
242
  emit ELe(lhs, rhs, result, id);
521
243
  setDigest(abi.encodePacked(result, id));
522
244
  }
523
245
 
524
- function eLt(
525
- euint256 lhs,
526
- euint256 rhs
527
- ) external checked(lhs, rhs) returns (ebool result) {
246
+ function eLt(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (ebool result) {
528
247
  result = ebool.wrap(
529
- createResultHandle(
530
- EOps.Lt,
531
- ETypes.Bool,
532
- abi.encodePacked(
533
- euint256.unwrap(lhs),
534
- euint256.unwrap(rhs)
535
- )
536
- )
248
+ createResultHandle(EOps.Lt, ETypes.Bool, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
537
249
  );
538
250
  uint256 id = getNextEventId();
539
251
  emit ELt(lhs, rhs, result, id);
540
252
  setDigest(abi.encodePacked(result, id));
541
253
  }
542
254
 
543
- function eMin(
544
- euint256 lhs,
545
- euint256 rhs
546
- ) external checked(lhs, rhs) returns (euint256 result) {
255
+ function eMin(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
547
256
  result = euint256.wrap(
548
- createResultHandle(
549
- EOps.Min,
550
- ETypes.Uint256,
551
- abi.encodePacked(
552
- euint256.unwrap(lhs),
553
- euint256.unwrap(rhs)
554
- )
555
- )
257
+ createResultHandle(EOps.Min, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
556
258
  );
557
259
  uint256 id = getNextEventId();
558
260
  emit EMin(lhs, rhs, result, id);
559
261
  setDigest(abi.encodePacked(result, id));
560
262
  }
561
263
 
562
- function eMax(
563
- euint256 lhs,
564
- euint256 rhs
565
- ) external checked(lhs, rhs) returns (euint256 result) {
264
+ function eMax(euint256 lhs, euint256 rhs) external checked(lhs, rhs) returns (euint256 result) {
566
265
  result = euint256.wrap(
567
- createResultHandle(
568
- EOps.Max,
569
- ETypes.Uint256,
570
- abi.encodePacked(
571
- euint256.unwrap(lhs),
572
- euint256.unwrap(rhs)
573
- )
574
- )
266
+ createResultHandle(EOps.Max, ETypes.Uint256, abi.encodePacked(euint256.unwrap(lhs), euint256.unwrap(rhs)))
575
267
  );
576
268
  uint256 id = getNextEventId();
577
269
  emit EMax(lhs, rhs, result, id);
@@ -580,98 +272,64 @@ abstract contract EncryptedOperations is
580
272
 
581
273
  function eNot(ebool operand) external returns (ebool result) {
582
274
  checkInput(ebool.unwrap(operand), typeToBitMask(ETypes.Bool));
583
- result = ebool.wrap(
584
- createResultHandle(EOps.Not, ETypes.Bool, abi.encodePacked(ebool.unwrap(operand)))
585
- );
275
+ result = ebool.wrap(createResultHandle(EOps.Not, ETypes.Bool, abi.encodePacked(ebool.unwrap(operand))));
586
276
  uint256 id = getNextEventId();
587
277
  emit ENot(operand, result, id);
588
278
  setDigest(abi.encodePacked(result, id));
589
279
  }
590
280
 
591
- function eCast(
592
- bytes32 ct,
593
- ETypes toType
594
- ) external returns (bytes32 result) {
595
- bytes32 baseHandle = keccak256(abi.encodePacked(EOps.Cast, ct));
596
- result = embedTypeVersion(baseHandle, toType);
281
+ function eCast(bytes32 ct, ETypes toType) external returns (bytes32 result) {
282
+ require(isTypeSupported(toType), UnsupportedType(toType));
283
+ result = createResultHandle(EOps.Cast, toType, abi.encodePacked(ct));
597
284
  allowTransientInternal(result, msg.sender);
598
285
  uint256 id = getNextEventId();
599
286
  emit ECast(ct, uint8(toType), result, id);
600
287
  setDigest(abi.encodePacked(result, id));
601
288
  }
602
289
 
603
- function eRand(
604
- ETypes randType
605
- ) external paying payable returns (bytes32 result) {
290
+ function eRand(ETypes randType) external payable paying returns (bytes32 result) {
606
291
  require(isTypeSupported(randType), UnsupportedType(randType));
607
292
  randCounter++;
608
- result = createResultHandle(
609
- EOps.Rand,
610
- randType,
611
- abi.encodePacked(bytes32(randCounter))
612
- );
293
+ result = createResultHandle(EOps.Rand, randType, abi.encodePacked(bytes32(randCounter)));
613
294
  uint256 id = getNextEventId();
614
295
  emit ERand(randCounter, randType, result, id);
615
296
  setDigest(abi.encodePacked(result, id));
616
297
  }
617
298
 
618
- function eRandBounded(
619
- bytes32 upperBound,
620
- ETypes randType
621
- ) external paying payable returns (bytes32 result) {
299
+ function eRandBounded(bytes32 upperBound, ETypes randType) external payable paying returns (bytes32 result) {
622
300
  require(isTypeSupported(randType), UnsupportedType(randType));
623
301
  checkInput(upperBound, typeToBitMask(ETypes.Uint256));
624
302
  randCounter++;
625
- result = createResultHandle(
626
- EOps.RandBounded,
627
- randType,
628
- abi.encodePacked(
629
- bytes32(randCounter),
630
- upperBound
631
- )
632
- );
303
+ result = createResultHandle(EOps.RandBounded, randType, abi.encodePacked(bytes32(randCounter), upperBound));
633
304
  uint256 id = getNextEventId();
634
305
  emit ERandBounded(randCounter, randType, upperBound, result, id);
635
306
  setDigest(abi.encodePacked(result, id));
636
307
  }
637
308
 
638
309
  // todo add support in testing framework
639
- function eIfThenElse(
640
- ebool control,
641
- bytes32 ifTrue,
642
- bytes32 ifFalse
643
- ) external returns (bytes32 result) {
310
+ function eIfThenElse(ebool control, bytes32 ifTrue, bytes32 ifFalse) external returns (bytes32 result) {
644
311
  ETypes returnType = checkEIfThenElseInputs(control, ifTrue, ifFalse);
645
- bytes32 baseHandle = keccak256(
646
- abi.encodePacked(
647
- EOps.IfThenElse,
648
- ebool.unwrap(control),
649
- ifTrue,
650
- ifFalse
651
- )
652
- );
653
- result = embedTypeVersion(baseHandle, returnType);
312
+ result =
313
+ createResultHandle(EOps.IfThenElse, returnType, abi.encodePacked(ebool.unwrap(control), ifTrue, ifFalse));
654
314
  allowTransientInternal(result, msg.sender);
655
315
  uint256 id = getNextEventId();
656
316
  emit EIfThenElse(control, ifTrue, ifFalse, result, id);
657
317
  setDigest(abi.encodePacked(result, id));
658
318
  }
659
319
 
660
- function checkEIfThenElseInputs(
661
- ebool control,
662
- bytes32 ifTrue,
663
- bytes32 ifFalse
664
- ) internal view returns (ETypes ifTrueType) {
320
+ function checkEIfThenElseInputs(ebool control, bytes32 ifTrue, bytes32 ifFalse)
321
+ internal
322
+ view
323
+ returns (ETypes ifTrueType)
324
+ {
665
325
  checkInput(ebool.unwrap(control), typeToBitMask(ETypes.Bool));
666
326
  ifTrueType = typeOf(ifTrue);
667
327
  require(
668
328
  ifTrueType == ETypes.Uint256 || ifTrueType == ETypes.Bool || ifTrueType == ETypes.AddressOrUint160OrBytes20,
669
329
  UnsupportedType(ifTrueType)
670
330
  );
671
- require(
672
- isAllowed(ifTrue, msg.sender),
673
- SenderNotAllowedForHandle(ifTrue, msg.sender)
674
- );
331
+ require(isAllowed(ifTrue, msg.sender), SenderNotAllowedForHandle(ifTrue, msg.sender));
675
332
  checkInput(ifFalse, typeToBitMask(ifTrueType));
676
333
  }
334
+
677
335
  }