Skip to content

Commit

Permalink
fix secp256k1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas committed Sep 4, 2019
1 parent e6d47a7 commit c33f00d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 139 deletions.
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
Loading

0 comments on commit c33f00d

Please sign in to comment.