From f57af0519db3fec6b5aa9855ca27912dd16ea390 Mon Sep 17 00:00:00 2001 From: Ajinkya Kulkarni Date: Thu, 18 Apr 2024 23:40:25 +0200 Subject: [PATCH] do RNG before proving --- program/src/main.rs | 10 +++++----- script/Cargo.lock | 1 + script/Cargo.toml | 1 + script/src/main.rs | 28 +++++++++++----------------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/program/src/main.rs b/program/src/main.rs index 09712b0..70ef6a8 100644 --- a/program/src/main.rs +++ b/program/src/main.rs @@ -3,7 +3,6 @@ use bincode::serialize; use ed25519_consensus::*; use mtcs_core::*; -use rand::thread_rng; use rs_merkle::{algorithms::Sha256, Hasher, MerkleProof}; sp1_zkvm::entrypoint!(main); @@ -11,7 +10,7 @@ pub fn main() { println!("reading inputs into guest..."); let cycle: Cycle = sp1_zkvm::io::read::(); - let _secret: PrivateKey = sp1_zkvm::io::read::(); + let key: SigningKey = sp1_zkvm::io::read::(); let proof: Proof = sp1_zkvm::io::read::(); let merkle_data: MerkleData = sp1_zkvm::io::read::(); assert_eq!( @@ -53,15 +52,16 @@ pub fn main() { let message = { let hashed_cycle = Sha256::hash(&serialize(&cycle).unwrap()); // Generate a signing key and sign the message - let sk = SigningKey::new(thread_rng()); - let sig = sk.sign(&bincode::serialize(&hashed_cycle).unwrap()[..]); - let vk_bytes: [u8; 32] = VerificationKey::from(&sk).into(); + let sig = key.sign(&bincode::serialize(&hashed_cycle).unwrap()[..]); + + let vk_bytes: [u8; 32] = VerificationKey::from(&key).into(); (hashed_cycle, vk_bytes, sig) }; sp1_zkvm::io::commit(&message); + println!("cryptography magic happening...") } // TODO: for every edge involved in clearing, the total offsets of all cycles // passing through that edge should be less than the value of that edge diff --git a/script/Cargo.lock b/script/Cargo.lock index 8b9b6e4..8ab0940 100644 --- a/script/Cargo.lock +++ b/script/Cargo.lock @@ -2243,6 +2243,7 @@ dependencies = [ "ed25519-consensus", "hex", "mtcs-core", + "rand", "rs_merkle", "serde_json", "sp1-helper", diff --git a/script/Cargo.toml b/script/Cargo.toml index bd2e988..2f08a42 100644 --- a/script/Cargo.toml +++ b/script/Cargo.toml @@ -12,6 +12,7 @@ bincode = "1.3.3" hex = "0.4.3" serde_json = "1.0" ed25519-consensus = "2.1.0" +rand = "0.8.5" [build-dependencies] sp1-helper = { git = "https://github.com/succinctlabs/sp1.git" } diff --git a/script/src/main.rs b/script/src/main.rs index 3481d4a..f21952f 100644 --- a/script/src/main.rs +++ b/script/src/main.rs @@ -1,8 +1,7 @@ -// These constants represent the RISC-V ELF and the image ID generated by risc0-build. -// The ELF is used for proving and the ID is used for verification. use bincode::serialize; use ed25519_consensus::*; use mtcs_core::*; +use rand::thread_rng; use rs_merkle::{algorithms::Sha256 as MerkleSha256, Hasher, MerkleTree}; use std::fs; use std::time::SystemTime; @@ -17,9 +16,6 @@ fn main() { println!("generating guest inputs..."); // For example: - let key: Vec = "passw0rdpassw0rdpassw0rdpassw0rd".as_bytes().to_vec(); - - let secret = PrivateKey { key }; let cycle_data: String = fs::read_to_string("../cycle.json").unwrap(); let all_obligation_data: String = fs::read_to_string("../obligations.json").unwrap(); @@ -50,7 +46,6 @@ fn main() { let bytes = merkle_proof.to_bytes(); - let key: PrivateKey = secret; let proof: Proof = Proof { bytes: bytes.as_slice().try_into().unwrap(), }; @@ -60,7 +55,7 @@ fn main() { indexes: indexes.try_into().unwrap(), len, }; - + let key = SigningKey::new(thread_rng()); let mut stdin = SP1Stdin::new(); let client = ProverClient::new(); @@ -70,8 +65,6 @@ fn main() { stdin.write(&proof); stdin.write(&aux); - println!("generating proof..."); - let mut proof = client.prove(ELF, stdin).expect("proving failed"); println!("proof generation completed!"); @@ -80,18 +73,19 @@ fn main() { client.verify(ELF, &proof).expect("verification failed"); println!("verification completed!"); - println!("validating digital signature..."); - - let (hash, verification_key, sign): &([u8; 32], VerificationKey, Signature) = - &proof.public_values.read(); - // let read_values: ([u8; 32], Signature) = pub_values.read(); - let sig_verify = verification_key.verify(sign, hash).is_ok(); - assert!(sig_verify); let end_time = SystemTime::now(); let difference = end_time .duration_since(start_time) .expect("Clock may have gone backwards"); - println!("Total time: {difference:?}"); + println!("Total prover time: {difference:?}"); + + println!("validating digital signature..."); + + let (hash, verification_key, sign): &([u8; 32], VerificationKey, Signature) = + &proof.public_values.read(); + + let sig_verify = verification_key.verify(sign, hash).is_ok(); + assert!(sig_verify); proof .save("proof-with-io.json")