-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
aleo.rs
494 lines (417 loc) · 16.7 KB
/
aleo.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkVM library.
// The snarkVM library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The snarkVM library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the snarkVM library. If not, see <https://www.gnu.org/licenses/>.
use crate::{
crypto_hash::{PoseidonCryptoHash, PoseidonDefaultParametersField},
hash_to_curve::hash_to_curve,
CryptoHash,
SignatureError,
SignatureScheme,
SignatureSchemeOperations,
};
use snarkvm_curves::{
templates::twisted_edwards_extended::{Affine as TEAffine, Projective as TEProjective},
AffineCurve,
Group,
ProjectiveCurve,
TwistedEdwardsParameters,
};
use snarkvm_fields::{ConstraintFieldError, Field, FieldParameters, PrimeField, ToConstraintField};
use snarkvm_utilities::{
fmt,
io::{Read, Result as IoResult, Write},
ops::Mul,
rand::UniformRand,
serialize::*,
str::FromStr,
FromBits,
FromBytes,
FromBytesDeserializer,
ToBits,
ToBytes,
ToBytesSerializer,
};
use anyhow::Result;
use itertools::Itertools;
use rand::{CryptoRng, Rng};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Derivative)]
#[derivative(
Copy(bound = "TE: TwistedEdwardsParameters"),
Clone(bound = "TE: TwistedEdwardsParameters"),
PartialEq(bound = "TE: TwistedEdwardsParameters"),
Eq(bound = "TE: TwistedEdwardsParameters"),
Debug(bound = "TE: TwistedEdwardsParameters"),
Default(bound = "TE: TwistedEdwardsParameters")
)]
pub struct AleoSignature<TE: TwistedEdwardsParameters> {
pub prover_response: TE::ScalarField,
pub verifier_challenge: TE::ScalarField,
root_public_key: TE::BaseField,
root_randomizer: TE::BaseField,
}
impl<TE: TwistedEdwardsParameters> AleoSignature<TE> {
#[inline]
pub fn size() -> usize {
2 * TE::ScalarField::SERIALIZED_SIZE + 2 * TE::BaseField::SERIALIZED_SIZE
}
#[inline]
pub fn root_public_key(&self) -> Result<TEAffine<TE>> {
if let Some(element) = TEAffine::<TE>::from_x_coordinate(self.root_public_key, true) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
if let Some(element) = TEAffine::<TE>::from_x_coordinate(self.root_public_key, false) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
Err(SignatureError::Message("Failed to read the signature root public key".into()).into())
}
#[inline]
pub fn root_randomizer(&self) -> Result<TEAffine<TE>> {
if let Some(element) = TEAffine::<TE>::from_x_coordinate(self.root_randomizer, true) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
if let Some(element) = TEAffine::<TE>::from_x_coordinate(self.root_randomizer, false) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
Err(SignatureError::Message("Failed to read the signature root randomizer".into()).into())
}
}
impl<TE: TwistedEdwardsParameters> FromBytes for AleoSignature<TE> {
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let prover_response = TE::ScalarField::read_le(&mut reader)?;
let verifier_challenge = TE::ScalarField::read_le(&mut reader)?;
let root_public_key = TE::BaseField::read_le(&mut reader)?;
let root_randomizer = TE::BaseField::read_le(&mut reader)?;
Ok(Self {
prover_response,
verifier_challenge,
root_public_key,
root_randomizer,
})
}
}
impl<TE: TwistedEdwardsParameters> ToBytes for AleoSignature<TE> {
#[inline]
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.prover_response.write_le(&mut writer)?;
self.verifier_challenge.write_le(&mut writer)?;
self.root_public_key.write_le(&mut writer)?;
self.root_randomizer.write_le(&mut writer)
}
}
impl<TE: TwistedEdwardsParameters> FromStr for AleoSignature<TE> {
type Err = anyhow::Error;
#[inline]
fn from_str(signature_hex: &str) -> Result<Self, Self::Err> {
Self::from_bytes_le(&hex::decode(signature_hex)?)
}
}
impl<TE: TwistedEdwardsParameters> fmt::Display for AleoSignature<TE> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let signature_hex = hex::encode(self.to_bytes_le().expect("Failed to convert signature to bytes"));
write!(f, "{}", signature_hex)
}
}
impl<TE: TwistedEdwardsParameters> Serialize for AleoSignature<TE> {
#[inline]
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match serializer.is_human_readable() {
true => serializer.collect_str(self),
false => ToBytesSerializer::serialize(self, serializer),
}
}
}
impl<'de, TE: TwistedEdwardsParameters> Deserialize<'de> for AleoSignature<TE> {
#[inline]
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
match deserializer.is_human_readable() {
true => {
let s: String = Deserialize::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(de::Error::custom)
}
false => FromBytesDeserializer::<Self>::deserialize(deserializer, "signature", Self::size()),
}
}
}
#[derive(Derivative)]
#[derivative(
Clone(bound = "TE: TwistedEdwardsParameters"),
Debug(bound = "TE: TwistedEdwardsParameters"),
PartialEq(bound = "TE: TwistedEdwardsParameters"),
Eq(bound = "TE: TwistedEdwardsParameters")
)]
pub struct AleoSignatureScheme<TE: TwistedEdwardsParameters>
where
TE::BaseField: PoseidonDefaultParametersField,
{
g_bases: Vec<TEProjective<TE>>,
crypto_hash: PoseidonCryptoHash<TE::BaseField, 4, false>,
}
impl<TE: TwistedEdwardsParameters> SignatureScheme for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
type Parameters = Vec<TEProjective<TE>>;
type PrivateKey = (TE::ScalarField, TE::ScalarField);
type PublicKey = TEAffine<TE>;
type Signature = AleoSignature<TE>;
fn setup(message: &str) -> Self {
assert!(
<TE::ScalarField as PrimeField>::Parameters::CAPACITY < <TE::BaseField as PrimeField>::Parameters::CAPACITY
);
// Compute the powers of G.
let g_bases = {
let (base, _, _) = hash_to_curve::<TEAffine<TE>>(&message);
let mut g = base.into_projective();
let mut g_bases = Vec::with_capacity(<TE::ScalarField as PrimeField>::Parameters::MODULUS_BITS as usize);
for _ in 0..<TE::ScalarField as PrimeField>::Parameters::MODULUS_BITS as usize {
g_bases.push(g);
g.double_in_place();
}
g_bases
};
let crypto_hash = PoseidonCryptoHash::<TE::BaseField, 4, false>::setup();
Self { g_bases, crypto_hash }
}
fn parameters(&self) -> &Self::Parameters {
&self.g_bases
}
///
/// Returns private key as (sk_sig, r_sig).
///
fn generate_private_key<R: Rng + CryptoRng>(&self, rng: &mut R) -> Self::PrivateKey {
(TE::ScalarField::rand(rng), TE::ScalarField::rand(rng))
}
///
/// Returns public key as (G^sk_sig G^r_sig G^sk_prf).
///
fn generate_public_key(&self, private_key: &Self::PrivateKey) -> Self::PublicKey {
// Extract (sk_sig, r_sig).
let (sk_sig, r_sig) = private_key;
// Compute G^sk_sig.
let g_sk_sig = self.g_scalar_multiply(sk_sig);
// Compute G^r_sig.
let g_r_sig = self.g_scalar_multiply(r_sig);
// Compute sk_prf := RO(G^sk_sig || G^r_sig).
let sk_prf = self.hash_to_scalar_field(&[g_sk_sig.x, g_r_sig.x]);
// Compute G^sk_prf.
let g_sk_prf = self.g_scalar_multiply(&sk_prf);
// Compute G^sk_sig G^r_sig G^sk_prf.
let public_key = g_sk_sig + g_r_sig + g_sk_prf;
public_key
}
///
/// Returns signature (c, s, G^sk_sig, G^r_sig), where:
/// c := Hash(G^sk_sig G^r_sig G^sk_prf, G^r, message)
/// s := r - c * sk_sig
///
fn sign<R: Rng + CryptoRng>(
&self,
private_key: &Self::PrivateKey,
message: &[u8],
rng: &mut R,
) -> Result<Self::Signature> {
// Sample a random scalar field element.
let r = TE::ScalarField::rand(rng);
// Compute G^r.
let g_r = self.g_scalar_multiply(&r);
// Extract (sk_sig, r_sig).
let (sk_sig, r_sig) = private_key;
// Compute G^sk_sig.
let g_sk_sig = self.g_scalar_multiply(sk_sig);
// Compute G^r_sig.
let g_r_sig = self.g_scalar_multiply(r_sig);
// Compute sk_prf := RO(G^sk_sig || G^r_sig).
let sk_prf = self.hash_to_scalar_field(&[g_sk_sig.x, g_r_sig.x]);
// Compute G^sk_prf.
let g_sk_prf = self.g_scalar_multiply(&sk_prf);
// Compute G^sk_sig G^r_sig G^sk_prf.
let public_key = g_sk_sig + g_r_sig + g_sk_prf;
// Compute the verifier challenge.
let verifier_challenge = {
// Construct the hash input (G^sk_sig G^r_sig G^sk_prf, G^r, message).
let mut preimage = vec![];
preimage.extend_from_slice(&public_key.x.to_field_elements()?);
preimage.extend_from_slice(&g_r.x.to_field_elements()?);
preimage.push(TE::BaseField::from(message.len() as u128));
preimage.extend_from_slice(&message.to_field_elements()?);
// Hash to derive the verifier challenge.
self.hash_to_scalar_field(&preimage)
};
// Compute the prover response.
let prover_response = r - (verifier_challenge * sk_sig);
Ok(AleoSignature {
prover_response,
verifier_challenge,
root_public_key: g_sk_sig.x,
root_randomizer: g_r_sig.x,
})
}
///
/// Verifies (c == c') && (public_key == G^sk_sig G^r_sig G^sk_prf) where:
/// c' := Hash(G^sk_sig G^r_sig G^sk_prf, G^s G^sk_sig^c, message)
///
fn verify(&self, public_key: &Self::PublicKey, message: &[u8], signature: &Self::Signature) -> Result<bool> {
// Extract the signature contents.
let AleoSignature {
prover_response,
verifier_challenge,
root_public_key,
root_randomizer,
} = signature;
// Recover G^sk_sig.
let g_sk_sig = Self::recover_from_x_coordinate(root_public_key)?;
// Compute G^sk_sig^c.
let g_sk_sig_c = self.scalar_multiply(g_sk_sig.into_projective(), &verifier_challenge);
// Compute G^r := G^s G^sk_sig^c.
let g_r = self.g_scalar_multiply(&prover_response) + g_sk_sig_c;
// Compute the candidate verifier challenge.
let candidate_verifier_challenge = {
// Construct the hash input (G^sk_sig G^r_sig G^sk_prf, G^r, message).
let mut preimage = vec![];
preimage.extend_from_slice(&public_key.x.to_field_elements()?);
preimage.extend_from_slice(&g_r.x.to_field_elements()?);
preimage.push(TE::BaseField::from(message.len() as u128));
preimage.extend_from_slice(&message.to_field_elements()?);
// Hash to derive the verifier challenge.
self.hash_to_scalar_field(&preimage)
};
// Recover G^r_sig.
let g_r_sig = Self::recover_from_x_coordinate(root_randomizer)?;
// Compute the candidate public key as (G^sk_sig G^r_sig G^sk_prf).
let candidate_public_key = {
// Compute sk_prf := RO(G^sk_sig || G^r_sig).
let sk_prf = self.hash_to_scalar_field(&[g_sk_sig.x, g_r_sig.x]);
// Compute G^sk_prf.
let g_sk_prf = self.g_scalar_multiply(&sk_prf);
// Compute G^sk_sig G^r_sig G^sk_prf.
g_sk_sig + g_r_sig + g_sk_prf
};
Ok(*verifier_challenge == candidate_verifier_challenge && *public_key == candidate_public_key)
}
}
impl<TE: TwistedEdwardsParameters> SignatureSchemeOperations for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
type AffineCurve = TEAffine<TE>;
type BaseField = TE::BaseField;
type ScalarField = TE::ScalarField;
type Signature = AleoSignature<TE>;
fn pk_sig(signature: &Self::Signature) -> Result<Self::AffineCurve> {
Self::recover_from_x_coordinate(&signature.root_public_key)
}
fn pr_sig(signature: &Self::Signature) -> Result<Self::AffineCurve> {
Self::recover_from_x_coordinate(&signature.root_randomizer)
}
fn g_scalar_multiply(&self, scalar: &Self::ScalarField) -> Self::AffineCurve {
self.g_bases
.iter()
.zip_eq(&scalar.to_bits_le())
.filter_map(|(base, bit)| match bit {
true => Some(base),
false => None,
})
.sum::<TEProjective<TE>>()
.into_affine()
}
fn hash_to_scalar_field(&self, input: &[Self::BaseField]) -> Self::ScalarField {
// Use Poseidon as a random oracle.
let output = self.crypto_hash.evaluate(&input);
// Truncate the output to CAPACITY bits (1 bit less than MODULUS_BITS) in the scalar field.
let mut bits = output.to_repr().to_bits_le();
bits.resize(<TE::ScalarField as PrimeField>::Parameters::CAPACITY as usize, false);
// Output the scalar field.
let biginteger = <TE::ScalarField as PrimeField>::BigInteger::from_bits_le(&bits);
match <TE::ScalarField as PrimeField>::from_repr(biginteger) {
// We know this case will always work, because we truncate the output to CAPACITY bits in the scalar field.
Some(scalar) => scalar,
_ => panic!("Failed to hash input into scalar field"),
}
}
}
impl<TE: TwistedEdwardsParameters> AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
fn scalar_multiply(&self, base: TEProjective<TE>, scalar: &TE::ScalarField) -> TEAffine<TE> {
base.mul(*scalar).into_affine()
}
fn recover_from_x_coordinate(x_coordinate: &TE::BaseField) -> Result<TEAffine<TE>> {
if let Some(element) = TEAffine::<TE>::from_x_coordinate(*x_coordinate, true) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
if let Some(element) = TEAffine::<TE>::from_x_coordinate(*x_coordinate, false) {
if element.is_in_correct_subgroup_assuming_on_curve() {
return Ok(element);
}
}
Err(SignatureError::Message("Failed to recover from x coordinate".into()).into())
}
}
impl<TE: TwistedEdwardsParameters> From<Vec<TEProjective<TE>>> for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
fn from(g_bases: Vec<TEProjective<TE>>) -> Self {
let crypto_hash = PoseidonCryptoHash::<TE::BaseField, 4, false>::setup();
Self { g_bases, crypto_hash }
}
}
impl<TE: TwistedEdwardsParameters> ToBytes for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
(self.g_bases.len() as u32).write_le(&mut writer)?;
for g in &self.g_bases {
g.into_affine().write_le(&mut writer)?;
}
Ok(())
}
}
impl<TE: TwistedEdwardsParameters> FromBytes for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let g_bases_length: u32 = FromBytes::read_le(&mut reader)?;
let mut g_bases = Vec::with_capacity(g_bases_length as usize);
for _ in 0..g_bases_length {
let g: TEAffine<TE> = FromBytes::read_le(&mut reader)?;
g_bases.push(g.into_projective());
}
Ok(Self::from(g_bases))
}
}
impl<F: Field, TE: TwistedEdwardsParameters + ToConstraintField<F>> ToConstraintField<F> for AleoSignatureScheme<TE>
where
TE::BaseField: PoseidonDefaultParametersField,
{
#[inline]
fn to_field_elements(&self) -> Result<Vec<F>, ConstraintFieldError> {
Ok(Vec::new())
}
}