@@ -5,8 +5,9 @@ extern crate alloc;
5
5
use alloc:: vec:: Vec ;
6
6
use core:: marker:: PhantomData ;
7
7
8
- use ark_ff:: { vec, BigInt } ;
8
+ use ark_ff:: vec;
9
9
use byteorder:: { BigEndian , ByteOrder } ;
10
+ use constants:: * ;
10
11
use hex_literal:: hex;
11
12
use sha2:: Sha256 ;
12
13
use sha3:: Digest ;
@@ -15,6 +16,8 @@ use unionlabs::{
15
16
hash:: H256 , ibc:: lightclients:: cometbls:: light_header:: LightHeader , uint:: U256 , ByteArrayExt ,
16
17
} ;
17
18
19
+ mod constants;
20
+
18
21
pub const NB_PUBLIC_INPUTS : usize = 2 ;
19
22
20
23
pub const HMAC_O : & [ u8 ] = & hex ! ( "1F333139281E100F5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C5C" ) ;
@@ -26,103 +29,7 @@ pub const PRIME_R_MINUS_ONE: U256 = U256::from_limbs([
26
29
3486998266802970665 ,
27
30
] ) ;
28
31
29
- fn make_g1 ( x : BigInt < 4 > , y : BigInt < 4 > ) -> substrate_bn:: AffineG1 {
30
- substrate_bn:: AffineG1 :: new (
31
- substrate_bn:: Fq :: from_u256 ( x. 0 . into ( ) ) . unwrap ( ) ,
32
- substrate_bn:: Fq :: from_u256 ( y. 0 . into ( ) ) . unwrap ( ) ,
33
- )
34
- . unwrap ( )
35
- }
36
-
37
- fn make_g2 ( x0 : BigInt < 4 > , x1 : BigInt < 4 > , y0 : BigInt < 4 > , y1 : BigInt < 4 > ) -> substrate_bn:: AffineG2 {
38
- substrate_bn:: AffineG2 :: new (
39
- substrate_bn:: Fq2 :: new (
40
- substrate_bn:: Fq :: from_u256 ( x0. 0 . into ( ) ) . unwrap ( ) ,
41
- substrate_bn:: Fq :: from_u256 ( x1. 0 . into ( ) ) . unwrap ( ) ,
42
- ) ,
43
- substrate_bn:: Fq2 :: new (
44
- substrate_bn:: Fq :: from_u256 ( y0. 0 . into ( ) ) . unwrap ( ) ,
45
- substrate_bn:: Fq :: from_u256 ( y1. 0 . into ( ) ) . unwrap ( ) ,
46
- ) ,
47
- )
48
- . unwrap ( )
49
- }
50
-
51
- // TODO: this should be computed at compile time
52
- pub fn pedersen_commitment_key ( ) -> ( substrate_bn:: AffineG2 , substrate_bn:: AffineG2 ) {
53
- let g_raw = hex ! ( "257DF6F8132CB0037F7DFDF1A29B04C1FF92BA082EDA513996BA2BFA9FBD198713F0D8D8879885CA567EF99298C30C397E6FBA584658F4127713A814C06DE55A1660EBCC60C7A3AC560EFCEA5993F528EE13685D3A39694ACD74FE67C80D798A15E80642C58DB4DBE0A87F92CE3C65E962F231278353783A691FD64078BA7F34" ) ;
54
- let g_root_sigma_neg_raw = hex ! ( "2FBFE141A7555CF7E3E86B092660B81CFB68A025AD817E45CEC0B0F2E2CA636802A104DF1C015F2307FA2859627098CDF9FDB521D61D323943343A12304E5BAF27DA3F93ECF3BFD0B3A3354AE2162A6C230C0E539B6D9F82C0826E2B006A59222C0838551CB9E5CF67DB57DE7E2250BB97807F6687F135A6EB910359BA7BDB8D" ) ;
55
- let G2Affine ( _, g) = G2Affine :: < BigEndian > :: try_from ( g_raw) . expect ( "impossible" ) ;
56
- let G2Affine ( _, g_root_sigma_neg) =
57
- G2Affine :: < BigEndian > :: try_from ( g_root_sigma_neg_raw) . expect ( "impossible" ) ;
58
- ( g, g_root_sigma_neg)
59
- }
60
-
61
- // TODO: this should be computed at compile time
62
- pub fn universal_vk ( ) -> VerifyingKey {
63
- VerifyingKey {
64
- alpha_g1 : make_g1 (
65
- BigInt ! ( "4252850302693242182654534639730627324742305503909561446344356971523664816281" ) ,
66
- BigInt ! ( "3971530409048238023625806606514600982127202826003358538821613170737831313919" ) ,
67
- ) ,
68
- beta_g2 : make_g2 (
69
- BigInt ! ( "9609903744775525881338738176064678545439912439219033822736570321349357348980" ) ,
70
- BigInt ! (
71
- "11402125448377072234752634956069960846261435348550776006069399216352815312229"
72
- ) ,
73
- BigInt ! ( "3876014193556985028076276590285094449745398487447250532380698384573245200038" ) ,
74
- BigInt ! ( "6131692356384648492800758325058748831519318785594820705365176509549681793745" ) ,
75
- ) ,
76
- gamma_g2 : make_g2 (
77
- BigInt ! (
78
- "15418804173338388766896385877623893969695670309009587476846726795628238714393"
79
- ) ,
80
- BigInt ! (
81
- "14882897597913405382982164467298010752166363844685258881581520272046793702095"
82
- ) ,
83
- BigInt ! ( "4166025151148225057462107057100265181139888889391061071239248954005945470477" ) ,
84
- BigInt ! ( "206728492847877950288262169260916452585500374823256459470367014125967964118" ) ,
85
- ) ,
86
- delta_g2 : make_g2 (
87
- BigInt ! ( "2636161939055419322743684458857549714230849256995406138405588958157843793131" ) ,
88
- BigInt ! (
89
- "18711435617866698040659011365354165232283248284733617156044102129651710736892"
90
- ) ,
91
- BigInt ! (
92
- "19240355865528042255113556794397480864884450537537107687508383548050491695680"
93
- ) ,
94
- BigInt ! (
95
- "12249371269602120664445362627662636389936048209522657338249293583990077475589"
96
- ) ,
97
- ) ,
98
- gamma_abc_g1 : vec ! [
99
- make_g1(
100
- BigInt !(
101
- "17683074019270049519594214298171697666582975915064153618004061598086681825921"
102
- ) ,
103
- BigInt !(
104
- "16826145467743906176166100307225491106961753217491843100452871479833450456070"
105
- ) ,
106
- ) ,
107
- make_g1(
108
- BigInt !(
109
- "4999724750322169039879775285047941133298355297928988655266615607529011563466"
110
- ) ,
111
- BigInt !(
112
- "8614448667589143428827059805500251818303043966026074735628377626634208993292"
113
- ) ,
114
- ) ,
115
- make_g1(
116
- BigInt !(
117
- "1184807858330365651919114999096473332175166887333719856514157833289677967559"
118
- ) ,
119
- BigInt !(
120
- "20327610427697660249999185524229068956160879388632193295649998184224119517657"
121
- ) ,
122
- ) ,
123
- ] ,
124
- }
125
- }
32
+ const _: ( ) = assert ! ( GAMMA_ABC_G1 . len( ) == NB_PUBLIC_INPUTS + 1 ) ;
126
33
127
34
fn hmac_keccak ( message : & [ u8 ] ) -> [ u8 ; 32 ] {
128
35
sha3:: Keccak256 :: new ( )
@@ -169,7 +76,6 @@ fn hash_commitment(proof_commitment: &substrate_bn::AffineG1) -> Result<U256, Er
169
76
pub const FQ_SIZE : usize = 32 ;
170
77
pub const G1_SIZE : usize = 2 * FQ_SIZE ;
171
78
pub const G2_SIZE : usize = 2 * G1_SIZE ;
172
- pub const COMMITMENT_HASH_SIZE : usize = 32 ;
173
79
174
80
pub struct G1Affine < FromOrder : ByteOrder > ( PhantomData < FromOrder > , substrate_bn:: AffineG1 ) ;
175
81
pub type G1AffineBE = G1Affine < BigEndian > ;
@@ -303,14 +209,12 @@ pub fn verify_zkp(
303
209
header : & LightHeader ,
304
210
zkp : impl Into < Vec < u8 > > ,
305
211
) -> Result < ( ) , Error > {
306
- let ( g, g_root_sigma_neg) = pedersen_commitment_key ( ) ;
307
212
verify_generic_zkp_2 (
308
- universal_vk ( ) ,
309
213
chain_id,
310
214
trusted_validators_hash,
311
215
header,
312
- g ,
313
- g_root_sigma_neg ,
216
+ PEDERSEN_G ,
217
+ PEDERSEN_G_ROOT_SIGMA_NEG ,
314
218
ZKP :: try_from ( zkp. into ( ) . as_ref ( ) ) ?,
315
219
)
316
220
}
@@ -329,7 +233,6 @@ fn g1_to_bytes(g1_point: &G1) -> Result<[u8; 64], Error> {
329
233
}
330
234
331
235
fn verify_generic_zkp_2 (
332
- vk : VerifyingKey ,
333
236
chain_id : & str ,
334
237
trusted_validators_hash : H256 ,
335
238
header : & LightHeader ,
@@ -341,9 +244,6 @@ fn verify_generic_zkp_2(
341
244
return Err ( Error :: InvalidChainId ) ;
342
245
}
343
246
// Constant + public inputs
344
- if vk. gamma_abc_g1 . len ( ) != NB_PUBLIC_INPUTS + 1 {
345
- return Err ( Error :: InvalidVerifyingKey ) ;
346
- }
347
247
let decode_scalar = move |x : U256 | -> Result < substrate_bn:: Fr , Error > {
348
248
substrate_bn:: Fr :: new ( x. 0 . 0 . into ( ) ) . ok_or ( Error :: InvalidPublicInput )
349
249
} ;
@@ -388,22 +288,11 @@ fn verify_generic_zkp_2(
388
288
decode_scalar ( U256 :: from_be_bytes ( inputs_hash) ) ?,
389
289
decode_scalar ( commitment_hash) ?,
390
290
] ;
391
- let initial_point = substrate_bn:: G1 :: from (
392
- vk. gamma_abc_g1
393
- . first ( )
394
- . copied ( )
395
- . ok_or ( Error :: InvalidVerifyingKey ) ?,
396
- ) + zkp. proof_commitment . into ( ) ;
291
+ let initial_point = substrate_bn:: G1 :: from ( GAMMA_ABC_G1 [ 0 ] ) + zkp. proof_commitment . into ( ) ;
397
292
let public_inputs_msm = public_inputs
398
293
. into_iter ( )
399
- . zip (
400
- vk. gamma_abc_g1
401
- . into_iter ( )
402
- . skip ( 1 )
403
- . map ( substrate_bn:: G1 :: from) ,
404
- )
294
+ . zip ( GAMMA_ABC_G1 . into_iter ( ) . skip ( 1 ) . map ( substrate_bn:: G1 :: from) )
405
295
. fold ( initial_point, |s, ( w_i, gamma_l_i) | s + gamma_l_i * w_i) ;
406
- // TODO: the verifying key transformation, pedersen key decoding and this negations should all be done at compile time
407
296
408
297
let proof_a: G1 = zkp. proof . a . into ( ) ;
409
298
let proof_c: G1 = zkp. proof . c . into ( ) ;
@@ -429,12 +318,9 @@ fn verify_generic_zkp_2(
429
318
430
319
let result = substrate_bn:: pairing_batch ( & [
431
320
( proof_a * r1, zkp. proof . b . into ( ) ) ,
432
- ( public_inputs_msm * r1, -substrate_bn:: G2 :: from ( vk. gamma_g2 ) ) ,
433
- ( proof_c * r1, -substrate_bn:: G2 :: from ( vk. delta_g2 ) ) ,
434
- (
435
- G1 :: from ( vk. alpha_g1 ) * r1,
436
- -substrate_bn:: G2 :: from ( vk. beta_g2 ) ,
437
- ) ,
321
+ ( public_inputs_msm * r1, -substrate_bn:: G2 :: from ( GAMMA_G2 ) ) ,
322
+ ( proof_c * r1, -substrate_bn:: G2 :: from ( DELTA_G2 ) ) ,
323
+ ( G1 :: from ( ALPHA_G1 ) * r1, -substrate_bn:: G2 :: from ( BETA_G2 ) ) ,
438
324
// Verify pedersen proof of knowledge
439
325
( pc * r2, g. into ( ) ) ,
440
326
( pok * r2, g_root_sigma_neg. into ( ) ) ,
0 commit comments