diff --git a/Cargo.toml b/Cargo.toml index efe1421..7878f7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ jolt = { package = "jolt-sdk", git = "https://github.com/a16z/jolt", features = guest = { path = "./guest" } k256 = "0.13.3" rand_core = "0.6.4" +# rsa = { version = "0.9.6", default-features = false, features = ["sha2"] } +# rand = "0.8.5" [patch.crates-io] ark-ff = { git = "https://github.com/a16z/arkworks-algebra", branch = "optimize/field-from-u64" } diff --git a/guest/Cargo.toml b/guest/Cargo.toml index 84f2661..4ceeef6 100644 --- a/guest/Cargo.toml +++ b/guest/Cargo.toml @@ -11,10 +11,6 @@ path = "./src/lib.rs" guest = [] [dependencies] -k256 = { version = "0.13.3", default-features = false, features = [ - "arithmetic", - "pkcs8", - "precomputed-tables", - "schnorr", -] } +k256 = { version = "0.13.3", default-features = false, features = ["schnorr"] } jolt = { package = "jolt-sdk", git = "https://github.com/a16z/jolt" } +# rsa = { version = "0.9.6", default-features = false, features = ["sha2"] } diff --git a/guest/src/lib.rs b/guest/src/lib.rs index 17fd460..c8851d8 100644 --- a/guest/src/lib.rs +++ b/guest/src/lib.rs @@ -1,8 +1,8 @@ #![cfg_attr(feature = "guest", no_std)] #![no_main] -use k256::schnorr::Signature; - +/// Fibonacci(50) +/// Trace length: 1301 #[jolt::provable] fn fib(n: u32) -> u128 { let mut a: u128 = 0; @@ -17,16 +17,28 @@ fn fib(n: u32) -> u128 { b } -use k256::schnorr::{signature::Verifier, VerifyingKey}; - -#[jolt::provable] +/// Verify Schnorr Signature +/// Trace length: 169422912 +#[jolt::provable(stack_size = 100000, memory_size = 10000000)] fn schnorr(message: &[u8], verifying_key_bytes: &[u8], signature_bytes: &[u8]) { - // - // Verification - // + use k256::schnorr::{Signature, signature::Verifier, VerifyingKey}; let verifying_key = VerifyingKey::from_bytes(&verifying_key_bytes).unwrap(); let signature = Signature::try_from(signature_bytes).unwrap(); verifying_key .verify(message, &signature) - .expect("invalid signature"); + .unwrap(); } + +// /// Verify RSA Signature +// /// Trace length: +// #[jolt::provable(stack_size = 100000, memory_size = 10000000)] +// fn rsa(message: &[u8], verifying_key_bytes: &[u8], signature_bytes: &[u8]) { +// use rsa::pkcs1v15::VerifyingKey; +// use rsa::signature::Verifier; +// use rsa::{pkcs1::DecodeRsaPublicKey, pkcs1v15::Signature, sha2::Sha256}; +// let verifying_key: VerifyingKey = VerifyingKey::from_pkcs1_der(&verifying_key_bytes).unwrap(); +// let signature = Signature::try_from(signature_bytes).unwrap(); +// verifying_key +// .verify(message, &signature) +// .unwrap(); +// } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 050245b..7f26d3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,87 @@ -use k256::schnorr::{signature::Signer, SigningKey}; -use rand_core::OsRng; // requires 'getrandom' feature +fn print_time(name: &str, f: F) { + let start_time = std::time::Instant::now(); + f(); + let elapsed_time = start_time.elapsed(); + println!("{} Elapsed time: {:?}", name, elapsed_time); +} pub fn main() { - let message = b"Schnorr signatures prove knowledge of a secret in the random oracle model"; - // - // Signing - // - let signing_key = SigningKey::random(&mut OsRng); // serialize with `.to_bytes()` - let verifying_key_bytes = signing_key.verifying_key().to_bytes(); // 32-bytes + print_time("native fib(1000)", || { + guest::execute_fib(1000); + }); + let (prove_fib, _verify_fib) = guest::build_fib(); + print_time("zkVM fib(1000)", || { + let (_output, _proof) = prove_fib(1000); + }); + + { + // Schnorr Signature + use k256::schnorr::{signature::Signer, SigningKey}; + use rand_core::OsRng; // requires 'getrandom' feature + + let message = b"Schnorr signatures prove knowledge of a secret in the random oracle model"; + // Signing + let signing_key = SigningKey::random(&mut OsRng); // serialize with `.to_bytes()` + let verifying_key_bytes = signing_key.verifying_key().to_bytes(); // 32-bytes + let signature = signing_key.sign(message); // returns `k256::schnorr::Signature` + let signature_bytes = signature.to_bytes(); // 64-bytes + print_time("native Schnorr verify", || { + guest::execute_schnorr( + message, + verifying_key_bytes.as_slice(), + signature_bytes.as_slice(), + ); + }); + let (prove_schnorr, _verify_schnorr) = guest::build_schnorr(); + print_time("zkVM Schnorr verify", || { + let (_output, _proof) = prove_schnorr( + message, + verifying_key_bytes.as_slice(), + signature_bytes.as_slice(), + ); + }); + // let is_valid = verify_schnorr(proof); + // println!("output: {}", output); + // println!("valid: {}", is_valid); + } + /* + { + // RSA Signature + use rsa::pkcs1v15::{SigningKey, VerifyingKey}; + use rsa::pkcs1::EncodeRsaPublicKey; + use rsa::sha2::{Digest, Sha256}; + use rsa::signature::{Keypair, RandomizedSigner, SignatureEncoding, Verifier}; + use rsa::RsaPrivateKey; - let signature = signing_key.sign(message); // returns `k256::schnorr::Signature` - let signature_bytes = signature.to_bytes(); // 64-bytes + let mut rng = rand::thread_rng(); - let (prove_schnorr, verify_schnorr) = guest::build_schnorr(); + let bits = 2048; + let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key"); + let signing_key = SigningKey::::new(private_key); + let verifying_key = signing_key.verifying_key(); - let (_output, proof) = prove_schnorr( - message, - verifying_key_bytes.as_slice(), - signature_bytes.as_slice(), - ); - let is_valid = verify_schnorr(proof); + // Sign + let data = b"hello world"; + let signature = signing_key.sign_with_rng(&mut rng, data); + assert_ne!(signature.to_bytes().as_ref(), data.as_slice()); + let signature_bytes = signature.to_bytes(); + let verifying_key_bytes = verifying_key.to_pkcs1_der().unwrap(); - // println!("output: {}", output); - println!("valid: {}", is_valid); + print_time("native RSA verify", || { + guest::execute_rsa( + data, + verifying_key_bytes.as_bytes(), + &signature_bytes.as_ref(), + ); + }); + let (prove_rsa, _verify_rsa) = guest::build_rsa(); + print_time("zkVM RSA verify", || { + let (_output, _proof) = prove_rsa( + data, + verifying_key_bytes.as_bytes(), + &signature_bytes.as_ref(), + ); + }); + } + */ }