From 88048849799e2a97b4677537bd090334dac01632 Mon Sep 17 00:00:00 2001 From: "J. Ayo Akinyele" Date: Thu, 17 Oct 2019 15:48:31 -0400 Subject: [PATCH] remove sodium as a dep --- Cargo.toml | 1 - src/channels.rs | 8 ++----- src/lib.rs | 18 ++++++++-------- src/util.rs | 55 +++++++++++++++++++------------------------------ 4 files changed, 32 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68d0007..c782fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,6 @@ rand_xorshift = "0.1" ff = { git = "https://github.com/boltlabs-inc/ff", branch = "master" } pairing = { git = "https://github.com/boltlabs-inc/pairing", branch = "master", features = ["serde"] } bincode = "0.6.1" -sodiumoxide = "0.2.5" libc = "*" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/src/channels.rs b/src/channels.rs index 8b5715c..5f4d252 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -15,7 +15,7 @@ use pairing::bls12_381::Bls12; use ff::PrimeField; use cl::{BlindKeyPair, KeyPair, Signature, PublicParams, setup}; use ped92::{CSParams, Commitment, CSMultiParams, CommitmentProof}; -use util::{hash_pubkey_to_fr, convert_int_to_fr, hash_to_fr, RevokedMessage, hash_to_slice, hash_slice_to_fr}; +use util::{hash_pubkey_to_fr, convert_int_to_fr, hash_to_fr, RevokedMessage, hash_to_slice}; use rand::Rng; use std::collections::HashMap; use std::fmt::Display; @@ -144,7 +144,7 @@ impl ChannelToken { input.extend(&ser_mpk); input.extend(&ser_comParams); - return hash_slice_to_fr::(&input); + return hash_to_fr::(input); } // add a method to compute hash on chain: SHA256 + RIPEMD160? @@ -168,10 +168,6 @@ impl ChannelState { } } - pub fn init() { - sodiumoxide::init(); - } - /// /// keygen - takes as input public parameters and generates a digital signature keypair /// diff --git a/src/lib.rs b/src/lib.rs index 9b9fc51..c073e2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ extern crate rand; extern crate rand_core; extern crate bincode; -extern crate sodiumoxide; +//extern crate sodiumoxide; extern crate secp256k1; extern crate time; extern crate sha2; @@ -39,7 +39,7 @@ extern crate libc; extern crate rand_xorshift; extern crate core; -pub mod sym; +//pub mod sym; pub mod cl; pub mod ccs08; pub mod ped92; @@ -53,9 +53,9 @@ use std::fmt; use std::str; use bincode::SizeLimit::Infinite; use bincode::rustc_serialize::{encode, decode}; -use sodiumoxide::randombytes; -use sodiumoxide::crypto::hash::sha512; -use sha2::Sha512; +//use sodiumoxide::randombytes; +//use sodiumoxide::crypto::hash::sha512; +//use sha2::Sha512; use std::collections::HashMap; use ff::{Rand, Field}; @@ -116,14 +116,14 @@ pub mod bidirectional { use util; use wallet; use pairing::Engine; - use sodiumoxide; + //use sodiumoxide; use cl; // for blind signature use secp256k1; // for on-chain keys use HashMap; - use sodiumoxide::crypto::hash::sha512; - use sha2::Sha512; + //use sodiumoxide::crypto::hash::sha512; + //use sha2::Sha512; use serde::{Serialize, Deserialize}; use util::{RevokedMessage, hash_to_slice}; @@ -173,7 +173,7 @@ pub mod bidirectional { } pub fn init() { - sodiumoxide::init(); + //sodiumoxide::init(); } /// diff --git a/src/util.rs b/src/util.rs index 70ca3db..8bda47b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,9 +1,10 @@ use super::*; -use sodiumoxide::crypto::hash::sha512; +//use sodiumoxide::crypto::hash::sha512; use pairing::Engine; use ff::{PrimeField}; use rand::Rng; use secp256k1::{Signature, PublicKey}; +use sha2::{Sha512, Sha256, Digest}; pub fn is_vec_fr_equal(a: &Vec, b: &Vec) -> bool { (a.len() == b.len()) && @@ -57,24 +58,23 @@ pub fn fmt_bytes_to_int(bytearray: [u8; 64]) -> String { return s; } -pub fn hash_to_fr(byteVec: Vec) -> E::Fr { - let sha2_digest = sha512::hash(byteVec.as_slice()); +pub fn compute_the_hash(bytes: &Vec) -> E::Fr { + let mut hasher = sha2::Sha512::new(); + hasher.input(&bytes.as_slice()); + let sha2_digest = hasher.result(); let mut hash_buf: [u8; 64] = [0; 64]; hash_buf.copy_from_slice(&sha2_digest[0..64]); let hexresult = fmt_bytes_to_int(hash_buf); - let result = E::Fr::from_str(&hexresult); - return result.unwrap(); + return E::Fr::from_str(&hexresult).unwrap(); +} + +pub fn hash_to_fr(byteVec: Vec) -> E::Fr { + return compute_the_hash::(&byteVec); } pub fn hash_pubkey_to_fr(wpk: &secp256k1::PublicKey) -> E::Fr { let x_slice = wpk.serialize_uncompressed(); - let sha2_digest = sha512::hash(&x_slice); - - let mut hash_buf: [u8; 64] = [0; 64]; - hash_buf.copy_from_slice(&sha2_digest[0..64]); - let hexresult = fmt_bytes_to_int(hash_buf); - let result = E::Fr::from_str(&hexresult); - return result.unwrap(); + return compute_the_hash::(&x_slice.to_vec()); } pub fn convert_int_to_fr(value: i64) -> E::Fr { @@ -86,14 +86,16 @@ pub fn convert_int_to_fr(value: i64) -> E::Fr { let mut res = E::Fr::zero(); let val = E::Fr::from_str(value2.to_string().as_str()).unwrap(); res.sub_assign(&val); - // TODO: look at how to do negation return res; } } pub fn compute_pub_key_fingerprint(wpk: &secp256k1::PublicKey) -> String { let x_slice = wpk.serialize(); - let sha2_digest = sha512::hash(&x_slice); + let mut hasher = sha2::Sha512::new(); + hasher.input(&x_slice.to_vec()); + let sha2_digest = hasher.result(); + // let sha2_digest = sha512::hash(&x_slice); let h = format!("{:x}", HexSlice::new(&sha2_digest[0..16])); return h; } @@ -102,34 +104,19 @@ pub fn hash_buffer_to_fr<'a, E: Engine>(prefix: &'a str, buf: &[u8; 64]) -> E::F let mut input_buf = Vec::new(); input_buf.extend_from_slice(prefix.as_bytes()); input_buf.extend_from_slice(buf); - - let sha2_digest = sha512::hash(&input_buf.as_slice()); - - let mut hash_buf: [u8; 64] = [0; 64]; - hash_buf.copy_from_slice(&sha2_digest[0..64]); - let hexresult = fmt_bytes_to_int(hash_buf); - let result = E::Fr::from_str(&hexresult); - return result.unwrap(); + return compute_the_hash::(&input_buf); } pub fn hash_to_slice(input_buf: &Vec) -> [u8; 32] { - let sha2_digest = sha512::hash(input_buf.as_slice()); + let mut hasher = sha2::Sha512::new(); + hasher.input(&input_buf.as_slice()); + let sha2_digest = hasher.result(); + let mut hash_buf: [u8; 32] = [0; 32]; hash_buf.copy_from_slice(&sha2_digest[0..32]); return hash_buf; } -pub fn hash_slice_to_fr(input_buf: &Vec) -> E::Fr { - let sha2_digest = sha512::hash(input_buf.as_slice()); - let mut hash_buf: [u8; 64] = [0; 64]; - hash_buf.copy_from_slice(&sha2_digest[0..64]); - let hexresult = fmt_bytes_to_int(hash_buf); - let result = E::Fr::from_str(&hexresult); - return result.unwrap(); -} - - - #[derive(Clone, Serialize, Deserialize)] pub struct RevokedMessage { pub msgtype: String,