|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.12; |
| 3 | + |
| 4 | +import {IBCHeight} from "@hyperledger-labs/yui-ibc-solidity/contracts/core/02-client/IBCHeight.sol"; |
| 5 | +import {Height} from "@hyperledger-labs/yui-ibc-solidity/contracts/proto/Client.sol"; |
| 6 | +import {IRiscZeroVerifier} from "risc0-ethereum/contracts/src/IRiscZeroVerifier.sol"; |
| 7 | +import { |
| 8 | + IbcLightclientsLcpV1ClientState as ProtoClientState, |
| 9 | + IbcLightclientsLcpV1ZKDCAPRegisterEnclaveKeyMessage as ZKDCAPRegisterEnclaveKeyMessage |
| 10 | +} from "./proto/ibc/lightclients/lcp/v1/LCP.sol"; |
| 11 | +import {LCPProtoMarshaler} from "./LCPProtoMarshaler.sol"; |
| 12 | +import {LCPClientBase} from "./LCPClientBase.sol"; |
| 13 | +import {LCPOperator} from "./LCPOperator.sol"; |
| 14 | +import {RemoteAttestation} from "./RemoteAttestation.sol"; |
| 15 | +import {DCAPValidator} from "./DCAPValidator.sol"; |
| 16 | + |
| 17 | +abstract contract LCPClientZKDCAPBase is LCPClientBase { |
| 18 | + using IBCHeight for Height.Data; |
| 19 | + // --------------------- Constants --------------------- |
| 20 | + |
| 21 | + uint8 internal constant ZKVM_TYPE_RISC_ZERO = 0x01; |
| 22 | + |
| 23 | + // --------------------- Events --------------------- |
| 24 | + |
| 25 | + event ZKDCAPRegisteredEnclaveKey(string clientId, address enclaveKey, uint256 expiredAt, address operator); |
| 26 | + |
| 27 | + // --------------------- Immutable fields --------------------- |
| 28 | + |
| 29 | + /// @dev if developmentMode is true, the client allows the target enclave which is debug mode enabled. |
| 30 | + /// @custom:oz-upgrades-unsafe-allow state-variable-immutable |
| 31 | + bool internal immutable developmentMode; |
| 32 | + |
| 33 | + /// @notice The hash of the root CA's public key certificate. |
| 34 | + /// @custom:oz-upgrades-unsafe-allow state-variable-immutable |
| 35 | + bytes32 public immutable intelRootCAHash; |
| 36 | + |
| 37 | + /// @notice RISC Zero verifier contract address. |
| 38 | + /// @custom:oz-upgrades-unsafe-allow state-variable-immutable |
| 39 | + IRiscZeroVerifier public immutable riscZeroVerifier; |
| 40 | + |
| 41 | + // --------------------- Storage fields --------------------- |
| 42 | + |
| 43 | + /// @dev Reserved storage space to allow for layout changes in the future |
| 44 | + uint256[50] private __gap; |
| 45 | + |
| 46 | + // --------------------- Constructor --------------------- |
| 47 | + |
| 48 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 49 | + /// @param ibcHandler_ the address of the IBC handler contract |
| 50 | + constructor(address ibcHandler_, bool developmentMode_, bytes memory intelRootCA, address riscZeroVerifier_) |
| 51 | + LCPClientBase(ibcHandler_) |
| 52 | + { |
| 53 | + if (intelRootCA.length == 0 || riscZeroVerifier_ == address(0)) { |
| 54 | + revert LCPClientZKDCAPInvalidConstructorParams(); |
| 55 | + } |
| 56 | + intelRootCAHash = keccak256(intelRootCA); |
| 57 | + riscZeroVerifier = IRiscZeroVerifier(riscZeroVerifier_); |
| 58 | + developmentMode = developmentMode_; |
| 59 | + } |
| 60 | + |
| 61 | + // --------------------- Public methods --------------------- |
| 62 | + |
| 63 | + /** |
| 64 | + * @dev initializeClient initializes a new client with the given state. |
| 65 | + * If succeeded, it returns heights at which the consensus state are stored. |
| 66 | + * This function is guaranteed by the IBC contract to be called only once for each `clientId`. |
| 67 | + * @param clientId the client identifier which is unique within the IBC handler |
| 68 | + */ |
| 69 | + function initializeClient( |
| 70 | + string calldata clientId, |
| 71 | + bytes calldata protoClientState, |
| 72 | + bytes calldata protoConsensusState |
| 73 | + ) public override onlyIBC returns (Height.Data memory height) { |
| 74 | + ClientStorage storage clientStorage = clientStorages[clientId]; |
| 75 | + (ProtoClientState.Data memory clientState,) = |
| 76 | + _initializeClient(clientStorage, protoClientState, protoConsensusState); |
| 77 | + if (clientState.zkdcap_verifier_infos.length != 1) { |
| 78 | + revert LCPClientZKDCAPInvalidVerifierInfos(); |
| 79 | + } |
| 80 | + bytes memory verifierInfo = clientState.zkdcap_verifier_infos[0]; |
| 81 | + if (verifierInfo.length != 64) { |
| 82 | + revert LCPClientZKDCAPInvalidVerifierInfoLength(); |
| 83 | + } |
| 84 | + if (uint8(verifierInfo[0]) != ZKVM_TYPE_RISC_ZERO) { |
| 85 | + revert LCPClientZKDCAPInvalidVerifierInfoZKVMType(); |
| 86 | + } |
| 87 | + // 32..64 bytes: image ID |
| 88 | + bytes32 imageId; |
| 89 | + assembly { |
| 90 | + imageId := mload(add(add(verifierInfo, 32), 32)) |
| 91 | + } |
| 92 | + clientStorage.zkDCAPRisc0ImageId = imageId; |
| 93 | + return clientState.latest_height; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @dev routeUpdateClient returns the calldata to the receiving function of the client message. |
| 98 | + * Light client contract may encode a client message as other encoding scheme(e.g. ethereum ABI) |
| 99 | + * Check ADR-001 for details. |
| 100 | + */ |
| 101 | + function routeUpdateClient(string calldata clientId, bytes calldata protoClientMessage) |
| 102 | + public |
| 103 | + pure |
| 104 | + override |
| 105 | + returns (bytes4, bytes memory) |
| 106 | + { |
| 107 | + (bytes32 typeUrlHash, bytes memory args) = LCPProtoMarshaler.routeClientMessage(clientId, protoClientMessage); |
| 108 | + if (typeUrlHash == LCPProtoMarshaler.UPDATE_CLIENT_MESSAGE_TYPE_URL_HASH) { |
| 109 | + return (this.updateClient.selector, args); |
| 110 | + } else if (typeUrlHash == LCPProtoMarshaler.ZKDCAP_REGISTER_ENCLAVE_KEY_MESSAGE_TYPE_URL_HASH) { |
| 111 | + return (this.zkDCAPRegisterEnclaveKey.selector, args); |
| 112 | + } else if (typeUrlHash == LCPProtoMarshaler.UPDATE_OPERATORS_MESSAGE_TYPE_URL_HASH) { |
| 113 | + return (this.updateOperators.selector, args); |
| 114 | + } else { |
| 115 | + revert LCPClientUnknownProtoTypeUrl(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + function zkDCAPRegisterEnclaveKey(string calldata clientId, ZKDCAPRegisterEnclaveKeyMessage.Data calldata message) |
| 120 | + public |
| 121 | + returns (Height.Data[] memory heights) |
| 122 | + { |
| 123 | + if (message.zkvm_type != ZKVM_TYPE_RISC_ZERO) { |
| 124 | + revert LCPClientZKDCAPUnsupportedZKVMType(); |
| 125 | + } |
| 126 | + ClientStorage storage clientStorage = clientStorages[clientId]; |
| 127 | + if (clientStorage.zkDCAPRisc0ImageId == bytes32(0)) { |
| 128 | + revert LCPClientZKDCAPRisc0ImageIdNotSet(); |
| 129 | + } |
| 130 | + riscZeroVerifier.verify(message.proof, clientStorage.zkDCAPRisc0ImageId, sha256(message.commit)); |
| 131 | + DCAPValidator.Output memory output = DCAPValidator.parseCommit(message.commit); |
| 132 | + if (output.sgxIntelRootCAHash != intelRootCAHash) { |
| 133 | + revert LCPClientZKDCAPUnexpectedIntelRootCAHash(); |
| 134 | + } |
| 135 | + if (output.mrenclave != bytes32(clientStorage.clientState.mrenclave)) { |
| 136 | + revert LCPClientClientStateUnexpectedMrenclave(); |
| 137 | + } |
| 138 | + |
| 139 | + if ( |
| 140 | + clientStorage.allowedStatuses.allowedQuoteStatuses[DCAPValidator.tcbStatusToString(output.tcbStatus)] |
| 141 | + != RemoteAttestation.FLAG_ALLOWED |
| 142 | + ) { |
| 143 | + revert LCPClientZKDCAPDisallowedTCBStatus(); |
| 144 | + } |
| 145 | + for (uint256 i = 0; i < output.advisoryIDs.length; i++) { |
| 146 | + if ( |
| 147 | + clientStorage.allowedStatuses.allowedAdvisories[output.advisoryIDs[i]] != RemoteAttestation.FLAG_ALLOWED |
| 148 | + ) { |
| 149 | + revert LCPClientZKDCAPDisallowedAdvisoryID(); |
| 150 | + } |
| 151 | + } |
| 152 | + if (output.enclaveDebugEnabled != developmentMode) { |
| 153 | + revert LCPClientZKDCAPUnexpectedEnclaveDebugMode(); |
| 154 | + } |
| 155 | + |
| 156 | + // if `operator_signature` is empty, the operator address is zero |
| 157 | + address operator; |
| 158 | + if (message.operator_signature.length != 0) { |
| 159 | + operator = verifyECDSASignature( |
| 160 | + keccak256( |
| 161 | + LCPOperator.computeEIP712ZKDCAPRegisterEnclaveKey( |
| 162 | + clientStorage.clientState.zkdcap_verifier_infos[0], keccak256(message.commit) |
| 163 | + ) |
| 164 | + ), |
| 165 | + message.operator_signature |
| 166 | + ); |
| 167 | + } |
| 168 | + if (output.operator != address(0) && output.operator != operator) { |
| 169 | + revert LCPClientAVRUnexpectedOperator(operator, output.operator); |
| 170 | + } |
| 171 | + if (block.timestamp < output.validityNotBeforeMax || block.timestamp > output.validityNotAfterMin) { |
| 172 | + revert LCPClientZKDCAPOutputNotValid(); |
| 173 | + } |
| 174 | + uint64 expiredAt = output.validityNotAfterMin; |
| 175 | + EKInfo storage ekInfo = clientStorage.ekInfos[output.enclaveKey]; |
| 176 | + if (ekInfo.expiredAt != 0) { |
| 177 | + if (ekInfo.operator != operator) { |
| 178 | + revert LCPClientEnclaveKeyUnexpectedOperator(ekInfo.operator, operator); |
| 179 | + } |
| 180 | + if (ekInfo.expiredAt != expiredAt) { |
| 181 | + revert LCPClientEnclaveKeyUnexpectedExpiredAt(); |
| 182 | + } |
| 183 | + // NOTE: if the key already exists, don't update any state |
| 184 | + return heights; |
| 185 | + } |
| 186 | + ekInfo.expiredAt = expiredAt; |
| 187 | + ekInfo.operator = operator; |
| 188 | + |
| 189 | + emit ZKDCAPRegisteredEnclaveKey(clientId, output.enclaveKey, expiredAt, operator); |
| 190 | + |
| 191 | + // Note: client and consensus state are not always updated in registerEnclaveKey |
| 192 | + return heights; |
| 193 | + } |
| 194 | +} |
0 commit comments