Skip to content

Commit

Permalink
feat(tee-proof-verifier): add support for Solidity-compatible pubkey …
Browse files Browse the repository at this point in the history
…in report_data

This PR is part of the effort to implement on-chain TEE proof
verification. This PR goes hand in hand with:
- matter-labs/zksync-era#3414
- #228
  • Loading branch information
pbeza committed Dec 31, 2024
1 parent a9b89ef commit 97a37fd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bin/tee-key-preexec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use anyhow::{Context, Result};
use clap::Parser;
use secp256k1::{rand, PublicKey, Secp256k1, SecretKey};
use secp256k1::{rand, PublicKey, Secp256k1};
use sha3::{Digest, Keccak256};
use std::{ffi::OsString, os::unix::process::CommandExt, process::Command};
use teepot::quote::get_quote;
Expand Down Expand Up @@ -99,6 +99,8 @@ fn main() -> Result<()> {

#[cfg(test)]
mod tests {
use secp256k1::SecretKey;

use super::*;

#[test]
Expand Down
3 changes: 2 additions & 1 deletion bin/verify-era-proof-attestation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ ctrlc.workspace = true
hex.workspace = true
jsonrpsee-types.workspace = true
reqwest.workspace = true
secp256k1.workspace = true
secp256k1 = { workspace = true, features = ["recovery"] }
serde.workspace = true
serde_with = { workspace = true, features = ["hex"] }
sha3.workspace = true
teepot.workspace = true
tokio.workspace = true
tracing.workspace = true
Expand Down
63 changes: 46 additions & 17 deletions bin/verify-era-proof-attestation/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
use crate::{args::AttestationPolicyArgs, client::JsonRpcClient};
use anyhow::{Context, Result};
use hex::encode;
use secp256k1::{constants::PUBLIC_KEY_SIZE, ecdsa::Signature, Message, PublicKey};
use secp256k1::{
ecdsa::{RecoverableSignature, RecoveryId},
Message, PublicKey, SECP256K1,
};
use sha3::{Digest, Keccak256};
use teepot::{
client::TcbLevel,
quote::{
Expand All @@ -27,22 +31,27 @@ pub async fn verify_batch_proof(
}

let batch_no = batch_number.0;

let public_key = PublicKey::from_slice(
&quote_verification_result.quote.get_report_data()[..PUBLIC_KEY_SIZE],
)?;
debug!(batch_no, "public key: {}", public_key);

let root_hash = node_client.get_root_hash(batch_number).await?;
debug!(batch_no, "root hash: {}", root_hash);
let ethereum_address_from_quote = &quote_verification_result.quote.get_report_data()[..20];
let signature_array: &[u8; 65] = signature
.try_into()
.map_err(|_| anyhow::anyhow!("Signature must be exactly 65 bytes long"))?;
let ethereum_address_from_signature = recover_signer(signature_array, root_hash)?;
let verification_successful = &ethereum_address_from_signature == ethereum_address_from_quote;
debug!(
batch_no,
"Root hash: {}. Ethereum address from the attestation quote: {}. Ethereum address from the signature: {}.",
root_hash,
encode(ethereum_address_from_quote),
encode(ethereum_address_from_signature),
);

let is_verified = verify_signature(signature, public_key, root_hash)?;
if is_verified {
if verification_successful {
info!(batch_no, signature = %encode(signature), "Signature verified successfully.");
} else {
warn!(batch_no, signature = %encode(signature), "Failed to verify signature!");
}
Ok(is_verified)
Ok(verification_successful)
}

pub fn verify_attestation_quote(attestation_quote_bytes: &[u8]) -> Result<QuoteVerificationResult> {
Expand Down Expand Up @@ -85,12 +94,6 @@ pub fn log_quote_verification_summary(quote_verification_result: &QuoteVerificat
);
}

fn verify_signature(signature: &[u8], public_key: PublicKey, root_hash: H256) -> Result<bool> {
let signature = Signature::from_compact(signature)?;
let root_hash_msg = Message::from_digest_slice(&root_hash.0)?;
Ok(signature.verify(&root_hash_msg, &public_key).is_ok())
}

fn is_quote_matching_policy(
attestation_policy: &AttestationPolicyArgs,
quote_verification_result: &QuoteVerificationResult,
Expand Down Expand Up @@ -136,3 +139,29 @@ fn check_policy(policy: Option<&str>, actual_value: &[u8], field_name: &str) ->
}
true
}

/// Equivalent to the ecrecover precompile, ensuring that the signatures we produce off-chain
/// can be recovered on-chain.
pub fn recover_signer(sig: &[u8; 65], root_hash: H256) -> Result<[u8; 20]> {
let root_hash_bytes = root_hash.as_bytes();
let msg = Message::from_digest_slice(root_hash_bytes)?;
let sig = RecoverableSignature::from_compact(
&sig[0..64],
RecoveryId::from_i32(sig[64] as i32 - 27)?,
)?;
let public = SECP256K1.recover_ecdsa(&msg, &sig)?;
Ok(public_key_to_ethereum_address(&public))
}

/// Converts a public key into an Ethereum address by hashing the encoded public key with Keccak256.
fn public_key_to_ethereum_address(public: &PublicKey) -> [u8; 20] {
let public_key_bytes = public.serialize_uncompressed();

// Skip the first byte (0x04) which indicates uncompressed key
let hash: [u8; 32] = Keccak256::digest(&public_key_bytes[1..]).into();

// Take the last 20 bytes of the hash to get the Ethereum address
let mut address = [0u8; 20];
address.copy_from_slice(&hash[12..]);
address
}

0 comments on commit 97a37fd

Please sign in to comment.