Merge Rust 2018 updates

This commit is contained in:
Paul Hauner
2018-12-24 10:03:56 +11:00
28 changed files with 82 additions and 53 deletions

View File

@@ -2,6 +2,7 @@
name = "bls"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
bls-aggregates = { git = "https://github.com/sigp/signature-schemes" }

View File

@@ -5,8 +5,8 @@ extern crate ssz;
mod aggregate_signature;
mod signature;
pub use aggregate_signature::AggregateSignature;
pub use signature::Signature;
pub use crate::aggregate_signature::AggregateSignature;
pub use crate::signature::Signature;
pub use self::bls_aggregates::AggregatePublicKey;
pub use self::bls_aggregates::Keypair;
@@ -15,16 +15,24 @@ pub use self::bls_aggregates::SecretKey;
pub const BLS_AGG_SIG_BYTE_SIZE: usize = 97;
use hashing::proof_of_possession_hash;
use hashing::canonical_hash;
use std::default::Default;
fn extend_if_needed(hash: &mut Vec<u8>) {
// NOTE: bls_aggregates crate demands 48 bytes, this may be removed as we get closer to production
hash.resize(48, Default::default())
}
/// For some signature and public key, ensure that the signature message was the public key and it
/// was signed by the secret key that corresponds to that public key.
pub fn verify_proof_of_possession(sig: &Signature, pubkey: &PublicKey) -> bool {
let hash = proof_of_possession_hash(&pubkey.as_bytes());
let mut hash = canonical_hash(&pubkey.as_bytes());
extend_if_needed(&mut hash);
sig.verify_hashed(&hash, &pubkey)
}
pub fn create_proof_of_possession(keypair: &Keypair) -> Signature {
let hash = proof_of_possession_hash(&keypair.pk.as_bytes());
let mut hash = canonical_hash(&keypair.pk.as_bytes());
extend_if_needed(&mut hash);
Signature::new_hashed(&hash, &keypair.sk)
}

View File

@@ -2,7 +2,8 @@
name = "boolean-bitfield"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
ssz = { path = "../ssz" }
bit-vec = "0.5.0"
bit-vec = "0.5.0"

View File

@@ -2,6 +2,7 @@
name = "hashing"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
blake2-rfc = "0.2.18"
tiny-keccak = "1.4.2"

View File

@@ -1,17 +1,30 @@
extern crate blake2_rfc;
extern crate tiny_keccak;
use self::blake2_rfc::blake2b::blake2b;
use tiny_keccak::Keccak;
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
let result = blake2b(64, &[], input);
result.as_bytes()[0..32].to_vec()
let mut keccak = Keccak::new_keccak256();
keccak.update(input);
let mut result = vec![0; 32];
keccak.finalize(result.as_mut_slice());
result
}
pub fn proof_of_possession_hash(input: &[u8]) -> Vec<u8> {
let result = blake2b(64, &[], input);
let mut hash = result.as_bytes()[32..64].to_vec();
// TODO: this padding is not part of the spec, it is required otherwise Milagro will panic.
// We should either drop the padding or ensure the padding is in the spec.
hash.append(&mut vec![0; 18]);
hash
#[cfg(test)]
mod tests {
use super::*;
use std::convert::From;
#[test]
fn test_hashing() {
let input: Vec<u8> = From::from("hello");
let output = canonical_hash(input.as_ref());
let expected = &[
0x1c, 0x8a, 0xff, 0x95, 0x06, 0x85, 0xc2, 0xed, 0x4b, 0xc3, 0x17, 0x4f, 0x34, 0x72,
0x28, 0x7b, 0x56, 0xd9, 0x51, 0x7b, 0x9c, 0x94, 0x81, 0x27, 0x31, 0x9a, 0x09, 0xa7,
0xa3, 0x6d, 0xea, 0xc8,
];
assert_eq!(expected, output.as_slice());
}
}

View File

@@ -2,5 +2,6 @@
name = "honey-badger-split"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]

View File

@@ -2,5 +2,6 @@
name = "slot-clock"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]

View File

@@ -2,6 +2,7 @@
name = "ssz"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
bytes = "0.4.9"

View File

@@ -16,8 +16,8 @@ pub mod encode;
mod impl_decode;
mod impl_encode;
pub use decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
pub use encode::{Encodable, SszStream};
pub use crate::decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
pub use crate::encode::{Encodable, SszStream};
pub const LENGTH_BYTES: usize = 4;
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);

View File

@@ -2,6 +2,7 @@
name = "vec_shuffle"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
hashing = { path = "../hashing" }

View File

@@ -47,7 +47,10 @@ mod tests {
use std::fs::File;
use std::io::prelude::*;
// TODO: update test vectors to use keccak instead of blake.
// https://github.com/sigp/lighthouse/issues/121
#[test]
#[should_panic]
fn test_shuffling() {
let mut file = File::open("./src/specs/shuffle_test_vectors.yaml").unwrap();
let mut yaml_str = String::new();

View File

@@ -87,15 +87,4 @@ mod tests {
x = int_from_byte_slice(&[0x8f, 0xbb, 0xc7], 0);
assert_eq!(x, 9419719);
}
#[test]
fn test_shuffling_hash_fn() {
let digest = canonical_hash(&canonical_hash(&"4kn4driuctg8".as_bytes())); // double-hash is intentional
let expected = [
103, 21, 99, 143, 60, 75, 116, 81, 248, 175, 190, 114, 54, 65, 23, 8, 3, 116, 160, 178,
7, 75, 63, 47, 180, 239, 191, 247, 57, 194, 144, 88,
];
assert_eq!(digest.len(), expected.len());
assert_eq!(digest, expected)
}
}