@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,187 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ pragma solidity ^0.8.4;
3
+
4
+ /**
5
+ * @title CreateX Factory Interface Definition
6
+ * @author pcaversaccio (https://web.archive.org/web/20230921103111/https://pcaversaccio.com/)
7
+ * @custom:coauthor Matt Solomon (https://web.archive.org/web/20230921103335/https://mattsolomon.dev/)
8
+ */
9
+ interface ICreateX {
10
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
11
+ /* TYPES */
12
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
13
+
14
+ struct Values {
15
+ uint256 constructorAmount;
16
+ uint256 initCallAmount;
17
+ }
18
+
19
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
20
+ /* EVENTS */
21
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
22
+
23
+ event ContractCreation(address indexed newContract, bytes32 indexed salt);
24
+ event ContractCreation(address indexed newContract);
25
+ event Create3ProxyContractCreation(
26
+ address indexed newContract,
27
+ bytes32 indexed salt
28
+ );
29
+
30
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
31
+ /* CUSTOM ERRORS */
32
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
33
+
34
+ error FailedContractCreation(address emitter);
35
+ error FailedContractInitialisation(address emitter, bytes revertData);
36
+ error InvalidSalt(address emitter);
37
+ error InvalidNonceValue(address emitter);
38
+ error FailedEtherTransfer(address emitter, bytes revertData);
39
+
40
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
41
+ /* CREATE */
42
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
43
+
44
+ function deployCreate(
45
+ bytes memory initCode
46
+ ) external payable returns (address newContract);
47
+
48
+ function deployCreateAndInit(
49
+ bytes memory initCode,
50
+ bytes memory data,
51
+ Values memory values,
52
+ address refundAddress
53
+ ) external payable returns (address newContract);
54
+
55
+ function deployCreateAndInit(
56
+ bytes memory initCode,
57
+ bytes memory data,
58
+ Values memory values
59
+ ) external payable returns (address newContract);
60
+
61
+ function deployCreateClone(
62
+ address implementation,
63
+ bytes memory data
64
+ ) external payable returns (address proxy);
65
+
66
+ function computeCreateAddress(
67
+ address deployer,
68
+ uint256 nonce
69
+ ) external view returns (address computedAddress);
70
+
71
+ function computeCreateAddress(
72
+ uint256 nonce
73
+ ) external view returns (address computedAddress);
74
+
75
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
76
+ /* CREATE2 */
77
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
78
+
79
+ function deployCreate2(
80
+ bytes32 salt,
81
+ bytes memory initCode
82
+ ) external payable returns (address newContract);
83
+
84
+ function deployCreate2(
85
+ bytes memory initCode
86
+ ) external payable returns (address newContract);
87
+
88
+ function deployCreate2AndInit(
89
+ bytes32 salt,
90
+ bytes memory initCode,
91
+ bytes memory data,
92
+ Values memory values,
93
+ address refundAddress
94
+ ) external payable returns (address newContract);
95
+
96
+ function deployCreate2AndInit(
97
+ bytes32 salt,
98
+ bytes memory initCode,
99
+ bytes memory data,
100
+ Values memory values
101
+ ) external payable returns (address newContract);
102
+
103
+ function deployCreate2AndInit(
104
+ bytes memory initCode,
105
+ bytes memory data,
106
+ Values memory values,
107
+ address refundAddress
108
+ ) external payable returns (address newContract);
109
+
110
+ function deployCreate2AndInit(
111
+ bytes memory initCode,
112
+ bytes memory data,
113
+ Values memory values
114
+ ) external payable returns (address newContract);
115
+
116
+ function deployCreate2Clone(
117
+ bytes32 salt,
118
+ address implementation,
119
+ bytes memory data
120
+ ) external payable returns (address proxy);
121
+
122
+ function deployCreate2Clone(
123
+ address implementation,
124
+ bytes memory data
125
+ ) external payable returns (address proxy);
126
+
127
+ function computeCreate2Address(
128
+ bytes32 salt,
129
+ bytes32 initCodeHash,
130
+ address deployer
131
+ ) external pure returns (address computedAddress);
132
+
133
+ function computeCreate2Address(
134
+ bytes32 salt,
135
+ bytes32 initCodeHash
136
+ ) external view returns (address computedAddress);
137
+
138
+ /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
139
+ /* CREATE3 */
140
+ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
141
+
142
+ function deployCreate3(
143
+ bytes32 salt,
144
+ bytes memory initCode
145
+ ) external payable returns (address newContract);
146
+
147
+ function deployCreate3(
148
+ bytes memory initCode
149
+ ) external payable returns (address newContract);
150
+
151
+ function deployCreate3AndInit(
152
+ bytes32 salt,
153
+ bytes memory initCode,
154
+ bytes memory data,
155
+ Values memory values,
156
+ address refundAddress
157
+ ) external payable returns (address newContract);
158
+
159
+ function deployCreate3AndInit(
160
+ bytes32 salt,
161
+ bytes memory initCode,
162
+ bytes memory data,
163
+ Values memory values
164
+ ) external payable returns (address newContract);
165
+
166
+ function deployCreate3AndInit(
167
+ bytes memory initCode,
168
+ bytes memory data,
169
+ Values memory values,
170
+ address refundAddress
171
+ ) external payable returns (address newContract);
172
+
173
+ function deployCreate3AndInit(
174
+ bytes memory initCode,
175
+ bytes memory data,
176
+ Values memory values
177
+ ) external payable returns (address newContract);
178
+
179
+ function computeCreate3Address(
180
+ bytes32 salt,
181
+ address deployer
182
+ ) external pure returns (address computedAddress);
183
+
184
+ function computeCreate3Address(
185
+ bytes32 salt
186
+ ) external view returns (address computedAddress);
187
+ }
@@ -0,0 +1,48 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {e, euint256} from "../Lib.sol";
5
+
6
+ contract AddTwo {
7
+ using e for euint256;
8
+ using e for uint256;
9
+ using e for bytes;
10
+
11
+ // Stores the result of the last callback.
12
+ uint256 public lastResult;
13
+
14
+ function addTwo(euint256 a) external returns (euint256) {
15
+ uint256 two = 2;
16
+ return a.add(two.asEuint256());
17
+ }
18
+
19
+ function addTwoScalar(euint256 a) external returns (euint256) {
20
+ uint256 two = 2;
21
+ return a.add(two);
22
+ }
23
+
24
+ // addTwoEOA is the equivalent of addTwo, but it allows an EOA to call it
25
+ // with an encrypted input.
26
+ function addTwoEOA(
27
+ bytes memory uint256EInput
28
+ ) external returns (uint256, euint256) {
29
+ euint256 value = uint256EInput.newEuint256(msg.sender);
30
+ euint256 result = this.addTwo(value);
31
+ e.allow(result, address(this));
32
+ e.allow(result, msg.sender);
33
+ uint256 requestId = e.requestDecryption(
34
+ result,
35
+ this.callback.selector,
36
+ ""
37
+ );
38
+ return (requestId, result);
39
+ }
40
+
41
+ function callback(
42
+ uint256 /* requestId */,
43
+ uint256 result,
44
+ bytes memory /* data */
45
+ ) external {
46
+ lastResult = result;
47
+ }
48
+ }
@@ -0,0 +1,137 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {EOps, ETypes} from "../../Types.sol";
5
+ import {asBytes32} from "@inco/shared/src/TypeUtils.sol";
6
+
7
+ contract FakeComputeServer {
8
+ function computeBinaryUintOp(
9
+ uint256 lhs,
10
+ uint256 rhs,
11
+ EOps op
12
+ ) internal pure returns (uint256) {
13
+ if (op == EOps.Add) {
14
+ return lhs + rhs;
15
+ } else if (op == EOps.Sub) {
16
+ return lhs - rhs;
17
+ } else if (op == EOps.Mul) {
18
+ return lhs * rhs;
19
+ } else if (op == EOps.Div) {
20
+ return lhs / rhs;
21
+ } else if (op == EOps.Rem) {
22
+ return lhs % rhs;
23
+ } else if (op == EOps.Min) {
24
+ return lhs < rhs ? lhs : rhs;
25
+ } else if (op == EOps.Max) {
26
+ return lhs > rhs ? lhs : rhs;
27
+ } else if (op == EOps.BitAnd) {
28
+ return lhs & rhs;
29
+ } else if (op == EOps.BitOr) {
30
+ return lhs | rhs;
31
+ } else if (op == EOps.BitXor) {
32
+ return lhs ^ rhs;
33
+ } else if (op == EOps.Shl) {
34
+ return lhs << rhs;
35
+ } else if (op == EOps.Shr) {
36
+ return lhs >> rhs;
37
+ } else if (op == EOps.Rotl) {
38
+ return (lhs << rhs) | (lhs >> (256 - rhs));
39
+ } else if (op == EOps.Rotr) {
40
+ return (lhs >> rhs) | (lhs << (256 - rhs));
41
+ } else {
42
+ revert("computeBinaryUintOp Invalid binary uint op");
43
+ }
44
+ }
45
+
46
+ function computeBinaryUintToBoolOp(
47
+ uint256 lhs,
48
+ uint256 rhs,
49
+ EOps op
50
+ ) internal pure returns (bool) {
51
+ if (op == EOps.Eq) {
52
+ return lhs == rhs;
53
+ } else if (op == EOps.Ne) {
54
+ return lhs != rhs;
55
+ } else if (op == EOps.Ge) {
56
+ return lhs >= rhs;
57
+ } else if (op == EOps.Gt) {
58
+ return lhs > rhs;
59
+ } else if (op == EOps.Le) {
60
+ return lhs <= rhs;
61
+ } else if (op == EOps.Lt) {
62
+ return lhs < rhs;
63
+ } else {
64
+ revert("computeBinaryUintToBoolOp Invalid binary uint to bool op");
65
+ }
66
+ }
67
+
68
+ function isBinaryUintToUintOp(EOps op) internal pure returns (bool) {
69
+ return
70
+ op == EOps.Add ||
71
+ op == EOps.Sub ||
72
+ op == EOps.Mul ||
73
+ op == EOps.Div ||
74
+ op == EOps.Rem ||
75
+ op == EOps.Min ||
76
+ op == EOps.Max ||
77
+ op == EOps.BitAnd ||
78
+ op == EOps.BitOr ||
79
+ op == EOps.BitXor ||
80
+ op == EOps.Shl ||
81
+ op == EOps.Shr ||
82
+ op == EOps.Rotl ||
83
+ op == EOps.Rotr;
84
+ }
85
+
86
+ function isBinaryUintToBoolOp(EOps op) internal pure returns (bool) {
87
+ return
88
+ op == EOps.Eq ||
89
+ op == EOps.Ne ||
90
+ op == EOps.Ge ||
91
+ op == EOps.Gt ||
92
+ op == EOps.Le ||
93
+ op == EOps.Lt;
94
+ }
95
+
96
+ function opToResultType(EOps op) internal pure returns (ETypes) {
97
+ if (isBinaryUintToUintOp(op)) {
98
+ return ETypes.Uint256;
99
+ } else if (isBinaryUintToBoolOp(op)) {
100
+ return ETypes.Bool;
101
+ } else {
102
+ revert("opToResultType Invalid op");
103
+ }
104
+ }
105
+
106
+ function isBinaryUintOp(EOps op) internal pure returns (bool) {
107
+ return isBinaryUintToUintOp(op) || isBinaryUintToBoolOp(op);
108
+ }
109
+
110
+ function computeBinaryOp(
111
+ bytes32 encodedLhs,
112
+ bytes32 encodedRhs,
113
+ EOps op
114
+ ) internal pure returns (bytes32) {
115
+ if (isBinaryUintToUintOp(op)) {
116
+ return
117
+ bytes32(
118
+ computeBinaryUintOp(
119
+ uint256(encodedLhs),
120
+ uint256(encodedRhs),
121
+ op
122
+ )
123
+ );
124
+ } else if (isBinaryUintToBoolOp(op)) {
125
+ return
126
+ asBytes32(
127
+ computeBinaryUintToBoolOp(
128
+ uint256(encodedLhs),
129
+ uint256(encodedRhs),
130
+ op
131
+ )
132
+ );
133
+ } else {
134
+ revert("computeBinaryOp Invalid binary op");
135
+ }
136
+ }
137
+ }
@@ -0,0 +1,77 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {ebool, euint256, ETypes} from "../../Types.sol";
5
+ import {inco} from "../../Lib.sol";
6
+ import {Vm} from "forge-std/Test.sol";
7
+ import {TestUtils} from "@inco/shared/src/TestUtils.sol";
8
+ import {DecryptionResult} from "../../lightning-parts/DecryptionHandler.sol";
9
+ import {KVStore} from "./KVStore.sol";
10
+
11
+ /// @notice simulates what inco does offchain but over plaintexts
12
+ contract FakeIncoInfraBase is TestUtils, KVStore {
13
+ error UnsupportedTypeInput(ETypes inputType);
14
+
15
+ address immutable teePubkeyAddress;
16
+ uint256 immutable teePrivKey;
17
+
18
+ constructor() {
19
+ (teePrivKey, teePubkeyAddress) = getLabeledKeyPair("tee");
20
+ }
21
+
22
+ function handleDecryptionRequest(
23
+ uint256 requestId,
24
+ bytes32 handle
25
+ ) internal {
26
+ DecryptionResult memory result = DecryptionResult({
27
+ abiEncodedResult: get(handle),
28
+ requestId: requestId
29
+ });
30
+ vm.prank(teePubkeyAddress);
31
+ inco.fulfillRequest(result, "");
32
+ }
33
+
34
+ function fakePrepareEuint256Ciphertext(
35
+ uint256 value
36
+ ) internal pure returns (bytes memory ciphertext) {
37
+ ciphertext = abi.encode(value);
38
+ }
39
+
40
+ function fakeDecryptEuint256Ciphertext(
41
+ bytes memory ciphertext
42
+ ) internal pure returns (uint256 value) {
43
+ value = abi.decode(ciphertext, (uint256));
44
+ }
45
+
46
+ function fakePrepareEboolCiphertext(
47
+ bool value
48
+ ) internal pure returns (bytes memory ciphertext) {
49
+ ciphertext = abi.encode(value);
50
+ }
51
+
52
+ function fakeDecryptEboolCiphertext(
53
+ bytes memory ciphertext
54
+ ) internal pure returns (bool value) {
55
+ value = abi.decode(ciphertext, (bool));
56
+ }
57
+
58
+ function fakeEncryptBool(bool value) internal pure returns (ebool) {
59
+ return ebool.wrap(bytes32(value ? uint256(1) : uint256(0)));
60
+ }
61
+
62
+ function fakeEncryptUint256(
63
+ uint256 value
64
+ ) internal pure returns (euint256) {
65
+ return euint256.wrap(bytes32(value));
66
+ }
67
+
68
+ function fakeDecryptEbool(ebool handle) internal pure returns (bool) {
69
+ return ebool.unwrap(handle) == bytes32(uint256(1));
70
+ }
71
+
72
+ function fakeDecryptEuint256(
73
+ euint256 handle
74
+ ) internal pure returns (uint256) {
75
+ return uint256(euint256.unwrap(handle));
76
+ }
77
+ }
@@ -0,0 +1,35 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {HandleMetadata} from "../../lightning-parts/primitives/HandleMetadata.sol";
5
+ import {ETypes, euint256, ebool} from "../../Types.sol";
6
+ import {asBool} from "@inco/shared/src/TypeUtils.sol";
7
+
8
+ /// @notice key-value store, knows the value behind each handle
9
+ contract KVStore is HandleMetadata {
10
+ mapping(bytes32 => bytes32) private store;
11
+
12
+ function set(bytes32 key, bytes32 value) internal {
13
+ store[key] = value;
14
+ }
15
+
16
+ function get(bytes32 key) internal view returns (bytes32) {
17
+ return store[key];
18
+ }
19
+
20
+ function getUint256Value(euint256 input) internal view returns (uint256) {
21
+ bytes32 handle = euint256.unwrap(input);
22
+ checkType(handle, ETypes.Uint256);
23
+ return uint256(get(handle));
24
+ }
25
+
26
+ function getBoolValue(ebool input) internal view returns (bool) {
27
+ bytes32 handle = ebool.unwrap(input);
28
+ checkType(handle, ETypes.Bool);
29
+ return asBool(get(handle));
30
+ }
31
+
32
+ function checkType(bytes32 handle, ETypes requiredType) private pure {
33
+ assert(typeOf(handle) == requiredType);
34
+ }
35
+ }
@@ -0,0 +1,140 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {Vm} from "forge-std/Test.sol";
5
+ import {inco} from "../../Lib.sol";
6
+ import {ebool, euint256, ETypes, EOps} from "../../Types.sol";
7
+ import {DecryptionRequested} from "../../lightning-parts/DecryptionHandler.sol";
8
+ import {FakeComputeServer} from "./FakeComputeServer.sol";
9
+ import {FakeIncoInfraBase} from "./FakeIncoInfraBase.sol";
10
+ import {asBytes32} from "@inco/shared/src/TypeUtils.sol";
11
+ import {getOpForSelector} from "./getOpForSelector.sol";
12
+
13
+ contract MockOpHandler is FakeIncoInfraBase, FakeComputeServer {
14
+ function processAllOperations() internal {
15
+ Vm.Log[] memory logs = vm.getRecordedLogs();
16
+ for (uint256 i = 0; i < logs.length; i++) {
17
+ if (logs[i].emitter == address(inco)) {
18
+ handleIncoLog(logs[i]);
19
+ }
20
+ }
21
+ }
22
+
23
+ function handleIncoLog(Vm.Log memory log) internal {
24
+ bytes32 eventSelector = log.topics[0];
25
+ EOps op = getOpForSelector(eventSelector);
26
+ if (op == EOps.TrivialEncrypt) {
27
+ bytes32 result = log.topics[1];
28
+ (bytes32 plainTextBytes, , ) = abi.decode(
29
+ log.data,
30
+ (bytes32, ETypes, uint256)
31
+ );
32
+ handleTrivialEncryption(result, plainTextBytes);
33
+ } else if (isBinaryUintOp(op)) {
34
+ bytes32 lhs = log.topics[1];
35
+ bytes32 rhs = log.topics[2];
36
+ bytes32 result = log.topics[3];
37
+ set(result, computeBinaryOp(get(lhs), get(rhs), op));
38
+ } else if (op == EOps.IfThenElse) {
39
+ bytes memory logData = log.data;
40
+ ebool control;
41
+ bytes32 ifTrue = log.topics[1];
42
+ bytes32 ifFalse = log.topics[2];
43
+ bytes32 result = log.topics[3];
44
+ assembly {
45
+ control := mload(add(logData, 0x20))
46
+ }
47
+ handleIfThenElse(control, ifTrue, ifFalse, result);
48
+ } else if (op == EOps.Not) {
49
+ ebool value = ebool.wrap(log.topics[1]);
50
+ ebool result = ebool.wrap(log.topics[2]);
51
+ handleENot(value, result);
52
+ } else if (op == EOps.Rand) {
53
+ uint256 counter = uint256(log.topics[1]);
54
+ euint256 result = euint256.wrap(log.topics[2]);
55
+ handleERand(counter, result);
56
+ } else if (op == EOps.RandBounded) {
57
+ uint256 counter = uint256(log.topics[1]);
58
+ euint256 upperBound = euint256.wrap(log.topics[2]);
59
+ euint256 result = euint256.wrap(log.topics[3]);
60
+ handleERandBounded(counter, upperBound, result);
61
+ } else if (op == EOps.Cast) {
62
+ bytes32 ct = log.topics[1];
63
+ uint8 toType = uint8(uint256(log.topics[2]));
64
+ bytes32 result = log.topics[3];
65
+ handleECast(ct, toType, result);
66
+ } else if (op == EOps.NewInput) {
67
+ bytes32 result = log.topics[1];
68
+ // contractAddress and user topics are ignored
69
+ (ETypes inputType, bytes memory ciphertext, ) = abi.decode(
70
+ log.data,
71
+ (ETypes, bytes, uint256)
72
+ );
73
+ handleEInput(result, inputType, ciphertext);
74
+ } else if (op == EOps.DecryptionRequested) {
75
+ uint256 requestId = uint256(log.topics[1]);
76
+ bytes32 handle = log.topics[2];
77
+ // other fields ignored for now
78
+ // todo cheats to trigger decryption later
79
+ handleDecryptionRequest(requestId, handle);
80
+ }
81
+ }
82
+
83
+ function handleEInput(
84
+ bytes32 result,
85
+ ETypes inputType,
86
+ bytes memory ciphertext
87
+ ) private {
88
+ if (inputType == ETypes.Uint256) {
89
+ set(result, bytes32(fakeDecryptEuint256Ciphertext(ciphertext)));
90
+ } else if (inputType == ETypes.Bool) {
91
+ set(result, asBytes32(fakeDecryptEboolCiphertext(ciphertext)));
92
+ } else {
93
+ revert UnsupportedTypeInput(inputType);
94
+ }
95
+ }
96
+
97
+ function handleENot(ebool value, ebool result) private {
98
+ set(ebool.unwrap(result), asBytes32(!getBoolValue(value)));
99
+ }
100
+
101
+ function handleERand(uint256 counter, euint256 result) private {
102
+ set(euint256.unwrap(result), bytes32(counter));
103
+ }
104
+
105
+ function handleERandBounded(
106
+ uint256 counter,
107
+ euint256 upperBound,
108
+ euint256 result
109
+ ) private {
110
+ set(
111
+ euint256.unwrap(result),
112
+ bytes32(counter % getUint256Value(upperBound))
113
+ );
114
+ }
115
+
116
+ function handleIfThenElse(
117
+ ebool control,
118
+ bytes32 lhs,
119
+ bytes32 rhs,
120
+ bytes32 result
121
+ ) private {
122
+ set(result, bytes32(getBoolValue(control) ? get(lhs) : get(rhs)));
123
+ }
124
+
125
+ function handleECast(bytes32 handle, uint8 toType, bytes32 result) private {
126
+ ETypes _toType = ETypes(toType);
127
+ if (_toType == ETypes.Bool || _toType == ETypes.Uint256) {
128
+ set(result, get(handle));
129
+ } else {
130
+ revert UnsupportedTypeInput(ETypes(toType));
131
+ }
132
+ }
133
+
134
+ function handleTrivialEncryption(
135
+ bytes32 result,
136
+ bytes32 plainTextBytes
137
+ ) private {
138
+ set(result, plainTextBytes);
139
+ }
140
+ }
@@ -0,0 +1,71 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {DecryptionRequested} from "../../lightning-parts/DecryptionHandler.sol";
5
+ import {EncryptedOperations} from "../../lightning-parts/EncryptedOperations.sol";
6
+ import {TrivialEncryption} from "../../lightning-parts/TrivialEncryption.sol";
7
+ import {EncryptedInput} from "../../lightning-parts/EncryptedInput.sol";
8
+ import {EOps} from "../../Types.sol";
9
+
10
+ // letting this func alone in its file as multiple of this type gets unreadable fast
11
+ function getOpForSelector(bytes32 opEventSelector) pure returns (EOps) {
12
+ if (opEventSelector == EncryptedOperations.EAdd.selector) {
13
+ return EOps.Add;
14
+ } else if (opEventSelector == EncryptedOperations.ESub.selector) {
15
+ return EOps.Sub;
16
+ } else if (opEventSelector == EncryptedOperations.EMul.selector) {
17
+ return EOps.Mul;
18
+ } else if (opEventSelector == EncryptedOperations.EDiv.selector) {
19
+ return EOps.Div;
20
+ } else if (opEventSelector == EncryptedOperations.ERem.selector) {
21
+ return EOps.Rem;
22
+ } else if (opEventSelector == EncryptedOperations.EBitAnd.selector) {
23
+ return EOps.BitAnd;
24
+ } else if (opEventSelector == EncryptedOperations.EBitOr.selector) {
25
+ return EOps.BitOr;
26
+ } else if (opEventSelector == EncryptedOperations.EBitXor.selector) {
27
+ return EOps.BitXor;
28
+ } else if (opEventSelector == EncryptedOperations.EShl.selector) {
29
+ return EOps.Shl;
30
+ } else if (opEventSelector == EncryptedOperations.EShr.selector) {
31
+ return EOps.Shr;
32
+ } else if (opEventSelector == EncryptedOperations.ERotl.selector) {
33
+ return EOps.Rotl;
34
+ } else if (opEventSelector == EncryptedOperations.ERotr.selector) {
35
+ return EOps.Rotr;
36
+ } else if (opEventSelector == EncryptedOperations.EEq.selector) {
37
+ return EOps.Eq;
38
+ } else if (opEventSelector == EncryptedOperations.ENe.selector) {
39
+ return EOps.Ne;
40
+ } else if (opEventSelector == EncryptedOperations.EGe.selector) {
41
+ return EOps.Ge;
42
+ } else if (opEventSelector == EncryptedOperations.EGt.selector) {
43
+ return EOps.Gt;
44
+ } else if (opEventSelector == EncryptedOperations.ELe.selector) {
45
+ return EOps.Le;
46
+ } else if (opEventSelector == EncryptedOperations.ELt.selector) {
47
+ return EOps.Lt;
48
+ } else if (opEventSelector == EncryptedOperations.EMin.selector) {
49
+ return EOps.Min;
50
+ } else if (opEventSelector == EncryptedOperations.EMax.selector) {
51
+ return EOps.Max;
52
+ } else if (opEventSelector == EncryptedOperations.ENot.selector) {
53
+ return EOps.Not;
54
+ } else if (opEventSelector == EncryptedInput.NewInput.selector) {
55
+ return EOps.NewInput;
56
+ } else if (opEventSelector == EncryptedOperations.ECast.selector) {
57
+ return EOps.Cast;
58
+ } else if (opEventSelector == TrivialEncryption.TrivialEncrypt.selector) {
59
+ return EOps.TrivialEncrypt;
60
+ } else if (opEventSelector == EncryptedOperations.EIfThenElse.selector) {
61
+ return EOps.IfThenElse;
62
+ } else if (opEventSelector == EncryptedOperations.ERand.selector) {
63
+ return EOps.Rand;
64
+ } else if (opEventSelector == EncryptedOperations.ERandBounded.selector) {
65
+ return EOps.RandBounded;
66
+ } else if (opEventSelector == DecryptionRequested.selector) {
67
+ return EOps.DecryptionRequested;
68
+ } else {
69
+ revert("getOpForSelector: Unsupported selector");
70
+ }
71
+ }