@inco/lightning 0.3.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +2 -8
  2. package/manifest.yaml +24 -11
  3. package/package.json +2 -5
  4. package/src/DeployUtils.sol +113 -77
  5. package/src/IIncoLightning.sol +16 -9
  6. package/src/IncoLightning.sol +18 -9
  7. package/src/IncoVerifier.sol +47 -0
  8. package/src/Lib.alphanet.sol +14 -15
  9. package/src/Lib.demonet.sol +14 -15
  10. package/src/Lib.devnet.sol +14 -15
  11. package/src/Lib.sol +14 -15
  12. package/src/Lib.template.sol +15 -44
  13. package/src/Lib.testnet.sol +14 -15
  14. package/src/Types.sol +7 -0
  15. package/src/interfaces/IIncoLightning.sol +20 -0
  16. package/src/interfaces/IIncoVerifier.sol +24 -0
  17. package/src/interfaces/automata-interfaces/BELE.sol +20 -0
  18. package/src/interfaces/automata-interfaces/IAutomataEnclaveIdentityDao.sol +28 -0
  19. package/src/interfaces/automata-interfaces/IFmspcTcbDao.sol +10 -0
  20. package/src/interfaces/automata-interfaces/IPCCSRouter.sol +94 -0
  21. package/src/interfaces/automata-interfaces/IPCCSRouterExtended.sol +10 -0
  22. package/src/interfaces/automata-interfaces/IPcsDao.sol +18 -0
  23. package/src/interfaces/automata-interfaces/IQuoteVerifier.sol +34 -0
  24. package/src/interfaces/automata-interfaces/Types.sol +193 -0
  25. package/src/libs/incoLightning_alphanet_v0_297966649.sol +14 -15
  26. package/src/libs/incoLightning_demonet_v0_863421733.sol +14 -15
  27. package/src/libs/incoLightning_devnet_v0_340846814.sol +14 -15
  28. package/src/libs/incoLightning_testnet_v0_183408998.sol +14 -15
  29. package/src/libs/incoLightning_testnet_v1_938327937.sol +451 -0
  30. package/src/lightning-parts/AccessControl/AdvancedAccessControl.sol +133 -0
  31. package/src/lightning-parts/AccessControl/AdvancedAccessControl.types.sol +18 -0
  32. package/src/lightning-parts/AccessControl/BaseAccessControlList.sol +42 -3
  33. package/src/lightning-parts/AccessControl/interfaces/IAdvancedAccessControl.sol +25 -0
  34. package/src/lightning-parts/AccessControl/interfaces/IBaseAccessControlList.sol +24 -0
  35. package/src/lightning-parts/AccessControl/test/TestAdvancedAccessControl.t.sol +234 -0
  36. package/src/lightning-parts/AccessControl/test/TestBaseAccessControl.t.sol +16 -2
  37. package/src/lightning-parts/DecryptionAttester.sol +45 -0
  38. package/src/lightning-parts/DecryptionAttester.types.sol +7 -0
  39. package/src/lightning-parts/EncryptedInput.sol +10 -13
  40. package/src/lightning-parts/EncryptedOperations.sol +78 -28
  41. package/src/lightning-parts/TEELifecycle.sol +180 -88
  42. package/src/lightning-parts/TrivialEncryption.sol +9 -9
  43. package/src/lightning-parts/interfaces/IDecryptionAttester.sol +9 -0
  44. package/src/lightning-parts/interfaces/IEncryptedInput.sol +19 -0
  45. package/src/lightning-parts/interfaces/IEncryptedOperations.sol +31 -0
  46. package/src/lightning-parts/interfaces/ITEELifecycle.sol +26 -0
  47. package/src/lightning-parts/interfaces/ITrivialEncryption.sol +10 -0
  48. package/src/lightning-parts/primitives/EventCounter.sol +15 -3
  49. package/src/lightning-parts/primitives/HandleGeneration.sol +8 -3
  50. package/src/lightning-parts/primitives/HandleMetadata.sol +1 -1
  51. package/src/lightning-parts/primitives/LightningAddressGetter.sol +10 -0
  52. package/src/lightning-parts/primitives/SignatureVerifier.sol +2 -9
  53. package/src/lightning-parts/primitives/VerifierAddressGetter.sol +13 -0
  54. package/src/lightning-parts/primitives/{EventCounter.gen.sol → interfaces/IEventCounter.sol} +4 -2
  55. package/src/lightning-parts/primitives/interfaces/IHandleGeneration.sol +41 -0
  56. package/src/lightning-parts/primitives/interfaces/ISignatureVerifier.sol +9 -0
  57. package/src/lightning-parts/primitives/interfaces/IVerifierAddressGetter.sol +8 -0
  58. package/src/lightning-parts/test/HandleMetadata.t.sol +25 -6
  59. package/src/lightning-parts/test/TestDecryptionAttestationInSynchronousFlow.t.sol +78 -0
  60. package/src/periphery/SessionVerifier.sol +63 -0
  61. package/src/test/AddTwo.sol +20 -24
  62. package/src/test/FakeIncoInfra/FakeDecryptionAttester.sol +198 -0
  63. package/src/test/FakeIncoInfra/FakeIncoInfraBase.sol +0 -15
  64. package/src/test/FakeIncoInfra/FakeQuoteVerifier.sol +10 -6
  65. package/src/test/FakeIncoInfra/MockOpHandler.sol +0 -7
  66. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +19 -7
  67. package/src/test/FakeIncoInfra/getOpForSelector.sol +0 -3
  68. package/src/test/IncoTest.sol +12 -11
  69. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +98 -77
  70. package/src/test/TestAddTwo.t.sol +16 -9
  71. package/src/test/TestFakeInfra.t.sol +10 -27
  72. package/src/test/TestUpgrade.t.sol +11 -4
  73. package/src/test/TestVersion.t.sol +0 -7
  74. package/src/version/IncoLightningConfig.sol +4 -3
  75. package/src/version/SessionVerifierConfig.sol +8 -0
  76. package/src/version/Version.sol +7 -9
  77. package/src/version/interfaces/IVersion.sol +8 -0
  78. package/src/DeployTEE.sol +0 -153
  79. package/src/IncoLightning.gen.sol +0 -15
  80. package/src/lightning-parts/AccessControl/BaseAccessControlList.gen.sol +0 -19
  81. package/src/lightning-parts/DecryptionHandler.gen.sol +0 -54
  82. package/src/lightning-parts/DecryptionHandler.sol +0 -307
  83. package/src/lightning-parts/DecryptionHandler.types.sol +0 -34
  84. package/src/lightning-parts/EncryptedInput.gen.sol +0 -16
  85. package/src/lightning-parts/EncryptedOperations.gen.sol +0 -59
  86. package/src/lightning-parts/TEELifecycle.gen.sol +0 -58
  87. package/src/lightning-parts/TrivialEncryption.gen.sol +0 -15
  88. package/src/lightning-parts/primitives/HandleGeneration.gen.sol +0 -19
  89. package/src/lightning-parts/primitives/HandleMetadata.gen.sol +0 -4
  90. package/src/lightning-parts/primitives/SignatureVerifier.gen.sol +0 -16
  91. package/src/test/FibonacciDecrypt.sol +0 -49
  92. package/src/test/TEELifecycle/TEELifecycleHWTest.t.sol +0 -119
  93. package/src/test/TEELifecycle/addnode_data/eoa.txt +0 -1
  94. package/src/test/TEELifecycle/addnode_data/quote.bin +0 -0
  95. package/src/test/TEELifecycle/bootstrap_data/ecies_pubkey.bin +0 -1
  96. package/src/test/TEELifecycle/bootstrap_data/eip712_signature.bin +0 -1
  97. package/src/test/TEELifecycle/bootstrap_data/eoa.txt +0 -1
  98. package/src/test/TEELifecycle/bootstrap_data/qe_identity +0 -1
  99. package/src/test/TEELifecycle/bootstrap_data/qe_identity_signature.bin +0 -1
  100. package/src/test/TEELifecycle/bootstrap_data/quote.bin +0 -0
  101. package/src/test/TEELifecycle/bootstrap_data/tcb_info +0 -1
  102. package/src/test/TEELifecycle/bootstrap_data/tcb_info_signature.bin +0 -1
  103. package/src/test/TEELifecycle/test_cert/AttestationReportSigningCA.crl +0 -0
  104. package/src/test/TEELifecycle/test_cert/Intel_SGX_Attestation_RootCA.cer +0 -0
  105. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_CRL.crl +0 -0
  106. package/src/test/TEELifecycle/test_cert/Intel_SGX_PCK_PlatformCA.cer +0 -0
  107. package/src/test/TEELifecycle/test_cert/Intel_SGX_TCB_Signing.cer +0 -0
  108. package/src/version/Version.gen.sol +0 -14
@@ -5,10 +5,10 @@ import {EventCounter} from "./primitives/EventCounter.sol";
5
5
  import {euint256, ebool, eaddress, ETypes} from "../Types.sol";
6
6
  import {BaseAccessControlList} from "./AccessControl/BaseAccessControlList.sol";
7
7
  import {HandleGeneration} from "./primitives/HandleGeneration.sol";
8
- import {ITrivialEncryptionGen} from "./TrivialEncryption.gen.sol";
8
+ import {ITrivialEncryption} from "./interfaces/ITrivialEncryption.sol";
9
9
 
10
10
  abstract contract TrivialEncryption is
11
- ITrivialEncryptionGen,
11
+ ITrivialEncryption,
12
12
  BaseAccessControlList,
13
13
  EventCounter,
14
14
  HandleGeneration
@@ -31,7 +31,10 @@ abstract contract TrivialEncryption is
31
31
 
32
32
  function asEaddress(address value) external returns (eaddress newEaddress) {
33
33
  bytes32 castedValue = bytes32(uint256(uint160(value)));
34
- return eaddress.wrap(newTrivialEncrypt(castedValue, ETypes.AddressOrUint160OrBytes20));
34
+ return
35
+ eaddress.wrap(
36
+ newTrivialEncrypt(castedValue, ETypes.AddressOrUint160OrBytes20)
37
+ );
35
38
  }
36
39
 
37
40
  function newTrivialEncrypt(
@@ -40,11 +43,8 @@ abstract contract TrivialEncryption is
40
43
  ) internal returns (bytes32 newHandle) {
41
44
  newHandle = getTrivialEncryptHandle(plainTextBytes, handleType);
42
45
  allowTransientInternal(newHandle, msg.sender);
43
- emit TrivialEncrypt(
44
- newHandle,
45
- plainTextBytes,
46
- handleType,
47
- getNewEventId()
48
- );
46
+ uint256 id = getNextEventId();
47
+ emit TrivialEncrypt(newHandle, plainTextBytes, handleType, id);
48
+ setDigest(abi.encodePacked(newHandle, id));
49
49
  }
50
50
  }
@@ -0,0 +1,9 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import { DecryptionAttestation } from "../DecryptionAttester.types.sol";
5
+
6
+ interface IDecryptionAttester {
7
+ function decryptionAttestationDigest(DecryptionAttestation memory decryption) external view returns (bytes32);
8
+ function isValidDecryptionAttestation(DecryptionAttestation memory decryption, bytes memory signature) external view returns (bool);
9
+ }
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {euint256, ebool, eaddress} from "../../Types.sol";
5
+
6
+ interface IEncryptedInput {
7
+ function newEuint256(
8
+ bytes memory ciphertext,
9
+ address user
10
+ ) external returns (euint256 newValue);
11
+ function newEbool(
12
+ bytes memory ciphertext,
13
+ address user
14
+ ) external returns (ebool newValue);
15
+ function newEaddress(
16
+ bytes memory ciphertext,
17
+ address user
18
+ ) external returns (eaddress newValue);
19
+ }
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {euint256} from "../../Types.sol";
5
+
6
+ interface IEncryptedOperations {
7
+ function eAdd(
8
+ euint256 lhs,
9
+ euint256 rhs
10
+ ) external returns (euint256 result);
11
+ function eSub(
12
+ euint256 lhs,
13
+ euint256 rhs
14
+ ) external returns (euint256 result);
15
+ function eMul(
16
+ euint256 lhs,
17
+ euint256 rhs
18
+ ) external returns (euint256 result);
19
+ function eDiv(
20
+ euint256 lhs,
21
+ euint256 rhs
22
+ ) external returns (euint256 result);
23
+ function eRem(
24
+ euint256 lhs,
25
+ euint256 rhs
26
+ ) external returns (euint256 result);
27
+ function eBitAnd(
28
+ bytes32 lhs,
29
+ bytes32 rhs
30
+ ) external returns (bytes32 result);
31
+ }
@@ -0,0 +1,26 @@
1
+ /// SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8.19;
3
+
4
+ import {BootstrapResult} from "../TEELifecycle.types.sol";
5
+ import {IQuoteVerifier} from "../../interfaces/automata-interfaces/IQuoteVerifier.sol";
6
+ import {TD10ReportBody} from "../../interfaces/automata-interfaces/Types.sol";
7
+
8
+ interface ITEELifecycle {
9
+ function verifyBootstrapResult(
10
+ BootstrapResult calldata bootstrapResult,
11
+ bytes calldata quote,
12
+ bytes calldata signature
13
+ ) external;
14
+ function approveNewTEEVersion(bytes calldata newMRTD) external;
15
+ function addNewCovalidator(bytes calldata quote) external;
16
+ function parseTD10ReportBody(
17
+ bytes calldata rawQuote
18
+ ) external pure returns (TD10ReportBody memory report);
19
+ function parseReport(
20
+ TD10ReportBody memory tdReport
21
+ ) external pure returns (address, bytes memory);
22
+ function bootstrapResultDigest(
23
+ BootstrapResult memory bootstrapResult
24
+ ) external view returns (bytes32);
25
+ function quoteVerifier() external view returns (IQuoteVerifier);
26
+ }
@@ -0,0 +1,10 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {euint256, ebool, eaddress} from "../../Types.sol";
5
+
6
+ interface ITrivialEncryption {
7
+ function asEuint256(uint256 value) external returns (euint256 newEuint256);
8
+ function asEbool(bool value) external returns (ebool newEbool);
9
+ function asEaddress(address value) external returns (eaddress newEaddress);
10
+ }
@@ -1,10 +1,11 @@
1
1
  // SPDX-License-Identifier: No License
2
2
  pragma solidity ^0.8;
3
3
 
4
- import {IEventCounterGen} from "./EventCounter.gen.sol";
4
+ import {IEventCounter} from "./interfaces/IEventCounter.sol";
5
5
 
6
6
  contract EventCounterStorage {
7
7
  struct Storage {
8
+ // TODO: change type to bytes32 when we rename away from "counter".
8
9
  uint256 eventCounter;
9
10
  }
10
11
 
@@ -23,12 +24,23 @@ contract EventCounterStorage {
23
24
  }
24
25
  }
25
26
 
26
- contract EventCounter is IEventCounterGen, EventCounterStorage {
27
+ contract EventCounter is IEventCounter, EventCounterStorage {
27
28
  function getNewEventId() internal returns (uint256 newEventId) {
28
29
  newEventId = getEventCounterStorage().eventCounter++;
29
30
  }
30
31
 
31
- function getEventCounter() external view returns (uint256) {
32
+ function setDigest(bytes memory serialization) internal {
33
+ getEventCounterStorage().eventCounter = uint256(keccak256(serialization));
34
+ }
35
+
36
+ // @notice Gives the next event ID value to use.
37
+ function getNextEventId() public view returns (uint256) {
32
38
  return getEventCounterStorage().eventCounter;
33
39
  }
40
+
41
+ // @notice Gives the current value of the event counter.
42
+ // @dev DEPRECATED: use getNextEventId() instead.
43
+ function getEventCounter() public view returns (uint256) {
44
+ return getNextEventId();
45
+ }
34
46
  }
@@ -1,11 +1,16 @@
1
1
  // SPDX-License-Identifier: No License
2
2
  pragma solidity ^0.8;
3
3
 
4
- import {ETypes, EOps, EVM_HOST_CHAIN_PREFIX, HANDLE_INDEX} from "../../Types.sol";
4
+ import {
5
+ ETypes,
6
+ EOps,
7
+ EVM_HOST_CHAIN_PREFIX,
8
+ HANDLE_INDEX
9
+ } from "../../Types.sol";
5
10
  import {HandleMetadata} from "./HandleMetadata.sol";
6
- import {IHandleGenerationGen} from "./HandleGeneration.gen.sol";
11
+ import {IHandleGeneration} from "./interfaces/IHandleGeneration.sol";
7
12
 
8
- contract HandleGeneration is IHandleGenerationGen, HandleMetadata {
13
+ contract HandleGeneration is IHandleGeneration, HandleMetadata {
9
14
  function getTrivialEncryptHandle(
10
15
  bytes32 plaintextBytes,
11
16
  ETypes handleType
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: No License
2
2
  pragma solidity ^0.8;
3
3
 
4
- import {HANDLE_VERSION, HANDLE_INDEX, ETypes} from "../../Types.sol";
4
+ import { HANDLE_VERSION, HANDLE_INDEX, ETypes } from "../../Types.sol";
5
5
 
6
6
  contract HandleMetadata {
7
7
  function embedIndexTypeVersion(
@@ -0,0 +1,10 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ abstract contract LightningAddressGetter {
5
+ address internal immutable incoLightningAddress;
6
+
7
+ constructor(address _incoLightningAddress) {
8
+ incoLightningAddress = _incoLightningAddress;
9
+ }
10
+ }
@@ -3,7 +3,7 @@ pragma solidity ^0.8;
3
3
 
4
4
  import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
5
5
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
6
- import {ISignatureVerifierGen} from "./SignatureVerifier.gen.sol";
6
+ import {ISignatureVerifier} from "./interfaces/ISignatureVerifier.sol";
7
7
  import {TEELifecycle} from "../TEELifecycle.sol";
8
8
 
9
9
  contract SignatureVerifierStorage {
@@ -28,7 +28,7 @@ contract SignatureVerifierStorage {
28
28
  }
29
29
  }
30
30
 
31
- contract SignatureVerifier is ISignatureVerifierGen, OwnableUpgradeable, SignatureVerifierStorage {
31
+ abstract contract SignatureVerifier is ISignatureVerifier, OwnableUpgradeable, SignatureVerifierStorage {
32
32
  using ECDSA for bytes32;
33
33
 
34
34
  error SignerNotFound(address signerAddress);
@@ -36,13 +36,6 @@ contract SignatureVerifier is ISignatureVerifierGen, OwnableUpgradeable, Signatu
36
36
  event AddedSignatureVerifier(address signerAddress);
37
37
  event RemovedSignatureVerifier(address signerAddress);
38
38
 
39
- // Reference to the TEELifecycle contract, to get the list of EOA signers
40
- TEELifecycle teeLifecycle;
41
-
42
- function __SignatureVerifier_init(address _teeLifecycleAddress) internal {
43
- teeLifecycle = TEELifecycle(_teeLifecycleAddress);
44
- }
45
-
46
39
  // @todo: This function should be removed once we have a way to read the signers from the TEELifecycle contract
47
40
  function addSigner(address signerAddress) external onlyOwner {
48
41
  getSigVerifierStorage().isSigner[signerAddress] = true;
@@ -0,0 +1,13 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {IIncoVerifier} from "../../interfaces/IIncoVerifier.sol";
5
+ import {IVerifierAddressGetter} from "./interfaces/IVerifierAddressGetter.sol";
6
+
7
+ abstract contract VerifierAddressGetter is IVerifierAddressGetter {
8
+ IIncoVerifier public immutable incoVerifier;
9
+
10
+ constructor(address _incoVerifier) {
11
+ incoVerifier = IIncoVerifier(_incoVerifier);
12
+ }
13
+ }
@@ -1,6 +1,8 @@
1
1
  /// SPDX-License-Identifier: No License
2
2
  pragma solidity ^0.8;
3
3
 
4
- interface IEventCounterGen {
4
+ interface IEventCounter {
5
+ function getNextEventId() external view returns (uint256);
6
+
5
7
  function getEventCounter() external view returns (uint256);
6
- }
8
+ }
@@ -0,0 +1,41 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {ETypes, EOps} from "../../../Types.sol";
5
+
6
+ interface IHandleGeneration {
7
+ function getTrivialEncryptHandle(
8
+ bytes32 plaintextBytes,
9
+ ETypes handleType
10
+ ) external view returns (bytes32 generatedHandle);
11
+ function getInputHandle(
12
+ bytes memory ciphertext,
13
+ address user,
14
+ address contractAddress,
15
+ ETypes inputType
16
+ ) external view returns (bytes32 generatedHandle);
17
+ function getOpResultHandle(
18
+ EOps op,
19
+ ETypes returnType,
20
+ bytes32 lhs,
21
+ bytes32 rhs
22
+ ) external pure returns (bytes32 generatedHandle);
23
+ function getOpResultHandle(
24
+ EOps op,
25
+ ETypes returnType,
26
+ uint256 counter,
27
+ bytes32 upperBound
28
+ ) external pure returns (bytes32 generatedHandle);
29
+ function getOpResultHandle(
30
+ EOps op,
31
+ ETypes returnType,
32
+ bytes32 value
33
+ ) external pure returns (bytes32 generatedHandle);
34
+ function getOpResultHandle(
35
+ EOps op,
36
+ ETypes returnType,
37
+ bytes32 inputA,
38
+ bytes32 inputB,
39
+ bytes32 inputC
40
+ ) external pure returns (bytes32 generatedHandle);
41
+ }
@@ -0,0 +1,9 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ interface ISignatureVerifier {
5
+ function addSigner(address signerAddress) external;
6
+ function removeSigner(address signerAddress) external;
7
+ function isSigner(address signerAddress) external view returns (bool);
8
+ function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bool);
9
+ }
@@ -0,0 +1,8 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {IIncoVerifier} from "../../../interfaces/IIncoVerifier.sol";
5
+
6
+ interface IVerifierAddressGetter {
7
+ function incoVerifier() external view returns (IIncoVerifier);
8
+ }
@@ -7,7 +7,14 @@ import {TrivialEncryption} from "../TrivialEncryption.sol";
7
7
  import {EncryptedOperations} from "../EncryptedOperations.sol";
8
8
  import {EncryptedInput} from "../EncryptedInput.sol";
9
9
  import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
10
- import {ETypes, ebool, euint256, eaddress, typeToBitMask} from "../../Types.sol";
10
+ import {
11
+ ETypes,
12
+ ebool,
13
+ euint256,
14
+ eaddress,
15
+ typeToBitMask
16
+ } from "../../Types.sol";
17
+ import {VerifierAddressGetter} from "../primitives/VerifierAddressGetter.sol";
11
18
 
12
19
  contract TestHandleMetadata is
13
20
  EIP712,
@@ -17,7 +24,7 @@ contract TestHandleMetadata is
17
24
  EncryptedOperations,
18
25
  EncryptedInput
19
26
  {
20
- constructor() EIP712("", "") {}
27
+ constructor() EIP712("", "") VerifierAddressGetter(address(0)) {}
21
28
 
22
29
  function testTypeAssignment() public pure {
23
30
  bytes32 someHandle = bytes32(keccak256("someHandle"));
@@ -30,8 +37,12 @@ contract TestHandleMetadata is
30
37
  ETypes.Uint256
31
38
  );
32
39
  assert(
33
- typeOf(embedIndexTypeVersion(someHandle, ETypes.AddressOrUint160OrBytes20)) ==
34
- ETypes.AddressOrUint160OrBytes20
40
+ typeOf(
41
+ embedIndexTypeVersion(
42
+ someHandle,
43
+ ETypes.AddressOrUint160OrBytes20
44
+ )
45
+ ) == ETypes.AddressOrUint160OrBytes20
35
46
  );
36
47
  }
37
48
 
@@ -40,7 +51,9 @@ contract TestHandleMetadata is
40
51
  assert(typeOf(boolHandle) == ETypes.Bool);
41
52
  bytes32 uintHandle = euint256.unwrap(this.asEuint256(42));
42
53
  assert(typeOf(uintHandle) == ETypes.Uint256);
43
- bytes32 addressHandle = eaddress.unwrap(this.asEaddress(address(0xdeadbeef)));
54
+ bytes32 addressHandle = eaddress.unwrap(
55
+ this.asEaddress(address(0xdeadbeef))
56
+ );
44
57
  assert(typeOf(addressHandle) == ETypes.AddressOrUint160OrBytes20);
45
58
  }
46
59
 
@@ -70,7 +83,13 @@ contract TestHandleMetadata is
70
83
  this.eIfThenElse(control, ebool.unwrap(c), ebool.unwrap(d))
71
84
  ) == ETypes.Bool
72
85
  );
73
- assert(typeOf(ebool.unwrap(this.eEq(eaddress.unwrap(addr1), eaddress.unwrap(addr2)))) == ETypes.Bool);
86
+ assert(
87
+ typeOf(
88
+ ebool.unwrap(
89
+ this.eEq(eaddress.unwrap(addr1), eaddress.unwrap(addr2))
90
+ )
91
+ ) == ETypes.Bool
92
+ );
74
93
  }
75
94
 
76
95
  function testEIfThenElseChecksTypeCoherence() public {
@@ -0,0 +1,78 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {IncoTest} from "../../test/IncoTest.sol";
5
+ import {DemoToken} from "@inco/confidential-token-demo/src/DemoToken.sol";
6
+ import {DecryptionAttestation} from "../DecryptionAttester.types.sol";
7
+ import {GWEI} from "@inco/shared/src/TypeUtils.sol";
8
+ import {euint256} from "@inco/lightning/src/Lib.sol"; // import via remapping or compiler fails
9
+ import {AllowanceProof} from "../AccessControl/AdvancedAccessControl.sol";
10
+ import {inco} from "../../Lib.sol";
11
+ import {euint256 as remappedEuint256} from "@inco/lightning/src/Lib.sol";
12
+
13
+ contract TokenBurnCurrentBalance is DemoToken {
14
+ function burnFullCurrentBalance(
15
+ DecryptionAttestation memory decryption,
16
+ bytes memory signature
17
+ ) public {
18
+ euint256 currentBalance = confidentialBalanceOf(msg.sender);
19
+ require(
20
+ inco.incoVerifier().isValidDecryptionAttestation(
21
+ decryption,
22
+ signature
23
+ ),
24
+ "Invalid Signature"
25
+ );
26
+ require(
27
+ euint256.unwrap(currentBalance) == decryption.handle,
28
+ "Handle mismatch"
29
+ );
30
+ publicBurn(msg.sender, uint256(decryption.value));
31
+ }
32
+ }
33
+
34
+ contract TestDecryptionAttestationInSynchronousFlow is IncoTest {
35
+ AllowanceProof emptyProof; // no proof needed when requester has the handle in persisted allowed pairs
36
+ function testSynchronousBurning() public {
37
+ TokenBurnCurrentBalance token = new TokenBurnCurrentBalance();
38
+ token.confidentialTransfer(
39
+ alice,
40
+ fakePrepareEuint256Ciphertext(10 * GWEI),
41
+ ""
42
+ );
43
+ processAllOperations(); // saves Alice's balance
44
+ bytes32 aliceCurrentBalanceHandle = euint256.unwrap(
45
+ token.confidentialBalanceOf(alice)
46
+ );
47
+ // simulates Alice requesting for a decryption attestation of Ge op on her balance and the amount
48
+ // she intends to burn, therefore proving to the token contract that the operation will succeed
49
+ (
50
+ DecryptionAttestation memory decryption,
51
+ bytes memory signature
52
+ ) = getDecryptionAttestation(
53
+ alice,
54
+ HandleWithProof({
55
+ handle: aliceCurrentBalanceHandle,
56
+ proof: emptyProof
57
+ })
58
+ );
59
+ vm.prank(alice);
60
+
61
+ // the decryption attestation is passed to the token burn method
62
+ token.burnFullCurrentBalance(decryption, signature);
63
+
64
+ processAllOperations();
65
+
66
+ remappedEuint256 remappedFinalAliceBalance = token
67
+ .confidentialBalanceOf(alice);
68
+ bytes32 finalAliceBalance = remappedEuint256.unwrap(
69
+ remappedFinalAliceBalance
70
+ ); // compilation trick
71
+
72
+ assertEq(
73
+ uint256(get(finalAliceBalance)),
74
+ 0,
75
+ "Alice should have burned all her tokens"
76
+ );
77
+ }
78
+ }
@@ -0,0 +1,63 @@
1
+ // SPDX-License-Identifier: No License
2
+ pragma solidity ^0.8;
3
+
4
+ import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
5
+ import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
6
+ import {
7
+ SESSION_VERIFIER_NAME,
8
+ SESSION_VERIFIER_MAJOR_VERSION,
9
+ SESSION_VERIFIER_MINOR_VERSION,
10
+ SESSION_VERIFIER_PATCH_VERSION
11
+ } from "../version/SessionVerifierConfig.sol";
12
+ import {Version} from "../version/Version.sol";
13
+ import {ALLOWANCE_GRANTED_MAGIC_VALUE} from "../Types.sol";
14
+
15
+ /// @notice a Session grants a temporary access to a decrypter to all data held by the sharer
16
+ /// @dev abi encode this struct in the sharerArgData field of the voucher
17
+ struct Session {
18
+ address decrypter;
19
+ uint256 expiresAt;
20
+ }
21
+
22
+ /// @notice Inco access sharing verifier mainly meant for browser dapp sessions, grants access to all data held by
23
+ /// the sharer to one decrypter for a limited time.
24
+ /// @dev define the selector of canUseSession in the voucher to use this verifier
25
+ contract SessionVerifier is UUPSUpgradeable, OwnableUpgradeable, Version {
26
+ constructor(
27
+ bytes32 salt
28
+ )
29
+ Version(
30
+ SESSION_VERIFIER_MAJOR_VERSION,
31
+ SESSION_VERIFIER_MINOR_VERSION,
32
+ SESSION_VERIFIER_PATCH_VERSION,
33
+ salt,
34
+ SESSION_VERIFIER_NAME
35
+ )
36
+ {}
37
+
38
+ // todo add text mention of what is being signed
39
+ function canUseSession(
40
+ bytes32 /* handle */,
41
+ address account,
42
+ bytes memory sharerArgData,
43
+ bytes memory /* requesterArgData */
44
+ ) external view returns (bytes32) {
45
+ Session memory session = abi.decode(sharerArgData, (Session));
46
+ if (
47
+ session.expiresAt >= block.timestamp && session.decrypter == account
48
+ ) {
49
+ return ALLOWANCE_GRANTED_MAGIC_VALUE;
50
+ }
51
+ return bytes32(0);
52
+ }
53
+
54
+ function _authorizeUpgrade(address) internal view override {
55
+ require(msg.sender == owner());
56
+ }
57
+
58
+ function initialize(address owner) public initializer {
59
+ __Ownable_init(owner);
60
+ }
61
+
62
+ fallback() external {} // must be included for createX deploy
63
+ }
@@ -1,11 +1,15 @@
1
1
  // SPDX-License-Identifier: No License
2
2
  pragma solidity ^0.8;
3
3
 
4
- import {IncoLightning} from "../IncoLightning.sol";
5
4
  import {euint256} from "../Types.sol";
5
+ import {IncoLightning} from "../IncoLightning.sol";
6
+
7
+ // import {console} from "forge-std/console.sol";
6
8
 
9
+ // To implement such a contract, we would normally import e form Lib.sol. For test purposes, we take inco as
10
+ // a constructor argument instead, so we can test it from other deployment addresses.
7
11
  contract AddTwo {
8
- IncoLightning immutable inco;
12
+ IncoLightning inco;
9
13
 
10
14
  constructor(IncoLightning _inco) {
11
15
  inco = _inco;
@@ -14,37 +18,29 @@ contract AddTwo {
14
18
  // Stores the result of the last callback.
15
19
  uint256 public lastResult;
16
20
 
17
- function addTwo(euint256 a) external returns (euint256) {
18
- uint256 two = 2;
19
- return inco.eAdd(a, inco.asEuint256(two));
21
+ function addTwo(euint256 a) public returns (euint256) {
22
+ euint256 two = inco.asEuint256(2);
23
+ return inco.eAdd(a, two);
20
24
  }
21
25
 
22
- function addTwoScalar(euint256 a) external returns (euint256) {
23
- uint256 two = 2;
24
- return inco.eAdd(a, inco.asEuint256(two));
26
+ // To generate a different handle with the same result, instead of adding 2 we add 1 + 1
27
+ function addTwoAlt(euint256 a) public returns (euint256) {
28
+ euint256 one = inco.asEuint256(1);
29
+ return inco.eAdd(a, inco.eAdd(one, one));
25
30
  }
26
31
 
27
32
  function addTwoEOA(
28
33
  bytes memory uint256EInput
29
- ) external returns (uint256, euint256) {
34
+ ) external returns (euint256 result, euint256 resultRevealed) {
30
35
  euint256 value = inco.newEuint256(uint256EInput, msg.sender);
31
- euint256 result = this.addTwo(value);
36
+ result = addTwo(value);
37
+
32
38
  inco.allow(euint256.unwrap(result), address(this));
33
39
  inco.allow(euint256.unwrap(result), msg.sender);
34
- uint256 requestId = inco.requestDecryption(
35
- this.callback.selector,
36
- block.timestamp + 2 hours,
37
- euint256.unwrap(result),
38
- ""
39
- );
40
- return (requestId, result);
41
- }
42
40
 
43
- function callback(
44
- uint256 /* requestId */,
45
- uint256 result,
46
- bytes memory /* data */
47
- ) external {
48
- lastResult = result;
41
+ // Used to test attested reveal functionality.
42
+ // Note that msg.sender is not allowed, instead we call .reveal() that gives permission to anyone.
43
+ resultRevealed = addTwoAlt(value);
44
+ inco.reveal(euint256.unwrap(resultRevealed));
49
45
  }
50
46
  }