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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::{SecretKey, Signature};
use blake2::Blake2b;
use bls12_381_plus::{G1Affine, G1Projective, Scalar};
use core::convert::TryFrom;
use digest::Digest;
use group::Curve;
use hmac_drbg::HmacDRBG;
use serde::{
de::{Error as DError, SeqAccess, Visitor},
ser::SerializeTuple,
Deserialize, Deserializer, Serialize, Serializer,
};
use signature_core::{constants::*, error::Error, lib::*, util::*};
use subtle::{Choice, CtOption};
use typenum::U64;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct BlindSignature {
pub(crate) sigma_1: G1Projective,
pub(crate) sigma_2: G1Projective,
pub(crate) m_tick: Scalar,
}
impl Serialize for BlindSignature {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = self.to_bytes();
let mut seq = s.serialize_tuple(bytes.len())?;
for b in &bytes {
seq.serialize_element(b)?;
}
seq.end()
}
}
impl<'de> Deserialize<'de> for BlindSignature {
fn deserialize<D>(d: D) -> Result<BlindSignature, D::Error>
where
D: Deserializer<'de>,
{
struct ArrayVisitor;
impl<'de> Visitor<'de> for ArrayVisitor {
type Value = BlindSignature;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "expected byte array")
}
fn visit_seq<A>(self, mut seq: A) -> Result<BlindSignature, A::Error>
where
A: SeqAccess<'de>,
{
let mut arr = [0u8; BlindSignature::BYTES];
#[allow(clippy::needless_range_loop)]
for i in 0..arr.len() {
arr[i] = seq
.next_element()?
.ok_or_else(|| DError::invalid_length(i, &self))?;
}
let res = BlindSignature::from_bytes(&arr);
if res.is_some().unwrap_u8() == 1 {
Ok(res.unwrap())
} else {
Err(DError::invalid_value(
serde::de::Unexpected::Bytes(&arr),
&self,
))
}
}
}
d.deserialize_tuple(BlindSignature::BYTES, ArrayVisitor)
}
}
impl Default for BlindSignature {
fn default() -> Self {
Self {
sigma_1: G1Projective::identity(),
sigma_2: G1Projective::identity(),
m_tick: Scalar::zero(),
}
}
}
impl BlindSignature {
pub const BYTES: usize = 128;
pub fn new(
commitment: Commitment,
sk: &SecretKey,
msgs: &[(usize, Message)],
) -> Result<Self, Error> {
if sk.y.len() < msgs.len() {
return Err(Error::new(1, "secret key is not big enough"));
}
if sk.is_invalid() {
return Err(Error::new(1, "invalid secret key"));
}
let mut hasher = Blake2b::new();
hasher.update(
(G1Projective::generator() * sk.w)
.to_affine()
.to_uncompressed(),
);
hasher.update(
(G1Projective::generator() * sk.x)
.to_affine()
.to_uncompressed(),
);
for y in &sk.y {
hasher.update(
(G1Projective::generator() * y)
.to_affine()
.to_uncompressed(),
);
}
for (_, m) in msgs.iter() {
hasher.update(m.to_bytes())
}
let nonce = hasher.finalize_reset();
hasher.update(sk.w.to_bytes());
hasher.update(sk.x.to_bytes());
for y in &sk.y {
hasher.update(y.to_bytes());
}
let entropy = hasher.finalize();
let mut drbg = HmacDRBG::<Blake2b>::new(&entropy[..], &nonce[..], &[]);
let u = Scalar::from_bytes_wide(
&<[u8; 64]>::try_from(&drbg.generate::<U64>(Some(&[1u8]))[..]).unwrap(),
);
let m_tick = Scalar::from_bytes_wide(
&<[u8; 64]>::try_from(&drbg.generate::<U64>(Some(&[2u8]))[..]).unwrap(),
);
let sigma_1 = G1Projective::generator() * u;
let mut exp = sk.x + m_tick * sk.w;
for (i, msg) in msgs {
exp += sk.y[*i] * msg.0;
}
let mut sigma_2 = (G1Projective::generator() * exp) + commitment.0;
sigma_2 *= u;
Ok(Self {
sigma_1,
sigma_2,
m_tick,
})
}
pub fn to_unblinded(self, blinding: SignatureBlinding) -> Signature {
Signature {
sigma_1: self.sigma_1,
sigma_2: self.sigma_2 - (self.sigma_1 * blinding.0),
m_tick: self.m_tick,
}
}
pub fn to_bytes(&self) -> [u8; Self::BYTES] {
let mut bytes = [0u8; Self::BYTES];
bytes[..48].copy_from_slice(&self.sigma_1.to_affine().to_compressed());
bytes[48..96].copy_from_slice(&self.sigma_2.to_affine().to_compressed());
#[allow(clippy::out_of_bounds_indexing)]
bytes[96..128].copy_from_slice(&scalar_to_bytes(self.m_tick));
bytes
}
pub fn from_bytes(data: &[u8; Self::BYTES]) -> CtOption<Self> {
let s1 = G1Affine::from_compressed(slicer!(data, 0, 48, COMMITMENT_BYTES))
.map(G1Projective::from);
let s2 = G1Affine::from_compressed(slicer!(data, 48, 96, COMMITMENT_BYTES))
.map(G1Projective::from);
#[allow(clippy::out_of_bounds_indexing)]
let m_t = scalar_from_bytes(slicer!(data, 96, 128, FIELD_BYTES));
s1.and_then(|sigma_1| {
s2.and_then(|sigma_2| {
m_t.and_then(|m_tick| {
CtOption::new(
BlindSignature {
sigma_1,
sigma_2,
m_tick,
},
Choice::from(1),
)
})
})
})
}
}