2019-05-21 00:32:14 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::case_result::compare_result;
|
|
|
|
use bls::{compress_g2, hash_on_g2};
|
|
|
|
use serde_derive::Deserialize;
|
|
|
|
|
|
|
|
#[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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 06:15:52 +00:00
|
|
|
impl Case for BlsG2Compressed {
|
2019-05-22 08:13:22 +00:00
|
|
|
fn result(&self, _case_index: usize) -> Result<(), Error> {
|
2019-05-22 06:15:52 +00:00
|
|
|
// Convert message and domain to required types
|
|
|
|
let msg = hex::decode(&self.input.message[2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
let d = hex::decode(&self.input.domain[2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
let d = bytes_to_u64(&d);
|
|
|
|
|
|
|
|
// Calculate the point and convert it to compressed bytes
|
|
|
|
let mut point = hash_on_g2(&msg, d);
|
|
|
|
let point = compress_g2(&mut point);
|
|
|
|
|
|
|
|
// Convert the output to one set of bytes
|
|
|
|
let mut decoded = hex::decode(&self.output[0][2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
let mut decoded_y = hex::decode(&self.output[1][2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
decoded.append(&mut decoded_y);
|
|
|
|
|
|
|
|
compare_result::<Vec<u8>, Vec<u8>>(&Ok(point), &Some(decoded))
|
2019-05-21 00:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 02:46:22 +00:00
|
|
|
// Converts a vector to u64 (from big endian)
|
2019-05-21 00:32:14 +00:00
|
|
|
fn bytes_to_u64(array: &Vec<u8>) -> u64 {
|
|
|
|
let mut result: u64 = 0;
|
2019-05-21 02:46:22 +00:00
|
|
|
for (i, value) in array.iter().rev().enumerate() {
|
2019-05-21 00:32:14 +00:00
|
|
|
if i == 8 {
|
|
|
|
break;
|
|
|
|
}
|
2019-05-21 02:46:22 +00:00
|
|
|
result += u64::pow(2, i as u32 * 8) * (*value as u64);
|
2019-05-21 00:32:14 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|