Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust 2018 & secp256k1 improvements #61

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["lib"]

[features]
default = ["rust-gmp"]
ec_secp256k1 = ["rust-gmp" ,"ecc", "secp256k1"]
ec_secp256k1 = ["rust-gmp" ,"ecc", "secp256k1", "merkle"]
ec_ristretto = ["rust-gmp", "ecc" , "curve25519-dalek"]
ec_ed25519 = ["rust-gmp", "ecc" , "cryptoxide"]
ec_jubjub = ["rust-gmp", "ecc" , "pairing", "sapling-crypto"]
Expand All @@ -17,15 +17,15 @@ merkle = ["rust-crypto", "merkle-sha3"]

[dependencies]
rand = "0.6"
serde = "1.0"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
zeroize = "0.10"
sha3 = "0.8.2"
sha2 = "0.8.0"
hmac = "0.7.1"
digest = "0.8.1"
hex = "^0.3"
blake2b_simd = "0.5.7"
blake2b_simd = "0.5.8"
lazy_static = "1.4"

[dependencies.rust-crypto]
version = "^0.2"
Expand Down Expand Up @@ -65,5 +65,5 @@ version = "0.1.2"
optional = true

[dev-dependencies]
bincode = "1.1"
bincode = "1.2.0"
serde_json = "1.0"
3 changes: 1 addition & 2 deletions examples/pedersen_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ pub fn ped_com(message: &BigInt) {

let security_bits = 256;
let blinding_factor = BigInt::sample(security_bits);
let com = PedersenCommitment::create_commitment_with_user_defined_randomness(
let _com = PedersenCommitment::create_commitment_with_user_defined_randomness(
message,
&blinding_factor,
);
(com, blinding_factor);
}

fn main() {
Expand Down
6 changes: 2 additions & 4 deletions examples/proof_of_knowledge_of_dlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub fn dlog_proof() {
let witness: FE = ECScalar::new_random();
let dlog_proof = DLogProof::prove(&witness);
let verified = DLogProof::verify(&dlog_proof);
match verified {
Ok(_t) => assert!(true),
Err(_e) => assert!(false),
}

assert!(verified.is_ok());
}

fn main() {
Expand Down
14 changes: 7 additions & 7 deletions examples/verifiable_secret_sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn secret_sharing_3_out_of_5() {
shares_vec.push(secret_shares[4].clone());
//test reconstruction

let secret_reconstructed = vss_scheme.reconstruct(&vec![0, 1, 2, 4], &shares_vec);
let secret_reconstructed = vss_scheme.reconstruct(&[0, 1, 2, 4], &shares_vec);

assert_eq!(secret, secret_reconstructed);
// test secret shares are verifiable
Expand All @@ -36,7 +36,7 @@ pub fn secret_sharing_3_out_of_5() {
assert!(valid1.is_ok());

let g: GE = GE::generator();
let share1_public = g * &secret_shares[0];
let share1_public = g * secret_shares[0];
let valid1_public = vss_scheme.validate_share_public(&share1_public, 1);
assert!(valid1_public.is_ok());

Expand All @@ -47,11 +47,11 @@ pub fn secret_sharing_3_out_of_5() {
let l2 = vss_scheme.map_share_to_new_params(2, &s);
let l3 = vss_scheme.map_share_to_new_params(3, &s);
let l4 = vss_scheme.map_share_to_new_params(4, &s);
let w = l0 * secret_shares[0].clone()
+ l1 * secret_shares[1].clone()
+ l2 * secret_shares[2].clone()
+ l3 * secret_shares[3].clone()
+ l4 * secret_shares[4].clone();
let w = l0 * secret_shares[0]
+ l1 * secret_shares[1]
+ l2 * secret_shares[2]
+ l3 * secret_shares[3]
+ l4 * secret_shares[4];
assert_eq!(w, secret_reconstructed);
}

Expand Down
4 changes: 2 additions & 2 deletions src/arithmetic/big_gmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Converter for Mpz {
self.to_str_radix(super::HEX_RADIX)
}

fn from_hex(value: &str) -> Mpz {
fn from_hex(value: &str) -> Self {
BigInt::from_str_radix(value, super::HEX_RADIX).expect("Error in serialization")
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Samplable for Mpz {
let bytes = (bit_size - 1) / 8 + 1;
let mut buf: Vec<u8> = vec![0; bytes];
rng.fill_bytes(&mut buf);
Self::from(&*buf) >> (bytes * 8 - bit_size)
Self::from(buf.as_slice()) >> (bytes * 8 - bit_size)
}

fn strict_sample(bit_size: usize) -> Self {
Expand Down
36 changes: 17 additions & 19 deletions src/cryptographic_primitives/commitments/hash_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ use super::traits::Commitment;
use super::SECURITY_BITS;
use crate::arithmetic::traits::Samplable;
use sha3::{Digest, Sha3_256};
//TODO: using the function with BigInt's as input instead of string's makes it impossible to commit to empty message or use empty randomness

impl Commitment<BigInt> for HashCommitment {
fn create_commitment_with_user_defined_randomness(
message: &BigInt,
blinding_factor: &BigInt,
) -> BigInt {
let mut digest = Sha3_256::new();

let bytes_message: Vec<u8> = message.into();
digest.input(&bytes_message);
let bytes_blinding_factor: Vec<u8> = blinding_factor.into();
digest.input(&bytes_blinding_factor);
digest.input(bytes_message.as_slice());

let salt: Vec<u8> = blinding_factor.into();
digest.input(salt.as_slice());

BigInt::from(digest.result().as_ref())
}

fn create_commitment(message: &BigInt) -> (BigInt, BigInt) {
let blinding_factor = BigInt::sample(SECURITY_BITS);
let blinding_factor = BigInt::strict_sample(SECURITY_BITS);
let com = HashCommitment::create_commitment_with_user_defined_randomness(
message,
&blinding_factor,
Expand Down Expand Up @@ -59,10 +62,10 @@ mod tests {
let message = BigInt::sample(SECURITY_BITS);
let (commitment, blind_factor) = HashCommitment::create_commitment(&message);
if commitment.to_str_radix(2).len() == hex_len {
ctr_commit_len = ctr_commit_len + 1;
ctr_commit_len += 1;
}
if blind_factor.to_str_radix(2).len() == hex_len {
ctr_blind_len = ctr_blind_len + 1;
ctr_blind_len += 1;
}
}
//test commitment length - works because SHA256 output length the same as sec_bits
Expand All @@ -74,18 +77,9 @@ mod tests {
assert!(ctr_blind_len / sample_size > 0.3);
}

#[test]
fn test_bit_length_create_commitment_with_user_defined_randomness() {
let message = BigInt::sample(SECURITY_BITS);
let (_commitment, blind_factor) = HashCommitment::create_commitment(&message);
let commitment2 =
HashCommitment::create_commitment_with_user_defined_randomness(&message, &blind_factor);
assert_eq!(commitment2.to_str_radix(16).len(), SECURITY_BITS / 4);
}

#[test]
fn test_random_num_generation_create_commitment_with_user_defined_randomness() {
let message = BigInt::sample(SECURITY_BITS);
let message = BigInt::strict_sample(SECURITY_BITS);
let (commitment, blind_factor) = HashCommitment::create_commitment(&message);
let commitment2 =
HashCommitment::create_commitment_with_user_defined_randomness(&message, &blind_factor);
Expand All @@ -96,14 +90,18 @@ mod tests {
fn test_hashing_create_commitment_with_user_defined_randomness() {
let mut digest = Sha3_256::new();
let message = BigInt::one();

let commitment = HashCommitment::create_commitment_with_user_defined_randomness(
&message,
&BigInt::zero(),
);

let message2: Vec<u8> = (&message).into();
digest.input(&message2);
digest.input(message2.as_slice());

let bytes_blinding_factor: Vec<u8> = (&BigInt::zero()).into();
digest.input(&bytes_blinding_factor);
digest.input(bytes_blinding_factor.as_slice());

let hash_result = BigInt::from(digest.result().as_ref());
assert_eq!(&commitment, &hash_result);
}
Expand Down
12 changes: 4 additions & 8 deletions src/cryptographic_primitives/hashing/blake2b512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,18 @@ mod tests {
#[test]
// Very basic test here, TODO: suggest better testing
fn create_hash_test() {
let result =
Blake::create_hash(&vec![&BigInt::one(), &BigInt::zero()], b"Zcash_RedJubjubH");
let result = Blake::create_hash(&[&BigInt::one(), &BigInt::zero()], b"Zcash_RedJubjubH");
assert!(result > BigInt::zero());
}

#[test]
fn create_hash_from_ge_test() {
let point = GE::base_point2();
let result1 =
Blake::create_hash_from_ge(&vec![&point, &GE::generator()], b"Zcash_RedJubjubH");
let result1 = Blake::create_hash_from_ge(&[&point, &GE::generator()], b"Zcash_RedJubjubH");
assert!(result1.to_big_int().to_str_radix(2).len() > 240);
let result2 =
Blake::create_hash_from_ge(&vec![&GE::generator(), &point], b"Zcash_RedJubjubH");
let result2 = Blake::create_hash_from_ge(&[&GE::generator(), &point], b"Zcash_RedJubjubH");
assert_ne!(result1, result2);
let result3 =
Blake::create_hash_from_ge(&vec![&GE::generator(), &point], b"Zcash_RedJubjubH");
let result3 = Blake::create_hash_from_ge(&[&GE::generator(), &point], b"Zcash_RedJubjubH");
assert_eq!(result2, result3);
}
}
14 changes: 7 additions & 7 deletions src/cryptographic_primitives/hashing/hash_sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ mod tests {
// https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs
fn vector_sha256_test() {
// Empty Message
let result: BigInt = HSha256::create_hash(&vec![]);
let result: BigInt = HSha256::create_hash(&[]);
assert_eq!(
result.to_str_radix(16),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);

// 256 bit message
let result: BigInt = HSha256::create_hash(&vec![&BigInt::from_str_radix(
let result: BigInt = HSha256::create_hash(&[&BigInt::from_str_radix(
"09fc1accc230a205e4a208e64a8f204291f581a12756392da4b8c0cf5ef02b95",
16,
)
Expand All @@ -73,7 +73,7 @@ mod tests {
);

// 2x128 bit messages
let result: BigInt = HSha256::create_hash(&vec![
let result: BigInt = HSha256::create_hash(&[
&BigInt::from_str_radix("09fc1accc230a205e4a208e64a8f2042", 16).unwrap(),
&BigInt::from_str_radix("91f581a12756392da4b8c0cf5ef02b95", 16).unwrap(),
]);
Expand All @@ -83,7 +83,7 @@ mod tests {
);

// 512 bit message
let result: BigInt = HSha256::create_hash(&vec![&BigInt::from_str_radix("5a86b737eaea8ee976a0a24da63e7ed7eefad18a101c1211e2b3650c5187c2a8a650547208251f6d4237e661c7bf4c77f335390394c37fa1a9f9be836ac28509", 16).unwrap()]);
let result: BigInt = HSha256::create_hash(&[&BigInt::from_str_radix("5a86b737eaea8ee976a0a24da63e7ed7eefad18a101c1211e2b3650c5187c2a8a650547208251f6d4237e661c7bf4c77f335390394c37fa1a9f9be836ac28509", 16).unwrap()]);
assert_eq!(
result.to_str_radix(16),
"42e61e174fbb3897d6dd6cef3dd2802fe67b331953b06114a65c772859dfc1aa"
Expand All @@ -93,11 +93,11 @@ mod tests {
#[test]
fn create_sha256_from_ge_test() {
let point = GE::base_point2();
let result1 = HSha256::create_hash_from_ge(&vec![&point, &GE::generator()]);
let result1 = HSha256::create_hash_from_ge(&[&point, &GE::generator()]);
assert!(result1.to_big_int().to_str_radix(2).len() > 240);
let result2 = HSha256::create_hash_from_ge(&vec![&GE::generator(), &point]);
let result2 = HSha256::create_hash_from_ge(&[&GE::generator(), &point]);
assert_ne!(result1, result2);
let result3 = HSha256::create_hash_from_ge(&vec![&GE::generator(), &point]);
let result3 = HSha256::create_hash_from_ge(&[&GE::generator(), &point]);
assert_eq!(result2, result3);
}
}
14 changes: 7 additions & 7 deletions src/cryptographic_primitives/hashing/hash_sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ mod tests {
// https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing#shavs
fn vector_sha512_test() {
// Empty message
let result: BigInt = HSha512::create_hash(&vec![]);
let result: BigInt = HSha512::create_hash(&[]);
assert_eq!(
result.to_str_radix(16),
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
);

// 2x256 bit message
let result: BigInt = HSha512::create_hash(&vec![
let result: BigInt = HSha512::create_hash(&[
&BigInt::from_str_radix(
"c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f7",
16,
Expand All @@ -80,7 +80,7 @@ mod tests {
);

// 512 bit message
let result: BigInt = HSha512::create_hash(&vec![&BigInt::from_str_radix(
let result: BigInt = HSha512::create_hash(&[&BigInt::from_str_radix(
"c1ca70ae1279ba0b918157558b4920d6b7fba8a06be515170f202fafd36fb7f79d69fad745dba6150568db1e2b728504113eeac34f527fc82f2200b462ecbf5d",
16,
)
Expand All @@ -91,7 +91,7 @@ mod tests {
);

// 1024 bit message
let result: BigInt = HSha512::create_hash(&vec![&BigInt::from_str_radix("fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8", 16).unwrap()]);
let result: BigInt = HSha512::create_hash(&[&BigInt::from_str_radix("fd2203e467574e834ab07c9097ae164532f24be1eb5d88f1af7748ceff0d2c67a21f4e4097f9d3bb4e9fbf97186e0db6db0100230a52b453d421f8ab9c9a6043aa3295ea20d2f06a2f37470d8a99075f1b8a8336f6228cf08b5942fc1fb4299c7d2480e8e82bce175540bdfad7752bc95b577f229515394f3ae5cec870a4b2f8", 16).unwrap()]);
assert_eq!(
result.to_str_radix(16),
"a21b1077d52b27ac545af63b32746c6e3c51cb0cb9f281eb9f3580a6d4996d5c9917d2a6e484627a9d5a06fa1b25327a9d710e027387fc3e07d7c4d14c6086cc"
Expand All @@ -101,11 +101,11 @@ mod tests {
#[test]
fn create_sha512_from_ge_test() {
let point = GE::base_point2();
let result1 = HSha512::create_hash_from_ge(&vec![&point, &GE::generator()]);
let result1 = HSha512::create_hash_from_ge(&[&point, &GE::generator()]);
assert!(result1.to_big_int().to_str_radix(2).len() > 240);
let result2 = HSha512::create_hash_from_ge(&vec![&GE::generator(), &point]);
let result2 = HSha512::create_hash_from_ge(&[&GE::generator(), &point]);
assert_ne!(result1, result2);
let result3 = HSha512::create_hash_from_ge(&vec![&GE::generator(), &point]);
let result3 = HSha512::create_hash_from_ge(&[&GE::generator(), &point]);
assert_eq!(result2, result3);
}
}
13 changes: 4 additions & 9 deletions src/cryptographic_primitives/hashing/hmac_sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,23 @@ impl KeyedHash for HMacSha512 {
mod tests {

use super::HMacSha512;
use crate::arithmetic::traits::Converter;
use crate::arithmetic::traits::Samplable;
use crate::cryptographic_primitives::hashing::traits::KeyedHash;
use crate::BigInt;

#[test]
fn create_hmac_test() {
let key = BigInt::sample(512);
let result1 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10)]);
let result1_bytes = &BigInt::to_vec(&result1)[..];
let mut array_result: [u8; 64] = [0u8; 64];
array_result.copy_from_slice(result1_bytes);
assert!(HMacSha512::verify(&key, &vec![&BigInt::from(10)], array_result).is_ok());
let result1 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]);
let key2 = BigInt::sample(512);
// same data , different key
let result2 = HMacSha512::create_hmac(&key2, &vec![&BigInt::from(10)]);
let result2 = HMacSha512::create_hmac(&key2, &[&BigInt::from(10)]);
assert_ne!(result1, result2);
// same key , different data
let result3 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10), &BigInt::from(11)]);
let result3 = HMacSha512::create_hmac(&key, &[&BigInt::from(10), &BigInt::from(11)]);
assert_ne!(result1, result3);
// same key, same data
let result4 = HMacSha512::create_hmac(&key, &vec![&BigInt::from(10)]);
let result4 = HMacSha512::create_hmac(&key, &[&BigInt::from(10)]);
assert_eq!(result1, result4)
}
}
Loading