@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,32 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ contract EventCounterStorage {
5
+ struct Storage {
6
+ uint256 eventCounter;
7
+ }
8
+
9
+ bytes32 private constant eventCounterStorageLocation =
10
+ keccak256("lightning.storage.EventCounter");
11
+
12
+ function getEventCounterStorage()
13
+ internal
14
+ pure
15
+ returns (Storage storage $)
16
+ {
17
+ bytes32 loc = eventCounterStorageLocation;
18
+ assembly {
19
+ $.slot := loc
20
+ }
21
+ }
22
+ }
23
+
24
+ contract EventCounter is EventCounterStorage {
25
+ function getNewEventId() internal returns (uint256 newEventId) {
26
+ newEventId = getEventCounterStorage().eventCounter++;
27
+ }
28
+
29
+ function getEventCounter() external view returns (uint256) {
30
+ return getEventCounterStorage().eventCounter;
31
+ }
32
+ }
@@ -0,0 +1,107 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {ETypes, EOps, EVM_HOST_CHAIN_PREFIX, HANDLE_INDEX} from "../../Types.sol";
5
+ import {HandleMetadata} from "./HandleMetadata.sol";
6
+
7
+ contract HandleGeneration is HandleMetadata {
8
+ function getTrivialEncryptHandle(
9
+ bytes32 plaintextBytes,
10
+ ETypes handleType
11
+ ) public view returns (bytes32 generatedHandle) {
12
+ generatedHandle = keccak256(
13
+ abi.encodePacked(
14
+ EVM_HOST_CHAIN_PREFIX,
15
+ block.chainid, // todo cache this
16
+ EOps.TrivialEncrypt,
17
+ plaintextBytes,
18
+ handleType,
19
+ address(this) // todo cache this
20
+ )
21
+ );
22
+ generatedHandle = embedTypeVersion(generatedHandle, handleType);
23
+ }
24
+
25
+ function getInputHandle(
26
+ bytes memory ciphertext,
27
+ address user,
28
+ address contractAddress,
29
+ ETypes inputType
30
+ ) public view returns (bytes32 generatedHandle) {
31
+ // Here we ensure that our hashing scheme is binary-compatible between IncoLightning and IncoFhevm, this helps
32
+ // keep client-side code consistent in its ciphertext, context => handle mappings
33
+ bytes32 ctIndexHash = keccak256(
34
+ abi.encodePacked(keccak256(ciphertext), HANDLE_INDEX)
35
+ );
36
+ bytes32 prehandle = embedIndexTypeVersion(ctIndexHash, inputType);
37
+ // We must also propagate the handle metadata to the final handle
38
+ generatedHandle = embedIndexTypeVersion(
39
+ keccak256(
40
+ abi.encodePacked(
41
+ prehandle,
42
+ EVM_HOST_CHAIN_PREFIX,
43
+ block.chainid, // todo cache this
44
+ address(this), // todo cache this
45
+ user,
46
+ contractAddress
47
+ )
48
+ ),
49
+ inputType
50
+ );
51
+ }
52
+
53
+ function getOpResultHandle(
54
+ EOps op,
55
+ ETypes returnType,
56
+ bytes32 lhs,
57
+ bytes32 rhs
58
+ ) public pure returns (bytes32 generatedHandle) {
59
+ generatedHandle = getOpResultHandle(
60
+ keccak256(abi.encodePacked(op, lhs, rhs)),
61
+ returnType
62
+ );
63
+ }
64
+
65
+ function getOpResultHandle(
66
+ EOps op,
67
+ ETypes returnType,
68
+ uint256 counter,
69
+ bytes32 upperBound
70
+ ) public pure returns (bytes32 generatedHandle) {
71
+ generatedHandle = getOpResultHandle(
72
+ keccak256(abi.encodePacked(op, counter, upperBound)),
73
+ returnType
74
+ );
75
+ }
76
+
77
+ function getOpResultHandle(
78
+ EOps op,
79
+ ETypes returnType,
80
+ bytes32 value
81
+ ) public pure returns (bytes32 generatedHandle) {
82
+ generatedHandle = getOpResultHandle(
83
+ keccak256(abi.encodePacked(op, value)),
84
+ returnType
85
+ );
86
+ }
87
+
88
+ function getOpResultHandle(
89
+ EOps op,
90
+ ETypes returnType,
91
+ bytes32 inputA,
92
+ bytes32 inputB,
93
+ bytes32 inputC
94
+ ) public pure returns (bytes32 generatedHandle) {
95
+ generatedHandle = getOpResultHandle(
96
+ keccak256(abi.encodePacked(op, inputA, inputB, inputC)),
97
+ returnType
98
+ );
99
+ }
100
+
101
+ function getOpResultHandle(
102
+ bytes32 baseHandle,
103
+ ETypes returnType
104
+ ) internal pure returns (bytes32 generatedHandle) {
105
+ generatedHandle = embedTypeVersion(baseHandle, returnType);
106
+ }
107
+ }
@@ -0,0 +1,38 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {HANDLE_VERSION, HANDLE_INDEX, ETypes} from "../../Types.sol";
5
+
6
+ contract HandleMetadata {
7
+ function embedIndexTypeVersion(
8
+ bytes32 prehandle,
9
+ ETypes inputType
10
+ ) internal pure returns (bytes32 result) {
11
+ // Create a mask to clear the last three bytes
12
+ bytes32 mask = bytes32(
13
+ uint256(
14
+ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FFFF
15
+ )
16
+ );
17
+ // Clear the last three bytes of the original value
18
+ bytes32 clearedOriginal = prehandle & mask;
19
+ // Combine the cleared original value with the new last three bytes
20
+ result = clearedOriginal | bytes32((uint256(HANDLE_INDEX) << 16));
21
+ result = embedTypeVersion(result, inputType);
22
+ }
23
+
24
+ function embedTypeVersion(
25
+ bytes32 prehandle,
26
+ ETypes handleType
27
+ ) internal pure returns (bytes32 result) {
28
+ result =
29
+ prehandle &
30
+ 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000;
31
+ result = bytes32(uint256(result) | (uint256(handleType) << 8)); // append type
32
+ result = bytes32(uint256(result) | HANDLE_VERSION);
33
+ }
34
+
35
+ function typeOf(bytes32 handle) internal pure returns (ETypes) {
36
+ return ETypes(uint8(uint256(handle) >> 8));
37
+ }
38
+ }
@@ -0,0 +1,47 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
5
+ import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
6
+
7
+ contract SignatureVerifierStorage {
8
+ struct StorageForSigVerifier {
9
+ address PubkeyAddress;
10
+ }
11
+
12
+ bytes32 private constant SignatureVerifierStorageLocation =
13
+ keccak256("inco.storage.SignatureVerifier");
14
+
15
+ function getSigVerifierStorage()
16
+ internal
17
+ pure
18
+ returns (StorageForSigVerifier storage $)
19
+ {
20
+ bytes32 loc = SignatureVerifierStorageLocation;
21
+ assembly {
22
+ $.slot := loc
23
+ }
24
+ }
25
+ }
26
+
27
+ contract SignatureVerifier is OwnableUpgradeable, SignatureVerifierStorage {
28
+ using ECDSA for bytes32;
29
+
30
+ event PubkeyAddressChanged(address newPubkeyAddress);
31
+
32
+ function editPubkeyAddress(address newPubkeyAddress) external onlyOwner {
33
+ getSigVerifierStorage().PubkeyAddress = newPubkeyAddress;
34
+ emit PubkeyAddressChanged(newPubkeyAddress);
35
+ }
36
+
37
+ function opSignerPubkeyAddress() public view returns (address) {
38
+ return getSigVerifierStorage().PubkeyAddress;
39
+ }
40
+
41
+ function isValidSignature(
42
+ bytes32 hash,
43
+ bytes memory signature
44
+ ) public view returns (bool) {
45
+ return hash.recover(signature) == getSigVerifierStorage().PubkeyAddress;
46
+ }
47
+ }
@@ -0,0 +1,87 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {TestUtils} from "@inco/shared/src/TestUtils.sol";
5
+ import {HandleMetadata} from "../primitives/HandleMetadata.sol";
6
+ import {TrivialEncryption} from "../TrivialEncryption.sol";
7
+ import {EncryptedOperations} from "../EncryptedOperations.sol";
8
+ import {EncryptedInput} from "../EncryptedInput.sol";
9
+ import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
10
+ import {ETypes, ebool, euint256} from "../../Types.sol";
11
+
12
+ contract TestHandleMetadata is
13
+ EIP712,
14
+ HandleMetadata,
15
+ TestUtils,
16
+ TrivialEncryption,
17
+ EncryptedOperations,
18
+ EncryptedInput
19
+ {
20
+ constructor() EIP712("", "") {}
21
+
22
+ function testTypeAssignment() public pure {
23
+ bytes32 someHandle = bytes32(keccak256("someHandle"));
24
+ assert(
25
+ typeOf(embedIndexTypeVersion(someHandle, ETypes.Bool)) ==
26
+ ETypes.Bool
27
+ );
28
+ assert(
29
+ typeOf(embedIndexTypeVersion(someHandle, ETypes.Uint256)) ==
30
+ ETypes.Uint256
31
+ );
32
+ }
33
+
34
+ function testTrivialEncryptionHandleType() public {
35
+ bytes32 boolHandle = ebool.unwrap(this.asEbool(true));
36
+ assert(typeOf(boolHandle) == ETypes.Bool);
37
+ bytes32 uintHandle = euint256.unwrap(this.asEuint256(42));
38
+ assert(typeOf(uintHandle) == ETypes.Uint256);
39
+ }
40
+
41
+ function testOperationsHandleType() public {
42
+ euint256 a = this.asEuint256(42);
43
+ euint256 b = this.asEuint256(12);
44
+ ebool control = this.asEbool(true);
45
+ ebool c = this.asEbool(false);
46
+ ebool d = this.asEbool(true);
47
+
48
+ assert(typeOf(euint256.unwrap(this.eAdd(a, b))) == ETypes.Uint256);
49
+ assert(typeOf(euint256.unwrap(this.eSub(a, b))) == ETypes.Uint256);
50
+ assert(typeOf(ebool.unwrap(this.eGe(a, b))) == ETypes.Bool);
51
+ assert(
52
+ typeOf(
53
+ this.eIfThenElse(
54
+ control,
55
+ euint256.unwrap(a),
56
+ euint256.unwrap(b)
57
+ )
58
+ ) == ETypes.Uint256
59
+ );
60
+ assert(
61
+ typeOf(
62
+ this.eIfThenElse(control, ebool.unwrap(c), ebool.unwrap(d))
63
+ ) == ETypes.Bool
64
+ );
65
+ }
66
+
67
+ function testEIfThenElseChecksTypeCoherence() public {
68
+ ebool control = this.asEbool(false);
69
+ euint256 a = this.asEuint256(42);
70
+ ebool b = this.asEbool(true);
71
+ vm.expectRevert(
72
+ abi.encodeWithSelector(
73
+ EncryptedOperations.UnexpectedType.selector,
74
+ ETypes.Bool,
75
+ ETypes.Uint256
76
+ )
77
+ );
78
+ this.eIfThenElse(control, euint256.unwrap(a), ebool.unwrap(b));
79
+ }
80
+
81
+ function testEncryptedInputHandleType() public {
82
+ euint256 a = this.newEuint256("ciphertext", address(this));
83
+ assert(typeOf(euint256.unwrap(a)) == ETypes.Uint256);
84
+ ebool b = this.newEbool("ciphertext", address(this));
85
+ assert(typeOf(ebool.unwrap(b)) == ETypes.Bool);
86
+ }
87
+ }