Skip to content

Commit

Permalink
Splits schnorr keygen into two methods
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed May 8, 2020
1 parent 51b71c1 commit 5e2de42
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
**/target
**.DS_Store
**storage_db
dpc/src/parameters/inner_snark.params
dpc/src/parameters/outer_snark.params
2 changes: 1 addition & 1 deletion algorithms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
26 changes: 16 additions & 10 deletions algorithms/src/signature/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
type PublicKey = SchnorrPublicKey<G>;

fn setup<R: Rng>(rng: &mut R) -> Result<Self, SignatureError> {
let setup_time = start_timer!(|| "SchnorrSig::Setup");
let setup_time = start_timer!(|| "SchnorrSignature::setup");

let mut salt = [0u8; 32];
rng.fill_bytes(&mut salt);
Expand All @@ -149,14 +149,20 @@ where
&self.parameters
}

fn keygen<R: Rng>(&self, rng: &mut R) -> Result<(Self::PublicKey, Self::PrivateKey), SignatureError> {
let keygen_time = start_timer!(|| "SchnorrSig::KeyGen");

fn generate_private_key<R: Rng>(&self, rng: &mut R) -> Result<Self::PrivateKey, SignatureError> {
let keygen_time = start_timer!(|| "SchnorrSignature::generate_private_key");
let private_key = <G as Group>::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<Self::PublicKey, SignatureError> {
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<R: Rng>(
Expand All @@ -165,7 +171,7 @@ where
message: &[u8],
rng: &mut R,
) -> Result<Self::Output, SignatureError> {
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.
Expand Down Expand Up @@ -203,7 +209,7 @@ where
message: &[u8],
signature: &Self::Output,
) -> Result<bool, SignatureError> {
let verify_time = start_timer!(|| "SchnorrSig::Verify");
let verify_time = start_timer!(|| "SchnorrSignature::Verify");

let SchnorrOutput {
prover_response,
Expand Down Expand Up @@ -234,7 +240,7 @@ where
public_key: &Self::PublicKey,
randomness: &[u8],
) -> Result<Self::PublicKey, SignatureError> {
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;
Expand All @@ -253,7 +259,7 @@ where
}

fn randomize_signature(&self, signature: &Self::Output, randomness: &[u8]) -> Result<Self::Output, SignatureError> {
let rand_signature_time = start_timer!(|| "SchnorrSig::RandomizeSig");
let rand_signature_time = start_timer!(|| "SchnorrSignature::randomize_signature");
let SchnorrOutput {
prover_response,
verifier_challenge,
Expand Down
28 changes: 16 additions & 12 deletions algorithms/src/signature/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,34 @@ const TEST_SIGNATURE_PARAMETERS_PATH: &str = "./schnorr_signature.params";
fn sign_and_verify<S: SignatureScheme>(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<S: SignatureScheme>(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<S: SignatureScheme>(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()
);
}
Expand Down
3 changes: 2 additions & 1 deletion gadgets/src/algorithms/signature/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
4 changes: 3 additions & 1 deletion models/src/algorithms/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub trait SignatureScheme: Sized + Clone + Storage {

fn parameters(&self) -> &Self::Parameters;

fn keygen<R: Rng>(&self, rng: &mut R) -> Result<(Self::PublicKey, Self::PrivateKey), SignatureError>;
fn generate_private_key<R: Rng>(&self, rng: &mut R) -> Result<Self::PrivateKey, SignatureError>;

fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Result<Self::PublicKey, SignatureError>;

fn sign<R: Rng>(
&self,
Expand Down

0 comments on commit 5e2de42

Please sign in to comment.