Begin working on bls test

This commit is contained in:
Kirk Baird 2019-05-21 10:32:14 +10:00
parent 63ee179def
commit 4f6447a62b
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
9 changed files with 211 additions and 1 deletions

View File

@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
bls-aggregates = { git = "https://github.com/sigp/signature-schemes", tag = "0.6.1" }
bls-aggregates = { git = "https://github.com/sigp/signature-schemes", branch = "secret-key-serialization" }
cached_tree_hash = { path = "../cached_tree_hash" }
hashing = { path = "../hashing" }
hex = "0.3"

View File

@ -25,4 +25,12 @@ impl FakeAggregatePublicKey {
pub fn add(&mut self, _public_key: &PublicKey) {
// No nothing.
}
pub fn as_raw(&self) -> &FakeAggregatePublicKey {
&self
}
pub fn as_bytes(&self) -> Vec<u8> {
self.bytes.clone()
}
}

View File

@ -8,6 +8,7 @@ mod secret_key;
pub use crate::keypair::Keypair;
pub use crate::secret_key::SecretKey;
pub use bls_aggregates::{compress_g2, hash_on_g2};
#[cfg(feature = "fake_crypto")]
mod fake_aggregate_public_key;

View File

@ -4,9 +4,13 @@ use yaml_rust::YamlLoader;
mod ssz_generic;
mod ssz_static;
mod bls_aggregate_pubkeys;
mod bls_aggregate_sigs;
pub use ssz_generic::*;
pub use ssz_static::*;
pub use bls_aggregate_pubkeys::*;
pub use bls_aggregate_sigs::*;
#[derive(Debug)]
pub struct Cases<T> {

View File

@ -0,0 +1,54 @@
use super::*;
use crate::case_result::compare_result;
use bls::{AggregatePublicKey, PublicKey};
use ethereum_types::{U128, U256};
use serde_derive::Deserialize;
use ssz::Decode;
use std::fmt::Debug;
use types::EthSpec;
#[derive(Debug, Clone, Deserialize)]
pub struct BlsAggregatePubkeys {
pub input: Vec<String>,
pub output: String,
}
impl YamlDecode for BlsAggregatePubkeys {
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
}
}
impl EfTest for Cases<BlsAggregatePubkeys> {
fn test_results<E: EthSpec>(&self) -> Vec<CaseResult> {
self.test_cases
.iter()
.enumerate()
.map(|(i, tc)| {
let result = bls_add_aggregates::<AggregatePublicKey>(&tc.input, &tc.output);
CaseResult::new(i, tc, result)
})
.collect()
}
}
/// Execute a `aggregate_pubkeys` test case.
fn bls_add_aggregates<T>(
inputs: &[String],
output: &String,
) -> Result<(), Error> {
let mut aggregate_pubkey = AggregatePublicKey::new();
for key_str in inputs {
let key = hex::decode(&key_str[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let key = PublicKey::from_bytes(&key).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
aggregate_pubkey.add(&key);
}
let output_bytes = Some(hex::decode(&output[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?);
let aggregate_pubkey = Ok(aggregate_pubkey.as_raw().as_bytes());
compare_result::<Vec<u8>, Vec<u8>>(&aggregate_pubkey, &output_bytes)
}

View File

@ -0,0 +1,54 @@
use super::*;
use crate::case_result::compare_result;
use bls::{AggregateSignature, Signature};
use ethereum_types::{U128, U256};
use serde_derive::Deserialize;
use ssz::Decode;
use std::fmt::Debug;
use types::EthSpec;
#[derive(Debug, Clone, Deserialize)]
pub struct BlsAggregateSigs {
pub input: Vec<String>,
pub output: String,
}
impl YamlDecode for BlsAggregateSigs {
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
}
}
impl EfTest for Cases<BlsAggregateSigs> {
fn test_results<E: EthSpec>(&self) -> Vec<CaseResult> {
self.test_cases
.iter()
.enumerate()
.map(|(i, tc)| {
let result = bls_add_aggregates::<AggregateSignature>(&tc.input, &tc.output);
CaseResult::new(i, tc, result)
})
.collect()
}
}
/// Execute a `aggregate_sigs` test case.
fn bls_add_aggregates<T>(
inputs: &[String],
output: &String,
) -> Result<(), Error> {
let mut aggregate_signature = AggregateSignature::new();
for key_str in inputs {
let sig = hex::decode(&key_str[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let sig = Signature::from_bytes(&sig).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
aggregate_signature.add(&sig);
}
let output_bytes = Some(hex::decode(&output[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?);
let aggregate_signature = Ok(aggregate_signature.as_bytes());
compare_result::<Vec<u8>, Vec<u8>>(&aggregate_signature, &output_bytes)
}

View File

@ -0,0 +1,75 @@
use super::*;
use crate::case_result::compare_result;
use bls::{compress_g2, hash_on_g2};
use ethereum_types::{U128, U256};
use serde_derive::Deserialize;
use ssz::Decode;
use std::fmt::Debug;
use types::EthSpec;
#[derive(Debug, Clone, Deserialize)]
pub struct BlsG2CompressedInput {
pub message: String,
pub domain: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BlsG2Compressed {
pub input: BlsG2CompressedInput,
pub output: Vec<String>,
}
impl YamlDecode for BlsG2Compressed {
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
}
}
impl EfTest for Cases<BlsG2Compressed> {
fn test_results<E: EthSpec>(&self) -> Vec<CaseResult> {
self.test_cases
.iter()
.enumerate()
.map(|(i, tc)| {
let result = compressed_hash(&tc.input.message, &tc.input.domain, &tc.output);
CaseResult::new(i, tc, result)
})
.collect()
}
}
/// Execute a `compressed hash to g2` test case.
fn compressed_hash<T>(
message: &String,
domain: &String,
output: &Vec<String>,
) -> Result<(), Error> {
let msg = hex::decode(&message[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let d = hex::decode(&domain[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let d = bytes_to_u64(&d);
let point = hash_on_g2
let mut output = hex::decode(&output[0][2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let output_y = hex::decode(&output[1][2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
output.append(&output_y);
let point = hash_on_g2(&msg, d);
let point = compress_g2(&point);
compare_result::<Vec<u8>, Vec<u8>>(Ok(point), Some(output))
}
// Converts a vector to u64 (from little endian)
fn bytes_to_u64(array: &Vec<u8>) -> u64 {
let mut result: u64 = 0;
for (i, value) in array.iter().enumerate() {
if i == 8 {
break;
}
result += u64::pow(2, i * 8) * *value;
}
result
}

View File

@ -35,6 +35,9 @@ impl Doc {
("ssz", "uint", _) => run_test::<SszGeneric, MainnetEthSpec>(&self.yaml),
("ssz", "static", "minimal") => run_test::<SszStatic, MinimalEthSpec>(&self.yaml),
("ssz", "static", "mainnet") => run_test::<SszStatic, MainnetEthSpec>(&self.yaml),
("bls", "aggregate_pubkeys", "mainnet") => run_test::<BlsAggregatePubkeys, MainnetEthSpec>(&self.yaml),
("bls", "aggregate_sigs", "mainnet") => run_test::<BlsAggregateSigs, MainnetEthSpec>(&self.yaml),
(runner, handler, config) => panic!(
"No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"",
runner, handler, config

View File

@ -26,6 +26,7 @@ fn yaml_files_in_test_dir(dir: &str) -> Vec<PathBuf> {
.collect()
}
/*
#[test]
fn ssz_generic() {
yaml_files_in_test_dir("ssz_generic")
@ -43,3 +44,13 @@ fn ssz_static() {
Doc::assert_tests_pass(file);
});
}
*/
#[test]
fn bls() {
yaml_files_in_test_dir("bls")
.into_par_iter()
.for_each(|file| {
Doc::assert_tests_pass(file);
});
}