Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): enable remote attestation in SGX prover #15559

Merged
merged 54 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
fe9d879
first code integration
Jan 24, 2024
476f107
remove submodules
Jan 24, 2024
9a7bf55
delete subm
Jan 24, 2024
9d9dc77
add test files
Jan 24, 2024
33ab224
remove p256
Jan 24, 2024
a8efc3f
remove files
Jan 24, 2024
8c5e3d3
own dit submodule
Jan 24, 2024
a607023
fix failing tests
Jan 24, 2024
d61243f
integrate to protocol (test failing)
Jan 25, 2024
f16255a
Integrate SGX attestation to every sgx block proof
Jan 25, 2024
0f0b730
gas limit
Jan 25, 2024
fda9b1d
add verifyParsedQuote support
Jan 25, 2024
35f1236
Support own registration per instance and stay proving as is
Jan 25, 2024
eef3ec6
add back long iterations in test
Jan 25, 2024
66bef6a
Merge branch 'alpha-6' into onchain_remote_attestation
adaki2004 Jan 25, 2024
16ed784
deploy and config setter scripts
Jan 26, 2024
59fa0bc
fix typo
Jan 26, 2024
de3701a
fix deploy
Jan 27, 2024
91bb1db
Merge branch 'submodules' into onchain_remote_attestation
dantaik Jan 29, 2024
89e274f
Merge branch 'alpha-6' into onchain_remote_attestation
dantaik Jan 29, 2024
99d7a60
add taiko on automata dcap submodule
adaki2004 Jan 29, 2024
4d4ebad
update readme
adaki2004 Jan 29, 2024
728fd6c
feat(protocol): add forge script to config dcap sgx verifier (#15596)
smtmfft Jan 29, 2024
5d84deb
Reverting to de3701a
Jan 29, 2024
859396c
forge fmt
Jan 29, 2024
b5aa209
fix set param
Jan 29, 2024
da73d6c
remove cached submodule
Jan 29, 2024
b78b7f0
Merge branch 'alpha-6' into onchain_remote_attestation
dantaik Jan 29, 2024
51e9236
Merge branch 'alpha-6' into onchain_remote_attestation
dantaik Jan 29, 2024
b06f34f
PR findings and renamings
Jan 29, 2024
ab74e40
changes
Jan 30, 2024
27c2683
delete old submodule
Jan 30, 2024
69680a6
Tests and 0.8.24 conform submodule
Jan 30, 2024
618c5c6
Merge branch 'alpha-6' into onchain_remote_attestation
adaki2004 Jan 30, 2024
bdba41d
adaptor and erc721 vault
Jan 30, 2024
75d2223
Use alpha6 for erc20 and nft ad
Jan 30, 2024
66f337b
forge fmt and move some test vars
Jan 30, 2024
4e6bff2
fix(protocol): remove useless env params (#15611)
smtmfft Jan 30, 2024
3dba5da
remove permissions as redundant
Jan 30, 2024
612e648
Merge branch 'alpha-6' into onchain_remote_attestation
adaki2004 Jan 30, 2024
381d947
Merge branch 'alpha-6' into onchain_remote_attestation
dantaik Jan 30, 2024
2af905f
reference thirdparty
Jan 30, 2024
7e4821a
Merge branch 'alpha-6' into onchain_remote_attestation
dantaik Jan 31, 2024
5c4ec04
Update SgxVerifier.sol
dantaik Jan 31, 2024
b15bd17
Update TaikoL1.sol
dantaik Jan 31, 2024
f78f429
Merge branch 'alpha-6' into onchain_remote_attestation
adaki2004 Jan 31, 2024
a2b06df
Brecht comments
Jan 31, 2024
424203c
Brechts findings nr2
Feb 1, 2024
43a6406
onlyFromOwnerOrNamed
Feb 1, 2024
63be182
fix rollup_watchdog and forge fmt
Feb 1, 2024
4c4eb3a
rename
Feb 1, 2024
1383ff5
Update packages/protocol/contracts/L1/verifiers/SgxVerifier.sol
adaki2004 Feb 1, 2024
4c96e7e
rename event var
Feb 1, 2024
bdd5a40
Merge branch 'alpha-6' into onchain_remote_attestation
adaki2004 Feb 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
path = packages/protocol/lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = chore/v1.5.1
dantaik marked this conversation as resolved.
Show resolved Hide resolved
[submodule "packages/protocol/lib/solady"]
dantaik marked this conversation as resolved.
Show resolved Hide resolved
path = packages/protocol/lib/solady
url = https://github.com/Vectorized/solady
[submodule "packages/protocol/lib/p256-verifier"]
path = packages/protocol/lib/p256-verifier
url = https://github.com/taikoxyz/p256-verifier
branch = use_at_taiko
99 changes: 64 additions & 35 deletions packages/protocol/contracts/L1/verifiers/SgxVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pragma solidity 0.8.24;
import "lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol";
import "../../common/EssentialContract.sol";
import "../../thirdparty/optimism/Bytes.sol";
import "../../thirdparty/automata-attestation/interfaces/IAttestation.sol";
import "../../thirdparty/automata-attestation/lib/QuoteV3Auth/V3Struct.sol";
import "../ITaikoL1.sol";
import "./IVerifier.sol";

Expand All @@ -32,10 +34,12 @@ contract SgxVerifier is EssentialContract, IVerifier {
/// bootstrapping the network with trustworthy instances.
struct Instance {
address addr;
uint64 addedAt; // We can calculate if expired
uint64 validSince;
}

uint256 public constant INSTANCE_EXPIRY = 180 days;
uint64 public constant INSTANCE_EXPIRY = 180 days;
// A security feature, a delay until an instanace is enabled when using onchain RA verification
uint64 public constant INSTANCE_VALIDITY_DELAY = 1 days;

/// @dev For gas savings, we shall assign each SGX instance with an id
/// so that when we need to set a new pub key, just write storage once.
Expand All @@ -52,12 +56,17 @@ contract SgxVerifier is EssentialContract, IVerifier {
uint256[48] private __gap;

event InstanceAdded(
uint256 indexed id, address indexed instance, address replaced, uint256 timstamp
uint256 indexed id, address indexed instance, address replaced, uint256 validSince
);
event InstanceDeleted(uint256 indexed id, address indexed instance);

error SGX_DELETE_NOT_AUTHORIZED();
error SGX_INVALID_ATTESTATION();
error SGX_INVALID_INSTANCE();
error SGX_INVALID_INSTANCES();
error SGX_INVALID_PROOF();
error SGX_MISSING_ATTESTATION();
error SGX_RA_NOT_SUPPORTED();

/// @notice Initializes the contract with the provided address manager.
/// @param _addressManager The address of the address manager contract.
Expand All @@ -74,40 +83,46 @@ contract SgxVerifier is EssentialContract, IVerifier {
returns (uint256[] memory ids)
{
if (_instances.length == 0) revert SGX_INVALID_INSTANCES();
ids = _addInstances(_instances);
ids = _addInstances(_instances, true);
}

/// @notice Adds SGX instances to the registry by another SGX instance.
Brechtpd marked this conversation as resolved.
Show resolved Hide resolved
/// @param id The id of the SGX instance who is adding new members.
/// @param newInstance The new address of this instance.
/// @param extraInstances The address array of SGX instances.
/// @param signature The signature proving authenticity.
/// @return ids The respective instanceId array per addresses.
function addInstances(
uint256 id,
address newInstance,
address[] calldata extraInstances,
bytes calldata signature
)
/// @notice Deletes SGX instances from the registry.
/// @param _ids The ids array of SGX instances.
function deleteInstances(uint256[] calldata _ids)
external
returns (uint256[] memory ids)
onlyFromOwnerOrNamed("rollup_watchdog")
{
address taikoL1 = resolve("taiko", false);
bytes32 signedHash = keccak256(
abi.encode(
"ADD_INSTANCES",
ITaikoL1(taikoL1).getConfig().chainId,
address(this),
newInstance,
extraInstances
)
);
address oldInstance = ECDSA.recover(signedHash, signature);
if (!_isInstanceValid(id, oldInstance)) revert SGX_INVALID_INSTANCE();
if (_ids.length == 0) revert SGX_INVALID_INSTANCES();
for (uint256 i; i < _ids.length; ++i) {
if (instances[_ids[i]].addr == address(0)) revert SGX_INVALID_INSTANCE();

_replaceInstance(id, oldInstance, newInstance);
emit InstanceDeleted(_ids[i], instances[_ids[i]].addr);

delete instances[_ids[i]];
}
}

/// @notice Adds an SGX instance after the attestation is verified
/// @param attestation The parsed attestation quote.
/// @return id The respective instanceId
function registerInstance(V3Struct.ParsedV3QuoteStruct calldata attestation)
Brechtpd marked this conversation as resolved.
Show resolved Hide resolved
external
returns (uint256)
{
address automataDcapAttestation = (resolve("automata_dcap_attestation", true));

if (automataDcapAttestation == address(0)) {
revert SGX_RA_NOT_SUPPORTED();
}

(bool verified,) = IAttestation(automataDcapAttestation).verifyParsedQuote(attestation);

if (!verified) revert SGX_INVALID_ATTESTATION();

ids = _addInstances(extraInstances);
address[] memory _address = new address[](1);
_address[0] = address(bytes20(attestation.localEnclaveReport.reportData));

return _addInstances(_address, false)[0];
}

/// @inheritdoc IVerifier
Expand Down Expand Up @@ -161,29 +176,43 @@ contract SgxVerifier is EssentialContract, IVerifier {
);
}

function _addInstances(address[] calldata _instances) private returns (uint256[] memory ids) {
function _addInstances(
address[] memory _instances,
bool instantValid
)
private
returns (uint256[] memory ids)
{
ids = new uint256[](_instances.length);

uint64 validSince = uint64(block.timestamp);

if (!instantValid) {
validSince += INSTANCE_VALIDITY_DELAY;
}

for (uint256 i; i < _instances.length; ++i) {
if (_instances[i] == address(0)) revert SGX_INVALID_INSTANCE();

instances[nextInstanceId] = Instance(_instances[i], uint64(block.timestamp));
instances[nextInstanceId] = Instance(_instances[i], validSince);
ids[i] = nextInstanceId;

emit InstanceAdded(nextInstanceId, _instances[i], address(0), block.timestamp);
emit InstanceAdded(nextInstanceId, _instances[i], address(0), validSince);

nextInstanceId++;
}
}

function _replaceInstance(uint256 id, address oldInstance, address newInstance) private {
// Replacing an instance means, it went through a cooldown (if added by on-chain RA) so no
// need to have a cooldown
instances[id] = Instance(newInstance, uint64(block.timestamp));
emit InstanceAdded(id, newInstance, oldInstance, block.timestamp);
}

function _isInstanceValid(uint256 id, address instance) private view returns (bool) {
if (instance == address(0)) return false;
if (instance != instances[id].addr) return false;
return instances[id].addedAt + INSTANCE_EXPIRY > block.timestamp;
return instances[id].validSince <= block.timestamp && block.timestamp <= instances[id].validSince + INSTANCE_EXPIRY;
}
}
5 changes: 5 additions & 0 deletions packages/protocol/contracts/thirdparty/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# ABOUT THIRDPARTY CODE

- /automata-attestation: original code (main branch) forked from https://github.com/automata-network/automata-dcap-v3-attestation and applied some gas optimizations here: https://github.com/smtmfft/automata-dcap-v3-attestation/tree/optimize-gas, which then got merged into taiko-mono.

- /optimism: code copied from https://github.com/ethereum-optimism/optimism/releases/tag/op-batcher%2Fv1.4.3 as-is with only solidity pragma changed.

Loading
Loading