added basic code for schnorr signatures

This commit is contained in:
narodnik
2021-05-03 18:11:37 +02:00
parent 08f44bab86
commit c9efd1a9ca
3 changed files with 37 additions and 0 deletions

View File

@@ -1,6 +1,8 @@
pub mod diffie_hellman;
pub mod mint_proof;
pub mod schnorr;
pub mod spend_proof;
pub mod util;
use bellman::groth16;
use bls12_381::Bls12;

25
src/crypto/schnorr.rs Normal file
View File

@@ -0,0 +1,25 @@
use ff::Field;
use group::{Group, GroupEncoding};
use rand::rngs::OsRng;
use super::util::hash_to_scalar;
#[test]
fn test_schnorr() {
let secret = jubjub::Fr::random(&mut OsRng);
let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
let mask = jubjub::Fr::random(&mut OsRng);
let commit = zcash_primitives::constants::SPENDING_KEY_GENERATOR * mask;
let msg = b"Foo bar";
let challenge = hash_to_scalar(b"DarkFi_Schnorr", &commit.to_bytes(), &msg[..]);
let response = mask + challenge * secret;
// Verify signature
assert_eq!(
zcash_primitives::constants::SPENDING_KEY_GENERATOR * response - public * challenge, commit);
}

10
src/crypto/util.rs Normal file
View File

@@ -0,0 +1,10 @@
use blake2b_simd::Params;
pub fn hash_to_scalar(persona: &[u8], a: &[u8], b: &[u8]) -> jubjub::Fr {
let mut hasher = Params::new().hash_length(64).personal(persona).to_state();
hasher.update(a);
hasher.update(b);
let ret = hasher.finalize();
jubjub::Fr::from_bytes_wide(ret.as_array())
}