Skip to content

Commit

Permalink
Merge pull request #17 from ZenGo-X/0.7-curv
Browse files Browse the repository at this point in the history
Bump curv to 0.7 and bump version
  • Loading branch information
omershlo authored May 13, 2021
2 parents 828d5bc + 77d0f4a commit 9bf90ad
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 42 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multi-party-eddsa"
version = "0.2.1"
version = "0.2.2"
authors = [
"Omer <[email protected]>",
"Gary <[email protected]>"
Expand All @@ -10,8 +10,11 @@ authors = [
crate-type = ["rlib", "dylib"]

[dependencies]
curv = { git = "https://github.com/KZen-networks/curv", tag = "v0.2.0-ed25519", features = ["ec_ed25519"]}
curv = { package = "curv-kzen", version = "0.7", default-features = false }
hex = "0.3.2"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"

[features]
default = ["curv/rust-gmp-kzen"]
23 changes: 12 additions & 11 deletions src/protocols/aggsig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
//! See https://tools.ietf.org/html/rfc8032
use curv::cryptographic_primitives::proofs::*;
pub use curv::elliptic::curves::traits::*;
pub use curv::{BigInt, FE, GE};
use curv::elliptic::curves::ed25519::{GE, FE};
use curv::BigInt;

use curv::cryptographic_primitives::hashing::hash_sha512::HSha512;
use curv::cryptographic_primitives::hashing::traits::*;
Expand Down Expand Up @@ -62,7 +63,7 @@ impl KeyPair {
fn create_from_private_key_internal(sk: &FE) -> KeyPair {
let ec_point: GE = ECPoint::generator();
let h = HSha512::create_hash(&vec![&sk.to_big_int()]);
let h_vec = BigInt::to_vec(&h);
let h_vec = BigInt::to_bytes(&h);
let mut h_vec_padded = vec![0; 64 - h_vec.len()]; // ensure hash result is padded to 64 bytes
h_vec_padded.extend_from_slice(&h_vec);
let mut private_key: [u8; 32] = [0u8; 32];
Expand All @@ -74,8 +75,8 @@ impl KeyPair {
private_key[31] |= 64;
let private_key = &private_key[..private_key.len()];
let prefix = &prefix[..prefix.len()];
let private_key: FE = ECScalar::from(&BigInt::from(private_key));
let prefix: FE = ECScalar::from(&BigInt::from(prefix));
let private_key: FE = ECScalar::from(&BigInt::from_bytes(private_key));
let prefix: FE = ECScalar::from(&BigInt::from_bytes(prefix));
let public_key = ec_point * &private_key;
KeyPair {
public_key,
Expand Down Expand Up @@ -159,7 +160,7 @@ impl Signature {
let r = HSha512::create_hash(&vec![
&BigInt::from(2), // domain seperation
&keys.expended_private_key.prefix.to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
&FE::new_random().to_big_int(),
]);
let r = reverse_bn_to_fe(&r);
Expand All @@ -177,7 +178,7 @@ impl Signature {
let k = HSha512::create_hash(&vec![
&R_tot.bytes_compressed_to_big_int(),
&apk.bytes_compressed_to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);
let k = reverse_bn_to_fe(&k);
k
Expand All @@ -201,15 +202,15 @@ impl Signature {
pub fn sign_single(message: &[u8], keys: &KeyPair) -> Signature {
let r = HSha512::create_hash(&vec![
&keys.expended_private_key.prefix.to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);
let r: FE = ECScalar::from(&r);
let ec_point: GE = ECPoint::generator();
let R = ec_point.scalar_mul(&r.get_element());
let k = HSha512::create_hash(&vec![
&R.bytes_compressed_to_big_int(),
&keys.public_key.bytes_compressed_to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);
let k = reverse_bn_to_fe(&k);
let k_mul_sk = k.mul(&keys.expended_private_key.private_key.get_element());
Expand All @@ -235,7 +236,7 @@ pub fn verify(signature: &Signature, message: &[u8], public_key: &GE) -> Result<
let k = HSha512::create_hash(&vec![
&signature.R.bytes_compressed_to_big_int(),
&public_key.bytes_compressed_to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);

let k_fe = reverse_bn_to_fe(&k);
Expand Down Expand Up @@ -264,8 +265,8 @@ pub fn test_com(r_to_test: &GE, blind_factor: &BigInt, comm: &BigInt) -> bool {
mod test;

pub fn reverse_bn_to_fe(scalar: &BigInt) -> FE {
let mut vec = BigInt::to_vec(&scalar);
let mut vec = BigInt::to_bytes(&scalar);
vec.reverse();
let scalar_out = BigInt::from(&vec[..]);
let scalar_out = BigInt::from_bytes(&vec[..]);
ECScalar::from(&scalar_out)
}
7 changes: 4 additions & 3 deletions src/protocols/aggsig/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
#[cfg(test)]
mod tests {
use curv::elliptic::curves::traits::ECPoint;
use curv::GE;
use curv::elliptic::curves::ed25519::{GE, FE};
use curv::BigInt;
use protocols::aggsig::{test_com, verify, KeyPair, Signature};
use curv::arithmetic::Converter;

#[test]
fn test_ed25519_one_party() {
Expand Down Expand Up @@ -196,7 +198,6 @@ mod tests {
}

use curv::elliptic::curves::traits::ECScalar;
use curv::{BigInt, FE};
use hex::decode;
#[test]
fn test_verify_standard_sig() {
Expand Down Expand Up @@ -227,7 +228,7 @@ mod tests {
let s_str = "5a180452743fac943b53728e4cbea288a566ba49f7695808d53b3f9f1cd6ed02";
let mut s_dec = decode(s_str).unwrap();
s_dec.reverse();
let s_bn = BigInt::from(&s_dec[..]);
let s_bn = BigInt::from_bytes(&s_dec[..]);
let s: FE = ECScalar::from(&s_bn);

let sig = Signature { R, s };
Expand Down
9 changes: 5 additions & 4 deletions src/protocols/multisig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use curv::cryptographic_primitives::hashing::hash_sha256::HSha256;
use curv::cryptographic_primitives::hashing::hash_sha512::HSha512;
use curv::cryptographic_primitives::hashing::traits::*;
use curv::elliptic::curves::traits::*;
use curv::{BigInt, FE, GE};
use curv::elliptic::curves::ed25519::{GE, FE};
use curv::BigInt;
use protocols::multisig;

// TODO: move to a common location to be used by all protocols.
Expand Down Expand Up @@ -78,7 +79,7 @@ impl ExpendedKeyPair {
pub fn create_from_private_key(sk: FE) -> ExpendedKeyPair {
let ec_point: GE = ECPoint::generator();
let h = HSha512::create_hash(&vec![&sk.to_big_int()]);
let h_vec = BigInt::to_vec(&h);
let h_vec = BigInt::to_bytes(&h);
let mut h_vec_padded = vec![0; 64 - h_vec.len()]; // ensure hash result is padded to 64 bytes
h_vec_padded.extend_from_slice(&h_vec);
let mut private_key: [u8; 32] = [0u8; 32];
Expand All @@ -90,8 +91,8 @@ impl ExpendedKeyPair {
private_key[31] |= 64;
let private_key = &private_key[..private_key.len()];
let prefix = &prefix[..prefix.len()];
let private_key: FE = ECScalar::from(&BigInt::from(private_key));
let prefix: FE = ECScalar::from(&BigInt::from(prefix));
let private_key: FE = ECScalar::from(&BigInt::from_bytes(private_key));
let prefix: FE = ECScalar::from(&BigInt::from_bytes(prefix));
let public_key = ec_point * &private_key;
ExpendedKeyPair {
public_key,
Expand Down
10 changes: 6 additions & 4 deletions src/protocols/multisig/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ mod tests {
use curv::cryptographic_primitives::hashing::merkle_tree::MT256;
use curv::cryptographic_primitives::hashing::traits::Hash;
use curv::elliptic::curves::traits::ECScalar;
use curv::{BigInt, FE};
use curv::elliptic::curves::ed25519::{GE, FE};
use curv::BigInt;
use curv::arithmetic::Converter;
use protocols::multisig::{partial_sign, verify, EphKey, Keys, Signature};

#[test]
Expand All @@ -33,7 +35,7 @@ mod tests {

fn two_party_key_gen_internal() {
let message_vec = vec![79, 77, 69, 82];
let message_bn = BigInt::from(&message_vec[..]);
let message_bn = BigInt::from_bytes(&message_vec[..]);
let message = HSha256::create_hash(&vec![&message_bn]);

// party1 key gen:
Expand Down Expand Up @@ -84,8 +86,8 @@ mod tests {
let sig = Signature::set_signature(&Xt, &y);
assert!(verify(&It, &sig, &es).is_ok());

assert!(MT256::validate_proof(&proof1, root).is_ok());
assert!(MT256::validate_proof(&proof2, root).is_ok());
assert!(MT256::<GE>::validate_proof(&proof1, root).is_ok());
assert!(MT256::<GE>::validate_proof(&proof2, root).is_ok());
}

}
31 changes: 16 additions & 15 deletions src/protocols/thresholdsig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use curv::cryptographic_primitives::commitments::traits::Commitment;
use curv::cryptographic_primitives::hashing::hash_sha512::HSha512;
use curv::cryptographic_primitives::hashing::traits::*;
use curv::cryptographic_primitives::secret_sharing::feldman_vss::VerifiableSS;
use curv::{BigInt, FE, GE};
use curv::elliptic::curves::ed25519::{GE, FE};
use curv::BigInt;

const SECURITY: usize = 256;

Expand Down Expand Up @@ -87,7 +88,7 @@ impl Keys {
fn phase1_create_from_private_key_internal(index: usize, sk: &FE) -> Keys {
let ec_point: GE = ECPoint::generator();
let h = HSha512::create_hash(&vec![&sk.to_big_int()]);
let h_vec = BigInt::to_vec(&h);
let h_vec = BigInt::to_bytes(&h);
let mut h_vec_padded = vec![0; 64 - h_vec.len()]; // ensure hash result is padded to 64 bytes
h_vec_padded.extend_from_slice(&h_vec);
let mut private_key: [u8; 32] = [0u8; 32];
Expand All @@ -99,8 +100,8 @@ impl Keys {
private_key[31] |= 64;
let private_key = &private_key[..private_key.len()];
let prefix = &prefix[..prefix.len()];
let private_key: FE = ECScalar::from(&BigInt::from(private_key));
let prefix: FE = ECScalar::from(&BigInt::from(prefix));
let private_key: FE = ECScalar::from(&BigInt::from_bytes(private_key));
let prefix: FE = ECScalar::from(&BigInt::from_bytes(prefix));
let public_key = ec_point * &private_key;

Keys {
Expand Down Expand Up @@ -128,7 +129,7 @@ impl Keys {
y_vec: &Vec<GE>,
bc1_vec: &Vec<KeyGenBroadcastMessage1>,
parties: &[usize],
) -> Result<(VerifiableSS, Vec<FE>, usize), Error> {
) -> Result<(VerifiableSS<GE>, Vec<FE>, usize), Error> {
// test length:
assert_eq!(blind_vec.len(), params.share_count);
assert_eq!(bc1_vec.len(), params.share_count);
Expand Down Expand Up @@ -161,7 +162,7 @@ impl Keys {
params: &Parameters,
y_vec: &Vec<GE>,
secret_shares_vec: &Vec<FE>,
vss_scheme_vec: &Vec<VerifiableSS>,
vss_scheme_vec: &Vec<VerifiableSS<GE>>,
index: &usize,
) -> Result<SharedKeys, Error> {
assert_eq!(y_vec.len(), params.share_count);
Expand Down Expand Up @@ -207,7 +208,7 @@ impl EphemeralKey {
// to the nonce
let r_local = HSha512::create_hash(&[
&keys.prefix.to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
&FE::new_random().to_big_int(),
]);
let r_i: FE = ECScalar::from(&r_local);
Expand Down Expand Up @@ -237,7 +238,7 @@ impl EphemeralKey {
R_vec: &Vec<GE>,
bc1_vec: &Vec<KeyGenBroadcastMessage1>,
parties: &[usize],
) -> Result<(VerifiableSS, Vec<FE>, usize), Error> {
) -> Result<(VerifiableSS<GE>, Vec<FE>, usize), Error> {
// test length:
assert!(blind_vec.len() > params.threshold && blind_vec.len() <= params.share_count);
assert!(bc1_vec.len() > params.threshold && bc1_vec.len() <= params.share_count);
Expand Down Expand Up @@ -270,7 +271,7 @@ impl EphemeralKey {
params: &Parameters,
R_vec: &Vec<GE>,
secret_shares_vec: &Vec<FE>,
vss_scheme_vec: &Vec<VerifiableSS>,
vss_scheme_vec: &Vec<VerifiableSS<GE>>,
index: &usize,
) -> Result<EphemeralSharedKeys, Error> {
assert!(R_vec.len() > params.threshold && R_vec.len() <= params.share_count);
Expand Down Expand Up @@ -316,7 +317,7 @@ impl LocalSig {
let e_bn = HSha512::create_hash(&[
&local_ephemaral_key.R.bytes_compressed_to_big_int(),
&local_private_key.y.bytes_compressed_to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);
let k: FE = ECScalar::from(&e_bn);
let gamma_i = r_i + k * s_i;
Expand All @@ -329,9 +330,9 @@ impl LocalSig {
pub fn verify_local_sigs(
gamma_vec: &Vec<LocalSig>,
parties_index_vec: &[usize],
vss_private_keys: &Vec<VerifiableSS>,
vss_ephemeral_keys: &Vec<VerifiableSS>,
) -> Result<(VerifiableSS), Error> {
vss_private_keys: &Vec<VerifiableSS<GE>>,
vss_ephemeral_keys: &Vec<VerifiableSS<GE>>,
) -> Result<VerifiableSS<GE>, Error> {
//parties_index_vec is a vector with indices of the parties that are participating and provided gamma_i for this step
// test that enough parties are in this round
assert!(parties_index_vec.len() > vss_private_keys[0].parameters.threshold);
Expand Down Expand Up @@ -380,7 +381,7 @@ impl LocalSig {

impl Signature {
pub fn generate(
vss_sum_local_sigs: &VerifiableSS,
vss_sum_local_sigs: &VerifiableSS<GE>,
local_sig_vec: &Vec<LocalSig>,
parties_index_vec: &[usize],
R: GE,
Expand All @@ -400,7 +401,7 @@ impl Signature {
let e_bn = HSha512::create_hash(&[
&self.R.bytes_compressed_to_big_int(),
&pubkey_y.bytes_compressed_to_big_int(),
&BigInt::from(message),
&BigInt::from_bytes(message),
]);

let e: FE = ECScalar::from(&e_bn);
Expand Down
6 changes: 3 additions & 3 deletions src/protocols/thresholdsig/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[cfg(test)]
mod tests {
use curv::cryptographic_primitives::secret_sharing::feldman_vss::VerifiableSS;
use curv::{FE, GE};
use curv::elliptic::curves::ed25519::{GE, FE};
use protocols::thresholdsig::*;

#[test]
Expand Down Expand Up @@ -132,7 +132,7 @@ mod tests {
t: usize,
n: usize,
parties: &[usize],
) -> (Vec<Keys>, Vec<SharedKeys>, GE, Vec<VerifiableSS>) {
) -> (Vec<Keys>, Vec<SharedKeys>, GE, Vec<VerifiableSS<GE>>) {
let parames = Parameters {
threshold: t,
share_count: n.clone(),
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
Vec<EphemeralKey>,
Vec<EphemeralSharedKeys>,
GE,
Vec<VerifiableSS>,
Vec<VerifiableSS<GE>>,
) {
let parames = Parameters {
threshold: t,
Expand Down

0 comments on commit 9bf90ad

Please sign in to comment.