@inco/lightning 0.4.0 → 0.5.1

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 (110) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +2 -0
  3. package/manifest.yaml +23 -0
  4. package/package.json +2 -5
  5. package/src/DeployUtils.sol +113 -77
  6. package/src/IIncoLightning.sol +16 -9
  7. package/src/IncoLightning.sol +18 -9
  8. package/src/IncoVerifier.sol +47 -0
  9. package/src/Lib.alphanet.sol +2 -15
  10. package/src/Lib.demonet.sol +2 -15
  11. package/src/Lib.devnet.sol +3 -16
  12. package/src/Lib.sol +3 -16
  13. package/src/Lib.template.sol +3 -44
  14. package/src/Lib.testnet.sol +2 -15
  15. package/src/Types.sol +7 -0
  16. package/src/interfaces/IIncoLightning.sol +20 -0
  17. package/src/interfaces/IIncoVerifier.sol +24 -0
  18. package/src/interfaces/automata-interfaces/BELE.sol +20 -0
  19. package/src/interfaces/automata-interfaces/IAutomataEnclaveIdentityDao.sol +28 -0
  20. package/src/interfaces/automata-interfaces/IFmspcTcbDao.sol +10 -0
  21. package/src/interfaces/automata-interfaces/IPCCSRouter.sol +94 -0
  22. package/src/interfaces/automata-interfaces/IPCCSRouterExtended.sol +10 -0
  23. package/src/interfaces/automata-interfaces/IPcsDao.sol +18 -0
  24. package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +34 -0
  25. package/src/interfaces/automata-interfaces/Types.sol +193 -0
  26. package/src/libs/incoLightning_alphanet_v0_297966649.sol +2 -15
  27. package/src/libs/incoLightning_demonet_v0_863421733.sol +2 -15
  28. package/src/libs/incoLightning_devnet_v0_340846814.sol +2 -15
  29. package/src/libs/incoLightning_devnet_v1_887305889.sol +451 -0
  30. package/src/libs/incoLightning_testnet_v0_183408998.sol +2 -15
  31. package/src/libs/incoLightning_testnet_v1_938327937.sol +451 -0
  32. package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +133 -0
  33. package/src/lightning-parts/AccessControl/AdvancedAccessControl.types.sol +18 -0
  34. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +22 -2
  35. package/src/lightning-parts/AccessControl/interfaces/IAdvancedAccessControl.sol +25 -0
  36. package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +24 -0
  37. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +234 -0
  38. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +4 -3
  39. package/src/lightning-parts/DecryptionAttester.sol +45 -0
  40. package/src/lightning-parts/DecryptionAttester.types.sol +7 -0
  41. package/src/lightning-parts/EncryptedInput.sol +8 -13
  42. package/src/lightning-parts/EncryptedOperations.sol +2 -2
  43. package/src/lightning-parts/TEELifecycle.sol +180 -88
  44. package/src/lightning-parts/TrivialEncryption.sol +8 -10
  45. package/src/lightning-parts/interfaces/IDecryptionAttester.sol +9 -0
  46. package/src/lightning-parts/interfaces/IEncryptedInput.sol +19 -0
  47. package/src/lightning-parts/interfaces/IEncryptedOperations.sol +31 -0
  48. package/src/lightning-parts/interfaces/ITEELifecycle.sol +26 -0
  49. package/src/lightning-parts/interfaces/ITrivialEncryption.sol +10 -0
  50. package/src/lightning-parts/primitives/EventCounter.sol +2 -2
  51. package/src/lightning-parts/primitives/HandleGeneration.sol +8 -3
  52. package/src/lightning-parts/primitives/HandleMetadata.sol +1 -1
  53. package/src/lightning-parts/primitives/LightningAddressGetter.sol +10 -0
  54. package/src/lightning-parts/primitives/SignatureVerifier.sol +2 -9
  55. package/src/lightning-parts/primitives/VerifierAddressGetter.sol +13 -0
  56. package/src/lightning-parts/primitives/{EventCounter.gen.sol → interfaces/IEventCounter.sol} +2 -2
  57. package/src/lightning-parts/primitives/interfaces/IHandleGeneration.sol +41 -0
  58. package/src/lightning-parts/primitives/interfaces/ISignatureVerifier.sol +9 -0
  59. package/src/lightning-parts/primitives/interfaces/IVerifierAddressGetter.sol +8 -0
  60. package/src/lightning-parts/test/HandleMetadata.t.sol +25 -6
  61. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +78 -0
  62. package/src/periphery/SessionVerifier.sol +63 -0
  63. package/src/test/AddTwo.sol +18 -33
  64. package/src/test/FakeIncoInfra/FakeDecryptionAttester.sol +198 -0
  65. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +0 -15
  66. package/src/test/FakeIncoInfra/FakeQuoteVerifier.sol +10 -6
  67. package/src/test/FakeIncoInfra/MockOpHandler.sol +0 -7
  68. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +19 -7
  69. package/src/test/FakeIncoInfra/getOpForSelector.sol +0 -3
  70. package/src/test/IncoTest.sol +12 -11
  71. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +98 -77
  72. package/src/test/TestAddTwo.t.sol +16 -9
  73. package/src/test/TestFakeInfra.t.sol +10 -27
  74. package/src/test/TestUpgrade.t.sol +11 -4
  75. package/src/test/TestVersion.t.sol +0 -7
  76. package/src/version/IncoLightningConfig.sol +5 -4
  77. package/src/version/SessionVerifierConfig.sol +8 -0
  78. package/src/version/Version.sol +7 -9
  79. package/src/version/interfaces/IVersion.sol +8 -0
  80. package/src/DeployTEE.sol +0 -153
  81. package/src/IncoLightning.gen.sol +0 -15
  82. package/src/lightning-parts/AccessControl/BaseAccessControlList.gen.sol +0 -24
  83. package/src/lightning-parts/DecryptionHandler.gen.sol +0 -54
  84. package/src/lightning-parts/DecryptionHandler.sol +0 -307
  85. package/src/lightning-parts/DecryptionHandler.types.sol +0 -34
  86. package/src/lightning-parts/EncryptedInput.gen.sol +0 -16
  87. package/src/lightning-parts/EncryptedOperations.gen.sol +0 -59
  88. package/src/lightning-parts/TEELifecycle.gen.sol +0 -58
  89. package/src/lightning-parts/TrivialEncryption.gen.sol +0 -15
  90. package/src/lightning-parts/primitives/HandleGeneration.gen.sol +0 -19
  91. package/src/lightning-parts/primitives/HandleMetadata.gen.sol +0 -4
  92. package/src/lightning-parts/primitives/SignatureVerifier.gen.sol +0 -16
  93. package/src/test/FibonacciDecrypt.sol +0 -49
  94. package/src/test/TEELifecycle/TEELifecycleHWTest.t.sol +0 -119
  95. package/src/test/TEELifecycle/addnode_data/eoa.txt +0 -1
  96. package/src/test/TEELifecycle/addnode_data/quote.bin +0 -0
  97. package/src/test/TEELifecycle/bootstrap_data/ecies_pubkey.bin +0 -1
  98. package/src/test/TEELifecycle/bootstrap_data/eip712_signature.bin +0 -1
  99. package/src/test/TEELifecycle/bootstrap_data/eoa.txt +0 -1
  100. package/src/test/TEELifecycle/bootstrap_data/qe_identity +0 -1
  101. package/src/test/TEELifecycle/bootstrap_data/qe_identity_signature.bin +0 -1
  102. package/src/test/TEELifecycle/bootstrap_data/quote.bin +0 -0
  103. package/src/test/TEELifecycle/bootstrap_data/tcb_info +0 -1
  104. package/src/test/TEELifecycle/bootstrap_data/tcb_info_signature.bin +0 -1
  105. package/src/test/TEELifecycle/test_cert/AttestationReportSigningCA.crl +0 -0
  106. package/src/test/TEELifecycle/test_cert/Intel_SGX_Attestation_RootCA.cer +0 -0
  107. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_CRL.crl +0 -0
  108. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_PlatformCA.cer +0 -0
  109. package/src/test/TEELifecycle/test_cert/Intel_SGX_TCB_Signing.cer +0 -0
  110. package/src/version/Version.gen.sol +0 -14
package/src/DeployTEE.sol DELETED
@@ -1,153 +0,0 @@
1
- // SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- import {Script} from "forge-std/Script.sol";
5
- import {Vm} from "forge-std/Vm.sol";
6
- import {console} from "forge-std/console.sol";
7
-
8
- import {IQuoteVerifier} from "automata-dcap-attestation/interfaces/IQuoteVerifier.sol";
9
- import {TEELifecycle} from "./lightning-parts/TEELifecycle.sol";
10
-
11
- import {CA} from "@automata-network/on-chain-pccs/Common.sol";
12
- import {EnclaveIdentityJsonObj, IdentityObj, EnclaveIdentityHelper} from "@automata-network/on-chain-pccs/helpers/EnclaveIdentityHelper.sol";
13
- import {TcbInfoJsonObj, FmspcTcbHelper} from "@automata-network/on-chain-pccs/helpers/FmspcTcbHelper.sol";
14
- import {PCKHelper} from "@automata-network/on-chain-pccs/helpers/PCKHelper.sol";
15
- import {X509CRLHelper} from "@automata-network/on-chain-pccs/helpers/X509CRLHelper.sol";
16
- import {AutomataFmspcTcbDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataFmspcTcbDao.sol";
17
- import {AutomataEnclaveIdentityDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataEnclaveIdentityDao.sol";
18
- import {AutomataPcsDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataPcsDao.sol";
19
- import {AutomataPckDao} from "@automata-network/on-chain-pccs/automata_pccs/AutomataPckDao.sol";
20
- import {AutomataDaoStorage} from "@automata-network/on-chain-pccs/automata_pccs/shared/AutomataDaoStorage.sol";
21
- import {PCCSRouter} from "@automata-network/dcap-attestation/PCCSRouter.sol";
22
- import {V4QuoteVerifier} from "@automata-network/dcap-attestation/verifiers/V4QuoteVerifier.sol";
23
-
24
- // For now we don't use deployments for TEELifecycle, as we do for IncoLightning.
25
- // @todo: Use deployments for TEELifecycle
26
- // ref: https://github.com/Inco-fhevm/inco-monorepo/issues/875
27
- string constant EIP712_NAME = "TEELifecycle";
28
- string constant EIP712_VERSION = "1.0.0";
29
-
30
- // DeployTEE is a script that deplots all TEE-related supporting contracts for IncoLightning.
31
- contract DeployTEE is Script {
32
-
33
- // The default address of the P256 Verifier contract that is used to verify the P256 signatures.
34
- // It is deployed with this address on Ethereum L1, OP Mainnet, Base, Arbitrum.
35
- // @dev From: https://github.com/daimo-eth/p256-verifier?tab=readme-ov-file#usage
36
- address public P256_VERIFIER = 0xc2b78104907F722DABAc4C69f826a522B2754De4;
37
-
38
- // The PCCSRouter contract is used as the entrypoint for the management of collateral used to verify quotes.
39
- // It is used by the QuoteVerifier to fetch collateral.
40
- PCCSRouter public pccsRouter;
41
-
42
- // These helpers are used to parse the collateral files and upload them to the PCCSRouter.
43
- AutomataFmspcTcbDao fmspcTcbDao;
44
- AutomataEnclaveIdentityDao enclaveIdDao;
45
-
46
- // This function deploys a TEELifecycle contract that is used to manage the TEE lifecycle for the IncoLightning contract.
47
- // @dev The TEELifecycle contract is used to manage the TEE lifecycle for the IncoLightning contract.
48
- // It is used to verify quotes, and to manage the TEE lifecycle.
49
- // It is also used to manage the EOA signers for the IncoLightning contract.
50
- // @param quoteVerifierAddress The address of the QuoteVerifier contract to be used by the TEELifecycle contract.
51
- function deployTEELifecycle(address deployer, address quoteVerifierAddress) internal returns (TEELifecycle) {
52
- // TODO: Currently TEELifecycle is not UUPS
53
- // ref: https://github.com/Inco-fhevm/inco-monorepo/issues/875
54
- TEELifecycle implementation = new TEELifecycle();
55
- implementation.initialize(
56
- deployer,
57
- EIP712_NAME,
58
- EIP712_VERSION,
59
- quoteVerifierAddress
60
- );
61
- return implementation;
62
- }
63
-
64
- // This function deploys a QuoteVerifier contract that is used to verify quotes for the IncoLightning contract.
65
- // @dev: TDX_HW environment variable is used to determine whether to use the FakeQuoteVerifier or the V4QuoteVerifier.
66
- // @dev We deploy two flavors of the QuoteVerifier contract:
67
- // - FakeQuoteVerifier: A fake implementation of the QuoteVerifier contract that is used to test the IncoLightning contract.
68
- // This is used for testing purposes, and returns true for all quote verifications.
69
- // - V4QuoteVerifier: The real implementation of the QuoteVerifier contract that is used to verify quotes for the IncoLightning contract.
70
- // This is taken from the automata-dcap-attestation package.
71
- function deployQuoteVerifier() internal returns (IQuoteVerifier quoteVerifier) {
72
- // deploys the QuoteVerifier contract
73
- quoteVerifier = new V4QuoteVerifier(
74
- P256_VERIFIER,
75
- address(pccsRouter)
76
- );
77
-
78
- pccsRouter.setAuthorized(address(quoteVerifier), true);
79
- }
80
-
81
- function deployPCCS(address owner, string memory collateralDir) internal {
82
-
83
- EnclaveIdentityHelper enclaveIdHelper = new EnclaveIdentityHelper();
84
- FmspcTcbHelper tcbHelper = new FmspcTcbHelper();
85
- PCKHelper x509 = new PCKHelper();
86
- X509CRLHelper x509Crl = new X509CRLHelper();
87
-
88
- AutomataDaoStorage pccsStorage = new AutomataDaoStorage(owner);
89
- AutomataPcsDao pcsDao = new AutomataPcsDao(address(pccsStorage), P256_VERIFIER, address(x509), address(x509Crl));
90
- AutomataPckDao pckDao = new AutomataPckDao(address(pccsStorage), P256_VERIFIER, address(pcsDao), address(x509), address(x509Crl));
91
- enclaveIdDao = new AutomataEnclaveIdentityDao(
92
- address(pccsStorage), P256_VERIFIER, address(pcsDao), address(enclaveIdHelper), address(x509), address(x509Crl)
93
- );
94
- fmspcTcbDao = new AutomataFmspcTcbDao(address(pccsStorage), P256_VERIFIER, address(pcsDao), address(tcbHelper), address(x509), address(x509Crl));
95
- // grants dao permissions to write to the storage
96
- pccsStorage.grantDao(address(pcsDao));
97
- pccsStorage.grantDao(address(pckDao));
98
- pccsStorage.grantDao(address(fmspcTcbDao));
99
- pccsStorage.grantDao(address(enclaveIdDao));
100
-
101
- // grants admin address permission to read collaterals
102
- pccsRouter = new PCCSRouter(
103
- owner,
104
- address(enclaveIdDao),
105
- address(fmspcTcbDao),
106
- address(pcsDao),
107
- address(pckDao),
108
- address(x509),
109
- address(x509Crl),
110
- address(tcbHelper)
111
- );
112
- // allow PCCS Router to read collaterals from the storage
113
- pccsStorage.setCallerAuthorization(address(pccsRouter), true);
114
-
115
- // Upload Root Certs/CRLs
116
- bytes memory rootCaDer = vm.readFileBinary(string.concat(collateralDir, "Intel_SGX_Attestation_RootCA.cer"));
117
- pcsDao.upsertPcsCertificates(CA.ROOT, rootCaDer);
118
- bytes memory tcbDer = vm.readFileBinary(string.concat(collateralDir, "Intel_SGX_TCB_Signing.cer"));
119
- pcsDao.upsertPcsCertificates(CA.SIGNING, tcbDer);
120
- bytes memory platformDer = vm.readFileBinary(string.concat(collateralDir, "Intel_SGX_PCK_PlatformCA.cer"));
121
- pcsDao.upsertPcsCertificates(CA.PLATFORM, platformDer);
122
- bytes memory platformCrlDer = vm.readFileBinary(string.concat(collateralDir, "Intel_SGX_PCK_CRL.crl"));
123
- pcsDao.upsertPckCrl(CA.PLATFORM, platformCrlDer);
124
- bytes memory rootCrlDer = vm.readFileBinary(string.concat(collateralDir, "AttestationReportSigningCA.crl"));
125
-
126
- pcsDao.upsertRootCACrl(rootCrlDer);
127
- }
128
-
129
- // This function deploys the P256 Verifier contract that is used to verify P256 signatures.
130
- // @dev The code is taken from https://github.com/automata-network/automata-on-chain-pccs/blob/v1.0.0/test/TestSetupBase.t.sol#L91
131
- function deployP256() internal {
132
- // Known chains that have the P256 Verifier deployed:
133
- // Ethereum Mainnet, Optimism Mainnet, Base, Arbitrum
134
- // https://github.com/daimo-eth/p256-verifier/tree/master/broadcast/Deploy.s.sol
135
- if (block.chainid == 1 || block.chainid == 10 || block.chainid == 8453 || block.chainid == 84531 ) {
136
- console.log("P256 Verifier contract is already deployed on this chain, skipping deployment.");
137
- return;
138
- }
139
- require(block.chainid == 31337, "Deploying P256 Verifier is only supported on Anvil chain");
140
- bytes memory txdata =
141
- hex"00000000000000000000000000000000000000000000000000000000000000006080806040523461001657610dd1908161001c8239f35b600080fdfe60e06040523461001a57610012366100c7565b602081519101f35b600080fd5b6040810190811067ffffffffffffffff82111761003b57604052565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60e0810190811067ffffffffffffffff82111761003b57604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff82111761003b57604052565b60a08103610193578060201161001a57600060409180831161018f578060601161018f578060801161018f5760a01161018c57815182810181811067ffffffffffffffff82111761015f579061013291845260603581526080356020820152833560203584356101ab565b15610156575060ff6001915b5191166020820152602081526101538161001f565b90565b60ff909161013e565b6024837f4e487b710000000000000000000000000000000000000000000000000000000081526041600452fd5b80fd5b5080fd5b5060405160006020820152602081526101538161001f565b909283158015610393575b801561038b575b8015610361575b6103585780519060206101dc818301938451906103bd565b1561034d57604051948186019082825282604088015282606088015260808701527fffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc63254f60a08701527fffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551958660c082015260c081526102588161006a565b600080928192519060055afa903d15610345573d9167ffffffffffffffff831161031857604051926102b1857fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8401160185610086565b83523d828585013e5b156102eb57828280518101031261018c5750015190516102e693929185908181890994099151906104eb565b061490565b807f4e487b7100000000000000000000000000000000000000000000000000000000602492526001600452fd5b6024827f4e487b710000000000000000000000000000000000000000000000000000000081526041600452fd5b6060916102ba565b505050505050600090565b50505050600090565b507fffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc6325518310156101c4565b5082156101bd565b507fffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc6325518410156101b6565b7fffffffff00000001000000000000000000000000ffffffffffffffffffffffff90818110801590610466575b8015610455575b61044d577f5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b8282818080957fffffffff00000001000000000000000000000000fffffffffffffffffffffffc0991818180090908089180091490565b505050600090565b50801580156103f1575082156103f1565b50818310156103ea565b7f800000000000000000000000000000000000000000000000000000000000000081146104bc577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0190565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b909192608052600091600160a05260a05193600092811580610718575b61034d57610516838261073d565b95909460ff60c05260005b600060c05112156106ef575b60a05181036106a1575050507f4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5957f6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2969594939291965b600060c05112156105c7575050505050507fffffffff00000001000000000000000000000000ffffffffffffffffffffffff91506105c260a051610ca2565b900990565b956105d9929394959660a05191610a98565b9097929181928960a0528192819a6105f66080518960c051610722565b61060160c051610470565b60c0528061061b5750505050505b96959493929196610583565b969b5061067b96939550919350916001810361068857507f4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5937f6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c29693610952565b979297919060a05261060f565b6002036106985786938a93610952565b88938893610952565b600281036106ba57505050829581959493929196610583565b9197917ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd0161060f575095508495849661060f565b506106ff6080518560c051610722565b8061070b60c051610470565b60c052156105215761052d565b5060805115610508565b91906002600192841c831b16921c1681018091116104bc5790565b8015806107ab575b6107635761075f91610756916107b3565b92919091610c42565b9091565b50507f6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296907f4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f590565b508115610745565b919082158061094a575b1561080f57507f6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c29691507f4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5906001908190565b7fb01cbd1c01e58065711814b583f061e9d431cca994cea1313449bf97c840ae0a917fffffffff00000001000000000000000000000000ffffffffffffffffffffffff808481600186090894817f94e82e0c1ed3bdb90743191a9c5bbf0d88fc827fd214cc5f0b5ec6ba27673d6981600184090893841561091b575050808084800993840994818460010994828088600109957f6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c29609918784038481116104bc5784908180867fffffffff00000001000000000000000000000000fffffffffffffffffffffffd0991818580090808978885038581116104bc578580949281930994080908935b93929190565b9350935050921560001461093b5761093291610b6d565b91939092610915565b50506000806000926000610915565b5080156107bd565b91949592939095811580610a90575b15610991575050831580610989575b61097a5793929190565b50600093508392508291508190565b508215610970565b85919294951580610a88575b610a78577fffffffff00000001000000000000000000000000ffffffffffffffffffffffff968703918783116104bc5787838189850908938689038981116104bc5789908184840908928315610a5d575050818880959493928180848196099b8c9485099b8c920999099609918784038481116104bc5784908180867fffffffff00000001000000000000000000000000fffffffffffffffffffffffd0991818580090808978885038581116104bc578580949281930994080908929190565b965096505050509093501560001461093b5761093291610b6d565b9550509150915091906001908190565b50851561099d565b508015610961565b939092821580610b65575b61097a577fffffffff00000001000000000000000000000000ffffffffffffffffffffffff908185600209948280878009809709948380888a0998818080808680097fffffffff00000001000000000000000000000000fffffffffffffffffffffffc099280096003090884808a7fffffffff00000001000000000000000000000000fffffffffffffffffffffffd09818380090898898603918683116104bc57888703908782116104bc578780969481809681950994089009089609930990565b508015610aa3565b919091801580610c3a575b610c2d577fffffffff00000001000000000000000000000000ffffffffffffffffffffffff90818460020991808084800980940991817fffffffff00000001000000000000000000000000fffffffffffffffffffffffc81808088860994800960030908958280837fffffffff00000001000000000000000000000000fffffffffffffffffffffffd09818980090896878403918483116104bc57858503928584116104bc5785809492819309940890090892565b5060009150819081908190565b508215610b78565b909392821580610c9a575b610c8d57610c5a90610ca2565b9182917fffffffff00000001000000000000000000000000ffffffffffffffffffffffff80809581940980099009930990565b5050509050600090600090565b508015610c4d565b604051906020918281019183835283604083015283606083015260808201527fffffffff00000001000000000000000000000000fffffffffffffffffffffffd60a08201527fffffffff00000001000000000000000000000000ffffffffffffffffffffffff60c082015260c08152610d1a8161006a565b600080928192519060055afa903d15610d93573d9167ffffffffffffffff83116103185760405192610d73857fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f8401160185610086565b83523d828585013e5b156102eb57828280518101031261018c5750015190565b606091610d7c56fea2646970667358221220fa55558b04ced380e93d0a46be01bb895ff30f015c50c516e898c341cd0a230264736f6c63430008150033";
142
-
143
- // deploys the P256 Verifier contract using the default CREATE2 deterministic deployment contract available of EVM chains including on Anvil
144
- // https://github.com/Arachnid/deterministic-deployment-proxy?tab=readme-ov-file#latest-outputs
145
- // https://getfoundry.sh/guides/deterministic-deployments-using-create2
146
- (bool succ,) = address(0x4e59b44847b379578588920cA78FbF26c0B4956C).call(txdata);
147
- require(succ, "Failed to deploy P256");
148
-
149
- // check code
150
- uint256 codesize = P256_VERIFIER.code.length;
151
- require(codesize > 0, "P256 deployed to the wrong address");
152
- }
153
- }
@@ -1,15 +0,0 @@
1
- /// SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- import { IIncoLightning } from "./IIncoLightning.sol";
5
- import { CONTRACT_NAME, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION } from "./version/IncoLightningConfig.sol";
6
- import { DecryptionHandler } from "./lightning-parts/DecryptionHandler.sol";
7
- import { EncryptedInput } from "./lightning-parts/EncryptedInput.sol";
8
- import { EncryptedOperations } from "./lightning-parts/EncryptedOperations.sol";
9
- import { TrivialEncryption } from "./lightning-parts/TrivialEncryption.sol";
10
- import { UUPSUpgradeable } from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
11
- import { Version } from "./version/Version.sol";
12
-
13
- interface IIncoLightningGen {
14
- function initialize(address owner, address teeLifecycleAddress) external;
15
- }
@@ -1,24 +0,0 @@
1
- /// SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- import { SenderNotAllowedForHandle } from "../../Types.sol";
5
-
6
- interface IBaseAccessControlListGen {
7
- /// @dev persistent
8
- function allow(bytes32 handle, address account) external;
9
-
10
- /// @dev Permanently allows public decryption/reencryption access to anyone for the given handle.
11
- function reveal(bytes32 handle) external;
12
-
13
- function allowTransient(bytes32 handle, address account) external;
14
-
15
- function allowedTransient(bytes32 handle, address account) external view returns (bool);
16
-
17
- function cleanTransientStorage() external;
18
-
19
- function persistAllowed(bytes32 handle, address account) external view returns (bool);
20
-
21
- function isAllowed(bytes32 handle, address account) external view returns (bool);
22
-
23
- function isRevealed(bytes32 handle) external view returns (bool);
24
- }
@@ -1,54 +0,0 @@
1
- /// SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- import "./DecryptionHandler.types.sol";
5
- import { BaseAccessControlList } from "./AccessControl/BaseAccessControlList.sol";
6
- import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
7
- import { EventCounter } from "./primitives/EventCounter.sol";
8
- import { IDecryptionHandlerEip712CheckerGen } from "./DecryptionHandler.gen.sol";
9
- import { SenderNotAllowedForHandle } from "../Types.sol";
10
- import { SignatureVerifier } from "./primitives/SignatureVerifier.sol";
11
-
12
- interface IDecryptionHandlerEip712CheckerGen {
13
- function decryptionResultDigest(DecryptionResult memory result) external view returns (bytes32);
14
- }
15
-
16
- interface IDecryptionHandlerGen {
17
- /// @dev callback function MUST be of form `function name(bytes32 requestId, bytes32 result, bytes memory data)`
18
- function requestDecryption(bytes4 callbackSelector, uint256 maxTimestamp, bytes32 handle, bytes memory data) external returns (uint256 requestId);
19
-
20
- function fulfillRequest(DecryptionResult memory result, bytes memory signature) external;
21
-
22
- function fulfillRequestsBatch(SignedDecryptionResult[] memory results) external;
23
-
24
- /// @dev Get some pending requests.
25
- /// @param offset A parameter for paginating through the whole set of pending requests.
26
- /// Use zero for an initial query, then use the previous query's nextOffset to continue traversal.
27
- /// @param maxRequests The maximum number of pending to return.
28
- /// @param iterationBound The maximum number of non-pending requests to iterate over before returning.
29
- /// @return nextOffset The next offset to use to continue to scan through the pending request set.
30
- /// Returns the input offset if the whole set has been scanned. Can re-query with this offset
31
- /// to see any new pending requests, or re-query with a zero offset to return the whole set again.
32
- /// @return requests Some pending requests. May be fewer than requested, possibly zero. There may be
33
- /// more requests available if the returned nextOffset is greater than the input offset.
34
- function getPendingRequests(uint256 offset, uint256 maxRequests, uint256 iterationBound) external view returns (uint256 nextOffset, Request[] memory requests);
35
-
36
- /// @param offset A parameter for paginating through the whole set of pending requests.
37
- /// Use zero for an initial query, then use the previous query's nextOffset to continue traversal.
38
- /// @param maxRequests The maximum number of pending to return.
39
- /// @return nextOffset The next offset to use to continue to scan through the pending request set.
40
- /// Returns the input offset if the whole set has been scanned. Can re-query with this offset
41
- /// to see any new pending requests, or re-query with a zero offset to return the whole set again.
42
- /// @return requests Some pending requests. May be fewer than requested, possibly zero. There may be
43
- /// more requests available if the returned nextOffset is greater than the input offset.
44
- function getPendingRequests(uint256 offset, uint256 maxRequests) external view returns (uint256 nextOffset, Request[] memory requests);
45
-
46
- function getNextRequestId() external view returns (uint256);
47
-
48
- function getLowWatermarkRequestId() external view returns (uint256);
49
-
50
- /// @dev Advance the low watermark until we hit a pending request, the requestId counter, or iterationBound steps.
51
- /// @param iterationBound The maximum number of requests to iterate over.
52
- /// @notice This function is public so that we can externally drive the low watermark in extreme cases.
53
- function advanceLowWatermark(uint256 iterationBound) external;
54
- }
@@ -1,307 +0,0 @@
1
- // SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- import "./DecryptionHandler.gen.sol";
5
- import "./DecryptionHandler.types.sol";
6
- import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
7
- import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
8
- import {EventCounter} from "./primitives/EventCounter.sol";
9
- import {IDecryptionHandlerEip712CheckerGen} from "./DecryptionHandler.gen.sol";
10
- import {SenderNotAllowedForHandle} from "../Types.sol";
11
- import {SignatureVerifier} from "./primitives/SignatureVerifier.sol";
12
-
13
- contract DecryptionHandlerStorage {
14
- struct DecryptionStorage {
15
- // The next request ID to assign. This and higher numbers have never been used,
16
- // hence there are no pending requests in that range.
17
- uint256 nextRequestId;
18
- mapping(uint256 => Request) requests;
19
- // A requestId for which all previous requests have been fulfilled or are expired.
20
- // Must be less than or equal to nextRequestId. Only increases.
21
- uint256 lowWatermark;
22
- }
23
-
24
- bytes32 private constant DecryptionStorageLocation =
25
- keccak256("inco.storage.DecryptionHandler");
26
-
27
- function getDecryptionStorage()
28
- internal
29
- pure
30
- returns (DecryptionStorage storage $)
31
- {
32
- bytes32 loc = DecryptionStorageLocation;
33
- assembly {
34
- $.slot := loc
35
- }
36
- }
37
- }
38
-
39
- abstract contract DecryptionHandlerEip712Checker is IDecryptionHandlerEip712CheckerGen, EIP712, SignatureVerifier {
40
- bytes32 constant DecryptionResultStructHash =
41
- keccak256(
42
- "DecryptionResult(bytes32 abiEncodedResult,uint256 requestId)"
43
- );
44
-
45
- function decryptionResultDigest(
46
- DecryptionResult memory result
47
- ) public view returns (bytes32) {
48
- return
49
- _hashTypedDataV4(
50
- keccak256(
51
- abi.encode(
52
- DecryptionResultStructHash,
53
- result.abiEncodedResult,
54
- result.requestId
55
- )
56
- )
57
- );
58
- }
59
-
60
- function isValidResult(
61
- DecryptionResult memory result,
62
- bytes memory signature
63
- ) internal view returns (bool) {
64
- return isValidSignature(decryptionResultDigest(result), signature);
65
- }
66
- }
67
-
68
- abstract contract DecryptionHandler is
69
- IDecryptionHandlerGen,
70
- BaseAccessControlList,
71
- EventCounter,
72
- DecryptionHandlerStorage,
73
- DecryptionHandlerEip712Checker
74
- {
75
- // The Covalidator callback machinery relies on these exact error signatures which are hardcoded in eth_callback.go
76
- error AllRequestsAlreadyFulfilledOrExpired();
77
- // Thrown when either msg.sender is not a valid signer or the signature is not from a valid signer
78
- error InvalidResultSignature(address msgSender, bytes signature);
79
-
80
- event RequestFulfilled(
81
- uint256 indexed requestId,
82
- bool success,
83
- uint256 eventId,
84
- bytes32 handle
85
- );
86
-
87
- // Maximum number of request mappings to access on any operation.
88
- uint256 constant ITERATION_BOUND = 100;
89
-
90
- function pending(Request memory r) internal view returns (bool) {
91
- return !r.fulfilled && r.maxTimestamp >= block.timestamp;
92
- }
93
-
94
- /// @dev callback function MUST be of form `function name(bytes32 requestId, bytes32 result, bytes memory data)`
95
- function requestDecryption(
96
- // todo support multiple handles per request
97
- bytes4 callbackSelector,
98
- uint256 maxTimestamp,
99
- bytes32 handle,
100
- bytes memory data
101
- ) external returns (uint256 requestId) {
102
- require(
103
- isAllowed(handle, msg.sender),
104
- SenderNotAllowedForHandle(handle, msg.sender)
105
- );
106
-
107
- DecryptionStorage storage $ = getDecryptionStorage();
108
- requestId = $.nextRequestId;
109
- $.nextRequestId++;
110
- $.requests[requestId] = Request({
111
- maxTimestamp: maxTimestamp,
112
- callbackContract: msg.sender,
113
- callbackSelector: callbackSelector,
114
- fulfilled: false,
115
- data: data,
116
- handle: handle,
117
- requestId: requestId
118
- });
119
-
120
- advanceLowWatermark(ITERATION_BOUND);
121
-
122
- emit DecryptionRequested(
123
- requestId,
124
- handle,
125
- maxTimestamp,
126
- getNewEventId()
127
- );
128
- }
129
-
130
- function fulfillRequest(
131
- DecryptionResult memory result,
132
- bytes memory signature
133
- ) external {
134
- SignedDecryptionResult[]
135
- memory signedResults = new SignedDecryptionResult[](1);
136
- signedResults[0] = SignedDecryptionResult({
137
- result: result,
138
- signature: signature
139
- });
140
- fulfillRequestsBatch(signedResults);
141
- }
142
-
143
- function fulfillRequestsBatch(
144
- SignedDecryptionResult[] memory results
145
- ) public {
146
- DecryptionStorage storage $ = getDecryptionStorage();
147
-
148
- bool fulfilledSome = false;
149
-
150
- for (uint i = 0; i < results.length; i++) {
151
- Request storage request = $.requests[results[i].result.requestId];
152
-
153
- if (!pending(request)) {
154
- // The request was already fulfilled, or has expired. Neither is an error.
155
- continue;
156
- }
157
- // Support either an EIP712 signature or a direct call from the signer
158
- require(
159
- isSigner(msg.sender) ||
160
- isValidResult(results[i].result, results[i].signature),
161
- InvalidResultSignature(msg.sender, results[i].signature)
162
- );
163
-
164
- bytes memory callbackCalldata = abi.encodeWithSelector(
165
- request.callbackSelector,
166
- results[i].result.requestId,
167
- results[i].result.abiEncodedResult,
168
- request.data
169
- // TODO: add handle as an integrity check - already added to DecryptionResult but adding here would mean
170
- // dapp callback function signatures would need to change - which would be a breaking change
171
- // request.handle
172
- );
173
-
174
- (bool success, ) = request.callbackContract.call(callbackCalldata);
175
-
176
- request.fulfilled = true;
177
- fulfilledSome = true;
178
-
179
- emit RequestFulfilled(
180
- results[i].result.requestId,
181
- success,
182
- getNewEventId(),
183
- results[i].result.handle
184
- );
185
- }
186
- // If we did not advance we revert, so that we can detect if everything is fulfilled during gas estimation
187
- require(fulfilledSome, AllRequestsAlreadyFulfilledOrExpired());
188
-
189
- // Otherwise, do some work to advance the low water mark.
190
- advanceLowWatermark(ITERATION_BOUND);
191
- }
192
-
193
- /**
194
- * @dev Get some pending requests.
195
- * @param offset A parameter for paginating through the whole set of pending requests.
196
- * Use zero for an initial query, then use the previous query's nextOffset to continue traversal.
197
- * @param maxRequests The maximum number of pending to return.
198
- * @param iterationBound The maximum number of non-pending requests to iterate over before returning.
199
- * @return nextOffset The next offset to use to continue to scan through the pending request set.
200
- * Returns the input offset if the whole set has been scanned. Can re-query with this offset
201
- * to see any new pending requests, or re-query with a zero offset to return the whole set again.
202
-
203
- * @return requests Some pending requests. May be fewer than requested, possibly zero. There may be
204
- * more requests available if the returned nextOffset is greater than the input offset.
205
- */
206
- function getPendingRequests(
207
- uint256 offset,
208
- uint256 maxRequests,
209
- uint256 iterationBound
210
- ) external view returns (uint256 nextOffset, Request[] memory requests) {
211
- DecryptionStorage storage $ = getDecryptionStorage();
212
-
213
- uint256 lwm = $.lowWatermark;
214
- uint256 nextRequestId = $.nextRequestId;
215
-
216
- // Start at the higher of offset and lwm, possibly skipping many completed requests.
217
- uint256 id = offset;
218
- if (id < lwm) {
219
- id = lwm;
220
- }
221
-
222
- // Don't scan more than iterationBound extra entries or pass the nextRequestId.
223
- uint256 limit = id + maxRequests + iterationBound;
224
- if (limit > nextRequestId) {
225
- limit = nextRequestId;
226
- }
227
-
228
- // Now id >= lmw and limit <= nextRequestId.
229
- // It's possible that id >= limit, which is fine,
230
- // and the next two loops will exit immediately.
231
-
232
- // First calculate the size of array we need to allocate.
233
- // (It's cheaper to scan the requests twice instead of allocating too much.)
234
- uint256 numRequests = 0;
235
- for (uint256 i = id; i < limit && numRequests < maxRequests; i++) {
236
- Request memory request = $.requests[i];
237
- if (pending(request)) {
238
- numRequests++;
239
- }
240
- }
241
-
242
- // Now populate the array
243
- requests = new Request[](numRequests);
244
- for (uint256 n = 0; id < limit && n < numRequests; id++) {
245
- Request memory request = $.requests[id];
246
- if (pending(request)) {
247
- requests[n] = request;
248
- n++;
249
- }
250
- }
251
- nextOffset = id;
252
- }
253
-
254
- /**
255
-
256
- * @param offset A parameter for paginating through the whole set of pending requests.
257
- * Use zero for an initial query, then use the previous query's nextOffset to continue traversal.
258
- * @param maxRequests The maximum number of pending to return.
259
- * @return nextOffset The next offset to use to continue to scan through the pending request set.
260
- * Returns the input offset if the whole set has been scanned. Can re-query with this offset
261
- * to see any new pending requests, or re-query with a zero offset to return the whole set again.
262
- * @return requests Some pending requests. May be fewer than requested, possibly zero. There may be
263
- * more requests available if the returned nextOffset is greater than the input offset.
264
- */
265
- function getPendingRequests(
266
- uint256 offset,
267
- uint256 maxRequests
268
- ) external view returns (uint256 nextOffset, Request[] memory requests) {
269
- return this.getPendingRequests(offset, maxRequests, ITERATION_BOUND);
270
- }
271
-
272
- function getNextRequestId() external view returns (uint256) {
273
- return getDecryptionStorage().nextRequestId;
274
- }
275
-
276
- function getLowWatermarkRequestId() external view returns (uint256) {
277
- return getDecryptionStorage().lowWatermark;
278
- }
279
-
280
- /**
281
- * @dev Advance the low watermark until we hit a pending request, the requestId counter, or iterationBound steps.
282
- * @param iterationBound The maximum number of requests to iterate over.
283
- * @notice This function is public so that we can externally drive the low watermark in extreme cases.
284
- */
285
- function advanceLowWatermark(uint256 iterationBound) public {
286
- DecryptionStorage storage $ = getDecryptionStorage();
287
- uint256 lwm = $.lowWatermark;
288
- uint256 nextRequestId = $.nextRequestId;
289
- // Calling advanceLowWatermark with a 0 iterationBound will have no effect, but it will waste gas, and previously
290
- // has been indicative of a bug, so leaving this revert as a affordance to callers.
291
- require(iterationBound > 0, "iterateBound is zero!");
292
- uint256 limit = lwm + iterationBound;
293
- if (limit > nextRequestId) {
294
- limit = nextRequestId;
295
- }
296
-
297
- while (lwm < limit) {
298
- Request storage request = $.requests[lwm];
299
- // Stop advancing when we hit the first still pending request
300
- if (pending(request)) {
301
- break;
302
- }
303
- lwm++;
304
- }
305
- $.lowWatermark = lwm;
306
- }
307
- }
@@ -1,34 +0,0 @@
1
- // SPDX-License-Identifier: No License
2
- pragma solidity ^0.8;
3
-
4
- struct Request {
5
- uint256 maxTimestamp;
6
- address callbackContract;
7
- bytes4 callbackSelector;
8
- bool fulfilled;
9
- bytes data;
10
- // Adding at end of struct which should be unused state slot for version 0.2.0
11
- bytes32 handle;
12
- // This value is redundant for the contract but required for getPendingRequests, rather than duplicate this struct
13
- // adding it here redundantly for now
14
- uint256 requestId;
15
- }
16
-
17
- struct DecryptionResult {
18
- bytes32 abiEncodedResult;
19
- uint256 requestId;
20
- // Added in 0.2.0 to offer an integrity check
21
- bytes32 handle;
22
- }
23
-
24
- struct SignedDecryptionResult {
25
- DecryptionResult result;
26
- bytes signature;
27
- }
28
-
29
- event DecryptionRequested(
30
- uint256 indexed requestId,
31
- bytes32 indexed handle,
32
- uint256 maxTimestamp,
33
- uint256 eventId
34
- );
@@ -1,16 +0,0 @@
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 { HandleGeneration } from "./primitives/HandleGeneration.sol";
7
- import { euint256, ebool, eaddress, EOps, ETypes, EVM_HOST_CHAIN_PREFIX, HANDLE_VERSION, HANDLE_INDEX } from "../Types.sol";
8
- import { HandleAlreadyExists } from "../Errors.sol";
9
-
10
- interface IEncryptedInputGen {
11
- function newEuint256(bytes memory ciphertext, address user) external returns (euint256 newValue);
12
-
13
- function newEbool(bytes memory ciphertext, address user) external returns (ebool newValue);
14
-
15
- function newEaddress(bytes memory ciphertext, address user) external returns (eaddress newValue);
16
- }