Skip to content

Commit

Permalink
Clean crypto crate and interfaces with Signature types (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell authored Feb 5, 2020
1 parent 75224a6 commit b41107d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ clean:
@cargo clean -p node
@cargo clean -p clock
@cargo clean -p forest_libp2p
@cargo clean -p network
@cargo clean -p blockchain
@cargo clean -p forest_blocks
@cargo clean -p chain_sync
Expand Down
4 changes: 2 additions & 2 deletions blockchain/blocks/src/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ mod tests {
#[test]
fn min_ticket_test() {
let tipset = setup();
let expected_value = vec![1, 4, 3, 6, 1, 1, 2, 2, 4, 5, 3, 12, 2];
let expected_value: &[u8] = &[1, 4, 3, 6, 1, 1, 2, 2, 4, 5, 3, 12, 2];
let min = Tipset::min_ticket(&tipset).unwrap();
assert_eq!(min.vrfproof.to_bytes(), expected_value);
assert_eq!(min.vrfproof.bytes(), expected_value);
}

#[test]
Expand Down
46 changes: 25 additions & 21 deletions crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use encoding::{blake2b_256, de, ser, serde_bytes};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use secp256k1::{recover, Message, RecoveryId, Signature as EcsdaSignature};
use std::ops::Deref;

pub const BLS_SIG_LEN: usize = 96; // bytes
pub const BLS_PUB_LEN: usize = 48; // bytes
/// BLS signature length in bytes
pub const BLS_SIG_LEN: usize = 96;
/// BLS Public key length in bytes
pub const BLS_PUB_LEN: usize = 48;

/// Signature variants for Forest signatures
#[derive(Clone, Debug, PartialEq, FromPrimitive, Copy)]
pub enum SignatureType {
Secp256 = 1,
Expand Down Expand Up @@ -42,14 +44,6 @@ pub struct Signature {
bytes: Vec<u8>,
}

impl Deref for Signature {
type Target = Vec<u8>;

fn deref(&self) -> &Self::Target {
&self.bytes
}
}

impl ser::Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -94,10 +88,20 @@ impl Signature {
bytes,
}
}

/// Returns reference to signature bytes
pub fn bytes(&self) -> &[u8] {
&self.bytes
}

/// Returns reference to signature type
pub fn signature_type(&self) -> SignatureType {
self.sig_type
}
}

/// Checks if a signature is valid given data and address
pub fn is_valid_signature(data: &[u8], addr: Address, sig: Signature) -> bool {
pub fn is_valid_signature(data: &[u8], addr: &Address, sig: &Signature) -> bool {
match addr.protocol() {
Protocol::BLS => verify_bls_sig(data, addr.payload(), sig),
Protocol::Secp256k1 => verify_secp256k1_sig(data, addr, sig),
Expand All @@ -106,8 +110,8 @@ pub fn is_valid_signature(data: &[u8], addr: Address, sig: Signature) -> bool {
}

/// Returns true if a bls signature is valid
pub(crate) fn verify_bls_sig(data: &[u8], pub_k: Vec<u8>, sig: Signature) -> bool {
if pub_k.len() != BLS_PUB_LEN || sig.len() != BLS_SIG_LEN {
pub(crate) fn verify_bls_sig(data: &[u8], pub_k: &[u8], sig: &Signature) -> bool {
if pub_k.len() != BLS_PUB_LEN || sig.bytes().len() != BLS_SIG_LEN {
// validates pubkey length and signature length for protocol
return false;
}
Expand All @@ -121,7 +125,7 @@ pub(crate) fn verify_bls_sig(data: &[u8], pub_k: Vec<u8>, sig: Signature) -> boo
Err(_) => return false,
};
// generate signature struct from bytes
let sig = match BlsSignature::from_bytes(sig.as_ref()) {
let sig = match BlsSignature::from_bytes(sig.bytes()) {
Ok(v) => v,
Err(_) => return false,
};
Expand All @@ -131,18 +135,18 @@ pub(crate) fn verify_bls_sig(data: &[u8], pub_k: Vec<u8>, sig: Signature) -> boo
}

/// Returns true if a secp256k1 signature is valid
fn verify_secp256k1_sig(data: &[u8], addr: Address, sig: Signature) -> bool {
fn verify_secp256k1_sig(data: &[u8], addr: &Address, sig: &Signature) -> bool {
// blake2b 256 hash
let hash = blake2b_256(data);

// Ecrecover with hash and signature
let mut signature = [0u8; 65];
signature[..].clone_from_slice(sig.as_ref());
signature[..].clone_from_slice(sig.bytes());
let rec_addr = ecrecover(&hash, &signature);

// check address against recovered address
match rec_addr {
Ok(r) => addr == r,
Ok(r) => addr == &r,
Err(_) => false,
}
}
Expand Down Expand Up @@ -196,14 +200,14 @@ mod tests {
let addr = Address::new_bls(pk.as_bytes()).unwrap();

assert_eq!(
is_valid_signature(&msg, addr, Signature::new_bls(signature_bytes.clone())),
is_valid_signature(&msg, &addr, &Signature::new_bls(signature_bytes.clone())),
true
);
assert_eq!(
verify_bls_sig(
&msg,
pk.as_bytes(),
Signature::new_bls(signature_bytes.clone())
&pk.as_bytes(),
&Signature::new_bls(signature_bytes.clone())
),
true
);
Expand Down
14 changes: 7 additions & 7 deletions crypto/src/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ impl VRFResult {
pub fn new(output: Vec<u8>) -> Self {
Self(output)
}
/// Returns clone of underlying vector
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
/// Returns reference to underlying vector
pub fn bytes(&self) -> &[u8] {
&self.0
}
/// Returns max value based on [BLS_SIG_LEN](constant.BLS_SIG_LEN.html)
pub fn max_value() -> Self {
Expand All @@ -40,9 +40,9 @@ impl VRFResult {
unimplemented!()
}
/// Asserts whether `input` was used with `pk` to produce this VRFOutput
pub fn verify(&self, input: Vec<u8>, pk: VRFPublicKey) -> bool {
pub fn verify(&self, input: &[u8], pk: &VRFPublicKey) -> bool {
match BLSSignature::from_bytes(&self.0) {
Ok(sig) => verify_bls_sig(&input, pk.0, Signature::new_bls(sig.as_bytes())),
Ok(sig) => verify_bls_sig(input, &pk.0, &Signature::new_bls(sig.as_bytes())),
Err(_) => false,
}
}
Expand All @@ -67,11 +67,11 @@ mod tests {

let genesis = VRFResult::new(input.as_bytes());

let sig = privk.sign(&genesis.to_bytes());
let sig = privk.sign(genesis.bytes());
let res = VRFResult::new(sig.as_bytes());

let pubk = VRFPublicKey::new(privk.public_key().as_bytes());

assert!(res.verify(genesis.to_bytes(), pubk));
assert!(res.verify(genesis.bytes(), &pubk));
}
}
8 changes: 4 additions & 4 deletions vm/address/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl Address {
self.protocol
}
/// Returns data payload of Address
pub fn payload(&self) -> Vec<u8> {
self.payload.clone()
pub fn payload(&self) -> &[u8] {
&self.payload
}
/// Returns encoded bytes of Address
pub fn to_bytes(&self) -> Vec<u8> {
let mut bz: Vec<u8> = self.payload();
let mut bz: Vec<u8> = self.payload().to_vec();
bz.insert(0, self.protocol() as u8);
bz
}
Expand Down Expand Up @@ -229,7 +229,7 @@ fn encode(addr: &Address) -> String {
match addr.protocol {
Protocol::Secp256k1 | Protocol::Actor | Protocol::BLS => {
let ingest = addr.to_bytes();
let mut bz = addr.payload();
let mut bz = addr.payload().to_vec();

// payload bytes followed by calculated checksum
bz.extend(checksum(&ingest));
Expand Down

0 comments on commit b41107d

Please sign in to comment.