generated from arkworks-rs/template
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmod.rs
194 lines (167 loc) · 6.21 KB
/
mod.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
use super::SignatureScheme;
use ark_crypto_primitives::Error;
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{
bytes::ToBytes,
fields::{Field, PrimeField},
to_bytes, ToConstraintField, UniformRand,
};
use ark_std::io::{Result as IoResult, Write};
use ark_std::rand::Rng;
use ark_std::{hash::Hash, marker::PhantomData, vec::Vec};
use blake2::Blake2s;
use digest::Digest;
use derivative::Derivative;
#[cfg(feature = "r1cs")]
pub mod constraints;
pub struct Schnorr<C: ProjectiveCurve> {
_group: PhantomData<C>,
}
#[derive(Derivative)]
#[derivative(Clone(bound = "C: ProjectiveCurve"), Debug)]
pub struct Parameters<C: ProjectiveCurve> {
pub generator: C::Affine,
pub salt: Option<[u8; 32]>,
}
pub type PublicKey<C> = <C as ProjectiveCurve>::Affine;
#[derive(Clone, Default, Debug)]
pub struct SecretKey<C: ProjectiveCurve> {
pub secret_key: C::ScalarField,
pub public_key: PublicKey<C>,
}
impl<C: ProjectiveCurve> ToBytes for SecretKey<C> {
#[inline]
fn write<W: Write>(&self, writer: W) -> IoResult<()> {
self.secret_key.write(writer)
}
}
#[derive(Clone, Default, Debug)]
pub struct Signature<C: ProjectiveCurve> {
pub prover_response: C::ScalarField,
pub verifier_challenge: [u8; 32],
}
impl<C: ProjectiveCurve + Hash> SignatureScheme for Schnorr<C>
where
C::ScalarField: PrimeField,
{
type Parameters = Parameters<C>;
type PublicKey = PublicKey<C>;
type SecretKey = SecretKey<C>;
type Signature = Signature<C>;
fn setup<R: Rng>(_rng: &mut R) -> Result<Self::Parameters, Error> {
// let setup_time = start_timer!(|| "SchnorrSig::Setup");
let salt = None;
let generator = C::prime_subgroup_generator().into();
// end_timer!(setup_time);
Ok(Parameters { generator, salt })
}
fn keygen<R: Rng>(
parameters: &Self::Parameters,
rng: &mut R,
) -> Result<(Self::PublicKey, Self::SecretKey), Error> {
// let keygen_time = start_timer!(|| "SchnorrSig::KeyGen");
// Secret is a random scalar x
// the pubkey is y = xG
let secret_key = C::ScalarField::rand(rng);
let public_key = parameters.generator.mul(secret_key).into();
// end_timer!(keygen_time);
Ok((
public_key,
SecretKey {
secret_key,
public_key,
},
))
}
fn sign<R: Rng>(
parameters: &Self::Parameters,
sk: &Self::SecretKey,
message: &[u8],
rng: &mut R,
) -> Result<Self::Signature, Error> {
// let sign_time = start_timer!(|| "SchnorrSig::Sign");
// (k, e);
let (random_scalar, verifier_challenge) = {
// Sample a random scalar `k` from the prime scalar field.
let random_scalar: C::ScalarField = C::ScalarField::rand(rng);
// Commit to the random scalar via r := k · G.
// This is the prover's first msg in the Sigma protocol.
let prover_commitment = parameters.generator.mul(random_scalar).into_affine();
// Hash everything to get verifier challenge.
// e := H(salt || pubkey || r || msg);
let mut hash_input = Vec::new();
if parameters.salt != None {
hash_input.extend_from_slice(¶meters.salt.unwrap());
}
hash_input.extend_from_slice(&to_bytes![sk.public_key]?);
hash_input.extend_from_slice(&to_bytes![prover_commitment]?);
hash_input.extend_from_slice(message);
let hash_digest = Blake2s::digest(&hash_input);
assert!(hash_digest.len() >= 32);
let mut verifier_challenge = [0u8; 32];
verifier_challenge.copy_from_slice(&hash_digest);
(random_scalar, verifier_challenge)
};
let verifier_challenge_fe = C::ScalarField::from_le_bytes_mod_order(&verifier_challenge);
// k - xe;
let prover_response = random_scalar - (verifier_challenge_fe * sk.secret_key);
let signature = Signature {
prover_response,
verifier_challenge,
};
// end_timer!(sign_time);
Ok(signature)
}
fn verify(
parameters: &Self::Parameters,
pk: &Self::PublicKey,
message: &[u8],
signature: &Self::Signature,
) -> Result<bool, Error> {
// let verify_time = start_timer!(|| "SchnorrSig::Verify");
let Signature {
prover_response,
verifier_challenge,
} = signature;
let verifier_challenge_fe = C::ScalarField::from_le_bytes_mod_order(verifier_challenge);
// sG = kG - eY
// kG = sG + eY
// so we first solve for kG.
let mut claimed_prover_commitment = parameters.generator.mul(*prover_response);
let public_key_times_verifier_challenge = pk.mul(verifier_challenge_fe);
claimed_prover_commitment += &public_key_times_verifier_challenge;
let claimed_prover_commitment = claimed_prover_commitment.into_affine();
// e = H(salt, kG, msg)
let mut hash_input = Vec::new();
if parameters.salt != None {
hash_input.extend_from_slice(¶meters.salt.unwrap());
}
hash_input.extend_from_slice(&to_bytes![pk]?);
hash_input.extend_from_slice(&to_bytes![claimed_prover_commitment]?);
hash_input.extend_from_slice(message);
// cast the hash output to get e
let obtained_verifier_challenge = &Blake2s::digest(&hash_input)[..];
// end_timer!(verify_time);
// The signature is valid iff the computed verifier challenge is the same as the one
// provided in the signature
Ok(verifier_challenge == obtained_verifier_challenge)
}
}
pub fn bytes_to_bits(bytes: &[u8]) -> Vec<bool> {
let mut bits = Vec::with_capacity(bytes.len() * 8);
for byte in bytes {
for i in 0..8 {
let bit = (*byte >> (8 - i - 1)) & 1;
bits.push(bit == 1);
}
}
bits
}
impl<ConstraintF: Field, C: ProjectiveCurve + ToConstraintField<ConstraintF>>
ToConstraintField<ConstraintF> for Parameters<C>
{
#[inline]
fn to_field_elements(&self) -> Option<Vec<ConstraintF>> {
self.generator.into_projective().to_field_elements()
}
}