Skip to content

Commit 17e79f4

Browse files
authored
feat: bump bignum (#12196)
@kashbrti I'm opening this against your branch so we can get extra test coverage.
1 parent 1733a2b commit 17e79f4

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

noir-projects/noir-protocol-circuits/crates/blob/Nargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ authors = [""]
55
compiler_version = ">=0.30.0"
66

77
[dependencies]
8-
bigint = { tag = "v0.5.4", git = "https://github.com/noir-lang/noir-bignum" }
8+
bigint = { tag = "v0.6.0", git = "https://github.com/noir-lang/noir-bignum" }
99
types = { path = "../types" }

noir-projects/noir-protocol-circuits/crates/blob/src/blob.nr

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
use crate::{
22
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},
44
};
55

6-
use bigint::{BigNum, BigNumTrait};
6+
use bigint::{BigNumTrait, BLS12_381_Fr as F};
77
use std::ops::{Mul, Neg};
88
use types::{
99
abis::sponge_blob::SpongeBlob,
10-
constants::{BLOBS_PER_BLOCK, FIELDS_PER_BLOB, TWO_POW_64},
10+
constants::{BLOBS_PER_BLOCK, FIELDS_PER_BLOB},
1111
hash::poseidon2_hash_subarray,
1212
traits::Empty,
1313
utils::arrays::array_splice,
1414
};
1515

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-
1916
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];
2118
for i in 0..FIELDS_PER_BLOB {
2219
blob[i] = F::from(blob_as_fields[i]);
2320
}
@@ -154,7 +151,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
154151
// i=0
155152

156153
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.
158155
let partial_sums: [F; FIELDS_PER_BLOB / 8] =
159156
unsafe { __compute_partial_sums(fracs, ROOTS) };
160157

@@ -170,7 +167,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
170167
[fracs[0]], [fracs[1]], [fracs[2]], [fracs[3]], [fracs[4]], [fracs[5]], [fracs[6]],
171168
[fracs[7]],
172169
];
173-
BigNum::evaluate_quadratic_expression(
170+
F::evaluate_quadratic_expression(
174171
lhs,
175172
[[false], [false], [false], [false], [false], [false], [false], [false]],
176173
rhs,
@@ -203,7 +200,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
203200
// => (lhs[8*i] * rhs[8*i] + ... + lhs[8*i + 7] * rhs[8*i + 7]) + partial_sums[i-1] - partial_sums[i] == 0
204201
let linear_terms = [partial_sums[i - 1], partial_sums[i]];
205202

206-
BigNum::evaluate_quadratic_expression(
203+
F::evaluate_quadratic_expression(
207204
/* lhs */ [
208205
[ROOTS[i * 8 + 0]],
209206
[ROOTS[i * 8 + 1]],
@@ -249,7 +246,7 @@ fn barycentric_evaluate_blob_at_z(z: F, ys: [F; FIELDS_PER_BLOB]) -> F {
249246
}
250247

251248
unconstrained fn __compute_factor_helper(z_pow_d: F) -> F {
252-
let one: F = BigNum::one();
249+
let one: F = F::one();
253250
z_pow_d.__sub(one).__mul(D_INV)
254251
}
255252

@@ -264,13 +261,13 @@ fn compute_factor(z: F) -> F {
264261
let z_pow_d = t;
265262

266263
// Safety: We immediately check that this result is correct in the following
267-
// `BigNum::evaluate_quadratic_expression` call.
264+
// `F::evaluate_quadratic_expression` call.
268265
let factor = unsafe { __compute_factor_helper(z_pow_d) };
269266

270267
// (z_pow_d - one) * (D_INV) - factor = 0
271268
// z_pow_d * D_INV - D_INV - factor = 0
272269
if !std::runtime::is_unconstrained() {
273-
BigNum::evaluate_quadratic_expression(
270+
F::evaluate_quadratic_expression(
274271
[[z_pow_d]],
275272
[[false]],
276273
[[D_INV]],
@@ -281,7 +278,7 @@ fn compute_factor(z: F) -> F {
281278
}
282279

283280
// This version doesn't work:
284-
// BigNum::evaluate_quadratic_expression(
281+
// F::evaluate_quadratic_expression(
285282
// [[z_pow_d, one]],
286283
// [[false, true]],
287284
// [[D_INV]],
@@ -298,7 +295,7 @@ unconstrained fn __compute_fracs(
298295
ys: [F; FIELDS_PER_BLOB],
299296
unconstrained_roots: [F; FIELDS_PER_BLOB],
300297
) -> [F; FIELDS_PER_BLOB] {
301-
let mut denoms = [BigNum::zero(); FIELDS_PER_BLOB];
298+
let mut denoms = [F::zero(); FIELDS_PER_BLOB];
302299
for i in 0..FIELDS_PER_BLOB {
303300
denoms[i] = z.__sub(unconstrained_roots[i]); // (z - omega^i)
304301
}
@@ -316,14 +313,14 @@ unconstrained fn __compute_fracs(
316313

317314
fn compute_fracs(z: F, ys: [F; FIELDS_PER_BLOB]) -> [F; FIELDS_PER_BLOB] {
318315
// 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`.
320317
let mut fracs: [F; FIELDS_PER_BLOB] = unsafe { __compute_fracs(z, ys, ROOTS) };
321318

322319
if !std::runtime::is_unconstrained() {
323320
for i in 0..FIELDS_PER_BLOB {
324321
// frac <-- ys[i] / (z + neg_roots[i])
325322
// frac * (z + neg_roots[i]) - ys[i] = 0
326-
BigNum::evaluate_quadratic_expression(
323+
F::evaluate_quadratic_expression(
327324
[[fracs[i]]],
328325
[[false]],
329326
[[z, ROOTS[i].neg()]],
@@ -352,7 +349,7 @@ unconstrained fn __compute_partial_sums(
352349
// k=i*8 + 0
353350

354351
// Need to split off the first iteration.
355-
let mut partial_sum: F = BigNum::zero();
352+
let mut partial_sum: F = F::zero();
356353
for i in 0..8 {
357354
// y_k * ( omega^k / (z - omega^k) )
358355
let summand = unconstrained_roots[i].__mul(fracs[i]);
@@ -395,7 +392,7 @@ unconstrained fn __compute_sum(
395392
// /____ z - omega^i
396393
// i=0
397394

398-
let mut sum: F = BigNum::zero();
395+
let mut sum: F = F::zero();
399396
for i in 0..FIELDS_PER_BLOB {
400397
// y_k * ( omega^k / (z - omega^k) )
401398
let summand = unconstrained_roots[i].__mul(fracs[i]);
@@ -413,11 +410,11 @@ mod tests {
413410
barycentric_evaluate_blob_at_z, check_block_blob_sponge, evaluate_blob, evaluate_blobs,
414411
},
415412
blob_public_inputs::BlobCommitment,
416-
config::{D, D_INV, F, ROOTS},
413+
config::{D, D_INV, ROOTS},
417414
};
418415
use super::{__compute_partial_sums, __compute_sum};
419416
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,
421418
params::BigNumParamsGetter,
422419
};
423420
use types::{
@@ -459,7 +456,7 @@ mod tests {
459456
//* d
460457
//
461458
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());
463460
let lhs = y.__mul(z_minus_1);
464461
assert_eq(lhs, rhs);
465462
}
@@ -517,7 +514,7 @@ mod tests {
517514
let output = evaluate_blob(blob, kzg_commitment_in, hashed_blob);
518515

519516
// y is a BLS field with value 0x212c4f0c0ee5e7dd037110686a4639d191dde7b57ab99b51e4b06e7d827b6c4c
520-
let expected_y: F = BigNum {
517+
let expected_y: F = F {
521518
limbs: [0xdde7b57ab99b51e4b06e7d827b6c4c, 0x4f0c0ee5e7dd037110686a4639d191, 0x212c],
522519
};
523520
assert(expected_y == output.y);
@@ -547,7 +544,7 @@ mod tests {
547544
let output = evaluate_blobs(blob, [kzg_commitment_in; BLOBS_PER_BLOCK], sponge_blob);
548545

549546
// y is a BLS field with value 0x52fd4e272015a79f3889cc9ab1d84bee4326de7d8ced52612ecc9ec137bd38ee
550-
let expected_y: F = BigNum {
547+
let expected_y: F = F {
551548
limbs: [0x26de7d8ced52612ecc9ec137bd38ee, 0x4e272015a79f3889cc9ab1d84bee43, 0x52fd],
552549
};
553550
for j in 0..BLOBS_PER_BLOCK {
@@ -598,19 +595,19 @@ mod tests {
598595

599596
#[test]
600597
unconstrained fn test_barycentric() {
601-
let z: F = BigNum { limbs: [2, 0, 0] };
598+
let z: F = F { limbs: [2, 0, 0] };
602599

603600
// 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];
605602

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] };
609606

610607
// evaluate the blob at z = 2 to yield y:
611608
let y = barycentric_evaluate_blob_at_z(z, ys);
612609

613-
let mut expected_y: [Field; 3] = [0; 3];
610+
let mut expected_y: [u128; 3] = [0; 3];
614611
if (FIELDS_PER_BLOB == 4096) {
615612
// Computed with the eth consensus specs py lib
616613
expected_y =
@@ -627,11 +624,11 @@ mod tests {
627624
}
628625

629626
// 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] {
631628
let two_p = [0x7b4805fffcb7fdfffffffe00000002, 0x4ea6533afa906673b0101343b00aa7, 0x00e7db];
632629
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];
635632

636633
double_modulus[0] = two_p[0] + two_pow_120;
637634
for i in 1..NUM_LIMBS - 1 {
@@ -655,7 +652,7 @@ mod tests {
655652

656653
#[test]
657654
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];
659656
for i in 0..FIELDS_PER_BLOB {
660657
fields[i] = F::from(i as Field);
661658
}

noir-projects/noir-protocol-circuits/crates/blob/src/blob_public_inputs.nr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::config::F;
2-
use bigint::{BigNum, bignum::BigNumTrait};
1+
use bigint::{BigNum, bignum::BigNumTrait, BLS12_381_Fr as F};
32
use std::ops::Add;
43
use types::{
54
constants::{BLOB_PUBLIC_INPUTS, BLOBS_PER_BLOCK},
@@ -58,9 +57,9 @@ impl Serialize<BLOB_PUBLIC_INPUTS> for BlobPublicInputs {
5857
fn serialize(self) -> [Field; BLOB_PUBLIC_INPUTS] {
5958
[
6059
self.z,
61-
self.y.limbs[0],
62-
self.y.limbs[1],
63-
self.y.limbs[2],
60+
self.y.limbs[0] as Field,
61+
self.y.limbs[1] as Field,
62+
self.y.limbs[2] as Field,
6463
self.kzg_commitment.inner[0],
6564
self.kzg_commitment.inner[1],
6665
]
@@ -71,7 +70,7 @@ impl Deserialize<BLOB_PUBLIC_INPUTS> for BlobPublicInputs {
7170
fn deserialize(fields: [Field; BLOB_PUBLIC_INPUTS]) -> Self {
7271
Self {
7372
z: fields[0],
74-
y: BigNum { limbs: [fields[1], fields[2], fields[3]] },
73+
y: BigNum { limbs: [fields[1] as u128, fields[2] as u128, fields[3] as u128] },
7574
kzg_commitment: BlobCommitment { inner: [fields[4], fields[5]] },
7675
}
7776
}

noir-projects/noir-protocol-circuits/crates/blob/src/config.nr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use bigint::{BigNum, fields::bls12_381Fr::BLS12_381_Fr_Params};
1+
use bigint::{BigNum, BLS12_381_Fr as F};
22
use types::constants::FIELDS_PER_BLOB;
33

4-
pub type F = BigNum<3, 255, BLS12_381_Fr_Params>;
5-
64
// TODO(#9982): Delete unconstrained_config.nr and go back to using this file - calculating ROOTS in unconstrained is insecure.
75

86
pub global LOG_FIELDS_PER_BLOB: u32 = 12;

0 commit comments

Comments
 (0)