forked from darkrenaissance/darkfi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbls_extensions.rs
81 lines (69 loc) · 2.3 KB
/
bls_extensions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use bls12_381 as bls;
use std::io;
use crate::error::{Error, Result};
use crate::serial::{Decodable, Encodable, ReadExt, WriteExt};
macro_rules! from_slice {
($data:expr, $len:literal) => {{
let mut array = [0; $len];
// panics if not enough data
let bytes = &$data[..array.len()];
array.copy_from_slice(bytes);
array
}};
}
pub trait BlsStringConversion {
fn to_string(&self) -> String;
fn from_string(object: &str) -> Self;
}
impl BlsStringConversion for bls::Scalar {
fn to_string(&self) -> String {
let mut bytes = self.to_bytes();
bytes.reverse();
hex::encode(bytes)
}
fn from_string(object: &str) -> Self {
let mut bytes = from_slice!(&hex::decode(object).unwrap(), 32);
bytes.reverse();
bls::Scalar::from_bytes(&bytes).unwrap()
}
}
macro_rules! serialization_bls {
($type:ty, $to_x:ident, $from_x:ident, $size:literal) => {
impl Encodable for $type {
fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
let data = self.$to_x();
assert_eq!(data.len(), $size);
s.write_slice(&data)?;
Ok(data.len())
}
}
impl Decodable for $type {
fn decode<D: io::Read>(mut d: D) -> Result<Self> {
let mut slice = [0u8; $size];
d.read_slice(&mut slice)?;
let result = Self::$from_x(&slice);
if bool::from(result.is_none()) {
return Err(Error::ParseFailed("$t conversion from slice failed"));
}
Ok(result.unwrap())
}
}
};
}
serialization_bls!(bls::Scalar, to_bytes, from_bytes, 32);
macro_rules! make_serialize_deserialize_test {
($name:ident, $type:ty, $default_func:ident) => {
#[test]
fn $name() {
let point = <$type>::$default_func();
let mut data: Vec<u8> = vec![];
let result = point.encode(&mut data);
assert!(result.is_ok());
let point2 = <$type>::decode(&data[..]);
assert!(point2.is_ok());
let point2 = point2.unwrap();
assert_eq!(point, point2);
}
};
}
make_serialize_deserialize_test!(serial_test_scalar, bls::Scalar, zero);