1
1
use crate:: {
2
2
blob_public_inputs ::{BlobCommitment , BlobPublicInputs , BlockBlobPublicInputs },
3
- config ::{D_INV , F , LOG_FIELDS_PER_BLOB , ROOTS },
3
+ config ::{D_INV , LOG_FIELDS_PER_BLOB , ROOTS },
4
4
};
5
5
6
- use bigint:: {BigNum , BigNumTrait };
6
+ use bigint:: {BigNumTrait , BLS12_381_Fr as F };
7
7
use std::ops:: {Mul , Neg };
8
8
use types:: {
9
9
abis::sponge_blob::SpongeBlob ,
10
- constants ::{BLOBS_PER_BLOCK , FIELDS_PER_BLOB , TWO_POW_64 },
10
+ constants ::{BLOBS_PER_BLOCK , FIELDS_PER_BLOB },
11
11
hash:: poseidon2_hash_subarray ,
12
12
traits::Empty ,
13
13
utils::arrays:: array_splice ,
14
14
};
15
15
16
- global LIMB_MAX : Field = 0x1000000000000000000000000000000 ; // 2^120
17
- global TWO_POW_56 : u64 = 0x100000000000000 ; // u64 to aid integer only modulo in __field_to_bignum_limbs
18
-
19
16
fn convert_blob_fields (blob_as_fields : [Field ; FIELDS_PER_BLOB ]) -> [F ; FIELDS_PER_BLOB ] {
20
- let mut blob : [F ; FIELDS_PER_BLOB ] = [BigNum ::zero (); FIELDS_PER_BLOB ];
17
+ let mut blob : [F ; FIELDS_PER_BLOB ] = [F ::zero (); FIELDS_PER_BLOB ];
21
18
for i in 0 ..FIELDS_PER_BLOB {
22
19
blob [i ] = F ::from (blob_as_fields [i ]);
23
20
}
@@ -154,7 +151,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
154
151
// i=0
155
152
156
153
let NUM_PARTIAL_SUMS = FIELDS_PER_BLOB / 8 ;
157
- // Safety: This sum is checked by the following `BigNum ::evaluate_quadratic_expression` calls.
154
+ // Safety: This sum is checked by the following `F ::evaluate_quadratic_expression` calls.
158
155
let partial_sums : [F ; FIELDS_PER_BLOB / 8 ] =
159
156
unsafe { __compute_partial_sums (fracs , ROOTS ) };
160
157
@@ -170,7 +167,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
170
167
[fracs [0 ]], [fracs [1 ]], [fracs [2 ]], [fracs [3 ]], [fracs [4 ]], [fracs [5 ]], [fracs [6 ]],
171
168
[fracs [7 ]],
172
169
];
173
- BigNum ::evaluate_quadratic_expression (
170
+ F ::evaluate_quadratic_expression (
174
171
lhs ,
175
172
[[false ], [false ], [false ], [false ], [false ], [false ], [false ], [false ]],
176
173
rhs ,
@@ -203,7 +200,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
203
200
// => (lhs[8*i] * rhs[8*i] + ... + lhs[8*i + 7] * rhs[8*i + 7]) + partial_sums[i-1] - partial_sums[i] == 0
204
201
let linear_terms = [partial_sums [i - 1 ], partial_sums [i ]];
205
202
206
- BigNum ::evaluate_quadratic_expression (
203
+ F ::evaluate_quadratic_expression (
207
204
/* lhs */ [
208
205
[ROOTS [i * 8 + 0 ]],
209
206
[ROOTS [i * 8 + 1 ]],
@@ -249,7 +246,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
249
246
}
250
247
251
248
unconstrained fn __compute_factor_helper (z_pow_d : F ) -> F {
252
- let one : F = BigNum ::one ();
249
+ let one : F = F ::one ();
253
250
z_pow_d .__sub (one ).__mul (D_INV )
254
251
}
255
252
@@ -264,13 +261,13 @@ fn compute_factor(z: F) -> F {
264
261
let z_pow_d = t ;
265
262
266
263
// Safety: We immediately check that this result is correct in the following
267
- // `BigNum ::evaluate_quadratic_expression` call.
264
+ // `F ::evaluate_quadratic_expression` call.
268
265
let factor = unsafe { __compute_factor_helper (z_pow_d ) };
269
266
270
267
// (z_pow_d - one) * (D_INV) - factor = 0
271
268
// z_pow_d * D_INV - D_INV - factor = 0
272
269
if !std::runtime:: is_unconstrained () {
273
- BigNum ::evaluate_quadratic_expression (
270
+ F ::evaluate_quadratic_expression (
274
271
[[z_pow_d ]],
275
272
[[false ]],
276
273
[[D_INV ]],
@@ -281,7 +278,7 @@ fn compute_factor(z: F) -> F {
281
278
}
282
279
283
280
// This version doesn't work:
284
- // BigNum ::evaluate_quadratic_expression(
281
+ // F ::evaluate_quadratic_expression(
285
282
// [[z_pow_d, one]],
286
283
// [[false, true]],
287
284
// [[D_INV]],
@@ -298,7 +295,7 @@ unconstrained fn __compute_fracs(
298
295
ys : [F ; FIELDS_PER_BLOB ],
299
296
unconstrained_roots : [F ; FIELDS_PER_BLOB ],
300
297
) -> [F ; FIELDS_PER_BLOB ] {
301
- let mut denoms = [BigNum ::zero (); FIELDS_PER_BLOB ];
298
+ let mut denoms = [F ::zero (); FIELDS_PER_BLOB ];
302
299
for i in 0 ..FIELDS_PER_BLOB {
303
300
denoms [i ] = z .__sub (unconstrained_roots [i ]); // (z - omega^i)
304
301
}
@@ -316,14 +313,14 @@ unconstrained fn __compute_fracs(
316
313
317
314
fn compute_fracs (z : F , ys : [F ; FIELDS_PER_BLOB ]) -> [F ; FIELDS_PER_BLOB ] {
318
315
// Safety: We immediately constrain these `fracs` to be correct in the following call
319
- // to `BigNum ::evaluate_quadratic_expression`.
316
+ // to `F ::evaluate_quadratic_expression`.
320
317
let mut fracs : [F ; FIELDS_PER_BLOB ] = unsafe { __compute_fracs (z , ys , ROOTS ) };
321
318
322
319
if !std::runtime:: is_unconstrained () {
323
320
for i in 0 ..FIELDS_PER_BLOB {
324
321
// frac <-- ys[i] / (z + neg_roots[i])
325
322
// frac * (z + neg_roots[i]) - ys[i] = 0
326
- BigNum ::evaluate_quadratic_expression (
323
+ F ::evaluate_quadratic_expression (
327
324
[[fracs [i ]]],
328
325
[[false ]],
329
326
[[z , ROOTS [i ].neg ()]],
@@ -352,7 +349,7 @@ unconstrained fn __compute_partial_sums(
352
349
// k=i*8 + 0
353
350
354
351
// Need to split off the first iteration.
355
- let mut partial_sum : F = BigNum ::zero ();
352
+ let mut partial_sum : F = F ::zero ();
356
353
for i in 0 ..8 {
357
354
// y_k * ( omega^k / (z - omega^k) )
358
355
let summand = unconstrained_roots [i ].__mul (fracs [i ]);
@@ -395,7 +392,7 @@ unconstrained fn __compute_sum(
395
392
// /____ z - omega^i
396
393
// i=0
397
394
398
- let mut sum : F = BigNum ::zero ();
395
+ let mut sum : F = F ::zero ();
399
396
for i in 0 ..FIELDS_PER_BLOB {
400
397
// y_k * ( omega^k / (z - omega^k) )
401
398
let summand = unconstrained_roots [i ].__mul (fracs [i ]);
@@ -413,11 +410,11 @@ mod tests {
413
410
barycentric_evaluate_blob_at_z , check_block_blob_sponge , evaluate_blob , evaluate_blobs ,
414
411
},
415
412
blob_public_inputs::BlobCommitment ,
416
- config ::{D , D_INV , F , ROOTS },
413
+ config ::{D , D_INV , ROOTS },
417
414
};
418
415
use super:: {__compute_partial_sums , __compute_sum };
419
416
use bigint:: {
420
- BigNum , bignum::BigNumTrait , fields::bls12_381Fr::BLS12_381_Fr_Params ,
417
+ BigNumTrait , BLS12_381_Fr as F , fields::bls12_381Fr::BLS12_381_Fr_Params ,
421
418
params::BigNumParamsGetter ,
422
419
};
423
420
use types:: {
@@ -459,7 +456,7 @@ mod tests {
459
456
//* d
460
457
//
461
458
let rhs = super:: compute_factor (challenge_z );
462
- let z_minus_1 = challenge_z .__sub (BigNum ::one ());
459
+ let z_minus_1 = challenge_z .__sub (F ::one ());
463
460
let lhs = y .__mul (z_minus_1 );
464
461
assert_eq (lhs , rhs );
465
462
}
@@ -517,7 +514,7 @@ mod tests {
517
514
let output = evaluate_blob (blob , kzg_commitment_in , hashed_blob );
518
515
519
516
// y is a BLS field with value 0x212c4f0c0ee5e7dd037110686a4639d191dde7b57ab99b51e4b06e7d827b6c4c
520
- let expected_y : F = BigNum {
517
+ let expected_y : F = F {
521
518
limbs : [0xdde7b57ab99b51e4b06e7d827b6c4c , 0x4f0c0ee5e7dd037110686a4639d191 , 0x212c ],
522
519
};
523
520
assert (expected_y == output .y );
@@ -547,7 +544,7 @@ mod tests {
547
544
let output = evaluate_blobs (blob , [kzg_commitment_in ; BLOBS_PER_BLOCK ], sponge_blob );
548
545
549
546
// y is a BLS field with value 0x52fd4e272015a79f3889cc9ab1d84bee4326de7d8ced52612ecc9ec137bd38ee
550
- let expected_y : F = BigNum {
547
+ let expected_y : F = F {
551
548
limbs : [0x26de7d8ced52612ecc9ec137bd38ee , 0x4e272015a79f3889cc9ab1d84bee43 , 0x52fd ],
552
549
};
553
550
for j in 0 ..BLOBS_PER_BLOCK {
@@ -598,19 +595,19 @@ mod tests {
598
595
599
596
#[test]
600
597
unconstrained fn test_barycentric () {
601
- let z : F = BigNum { limbs : [2 , 0 , 0 ] };
598
+ let z : F = F { limbs : [2 , 0 , 0 ] };
602
599
603
600
// many y's form a blob:
604
- let mut ys : [F ; FIELDS_PER_BLOB ] = [BigNum ::zero (); FIELDS_PER_BLOB ];
601
+ let mut ys : [F ; FIELDS_PER_BLOB ] = [F ::zero (); FIELDS_PER_BLOB ];
605
602
606
- ys [0 ] = BigNum { limbs : [0x1234 , 0 , 0 ] };
607
- ys [1 ] = BigNum { limbs : [0xabcd , 0 , 0 ] };
608
- ys [2 ] = BigNum { limbs : [0x69 , 0 , 0 ] };
603
+ ys [0 ] = F { limbs : [0x1234 , 0 , 0 ] };
604
+ ys [1 ] = F { limbs : [0xabcd , 0 , 0 ] };
605
+ ys [2 ] = F { limbs : [0x69 , 0 , 0 ] };
609
606
610
607
// evaluate the blob at z = 2 to yield y:
611
608
let y = barycentric_evaluate_blob_at_z (z , ys );
612
609
613
- let mut expected_y : [Field ; 3 ] = [0 ; 3 ];
610
+ let mut expected_y : [u128 ; 3 ] = [0 ; 3 ];
614
611
if (FIELDS_PER_BLOB == 4096 ) {
615
612
// Computed with the eth consensus specs py lib
616
613
expected_y =
@@ -627,11 +624,11 @@ mod tests {
627
624
}
628
625
629
626
// Helper function used to populate the hard-coded double_modulus value in the bls12381Fr.nr file in the bignum library.
630
- unconstrained fn compute_double_modulus () -> [Field ; 3 ] {
627
+ unconstrained fn compute_double_modulus () -> [u128 ; 3 ] {
631
628
let two_p = [0x7b4805fffcb7fdfffffffe00000002 , 0x4ea6533afa906673b0101343b00aa7 , 0x00e7db ];
632
629
let NUM_LIMBS = 3 ; // must be >= 3
633
- let two_pow_120 = 2 .pow_32 (120 );
634
- let mut double_modulus : [Field ; 3 ] = [0 ; 3 ];
630
+ let two_pow_120 : u128 = 2 .pow_32 (120 ) as u128 ;
631
+ let mut double_modulus : [u128 ; 3 ] = [0 ; 3 ];
635
632
636
633
double_modulus [0 ] = two_p [0 ] + two_pow_120 ;
637
634
for i in 1 ..NUM_LIMBS - 1 {
@@ -655,7 +652,7 @@ mod tests {
655
652
656
653
#[test]
657
654
fn compute_sum_and_compute_partial_sums_agree () {
658
- let mut fields = [BigNum ::zero (); FIELDS_PER_BLOB ];
655
+ let mut fields = [F ::zero (); FIELDS_PER_BLOB ];
659
656
for i in 0 ..FIELDS_PER_BLOB {
660
657
fields [i ] = F ::from (i as Field );
661
658
}
0 commit comments