@inco/lightning 0.1.20

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 (50) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +69 -0
  3. package/dumps/incoLightning_0_1_23__547622051.dump.json +1 -0
  4. package/dumps/incoLightning_0_1_23__547622051.env +15 -0
  5. package/dumps/incoLightning_0_1_23__830342853.dump.json +1 -0
  6. package/dumps/incoLightning_0_1_23__830342853.env +15 -0
  7. package/dumps/incoLightning_0_1_24__266705097.dump.json +1 -0
  8. package/dumps/incoLightning_0_1_24__266705097.env +15 -0
  9. package/dumps/incoLightning_0_1_25__986372984.dump.json +1 -0
  10. package/dumps/incoLightning_0_1_25__986372984.env +15 -0
  11. package/foundry.toml +20 -0
  12. package/package.json +27 -0
  13. package/remappings.txt +4 -0
  14. package/src/DeployUtils.sol +109 -0
  15. package/src/IncoLightning.sol +45 -0
  16. package/src/Lib.sol +389 -0
  17. package/src/Lib.template.sol +464 -0
  18. package/src/Types.sol +74 -0
  19. package/src/libs/incoLightning_0_1_22__761766708.sol +389 -0
  20. package/src/libs/incoLightning_0_1_23__547622051.sol +389 -0
  21. package/src/libs/incoLightning_0_1_23__830342853.sol +389 -0
  22. package/src/libs/incoLightning_0_1_24__266705097.sol +389 -0
  23. package/src/libs/incoLightning_0_1_25__986372984.sol +389 -0
  24. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +105 -0
  25. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +12 -0
  26. package/src/lightning-parts/DecryptionHandler.sol +164 -0
  27. package/src/lightning-parts/EncryptedInput.sol +61 -0
  28. package/src/lightning-parts/EncryptedOperations.sol +610 -0
  29. package/src/lightning-parts/TrivialEncryption.sol +43 -0
  30. package/src/lightning-parts/primitives/EventCounter.sol +32 -0
  31. package/src/lightning-parts/primitives/HandleGeneration.sol +107 -0
  32. package/src/lightning-parts/primitives/HandleMetadata.sol +38 -0
  33. package/src/lightning-parts/primitives/SignatureVerifier.sol +47 -0
  34. package/src/lightning-parts/test/HandleMetadata.t.sol +87 -0
  35. package/src/pasted-dependencies/CreateX.sol +1293 -0
  36. package/src/pasted-dependencies/ICreateX.sol +187 -0
  37. package/src/test/AddTwo.sol +48 -0
  38. package/src/test/FakeIncoInfra/FakeComputeServer.sol +137 -0
  39. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +77 -0
  40. package/src/test/FakeIncoInfra/KVStore.sol +35 -0
  41. package/src/test/FakeIncoInfra/MockOpHandler.sol +140 -0
  42. package/src/test/FakeIncoInfra/getOpForSelector.sol +71 -0
  43. package/src/test/IncoTest.sol +48 -0
  44. package/src/test/TestAddTwo.t.sol +31 -0
  45. package/src/test/TestDeploy.t.sol +39 -0
  46. package/src/test/TestExtractDataOfEventTooLarge.t.sol +43 -0
  47. package/src/test/TestFakeInfra.t.sol +301 -0
  48. package/src/test/TestVersion.t.sol +36 -0
  49. package/src/version/IncoLightningConfig.sol +13 -0
  50. package/src/version/Version.sol +76 -0
@@ -0,0 +1,61 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
5
+ import {EventCounter} from "./primitives/EventCounter.sol";
6
+ // import {console} from "forge-std/console.sol";
7
+ import {euint256, ebool, ETypes, EVM_HOST_CHAIN_PREFIX, HANDLE_VERSION, HANDLE_INDEX} from "../Types.sol";
8
+ import {HandleGeneration} from "./primitives/HandleGeneration.sol";
9
+
10
+ abstract contract EncryptedInput is
11
+ BaseAccessControlList,
12
+ EventCounter,
13
+ HandleGeneration
14
+ {
15
+ error HandleAlreadyExists();
16
+
17
+ event NewInput(
18
+ bytes32 indexed result,
19
+ address indexed contractAddress,
20
+ address indexed user,
21
+ ETypes inputType,
22
+ bytes ciphertext,
23
+ uint256 eventId
24
+ );
25
+
26
+ function newEuint256(
27
+ bytes memory ciphertext,
28
+ address user
29
+ ) external returns (euint256 newValue) {
30
+ return euint256.wrap(newInput(ciphertext, user, ETypes.Uint256));
31
+ }
32
+
33
+ function newEbool(
34
+ bytes memory ciphertext,
35
+ address user
36
+ ) external returns (ebool newValue) {
37
+ return ebool.wrap(newInput(ciphertext, user, ETypes.Bool));
38
+ }
39
+
40
+ function newInput(
41
+ bytes memory ciphertext,
42
+ address user,
43
+ ETypes inputType
44
+ ) internal returns (bytes32 newHandle) {
45
+ newHandle = getInputHandle(ciphertext, user, msg.sender, inputType);
46
+ // We assume that providing the same handle (which via HADU implies same plaintext, same context, and same
47
+ // instance of encryption)
48
+ require(!isAllowed(newHandle, user), HandleAlreadyExists());
49
+ // We allow to user since this is harmless and it is convenient to use the allow mapping to track existing
50
+ allowInternal(newHandle, user);
51
+ allowTransientInternal(newHandle, msg.sender);
52
+ emit NewInput(
53
+ newHandle,
54
+ msg.sender,
55
+ user,
56
+ inputType,
57
+ ciphertext,
58
+ getNewEventId()
59
+ );
60
+ }
61
+ }
@@ -0,0 +1,610 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {euint256, ebool, EOps, SenderNotAllowedForHandle, ETypes, isTypeSupported} from "../Types.sol";
5
+ import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
6
+ import {EventCounter} from "./primitives/EventCounter.sol";
7
+ import {HandleGeneration} from "./primitives/HandleGeneration.sol";
8
+
9
+ abstract contract EncryptedOperations is
10
+ BaseAccessControlList,
11
+ EventCounter,
12
+ HandleGeneration
13
+ {
14
+ error UnexpectedType(ETypes actual, ETypes expected);
15
+ error UnsupportedType(ETypes actual);
16
+
17
+ uint256 private randCounter;
18
+
19
+ event EAdd(
20
+ euint256 indexed lhs,
21
+ euint256 indexed rhs,
22
+ euint256 indexed result,
23
+ uint256 eventId
24
+ );
25
+ event ESub(
26
+ euint256 indexed lhs,
27
+ euint256 indexed rhs,
28
+ euint256 indexed result,
29
+ uint256 eventId
30
+ );
31
+ event EMul(
32
+ euint256 indexed lhs,
33
+ euint256 indexed rhs,
34
+ euint256 indexed result,
35
+ uint256 eventId
36
+ );
37
+ event EDiv(
38
+ euint256 indexed lhs,
39
+ euint256 indexed rhs,
40
+ euint256 indexed result,
41
+ uint256 eventId
42
+ );
43
+ event ERem(
44
+ euint256 indexed lhs,
45
+ euint256 indexed rhs,
46
+ euint256 indexed result,
47
+ uint256 eventId
48
+ );
49
+ event EBitAnd(
50
+ bytes32 indexed lhs,
51
+ bytes32 indexed rhs,
52
+ bytes32 indexed result,
53
+ uint256 eventId
54
+ );
55
+ event EBitOr(
56
+ bytes32 indexed lhs,
57
+ bytes32 indexed rhs,
58
+ bytes32 indexed result,
59
+ uint256 eventId
60
+ );
61
+ event EBitXor(
62
+ bytes32 indexed lhs,
63
+ bytes32 indexed rhs,
64
+ bytes32 indexed result,
65
+ uint256 eventId
66
+ );
67
+ event EShl(
68
+ euint256 indexed lhs,
69
+ euint256 indexed rhs,
70
+ euint256 indexed result,
71
+ uint256 eventId
72
+ );
73
+ event EShr(
74
+ euint256 indexed lhs,
75
+ euint256 indexed rhs,
76
+ euint256 indexed result,
77
+ uint256 eventId
78
+ );
79
+ event ERotl(
80
+ euint256 indexed lhs,
81
+ euint256 indexed rhs,
82
+ euint256 indexed result,
83
+ uint256 eventId
84
+ );
85
+ event ERotr(
86
+ euint256 indexed lhs,
87
+ euint256 indexed rhs,
88
+ euint256 indexed result,
89
+ uint256 eventId
90
+ );
91
+ event EEq(
92
+ euint256 indexed lhs,
93
+ euint256 indexed rhs,
94
+ ebool indexed result,
95
+ uint256 eventId
96
+ );
97
+ event ENe(
98
+ euint256 indexed lhs,
99
+ euint256 indexed rhs,
100
+ ebool indexed result,
101
+ uint256 eventId
102
+ );
103
+ event EGe(
104
+ euint256 indexed lhs,
105
+ euint256 indexed rhs,
106
+ ebool indexed result,
107
+ uint256 eventId
108
+ );
109
+ event EGt(
110
+ euint256 indexed lhs,
111
+ euint256 indexed rhs,
112
+ ebool indexed result,
113
+ uint256 eventId
114
+ );
115
+ event ELe(
116
+ euint256 indexed lhs,
117
+ euint256 indexed rhs,
118
+ ebool indexed result,
119
+ uint256 eventId
120
+ );
121
+ event ELt(
122
+ euint256 indexed lhs,
123
+ euint256 indexed rhs,
124
+ ebool indexed result,
125
+ uint256 eventId
126
+ );
127
+ event EMin(
128
+ euint256 indexed lhs,
129
+ euint256 indexed rhs,
130
+ euint256 indexed result,
131
+ uint256 eventId
132
+ );
133
+ event EMax(
134
+ euint256 indexed lhs,
135
+ euint256 indexed rhs,
136
+ euint256 indexed result,
137
+ uint256 eventId
138
+ );
139
+ event ERand(
140
+ uint256 indexed counter,
141
+ ETypes randType,
142
+ bytes32 indexed result,
143
+ uint256 eventId
144
+ );
145
+ event ERandBounded(
146
+ uint256 indexed counter,
147
+ ETypes randType,
148
+ bytes32 indexed upperBound,
149
+ bytes32 indexed result,
150
+ uint256 eventId
151
+ );
152
+ event EIfThenElse(
153
+ ebool control, // can't index >3 fields
154
+ bytes32 indexed ifTrue,
155
+ bytes32 indexed ifFalse,
156
+ bytes32 indexed result,
157
+ uint256 eventId
158
+ );
159
+ event ENot(ebool indexed operand, ebool indexed result, uint256 eventId);
160
+ event ECast(
161
+ bytes32 indexed ct,
162
+ uint8 indexed toType,
163
+ bytes32 indexed result,
164
+ uint256 eventId
165
+ );
166
+
167
+ modifier checked(euint256 lhs, euint256 rhs) {
168
+ checkInput(euint256.unwrap(lhs), ETypes.Uint256);
169
+ checkInput(euint256.unwrap(rhs), ETypes.Uint256);
170
+ _;
171
+ }
172
+
173
+ modifier checkedHandle(euint256 handle) {
174
+ checkInput(euint256.unwrap(handle), ETypes.Uint256);
175
+ _;
176
+ }
177
+
178
+ function checkInput(bytes32 input, ETypes requiredType) internal view {
179
+ require(
180
+ isAllowed(input, msg.sender),
181
+ SenderNotAllowedForHandle(input, msg.sender)
182
+ );
183
+ require(
184
+ typeOf(input) == requiredType,
185
+ UnexpectedType(typeOf(input), requiredType)
186
+ );
187
+ }
188
+
189
+ function createResultHandle(
190
+ EOps op,
191
+ ETypes returnType,
192
+ bytes32 lhs,
193
+ bytes32 rhs
194
+ ) internal returns (bytes32 result) {
195
+ result = getOpResultHandle(op, returnType, lhs, rhs);
196
+ allowTransientInternal(result, msg.sender);
197
+ }
198
+
199
+ function createResultHandle(
200
+ EOps op,
201
+ ETypes returnType,
202
+ bytes32 inputA,
203
+ bytes32 inputB,
204
+ bytes32 inputC
205
+ ) internal returns (bytes32 result) {
206
+ result = getOpResultHandle(op, returnType, inputA, inputB, inputC);
207
+ allowTransientInternal(result, msg.sender);
208
+ }
209
+
210
+ // todo replace with production implem
211
+ function createResultHandle(
212
+ EOps op,
213
+ ETypes returnType,
214
+ uint256 counter,
215
+ bytes32 upperBound
216
+ ) internal returns (bytes32 result) {
217
+ result = getOpResultHandle(op, returnType, counter, upperBound);
218
+ allowTransientInternal(result, msg.sender);
219
+ }
220
+
221
+ function createResultHandle(
222
+ EOps op,
223
+ ETypes returnType,
224
+ bytes32 value
225
+ ) internal returns (bytes32 result) {
226
+ result = getOpResultHandle(op, returnType, value);
227
+ allowTransientInternal(result, msg.sender);
228
+ }
229
+
230
+ function eAdd(
231
+ euint256 lhs,
232
+ euint256 rhs
233
+ ) external checked(lhs, rhs) returns (euint256 result) {
234
+ result = euint256.wrap(
235
+ createResultHandle(
236
+ EOps.Add,
237
+ ETypes.Uint256,
238
+ euint256.unwrap(lhs),
239
+ euint256.unwrap(rhs)
240
+ )
241
+ );
242
+ emit EAdd(lhs, rhs, result, getNewEventId());
243
+ }
244
+
245
+ function eSub(
246
+ euint256 lhs,
247
+ euint256 rhs
248
+ ) external checked(lhs, rhs) returns (euint256 result) {
249
+ result = euint256.wrap(
250
+ createResultHandle(
251
+ EOps.Sub,
252
+ ETypes.Uint256,
253
+ euint256.unwrap(lhs),
254
+ euint256.unwrap(rhs)
255
+ )
256
+ );
257
+ emit ESub(lhs, rhs, result, getNewEventId());
258
+ }
259
+
260
+ function eMul(
261
+ euint256 lhs,
262
+ euint256 rhs
263
+ ) external checked(lhs, rhs) returns (euint256 result) {
264
+ result = euint256.wrap(
265
+ createResultHandle(
266
+ EOps.Mul,
267
+ ETypes.Uint256,
268
+ euint256.unwrap(lhs),
269
+ euint256.unwrap(rhs)
270
+ )
271
+ );
272
+ emit EMul(lhs, rhs, result, getNewEventId());
273
+ }
274
+
275
+ function eDiv(
276
+ euint256 lhs,
277
+ euint256 rhs
278
+ ) external checked(lhs, rhs) returns (euint256 result) {
279
+ result = euint256.wrap(
280
+ createResultHandle(
281
+ EOps.Div,
282
+ ETypes.Uint256,
283
+ euint256.unwrap(lhs),
284
+ euint256.unwrap(rhs)
285
+ )
286
+ );
287
+ emit EDiv(lhs, rhs, result, getNewEventId());
288
+ }
289
+
290
+ function eRem(
291
+ euint256 lhs,
292
+ euint256 rhs
293
+ ) external checked(lhs, rhs) returns (euint256 result) {
294
+ result = euint256.wrap(
295
+ createResultHandle(
296
+ EOps.Rem,
297
+ ETypes.Uint256,
298
+ euint256.unwrap(lhs),
299
+ euint256.unwrap(rhs)
300
+ )
301
+ );
302
+ emit ERem(lhs, rhs, result, getNewEventId());
303
+ }
304
+
305
+ function eBitAnd(
306
+ bytes32 lhs,
307
+ bytes32 rhs
308
+ ) external returns (bytes32 result) {
309
+ ETypes lhsType = typeOf(lhs);
310
+ ETypes rhsType = typeOf(rhs);
311
+ checkInput(lhs, lhsType);
312
+ checkInput(rhs, rhsType);
313
+ require(lhsType == rhsType, UnexpectedType(lhsType, rhsType));
314
+ result = createResultHandle(EOps.BitAnd, lhsType, lhs, rhs);
315
+ emit EBitAnd(lhs, rhs, result, getNewEventId());
316
+ }
317
+
318
+ function eBitOr(
319
+ bytes32 lhs,
320
+ bytes32 rhs
321
+ ) external returns (bytes32 result) {
322
+ ETypes lhsType = typeOf(lhs);
323
+ ETypes rhsType = typeOf(rhs);
324
+ checkInput(lhs, lhsType);
325
+ checkInput(rhs, rhsType);
326
+ require(lhsType == rhsType, UnexpectedType(lhsType, rhsType));
327
+ result = createResultHandle(EOps.BitOr, lhsType, lhs, rhs);
328
+ emit EBitOr(lhs, rhs, result, getNewEventId());
329
+ }
330
+
331
+ function eBitXor(
332
+ bytes32 lhs,
333
+ bytes32 rhs
334
+ ) external returns (bytes32 result) {
335
+ ETypes lhsType = typeOf(lhs);
336
+ ETypes rhsType = typeOf(rhs);
337
+ checkInput(lhs, lhsType);
338
+ checkInput(rhs, rhsType);
339
+ require(lhsType == rhsType, UnexpectedType(lhsType, rhsType));
340
+ result = createResultHandle(EOps.BitXor, lhsType, lhs, rhs);
341
+ emit EBitXor(lhs, rhs, result, getNewEventId());
342
+ }
343
+
344
+ function eShl(
345
+ euint256 lhs,
346
+ euint256 rhs
347
+ ) external checked(lhs, rhs) returns (euint256 result) {
348
+ result = euint256.wrap(
349
+ createResultHandle(
350
+ EOps.Shl,
351
+ ETypes.Uint256,
352
+ euint256.unwrap(lhs),
353
+ euint256.unwrap(rhs)
354
+ )
355
+ );
356
+ emit EShl(lhs, rhs, result, getNewEventId());
357
+ }
358
+
359
+ function eShr(
360
+ euint256 lhs,
361
+ euint256 rhs
362
+ ) external checked(lhs, rhs) returns (euint256 result) {
363
+ result = euint256.wrap(
364
+ createResultHandle(
365
+ EOps.Shr,
366
+ ETypes.Uint256,
367
+ euint256.unwrap(lhs),
368
+ euint256.unwrap(rhs)
369
+ )
370
+ );
371
+ emit EShr(lhs, rhs, result, getNewEventId());
372
+ }
373
+
374
+ function eRotl(
375
+ euint256 lhs,
376
+ euint256 rhs
377
+ ) external checked(lhs, rhs) returns (euint256 result) {
378
+ result = euint256.wrap(
379
+ createResultHandle(
380
+ EOps.Rotl,
381
+ ETypes.Uint256,
382
+ euint256.unwrap(lhs),
383
+ euint256.unwrap(rhs)
384
+ )
385
+ );
386
+ emit ERotl(lhs, rhs, result, getNewEventId());
387
+ }
388
+
389
+ function eRotr(
390
+ euint256 lhs,
391
+ euint256 rhs
392
+ ) external checked(lhs, rhs) returns (euint256 result) {
393
+ result = euint256.wrap(
394
+ createResultHandle(
395
+ EOps.Rotr,
396
+ ETypes.Uint256,
397
+ euint256.unwrap(lhs),
398
+ euint256.unwrap(rhs)
399
+ )
400
+ );
401
+ emit ERotr(lhs, rhs, result, getNewEventId());
402
+ }
403
+
404
+ function eEq(
405
+ euint256 lhs,
406
+ euint256 rhs
407
+ ) external checked(lhs, rhs) returns (ebool result) {
408
+ result = ebool.wrap(
409
+ createResultHandle(
410
+ EOps.Eq,
411
+ ETypes.Bool,
412
+ euint256.unwrap(lhs),
413
+ euint256.unwrap(rhs)
414
+ )
415
+ );
416
+ emit EEq(lhs, rhs, result, getNewEventId());
417
+ }
418
+
419
+ function eNe(
420
+ euint256 lhs,
421
+ euint256 rhs
422
+ ) external checked(lhs, rhs) returns (ebool result) {
423
+ result = ebool.wrap(
424
+ createResultHandle(
425
+ EOps.Ne,
426
+ ETypes.Bool,
427
+ euint256.unwrap(lhs),
428
+ euint256.unwrap(rhs)
429
+ )
430
+ );
431
+ emit ENe(lhs, rhs, result, getNewEventId());
432
+ }
433
+
434
+ function eGe(
435
+ euint256 lhs,
436
+ euint256 rhs
437
+ ) external checked(lhs, rhs) returns (ebool result) {
438
+ result = ebool.wrap(
439
+ createResultHandle(
440
+ EOps.Ge,
441
+ ETypes.Bool,
442
+ euint256.unwrap(lhs),
443
+ euint256.unwrap(rhs)
444
+ )
445
+ );
446
+ emit EGe(lhs, rhs, result, getNewEventId());
447
+ }
448
+
449
+ function eGt(
450
+ euint256 lhs,
451
+ euint256 rhs
452
+ ) external checked(lhs, rhs) returns (ebool result) {
453
+ result = ebool.wrap(
454
+ createResultHandle(
455
+ EOps.Gt,
456
+ ETypes.Bool,
457
+ euint256.unwrap(lhs),
458
+ euint256.unwrap(rhs)
459
+ )
460
+ );
461
+ emit EGt(lhs, rhs, result, getNewEventId());
462
+ }
463
+
464
+ function eLe(
465
+ euint256 lhs,
466
+ euint256 rhs
467
+ ) external checked(lhs, rhs) returns (ebool result) {
468
+ result = ebool.wrap(
469
+ createResultHandle(
470
+ EOps.Le,
471
+ ETypes.Bool,
472
+ euint256.unwrap(lhs),
473
+ euint256.unwrap(rhs)
474
+ )
475
+ );
476
+ emit ELe(lhs, rhs, result, getNewEventId());
477
+ }
478
+
479
+ function eLt(
480
+ euint256 lhs,
481
+ euint256 rhs
482
+ ) external checked(lhs, rhs) returns (ebool result) {
483
+ result = ebool.wrap(
484
+ createResultHandle(
485
+ EOps.Lt,
486
+ ETypes.Bool,
487
+ euint256.unwrap(lhs),
488
+ euint256.unwrap(rhs)
489
+ )
490
+ );
491
+ emit ELt(lhs, rhs, result, getNewEventId());
492
+ }
493
+
494
+ function eMin(
495
+ euint256 lhs,
496
+ euint256 rhs
497
+ ) external checked(lhs, rhs) returns (euint256 result) {
498
+ result = euint256.wrap(
499
+ createResultHandle(
500
+ EOps.Min,
501
+ ETypes.Uint256,
502
+ euint256.unwrap(lhs),
503
+ euint256.unwrap(rhs)
504
+ )
505
+ );
506
+ emit EMin(lhs, rhs, result, getNewEventId());
507
+ }
508
+
509
+ function eMax(
510
+ euint256 lhs,
511
+ euint256 rhs
512
+ ) external checked(lhs, rhs) returns (euint256 result) {
513
+ result = euint256.wrap(
514
+ createResultHandle(
515
+ EOps.Max,
516
+ ETypes.Uint256,
517
+ euint256.unwrap(lhs),
518
+ euint256.unwrap(rhs)
519
+ )
520
+ );
521
+ emit EMax(lhs, rhs, result, getNewEventId());
522
+ }
523
+
524
+ function eNot(ebool operand) external returns (ebool result) {
525
+ checkInput(ebool.unwrap(operand), ETypes.Bool);
526
+ result = ebool.wrap(
527
+ createResultHandle(EOps.Not, ETypes.Bool, ebool.unwrap(operand))
528
+ );
529
+ emit ENot(operand, result, getNewEventId());
530
+ }
531
+
532
+ function eCast(
533
+ bytes32 ct,
534
+ ETypes toType
535
+ ) external returns (bytes32 result) {
536
+ bytes32 baseHandle = keccak256(abi.encodePacked(EOps.Cast, ct, toType));
537
+ result = embedTypeVersion(baseHandle, toType);
538
+ allowTransientInternal(result, msg.sender);
539
+ emit ECast(ct, uint8(toType), result, getNewEventId());
540
+ }
541
+
542
+ function eRand(
543
+ ETypes randType
544
+ ) external returns (bytes32 result) {
545
+ require(isTypeSupported(randType), UnsupportedType(randType));
546
+
547
+ result = createResultHandle(
548
+ EOps.Rand,
549
+ randType,
550
+ bytes32(randCounter++),
551
+ bytes32(uint256(randType))
552
+ );
553
+ emit ERand(randCounter, randType, result, getNewEventId());
554
+ }
555
+
556
+ function eRandBounded(
557
+ bytes32 upperBound,
558
+ ETypes randType
559
+ ) external returns (bytes32 result) {
560
+ require(isTypeSupported(randType), UnsupportedType(randType));
561
+ checkInput(upperBound, ETypes.Uint256);
562
+
563
+ result = createResultHandle(
564
+ EOps.RandBounded,
565
+ randType,
566
+ bytes32(randCounter++),
567
+ upperBound,
568
+ bytes32(uint256(randType))
569
+ );
570
+ emit ERandBounded(randCounter, randType, upperBound, result, getNewEventId());
571
+ }
572
+
573
+ // todo add support in testing framework
574
+ function eIfThenElse(
575
+ ebool control,
576
+ bytes32 ifTrue,
577
+ bytes32 ifFalse
578
+ ) external returns (bytes32 result) {
579
+ ETypes returnType = checkEIfThenElseInputs(control, ifTrue, ifFalse);
580
+ bytes32 baseHandle = keccak256(
581
+ abi.encodePacked(
582
+ EOps.IfThenElse,
583
+ ebool.unwrap(control),
584
+ ifTrue,
585
+ ifFalse
586
+ )
587
+ );
588
+ result = embedTypeVersion(baseHandle, returnType);
589
+ allowTransientInternal(result, msg.sender);
590
+ emit EIfThenElse(control, ifTrue, ifFalse, result, getNewEventId());
591
+ }
592
+
593
+ function checkEIfThenElseInputs(
594
+ ebool control,
595
+ bytes32 ifTrue,
596
+ bytes32 ifFalse
597
+ ) internal view returns (ETypes ifTrueType) {
598
+ checkInput(ebool.unwrap(control), ETypes.Bool);
599
+ ifTrueType = typeOf(ifTrue);
600
+ require(
601
+ ifTrueType == ETypes.Uint256 || ifTrueType == ETypes.Bool,
602
+ UnsupportedType(ifTrueType)
603
+ );
604
+ require(
605
+ isAllowed(ifTrue, msg.sender),
606
+ SenderNotAllowedForHandle(ifTrue, msg.sender)
607
+ );
608
+ checkInput(ifFalse, ifTrueType);
609
+ }
610
+ }
@@ -0,0 +1,43 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {EventCounter} from "./primitives/EventCounter.sol";
5
+ import {euint256, ebool, ETypes} from "../Types.sol";
6
+ import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
7
+ import {HandleGeneration} from "./primitives/HandleGeneration.sol";
8
+
9
+ abstract contract TrivialEncryption is
10
+ BaseAccessControlList,
11
+ EventCounter,
12
+ HandleGeneration
13
+ {
14
+ event TrivialEncrypt(
15
+ bytes32 indexed result,
16
+ bytes32 plainTextBytes,
17
+ ETypes handleType,
18
+ uint256 eventId
19
+ );
20
+
21
+ function asEuint256(uint256 value) external returns (euint256 newEuint256) {
22
+ return euint256.wrap(newTrivialEncrypt(bytes32(value), ETypes.Uint256));
23
+ }
24
+
25
+ function asEbool(bool value) external returns (ebool newEbool) {
26
+ bytes32 castedValue = bytes32(uint256(value ? 1 : 0));
27
+ return ebool.wrap(newTrivialEncrypt(castedValue, ETypes.Bool));
28
+ }
29
+
30
+ function newTrivialEncrypt(
31
+ bytes32 plainTextBytes,
32
+ ETypes handleType
33
+ ) internal returns (bytes32 newHandle) {
34
+ newHandle = getTrivialEncryptHandle(plainTextBytes, handleType);
35
+ allowTransientInternal(newHandle, msg.sender);
36
+ emit TrivialEncrypt(
37
+ newHandle,
38
+ plainTextBytes,
39
+ handleType,
40
+ getNewEventId()
41
+ );
42
+ }
43
+ }