diff --git a/.gitignore b/.gitignore index 0a5ad4a1b3..214f33b324 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ **/target **.DS_Store **storage_db +dpc/src/parameters/inner_snark.params +dpc/src/parameters/outer_snark.params diff --git a/algorithms/Cargo.toml b/algorithms/Cargo.toml index 70fae4cbad..57bdfd3ff5 100644 --- a/algorithms/Cargo.toml +++ b/algorithms/Cargo.toml @@ -25,7 +25,7 @@ snarkos-utilities = { path = "../utilities", version = "0.8.0" } blake2 = { version = "0.7" } derivative = { version = "1" } digest = { version = "0.7" } -rand = { version = "0.7", default-features = false } +rand = { version = "0.7" } rayon = { version = "1" } sha2 = { version = "0.8" } smallvec = { version = "1.2" } diff --git a/algorithms/src/signature/schnorr.rs b/algorithms/src/signature/schnorr.rs index 09dc2fb260..665c931a1d 100644 --- a/algorithms/src/signature/schnorr.rs +++ b/algorithms/src/signature/schnorr.rs @@ -128,7 +128,7 @@ where type PublicKey = SchnorrPublicKey; fn setup(rng: &mut R) -> Result { - let setup_time = start_timer!(|| "SchnorrSig::Setup"); + let setup_time = start_timer!(|| "SchnorrSignature::setup"); let mut salt = [0u8; 32]; rng.fill_bytes(&mut salt); @@ -149,14 +149,20 @@ where &self.parameters } - fn keygen(&self, rng: &mut R) -> Result<(Self::PublicKey, Self::PrivateKey), SignatureError> { - let keygen_time = start_timer!(|| "SchnorrSig::KeyGen"); - + fn generate_private_key(&self, rng: &mut R) -> Result { + let keygen_time = start_timer!(|| "SchnorrSignature::generate_private_key"); let private_key = ::ScalarField::rand(rng); - let public_key = self.parameters.generator.mul(&private_key); + end_timer!(keygen_time); + Ok(private_key) + } + + fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Result { + let keygen_time = start_timer!(|| "SchnorrSignature::generate_public_key"); + let public_key = self.parameters.generator.mul(private_key); end_timer!(keygen_time); - Ok((SchnorrPublicKey(public_key), private_key)) + + Ok(SchnorrPublicKey(public_key)) } fn sign( @@ -165,7 +171,7 @@ where message: &[u8], rng: &mut R, ) -> Result { - let sign_time = start_timer!(|| "SchnorrSig::Sign"); + let sign_time = start_timer!(|| "SchnorrSignature::sign"); // (k, e); let (random_scalar, verifier_challenge) = loop { // Sample a random scalar `k` from the prime scalar field. @@ -203,7 +209,7 @@ where message: &[u8], signature: &Self::Output, ) -> Result { - let verify_time = start_timer!(|| "SchnorrSig::Verify"); + let verify_time = start_timer!(|| "SchnorrSignature::Verify"); let SchnorrOutput { prover_response, @@ -234,7 +240,7 @@ where public_key: &Self::PublicKey, randomness: &[u8], ) -> Result { - let rand_pk_time = start_timer!(|| "SchnorrSig::RandomizePubKey"); + let rand_pk_time = start_timer!(|| "SchnorrSignature::randomize_public_key"); let mut randomized_pk = public_key.0.clone(); let mut base = self.parameters.generator; @@ -253,7 +259,7 @@ where } fn randomize_signature(&self, signature: &Self::Output, randomness: &[u8]) -> Result { - let rand_signature_time = start_timer!(|| "SchnorrSig::RandomizeSig"); + let rand_signature_time = start_timer!(|| "SchnorrSignature::randomize_signature"); let SchnorrOutput { prover_response, verifier_challenge, diff --git a/algorithms/src/signature/tests.rs b/algorithms/src/signature/tests.rs index f919445c7a..26ca373455 100644 --- a/algorithms/src/signature/tests.rs +++ b/algorithms/src/signature/tests.rs @@ -17,30 +17,34 @@ const TEST_SIGNATURE_PARAMETERS_PATH: &str = "./schnorr_signature.params"; fn sign_and_verify(message: &[u8]) { let rng = &mut thread_rng(); let schnorr_signature = S::setup::<_>(rng).unwrap(); - let (pk, sk) = schnorr_signature.keygen(rng).unwrap(); - let sig = schnorr_signature.sign(&sk, message, rng).unwrap(); - assert!(schnorr_signature.verify(&pk, &message, &sig).unwrap()); + let private_key = schnorr_signature.generate_private_key(rng).unwrap(); + let public_key = schnorr_signature.generate_public_key(&private_key).unwrap(); + let signature = schnorr_signature.sign(&private_key, message, rng).unwrap(); + assert!(schnorr_signature.verify(&public_key, &message, &signature).unwrap()); } fn failed_verification(message: &[u8], bad_message: &[u8]) { let rng = &mut thread_rng(); let schnorr_signature = S::setup::<_>(rng).unwrap(); - let (pk, sk) = schnorr_signature.keygen(rng).unwrap(); - let sig = schnorr_signature.sign(&sk, message, rng).unwrap(); - assert!(!schnorr_signature.verify(&pk, bad_message, &sig).unwrap()); + let private_key = schnorr_signature.generate_private_key(rng).unwrap(); + let public_key = schnorr_signature.generate_public_key(&private_key).unwrap(); + let signature = schnorr_signature.sign(&private_key, message, rng).unwrap(); + assert!(!schnorr_signature.verify(&public_key, bad_message, &signature).unwrap()); } fn randomize_and_verify(message: &[u8], randomness: &[u8]) { let rng = &mut thread_rng(); let schnorr_signature = S::setup::<_>(rng).unwrap(); - let (pk, sk) = schnorr_signature.keygen(rng).unwrap(); - let sig = schnorr_signature.sign(&sk, message, rng).unwrap(); - assert!(schnorr_signature.verify(&pk, message, &sig).unwrap()); - let randomized_pk = schnorr_signature.randomize_public_key(&pk, randomness).unwrap(); - let randomized_sig = schnorr_signature.randomize_signature(&sig, randomness).unwrap(); + let private_key = schnorr_signature.generate_private_key(rng).unwrap(); + let public_key = schnorr_signature.generate_public_key(&private_key).unwrap(); + let signature = schnorr_signature.sign(&private_key, message, rng).unwrap(); + assert!(schnorr_signature.verify(&public_key, message, &signature).unwrap()); + + let randomized_public_key = schnorr_signature.randomize_public_key(&public_key, randomness).unwrap(); + let randomized_signature = schnorr_signature.randomize_signature(&signature, randomness).unwrap(); assert!( schnorr_signature - .verify(&randomized_pk, &message, &randomized_sig) + .verify(&randomized_public_key, &message, &randomized_signature) .unwrap() ); } diff --git a/gadgets/src/algorithms/signature/tests.rs b/gadgets/src/algorithms/signature/tests.rs index 3ef903e9c0..cfbbefda68 100644 --- a/gadgets/src/algorithms/signature/tests.rs +++ b/gadgets/src/algorithms/signature/tests.rs @@ -35,7 +35,8 @@ fn test_schnorr_signature_gadget() { // Native Schnorr signing let schnorr_signature = Schnorr::setup::<_>(rng).unwrap(); - let (public_key, private_key) = schnorr_signature.keygen(rng).unwrap(); + let private_key = schnorr_signature.generate_private_key(rng).unwrap(); + let public_key = schnorr_signature.generate_public_key(&private_key).unwrap(); let signature = schnorr_signature.sign(&private_key, &message, rng).unwrap(); assert!(schnorr_signature.verify(&public_key, &message, &signature).unwrap()); diff --git a/models/src/algorithms/signature.rs b/models/src/algorithms/signature.rs index d6690cbd62..d9919281c4 100644 --- a/models/src/algorithms/signature.rs +++ b/models/src/algorithms/signature.rs @@ -15,7 +15,9 @@ pub trait SignatureScheme: Sized + Clone + Storage { fn parameters(&self) -> &Self::Parameters; - fn keygen(&self, rng: &mut R) -> Result<(Self::PublicKey, Self::PrivateKey), SignatureError>; + fn generate_private_key(&self, rng: &mut R) -> Result; + + fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Result; fn sign( &self,