2019-05-21 02:46:22 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::case_result::compare_result;
|
|
|
|
use bls::hash_on_g2;
|
|
|
|
use serde_derive::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct BlsG2UncompressedInput {
|
|
|
|
pub message: String,
|
|
|
|
pub domain: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct BlsG2Uncompressed {
|
|
|
|
pub input: BlsG2UncompressedInput,
|
|
|
|
pub output: Vec<Vec<String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl YamlDecode for BlsG2Uncompressed {
|
2019-06-10 15:01:25 +00:00
|
|
|
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
|
|
|
|
Ok(serde_yaml::from_str(yaml).unwrap())
|
2019-05-21 02:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 06:15:52 +00:00
|
|
|
impl Case for BlsG2Uncompressed {
|
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);
|
2019-05-21 02:46:22 +00:00
|
|
|
|
2019-05-22 06:15:52 +00:00
|
|
|
// Calculate the point and convert it to compressed bytes
|
|
|
|
let point = hash_on_g2(&msg, d);
|
|
|
|
let mut point_bytes = [0 as u8; 288];
|
|
|
|
point.getpx().geta().tobytearray(&mut point_bytes, 0);
|
|
|
|
point.getpx().getb().tobytearray(&mut point_bytes, 48);
|
|
|
|
point.getpy().geta().tobytearray(&mut point_bytes, 96);
|
|
|
|
point.getpy().getb().tobytearray(&mut point_bytes, 144);
|
|
|
|
point.getpz().geta().tobytearray(&mut point_bytes, 192);
|
|
|
|
point.getpz().getb().tobytearray(&mut point_bytes, 240);
|
2019-05-21 02:46:22 +00:00
|
|
|
|
2019-05-22 06:15:52 +00:00
|
|
|
// Convert the output to one set of bytes (x.a, x.b, y.a, y.b, z.a, z.b)
|
|
|
|
let mut decoded: Vec<u8> = vec![];
|
|
|
|
for coordinate in &self.output {
|
|
|
|
let mut decoded_part = hex::decode(&coordinate[0][2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
decoded.append(&mut decoded_part);
|
|
|
|
decoded_part = hex::decode(&coordinate[1][2..])
|
|
|
|
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
|
|
|
decoded.append(&mut decoded_part);
|
|
|
|
}
|
2019-05-21 02:46:22 +00:00
|
|
|
|
2019-05-22 06:15:52 +00:00
|
|
|
compare_result::<Vec<u8>, Vec<u8>>(&Ok(point_bytes.to_vec()), &Some(decoded))
|
2019-05-21 02:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a vector to u64 (from big endian)
|
2019-06-10 15:01:25 +00:00
|
|
|
fn bytes_to_u64(array: &[u8]) -> u64 {
|
2019-05-21 02:46:22 +00:00
|
|
|
let mut result: u64 = 0;
|
|
|
|
for (i, value) in array.iter().rev().enumerate() {
|
|
|
|
if i == 8 {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-10 15:01:25 +00:00
|
|
|
result += u64::pow(2, i as u32 * 8) * u64::from(*value);
|
2019-05-21 02:46:22 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|