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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use bls12_381_plus::{ExpandMsgXmd, G1Projective};
use core::convert::TryFrom;
use group::Curve;
use signature_bls::{PublicKey, SecretKey};
use signature_core::lib::*;
const DATA_SIZE: usize = 201;
const DST: &[u8] = b"BLS12381G1_XMD:BLAKE2B_SSWU_RO_BBS+_SIGNATURES:1_0_0";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MessageGenerators {
pub(crate) h0: G1Projective,
length: usize,
state: [u8; DATA_SIZE],
}
pub struct MessageGeneratorIter {
index: usize,
length: usize,
state: [u8; DATA_SIZE],
}
impl Default for MessageGenerators {
fn default() -> Self {
Self {
h0: G1Projective::identity(),
length: 0,
state: [0u8; DATA_SIZE],
}
}
}
impl Iterator for MessageGeneratorIter {
type Item = G1Projective;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.length {
return None;
}
self.index += 1;
self.state[193..197].copy_from_slice(&(self.index as u32).to_be_bytes());
Some(G1Projective::hash::<ExpandMsgXmd<blake2::Blake2b>>(
&self.state[..],
DST,
))
}
}
impl MessageGenerators {
pub const GENERATOR_BYTES: usize = 48;
pub fn len(&self) -> usize {
self.length
}
pub fn is_empty(&self) -> bool {
self.length == 0
}
pub fn get(&self, index: usize) -> G1Projective {
let mut state = self.state;
state[193..197].copy_from_slice(&((index + 1) as u32).to_be_bytes());
G1Projective::hash::<ExpandMsgXmd<blake2::Blake2b>>(&state[..], DST)
}
pub fn from_secret_key(sk: &SecretKey, length: usize) -> Self {
Self::from_public_key(PublicKey::from(sk), length)
}
pub fn from_public_key(pk: PublicKey, length: usize) -> Self {
let count = (length as u32).to_be_bytes();
let mut state = [0u8; DATA_SIZE];
state[..192].copy_from_slice(&pk.0.to_affine().to_uncompressed());
state[197..201].copy_from_slice(&count);
let h0 = G1Projective::hash::<ExpandMsgXmd<blake2::Blake2b>>(&state[..], DST);
Self { h0, length, state }
}
pub fn to_bytes(&self) -> [u8; 205] {
let data: [u8; 4] = (self.length as u32).to_be_bytes();
let mut output = [0u8; 205];
output[..4].copy_from_slice(&data);
output[4..].copy_from_slice(&self.state[..]);
output
}
pub fn from_bytes(bytes: &[u8; 205]) -> Self {
let length = u32::from_be_bytes(<[u8; 4]>::try_from(&bytes[..4]).unwrap()) as usize;
let mut state = [0u8; 201];
state.copy_from_slice(&bytes[4..]);
state[193] = 0;
state[194] = 0;
state[195] = 0;
state[196] = 0;
let h0 = G1Projective::hash::<ExpandMsgXmd<blake2::Blake2b>>(&state[..], DST);
Self { h0, length, state }
}
pub fn iter(&self) -> MessageGeneratorIter {
MessageGeneratorIter {
index: 0,
state: self.state,
length: self.length,
}
}
}