Skip to content

Commit e95728f

Browse files
committed
feat(tee-key-preexec): add support for Solidity-compatible pubkey in report_data
This PR goes hand in hand with matter-labs/zksync-era#3414
1 parent d2fbdb5 commit e95728f

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ serde = { version = "1", features = ["derive", "rc"] }
4949
serde_json = "1"
5050
serde_with = { version = "3.8", features = ["base64", "hex"] }
5151
sha2 = "0.10.8"
52+
sha3 = "0.10.8"
5253
signature = "2.2.0"
5354
tdx-attest-rs = { version = "0.1.2", git = "https://github.com/intel/SGXDataCenterAttestationPrimitives.git", rev = "aa239d25a437a28f3f4de92c38f5b6809faac842" }
5455
teepot = { path = "crates/teepot" }

bin/tee-key-preexec/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ repository.workspace = true
1212
[dependencies]
1313
anyhow.workspace = true
1414
clap.workspace = true
15+
hex.workspace = true
1516
rand.workspace = true
1617
secp256k1.workspace = true
18+
sha3.workspace = true
1719
teepot.workspace = true
1820
tracing.workspace = true
1921
tracing-log.workspace = true

bin/tee-key-preexec/src/main.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
use anyhow::{Context, Result};
1010
use clap::Parser;
11-
use secp256k1::{rand, Keypair, PublicKey, Secp256k1, SecretKey};
11+
use secp256k1::{rand, PublicKey, Secp256k1, SecretKey};
12+
use sha3::{Digest, Keccak256};
1213
use std::{ffi::OsString, os::unix::process::CommandExt, process::Command};
1314
use teepot::quote::get_quote;
1415
use tracing::error;
@@ -28,6 +29,19 @@ struct Args {
2829
cmd_args: Vec<OsString>,
2930
}
3031

32+
/// Converts a public key into an Ethereum address by hashing the encoded public key with Keccak256.
33+
pub fn public_key_to_address(public: &PublicKey) -> [u8; 20] {
34+
let public_key_bytes = public.serialize_uncompressed();
35+
36+
// Skip the first byte (0x04) which indicates uncompressed key
37+
let hash: [u8; 32] = Keccak256::digest(&public_key_bytes[1..]).into();
38+
39+
// Take the last 20 bytes of the hash to get the Ethereum address
40+
let mut address = [0u8; 20];
41+
address.copy_from_slice(&hash[12..]);
42+
address
43+
}
44+
3145
fn main_with_error() -> Result<()> {
3246
LogTracer::init().context("Failed to set logger")?;
3347

@@ -37,14 +51,11 @@ fn main_with_error() -> Result<()> {
3751
tracing::subscriber::set_global_default(subscriber).context("Failed to set logger")?;
3852

3953
let args = Args::parse();
40-
4154
let mut rng = rand::thread_rng();
4255
let secp = Secp256k1::new();
43-
let keypair = Keypair::new(&secp, &mut rng);
44-
let signing_key = SecretKey::from_keypair(&keypair);
45-
let verifying_key = PublicKey::from_keypair(&keypair);
46-
let verifying_key_bytes = verifying_key.serialize();
47-
let tee_type = match get_quote(verifying_key_bytes.as_ref()) {
56+
let (signing_key, verifying_key) = secp.generate_keypair(&mut rng);
57+
let ethereum_address = public_key_to_address(&verifying_key);
58+
let tee_type = match get_quote(ethereum_address.as_ref()) {
4859
Ok((tee_type, quote)) => {
4960
// save quote to file
5061
std::fs::write(TEE_QUOTE_FILE, quote)?;
@@ -85,3 +96,22 @@ fn main() -> Result<()> {
8596
}
8697
ret
8798
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
#[test]
105+
fn test_public_key_to_address() {
106+
let secp = Secp256k1::new();
107+
let secret_key_bytes =
108+
hex::decode("c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3")
109+
.unwrap();
110+
let secret_key = SecretKey::from_slice(&secret_key_bytes).unwrap();
111+
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
112+
let expected_address = hex::decode("627306090abaB3A6e1400e9345bC60c78a8BEf57").unwrap();
113+
let address = public_key_to_address(&public_key);
114+
115+
assert_eq!(address, expected_address.as_slice());
116+
}
117+
}

0 commit comments

Comments
 (0)