-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
bn254_circom.rs
382 lines (331 loc) · 13.3 KB
/
bn254_circom.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
// Copyright © Aptos Foundation
use crate::{
jwks::rsa::RSA_JWK,
keyless::{
base64url_encode_str, Configuration, EphemeralCertificate, IdCommitment, KeylessPublicKey,
KeylessSignature,
},
serialize,
};
use anyhow::bail;
use aptos_crypto::{poseidon_bn254, CryptoMaterialError};
use ark_bn254::{Fq, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use num_traits::{One, Zero};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_big_array::BigArray;
// TODO(keyless): Some of this stuff, if not all, belongs to the aptos-crypto crate
pub const G1_PROJECTIVE_COMPRESSED_NUM_BYTES: usize = 32;
pub const G2_PROJECTIVE_COMPRESSED_NUM_BYTES: usize = 64;
/// This will do the proper subgroup membership checks.
pub fn g1_projective_str_to_affine(x: &str, y: &str) -> anyhow::Result<G1Affine> {
let g1_affine = G1Bytes::new_unchecked(x, y)?.deserialize_into_affine()?;
Ok(g1_affine)
}
/// This will do the proper subgroup membership checks.
pub fn g2_projective_str_to_affine(x: [&str; 2], y: [&str; 2]) -> anyhow::Result<G2Affine> {
let g2_affine = G2Bytes::new_unchecked(x, y)?.as_affine()?;
Ok(g2_affine)
}
/// Converts a decimal string to an Fq
fn parse_fq_element(s: &str) -> Result<Fq, CryptoMaterialError> {
s.parse::<Fq>()
.map_err(|_e| CryptoMaterialError::DeserializationError)
}
#[allow(unused)]
/// Converts a decimal string to an Fr
pub fn parse_fr_element(s: &str) -> Result<Fr, CryptoMaterialError> {
s.parse::<Fr>()
.map_err(|_e| CryptoMaterialError::DeserializationError)
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct G1Bytes(pub(crate) [u8; G1_PROJECTIVE_COMPRESSED_NUM_BYTES]);
impl G1Bytes {
pub fn new_unchecked(x: &str, y: &str) -> anyhow::Result<Self> {
let g1 = G1Projective::new_unchecked(
parse_fq_element(x)?,
parse_fq_element(y)?,
parse_fq_element("1")?,
);
let bytes: Vec<u8> = serialize!(g1);
Self::new_from_vec(bytes)
}
/// Used internally or for testing.
pub fn new_from_vec(vec: Vec<u8>) -> anyhow::Result<Self> {
if vec.len() == G1_PROJECTIVE_COMPRESSED_NUM_BYTES {
let mut bytes = [0; G1_PROJECTIVE_COMPRESSED_NUM_BYTES];
bytes.copy_from_slice(&vec);
Ok(Self(bytes))
} else {
bail!(
"Serialized BN254 G1 must have exactly {} bytes",
G1_PROJECTIVE_COMPRESSED_NUM_BYTES
)
}
}
pub fn deserialize_into_affine(&self) -> Result<G1Affine, CryptoMaterialError> {
self.try_into()
}
}
impl<'de> Deserialize<'de> for G1Bytes {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
let s = <String>::deserialize(deserializer)?;
let bytes = hex::decode(s).map_err(serde::de::Error::custom)?;
G1Bytes::new_from_vec(bytes).map_err(serde::de::Error::custom)
} else {
// In order to preserve the Serde data model and help analysis tools,
// make sure to wrap our value in a container with the same name
// as the original type.
#[derive(::serde::Deserialize)]
#[serde(rename = "G1Bytes")]
struct Value([u8; G1_PROJECTIVE_COMPRESSED_NUM_BYTES]);
let value = Value::deserialize(deserializer)?;
Ok(G1Bytes(value.0))
}
}
}
impl Serialize for G1Bytes {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
hex::encode(self.0).serialize(serializer)
} else {
// See comment in deserialize.
serializer.serialize_newtype_struct("G1Bytes", &self.0)
}
}
}
impl TryInto<G1Projective> for &G1Bytes {
type Error = CryptoMaterialError;
fn try_into(self) -> Result<G1Projective, CryptoMaterialError> {
G1Projective::deserialize_compressed(self.0.as_slice())
.map_err(|_| CryptoMaterialError::DeserializationError)
}
}
impl TryInto<G1Affine> for &G1Bytes {
type Error = CryptoMaterialError;
fn try_into(self) -> Result<G1Affine, CryptoMaterialError> {
let g1_projective: G1Projective = self.try_into()?;
Ok(g1_projective.into())
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct G2Bytes(pub(crate) [u8; G2_PROJECTIVE_COMPRESSED_NUM_BYTES]);
impl G2Bytes {
pub fn new_unchecked(x: [&str; 2], y: [&str; 2]) -> anyhow::Result<Self> {
let g2 = G2Projective::new_unchecked(
Fq2::new(parse_fq_element(x[0])?, parse_fq_element(x[1])?),
Fq2::new(parse_fq_element(y[0])?, parse_fq_element(y[1])?),
Fq2::new(parse_fq_element("1")?, parse_fq_element("0")?),
);
let bytes: Vec<u8> = serialize!(g2);
Self::new_from_vec(bytes)
}
pub fn new_from_vec(vec: Vec<u8>) -> anyhow::Result<Self> {
if vec.len() == G2_PROJECTIVE_COMPRESSED_NUM_BYTES {
let mut bytes = [0; G2_PROJECTIVE_COMPRESSED_NUM_BYTES];
bytes.copy_from_slice(&vec);
Ok(Self(bytes))
} else {
bail!(
"Serialized BN254 G2 must have exactly {} bytes",
G2_PROJECTIVE_COMPRESSED_NUM_BYTES
)
}
}
pub fn as_affine(&self) -> Result<G2Affine, CryptoMaterialError> {
self.try_into()
}
}
impl<'de> Deserialize<'de> for G2Bytes {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
let s = <String>::deserialize(deserializer)?;
let bytes = hex::decode(s).map_err(serde::de::Error::custom)?;
G2Bytes::new_from_vec(bytes).map_err(serde::de::Error::custom)
} else {
// In order to preserve the Serde data model and help analysis tools,
// make sure to wrap our value in a container with the same name
// as the original type.
#[derive(::serde::Deserialize)]
#[serde(rename = "G2Bytes")]
struct Value(#[serde(with = "BigArray")] [u8; G2_PROJECTIVE_COMPRESSED_NUM_BYTES]);
let value = Value::deserialize(deserializer)?;
Ok(G2Bytes(value.0))
}
}
}
impl Serialize for G2Bytes {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
hex::encode(self.0).serialize(serializer)
} else {
// Doing this differently than G1Bytes in order to use serde(with = "BigArray"). This
// apparently is needed to correctly deserialize arrays with size greater than 32.
#[derive(::serde::Serialize)]
#[serde(rename = "G2Bytes")]
struct Value(#[serde(with = "BigArray")] [u8; G2_PROJECTIVE_COMPRESSED_NUM_BYTES]);
let value = Value(self.0);
// See comment in deserialize.
value.serialize(serializer)
}
}
}
impl TryInto<G2Projective> for &G2Bytes {
type Error = CryptoMaterialError;
fn try_into(self) -> Result<G2Projective, CryptoMaterialError> {
G2Projective::deserialize_compressed(self.0.as_slice())
.map_err(|_| CryptoMaterialError::DeserializationError)
}
}
impl TryInto<G2Affine> for &G2Bytes {
type Error = CryptoMaterialError;
fn try_into(self) -> Result<G2Affine, CryptoMaterialError> {
let g2_projective: G2Projective = self.try_into()?;
Ok(g2_projective.into())
}
}
pub fn get_public_inputs_hash(
sig: &KeylessSignature,
pk: &KeylessPublicKey,
jwk: &RSA_JWK,
config: &Configuration,
) -> anyhow::Result<Fr> {
if let EphemeralCertificate::ZeroKnowledgeSig(proof) = &sig.cert {
let (has_extra_field, extra_field_hash) = match &proof.extra_field {
None => (Fr::zero(), Fr::zero()),
Some(extra_field) => (
Fr::one(),
poseidon_bn254::pad_and_hash_string(
extra_field,
config.max_extra_field_bytes as usize,
)?,
),
};
let (override_aud_val_hash, use_override_aud) = match &proof.override_aud_val {
Some(override_aud_val) => (
poseidon_bn254::pad_and_hash_string(
override_aud_val,
IdCommitment::MAX_AUD_VAL_BYTES,
)?,
ark_bn254::Fr::from(1),
),
None => (
poseidon_bn254::pad_and_hash_string("", IdCommitment::MAX_AUD_VAL_BYTES)?,
ark_bn254::Fr::from(0),
),
};
// Add the hash of the jwt_header with the "." separator appended
let jwt_header_b64_with_separator =
format!("{}.", base64url_encode_str(sig.jwt_header_json.as_str()));
let jwt_header_hash = poseidon_bn254::pad_and_hash_string(
&jwt_header_b64_with_separator,
config.max_jwt_header_b64_bytes as usize,
)?;
let jwk_hash = jwk.to_poseidon_scalar()?;
// Add the hash of the value of the `iss` field
let iss_field_hash = poseidon_bn254::pad_and_hash_string(
pk.iss_val.as_str(),
config.max_iss_val_bytes as usize,
)?;
// Add the id_commitment as a scalar
let idc = Fr::from_le_bytes_mod_order(&pk.idc.0);
// Add the exp_timestamp_secs as a scalar
let exp_timestamp_secs = Fr::from(sig.exp_date_secs);
// Add the epk lifespan as a scalar
let exp_horizon_secs = Fr::from(proof.exp_horizon_secs);
// Add the epk as padded and packed scalars
let mut epk_frs = poseidon_bn254::pad_and_pack_bytes_to_scalars_with_len(
sig.ephemeral_pubkey.to_bytes().as_slice(),
config.max_commited_epk_bytes as usize,
)?;
// println!("Num EPK scalars: {}", epk_frs.len());
// for (i, e) in epk_frs.iter().enumerate() {
// println!("EPK Fr[{}]: {}", i, e.to_string())
// }
// println!("IDC: {}", idc);
// println!("exp_timestamp_secs: {}", exp_timestamp_secs);
// println!("exp_horizon_secs: {}", exp_horizon_secs);
// println!("iss field hash: {}", pk.iss_val);
// println!("Has extra field: {}", has_extra_field);
// println!("Extra field val: {:?}", proof.extra_field);
// println!("Extra field hash: {}", extra_field_hash);
// println!("JWT header val: {}", jwt_header_with_separator);
// println!("JWT header hash: {}", jwt_header_hash);
// println!("JWK hash: {}", jwk_hash);
// println!("Override aud hash: {}", override_aud_val_hash);
// println!("Use override aud: {}", use_override_aud.to_string());
let mut frs = vec![];
frs.append(&mut epk_frs);
frs.push(idc);
frs.push(exp_timestamp_secs);
frs.push(exp_horizon_secs);
frs.push(iss_field_hash);
frs.push(has_extra_field);
frs.push(extra_field_hash);
frs.push(jwt_header_hash);
frs.push(jwk_hash);
frs.push(override_aud_val_hash);
frs.push(use_override_aud);
poseidon_bn254::hash_scalars(frs)
} else {
bail!("Can only call `get_public_inputs_hash` on keyless::Signature with Groth16 ZK proof")
}
}
#[cfg(test)]
mod test {
use crate::keyless::{
bn254_circom::{
G1Bytes, G2Bytes, G1_PROJECTIVE_COMPRESSED_NUM_BYTES,
G2_PROJECTIVE_COMPRESSED_NUM_BYTES,
},
circuit_constants::devnet_prepared_vk,
Groth16VerificationKey,
};
use ark_bn254::Bn254;
use ark_groth16::PreparedVerifyingKey;
#[test]
pub fn test_bn254_serialized_sizes() {
let g1 = G1Bytes::new_unchecked(
"16672231080302629756836614130913173861541009360974119524782950408048375831661",
"1076145001163048025135533382088266750240489485046298539187659509488738517245",
)
.unwrap();
let g2 = G2Bytes::new_unchecked(
[
"1125365732643211423779651913319958385653115422366520671538751860820509133538",
"10055196097002324305342942912758079446356594743098794928675544207400347950287",
],
[
"10879716754714953827605171295191459580695363989155343984818520267224463075503",
"440220374146936557739765173414663598678359360031905981547938788314460390904",
],
)
.unwrap();
let g1_bytes = bcs::to_bytes(&g1).unwrap();
assert_eq!(g1_bytes.len(), G1_PROJECTIVE_COMPRESSED_NUM_BYTES);
let g2_bytes = bcs::to_bytes(&g2).unwrap();
assert_eq!(g2_bytes.len(), G2_PROJECTIVE_COMPRESSED_NUM_BYTES);
}
#[test]
// Tests conversion between the devnet ark_groth16::PreparedVerificationKey and our Move
// representation of it.
fn print_groth16_pvk() {
let groth16_vk: Groth16VerificationKey = devnet_prepared_vk().into();
let same_pvk: PreparedVerifyingKey<Bn254> = groth16_vk.try_into().unwrap();
assert_eq!(same_pvk, devnet_prepared_vk());
}
}