22// Copyright (c) 2023-2024 Matter Labs
33
44use crate :: { args:: AttestationPolicyArgs , client:: JsonRpcClient } ;
5- use anyhow:: { Context , Result } ;
5+ use anyhow:: { anyhow , Context , Result } ;
66use hex:: encode;
7- use secp256k1:: { constants :: PUBLIC_KEY_SIZE , ecdsa:: Signature , Message , PublicKey } ;
7+ use secp256k1:: { ecdsa:: Signature , Message } ;
88use teepot:: {
99 client:: TcbLevel ,
10+ ethereum:: recover_signer,
11+ prover:: reportdata:: ReportData ,
1012 quote:: {
1113 error:: QuoteContext , tee_qv_get_collateral, verify_quote_with_collateral,
1214 QuoteVerificationResult , Report ,
@@ -15,6 +17,51 @@ use teepot::{
1517use tracing:: { debug, info, warn} ;
1618use zksync_basic_types:: { L1BatchNumber , H256 } ;
1719
20+ struct TeeProof {
21+ report : ReportData ,
22+ root_hash : H256 ,
23+ signature : Vec < u8 > ,
24+ }
25+
26+ impl TeeProof {
27+ pub fn new ( report : ReportData , root_hash : H256 , signature : Vec < u8 > ) -> Self {
28+ Self {
29+ report,
30+ root_hash,
31+ signature,
32+ }
33+ }
34+
35+ pub fn verify ( & self ) -> Result < bool > {
36+ match & self . report {
37+ ReportData :: V0 ( report) => {
38+ let signature = Signature :: from_compact ( & self . signature ) ?;
39+ let root_hash_msg = Message :: from_digest_slice ( & self . root_hash . 0 ) ?;
40+ Ok ( signature. verify ( & root_hash_msg, & report. pubkey ) . is_ok ( ) )
41+ }
42+ ReportData :: V1 ( report) => {
43+ let ethereum_address_from_report = report. ethereum_address ;
44+ let root_hash_msg = Message :: from_digest_slice ( self . root_hash . as_bytes ( ) ) ?;
45+ let signature_bytes: [ u8 ; 65 ] = self
46+ . signature
47+ . clone ( )
48+ . try_into ( )
49+ . map_err ( |e| anyhow ! ( "{:?}" , e) ) ?;
50+ let ethereum_address_from_signature =
51+ recover_signer ( & signature_bytes, & root_hash_msg) ?;
52+ debug ! (
53+ "Root hash: {}. Ethereum address from the attestation quote: {}. Ethereum address from the signature: {}." ,
54+ self . root_hash,
55+ encode( ethereum_address_from_report) ,
56+ encode( ethereum_address_from_signature) ,
57+ ) ;
58+ Ok ( ethereum_address_from_signature == ethereum_address_from_report)
59+ }
60+ ReportData :: Unknown ( _) => Ok ( false ) ,
61+ }
62+ }
63+ }
64+
1865pub async fn verify_batch_proof (
1966 quote_verification_result : & QuoteVerificationResult ,
2067 attestation_policy : & AttestationPolicyArgs ,
@@ -26,23 +73,12 @@ pub async fn verify_batch_proof(
2673 return Ok ( false ) ;
2774 }
2875
29- let batch_no = batch_number. 0 ;
30-
31- let public_key = PublicKey :: from_slice (
32- & quote_verification_result. quote . get_report_data ( ) [ ..PUBLIC_KEY_SIZE ] ,
33- ) ?;
34- debug ! ( batch_no, "public key: {}" , public_key) ;
35-
3676 let root_hash = node_client. get_root_hash ( batch_number) . await ?;
37- debug ! ( batch_no, "root hash: {}" , root_hash) ;
38-
39- let is_verified = verify_signature ( signature, public_key, root_hash) ?;
40- if is_verified {
41- info ! ( batch_no, signature = %encode( signature) , "Signature verified successfully." ) ;
42- } else {
43- warn ! ( batch_no, signature = %encode( signature) , "Failed to verify signature!" ) ;
44- }
45- Ok ( is_verified)
77+ let report_data_bytes = quote_verification_result. quote . get_report_data ( ) ;
78+ let report_data = ReportData :: try_from ( report_data_bytes) ?;
79+ let tee_proof = TeeProof :: new ( report_data, root_hash, signature. to_vec ( ) ) ;
80+ let verification_successful = tee_proof. verify ( ) . is_ok ( ) ;
81+ Ok ( verification_successful)
4682}
4783
4884pub fn verify_attestation_quote ( attestation_quote_bytes : & [ u8 ] ) -> Result < QuoteVerificationResult > {
@@ -85,12 +121,6 @@ pub fn log_quote_verification_summary(quote_verification_result: &QuoteVerificat
85121 ) ;
86122}
87123
88- fn verify_signature ( signature : & [ u8 ] , public_key : PublicKey , root_hash : H256 ) -> Result < bool > {
89- let signature = Signature :: from_compact ( signature) ?;
90- let root_hash_msg = Message :: from_digest_slice ( & root_hash. 0 ) ?;
91- Ok ( signature. verify ( & root_hash_msg, & public_key) . is_ok ( ) )
92- }
93-
94124fn is_quote_matching_policy (
95125 attestation_policy : & AttestationPolicyArgs ,
96126 quote_verification_result : & QuoteVerificationResult ,
0 commit comments