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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use key::{PrivateKey, PublicKey};
use blake2::{Blake2b, Blake2s};
use curve25519_dalek::edwards::Identity;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use digest::Digest;
use rand::OsRng;
static KEY0: &'static [u8] = b"rustfujisakisuzukihash0";
static KEY1: &'static [u8] = b"rustfujisakisuzukihash1";
static KEY2: &'static [u8] = b"rustfujisakisuzukihash2";
#[derive(Debug, Eq, PartialEq)]
pub struct Signature {
aa1: RistrettoPoint,
cs: Vec<Scalar>,
zs: Vec<Scalar>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Tag {
pub pubkeys: Vec<PublicKey>,
pub issue: Vec<u8>,
}
impl Tag {
fn hash_self<T: Digest>(&self, mut h: T) -> T {
for pubkey in &self.pubkeys {
let pubkey_c = pubkey.0.compress();
h.input(pubkey_c.as_bytes());
}
h.input(&*self.issue);
h
}
fn hash0(&self) -> Blake2s {
let h = Blake2s::new_keyed(KEY0);
self.hash_self(h)
}
fn hash1(&self) -> Blake2s {
let h = Blake2s::new_keyed(KEY1);
self.hash_self(h)
}
fn hash2(&self) -> Blake2b {
let h = Blake2b::new_keyed(KEY2);
self.hash_self(h)
}
}
pub(crate) fn compute_sigma(msg: &[u8], tag: &Tag, sig: &Signature)
-> (RistrettoPoint, Vec<RistrettoPoint>) {
let ring_size = tag.pubkeys.len();
let aa1 = sig.aa1;
let aa0 = {
let mut d = tag.hash1();
d.input(msg);
RistrettoPoint::from_hash(d)
};
let sigma: Vec<RistrettoPoint> = {
let mut vals = Vec::new();
for i in 0..ring_size {
let s = Scalar::from_u64((i+1) as u64);
let aa1i = &s * &aa1;
vals.push(&aa0 + &aa1i);
}
vals
};
(aa0, sigma)
}
pub fn sign(msg: &[u8], tag: &Tag, privkey: &PrivateKey) -> Signature {
let ring_size = tag.pubkeys.len();
let mut privkey_idx: Option<usize> = None;
for (i, pubkey) in tag.pubkeys.iter().enumerate() {
if pubkey.0 == privkey.1 {
privkey_idx = Some(i);
}
}
let privkey_idx = privkey_idx.expect("Could not find private key position in ring");
let h = RistrettoPoint::from_hash(tag.hash0());
let mut sigma: Vec<RistrettoPoint> = vec![RistrettoPoint::identity(); ring_size];
sigma[privkey_idx] = &privkey.0 * &h;
let aa0 = {
let mut d = tag.hash1();
d.input(msg);
RistrettoPoint::from_hash(d)
};
let aa1 = {
let t = &sigma[privkey_idx] - &aa0;
let s = Scalar::from_u64((privkey_idx+1) as u64);
let sinv = s.invert();
&sinv * &t
};
for i in (0..ring_size).filter(|&j| j != privkey_idx) {
let s = Scalar::from_u64((i+1) as u64);
let aa1i = &s * &aa1;
sigma[i] = &aa0 + &aa1i;
}
let mut c: Vec<Scalar> = vec![Scalar::zero(); ring_size];
let mut z: Vec<Scalar> = vec![Scalar::zero(); ring_size];
let mut a: Vec<RistrettoPoint> = vec![RistrettoPoint::identity(); ring_size];
let mut b: Vec<RistrettoPoint> = vec![RistrettoPoint::identity(); ring_size];
let mut csprng = OsRng::new().expect("Could not instantiate CSPRNG");
let w = Scalar::random(&mut csprng);
a[privkey_idx] = &w * &RISTRETTO_BASEPOINT_POINT;
b[privkey_idx] = &w * &h;
for i in (0..ring_size).filter(|&j| j != privkey_idx) {
c[i] = Scalar::random(&mut csprng);
z[i] = Scalar::random(&mut csprng);
a[i] = {
let gzi = &z[i] * &RISTRETTO_BASEPOINT_POINT;
let yici = &c[i] * &tag.pubkeys[i].0;
&gzi + &yici
};
b[i] = {
let hzi = &z[i] * &h;
let sici = &c[i] * &sigma[i];
&hzi + &sici
};
}
let cc = {
let mut d = tag.hash2();
let aa0c = aa0.compress();
let aa1c = aa1.compress();
d.input(aa0c.as_bytes());
d.input(aa1c.as_bytes());
for ai in a.iter() {
let aic = ai.compress();
d.input(aic.as_bytes());
}
for bi in b.iter() {
let bic = bi.compress();
d.input(bic.as_bytes());
}
Scalar::from_hash(d)
};
c[privkey_idx] = {
let sum = c.iter()
.enumerate()
.filter(|&(i, _)| i != privkey_idx)
.fold(Scalar::zero(), |acc, (_, v)| &acc + &v);
&cc - &sum
};
z[privkey_idx] = {
let cixi = &c[privkey_idx] * &privkey.0;
&w - &cixi
};
Signature {
aa1: aa1,
cs: c,
zs: z,
}
}
pub fn verify(msg: &[u8], tag: &Tag, sig: &Signature) -> bool {
let c = &sig.cs;
let z = &sig.zs;
let aa1 = sig.aa1;
let h = RistrettoPoint::from_hash(tag.hash0());
let (aa0, sigma) = compute_sigma(msg, tag, sig);
let a: Vec<RistrettoPoint> = {
let mut vals = Vec::new();
for (zi, (pubi, ci)) in z.iter().zip(tag.pubkeys.iter().zip(c.iter())) {
let gzi = zi * &RISTRETTO_BASEPOINT_POINT;
let yici = ci * &pubi.0;
vals.push(&gzi + &yici);
}
vals
};
let b: Vec<RistrettoPoint> = {
let mut vals = Vec::new();
for (zi, (sigmai, ci)) in z.iter().zip(sigma.iter().zip(c.iter())) {
let hzi = zi * &h;
let sici = ci * sigmai;
vals.push(&hzi + &sici)
}
vals
};
let cc = {
let mut d = tag.hash2();
let aa0c = aa0.compress();
let aa1c = aa1.compress();
d.input(aa0c.as_bytes());
d.input(aa1c.as_bytes());
for ai in a.iter() {
let aic = ai.compress();
d.input(aic.as_bytes());
}
for bi in b.iter() {
let bic = bi.compress();
d.input(bic.as_bytes());
}
Scalar::from_hash(d)
};
let sum = c.iter().fold(Scalar::zero(), |acc, v| &acc + &v);
sum == cc
}
#[cfg(test)]
mod test {
use key::KeyPair;
use super::{sign, verify};
use test_utils::{remove_privkey, setup, Context};
use rand::{self, Rng};
#[test]
fn test_sig_correctness() {
let Context { msg, tag, mut keypairs } = setup(1);
let privkey = remove_privkey(&mut keypairs);
let sig = sign(&msg, &tag, &privkey);
assert!(verify(&msg, &tag, &sig));
}
#[test]
fn test_sig_nondeterminism() {
let Context { msg, tag, mut keypairs } = setup(1);
let privkey = remove_privkey(&mut keypairs);
let sig1 = sign(&msg, &tag, &privkey);
let sig2 = sign(&msg, &tag, &privkey);
assert!(sig1 != sig2);
}
#[test]
fn test_sig_msg_linkage() {
let mut rng = rand::thread_rng();
let Context { msg, tag, mut keypairs } = setup(1);
let privkey = remove_privkey(&mut keypairs);
let sig = sign(&msg, &tag, &privkey);
let mut bad_msg = msg.clone();
let byte_idx = rng.gen_range(0, msg.len());
bad_msg[byte_idx] = !bad_msg[byte_idx];
assert!(!verify(&*bad_msg, &tag, &sig));
}
#[test]
fn test_sig_tag_linkage() {
let mut rng = rand::thread_rng();
let Context { msg, tag, mut keypairs } = setup(1);
let privkey = remove_privkey(&mut keypairs);
let sig = sign(&msg, &tag, &privkey);
let mut bad_tag = tag.clone();
let new_pubkey = KeyPair::generate().pubkey;
let pubkey_idx = rng.gen_range(0, tag.pubkeys.len());
bad_tag.pubkeys[pubkey_idx] = new_pubkey;
assert!(!verify(&msg, &bad_tag, &sig));
let mut bad_tag = tag.clone();
let byte_idx = rng.gen_range(0, tag.issue.len());
bad_tag.issue[byte_idx] = !bad_tag.issue[byte_idx];
assert!(!verify(&msg, &bad_tag, &sig));
}
}