Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mul_023_by_023 #735

Merged
merged 38 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5176d27
WIP: need to apply FieldExtension on Fq, Fq2 for mul_013_by_013 test
ytham Oct 29, 2024
b64fc74
Fix merge conflicts
ytham Oct 29, 2024
bfb5822
Fix mul_013_by_013
ytham Oct 30, 2024
cd64eac
WIP: isolate issue
ytham Oct 30, 2024
3282f24
Merge branch 'main' of github.com:axiom-crypto/afs-prototype into fea…
ytham Oct 30, 2024
8f4a575
Fix xi
ytham Oct 30, 2024
52a2768
Update IntAdd
ytham Oct 30, 2024
691cf56
fix
luffykai Oct 30, 2024
2524a52
Clean up
ytham Oct 30, 2024
b948ab3
Add chip for mul_013_by_013
ytham Oct 30, 2024
c4a2794
Fix merge conflicts
ytham Oct 30, 2024
62af254
Update w/ new ExprBuilderConfig paradigm
ytham Oct 30, 2024
d2ff3c9
Remove extraneous items
ytham Oct 30, 2024
7773d5c
Fix lints
ytham Oct 30, 2024
390ac31
Add mul_023_by_023, refactor test_utils
ytham Oct 31, 2024
cd021d7
Add chips/chip_set items
ytham Oct 31, 2024
2747516
Fix merge conflicts
ytham Oct 31, 2024
ae2ba7e
address PR comments
ytham Oct 31, 2024
6e1be8f
Additional fixes
ytham Oct 31, 2024
97a6952
Rebase
ytham Oct 31, 2024
bbc5666
Rebase
ytham Oct 31, 2024
b6af597
Fix merge conflicts
ytham Oct 31, 2024
581ed8b
Merge main
ytham Oct 31, 2024
5e935fa
Merge branch 'main' into feat/mul_023_by_023
jonathanpwang Nov 1, 2024
c309450
WIP: test is failing at execute
ytham Nov 1, 2024
6bbb12a
Merge branch 'feat/mul_023_by_023' of github.com:axiom-crypto/afs-pro…
ytham Nov 1, 2024
e998d23
Merge main
ytham Nov 1, 2024
7714fde
Rename tangent_line_023
ytham Nov 1, 2024
c03a5eb
switch test back to pass
ytham Nov 1, 2024
844ad9d
Extra clone
ytham Nov 1, 2024
06951a5
Update adapter to use block size
ytham Nov 1, 2024
551b62c
Fix merge conflicts
ytham Nov 1, 2024
f662df5
use BLOCK_SIZE const in miller tests to prevent confusion
ytham Nov 1, 2024
197ebfe
Fix lint
ytham Nov 1, 2024
b3fb1cd
Update tests to use BLOCK_SIZE const for Rv32VecHeapAdapterChip
ytham Nov 1, 2024
39cf2ac
Merge branch 'main' into feat/mul_023_by_023
jonathanpwang Nov 5, 2024
2cff345
chore: move xi to constructor
jonathanpwang Nov 5, 2024
d4fefd2
chore: clippy
jonathanpwang Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 2 additions & 34 deletions circuits/ecc/src/field_expression/tests.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
use std::{cell::RefCell, rc::Rc, sync::Arc};

use ax_circuit_primitives::{
bigint::utils::*,
var_range::{VariableRangeCheckerBus, VariableRangeCheckerChip},
SubAir, TraceSubRowGenerator,
};
use ax_circuit_primitives::{bigint::utils::*, SubAir, TraceSubRowGenerator};
use ax_stark_backend::interaction::InteractionBuilder;
use ax_stark_sdk::{
any_rap_arc_vec, config::baby_bear_blake3::BabyBearBlake3Engine, engine::StarkFriEngine,
utils::create_seeded_rng,
};
use num_bigint_dig::BigUint;
use p3_air::{Air, BaseAir};
use p3_baby_bear::BabyBear;
use p3_field::AbstractField;
use p3_matrix::{dense::RowMajorMatrix, Matrix};
use rand::RngCore;

use super::{super::test_utils::*, ExprBuilder, ExprBuilderConfig, FieldExpr, SymbolicExpr};
use super::{super::test_utils::*, ExprBuilder, FieldExpr, SymbolicExpr};
use crate::field_expression::{FieldExprCols, FieldVariable};

const LIMB_BITS: usize = 8;

pub fn generate_random_biguint(prime: &BigUint) -> BigUint {
let mut rng = create_seeded_rng();
let len = 32;
let x = (0..len).map(|_| rng.next_u32()).collect();
let x = BigUint::new(x);
x % prime
}

impl<AB: InteractionBuilder> Air<AB> for FieldExpr {
fn eval(&self, builder: &mut AB) {
let main = builder.main();
Expand All @@ -38,22 +22,6 @@ impl<AB: InteractionBuilder> Air<AB> for FieldExpr {
}
}

fn setup(prime: &BigUint) -> (Arc<VariableRangeCheckerChip>, Rc<RefCell<ExprBuilder>>) {
let range_bus = 1;
let range_decomp = 17; // double needs 17, rests need 16.
let range_checker = Arc::new(VariableRangeCheckerChip::new(VariableRangeCheckerBus::new(
range_bus,
range_decomp,
)));
let config = ExprBuilderConfig {
modulus: prime.clone(),
limb_bits: LIMB_BITS,
num_limbs: 32,
};
let builder = ExprBuilder::new(config, range_checker.range_max_bits());
(range_checker, Rc::new(RefCell::new(builder)))
}

#[test]
fn test_add() {
let prime = secp256k1_coord_prime();
Expand Down
29 changes: 15 additions & 14 deletions circuits/ecc/src/field_extension/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Fp12 {
x4: &mut Fp2,
xi: [isize; 2],
) -> Fp12 {
// The following uses the formula from Fp12 mul with co5 (x5) = 0
// c0 = cs0co0 + xi(cs1co2 + cs2co1 + cs4co4 + cs5co3)
// c1 = cs0co1 + cs1co0 + cs3co3 + xi(cs2co2 + cs5co4)
// c2 = cs0co2 + cs1co1 + cs2co0 + cs3co4 + cs4co3
Expand Down Expand Up @@ -374,8 +375,8 @@ mod tests {
let x_fq12 = x;
let y_fq12 = y;
let r_fq12 = fq12_fn(&x_fq12, &y_fq12);
let mut inputs = bn254_fq12_to_biguint_vec(&x_fq12);
inputs.extend(bn254_fq12_to_biguint_vec(&y_fq12));
let mut inputs = bn254_fq12_to_biguint_vec(x_fq12);
inputs.extend(bn254_fq12_to_biguint_vec(y_fq12));

let mut row = vec![BabyBear::zero(); width];
air.generate_subrow((&range_checker, inputs, vec![]), &mut row);
Expand All @@ -395,18 +396,18 @@ mod tests {
let r_c9 = evaluate_biguint(&vars[indices[9]], LIMB_BITS);
let r_c10 = evaluate_biguint(&vars[indices[10]], LIMB_BITS);
let r_c11 = evaluate_biguint(&vars[indices[11]], LIMB_BITS);
let exp_r_c0_c0_c0 = bn254_fq_to_biguint(&r_fq12.c0.c0.c0);
let exp_r_c0_c0_c1 = bn254_fq_to_biguint(&r_fq12.c0.c0.c1);
let exp_r_c0_c1_c0 = bn254_fq_to_biguint(&r_fq12.c0.c1.c0);
let exp_r_c0_c1_c1 = bn254_fq_to_biguint(&r_fq12.c0.c1.c1);
let exp_r_c0_c2_c0 = bn254_fq_to_biguint(&r_fq12.c0.c2.c0);
let exp_r_c0_c2_c1 = bn254_fq_to_biguint(&r_fq12.c0.c2.c1);
let exp_r_c1_c0_c0 = bn254_fq_to_biguint(&r_fq12.c1.c0.c0);
let exp_r_c1_c0_c1 = bn254_fq_to_biguint(&r_fq12.c1.c0.c1);
let exp_r_c1_c1_c0 = bn254_fq_to_biguint(&r_fq12.c1.c1.c0);
let exp_r_c1_c1_c1 = bn254_fq_to_biguint(&r_fq12.c1.c1.c1);
let exp_r_c1_c2_c0 = bn254_fq_to_biguint(&r_fq12.c1.c2.c0);
let exp_r_c1_c2_c1 = bn254_fq_to_biguint(&r_fq12.c1.c2.c1);
let exp_r_c0_c0_c0 = bn254_fq_to_biguint(r_fq12.c0.c0.c0);
let exp_r_c0_c0_c1 = bn254_fq_to_biguint(r_fq12.c0.c0.c1);
let exp_r_c0_c1_c0 = bn254_fq_to_biguint(r_fq12.c0.c1.c0);
let exp_r_c0_c1_c1 = bn254_fq_to_biguint(r_fq12.c0.c1.c1);
let exp_r_c0_c2_c0 = bn254_fq_to_biguint(r_fq12.c0.c2.c0);
let exp_r_c0_c2_c1 = bn254_fq_to_biguint(r_fq12.c0.c2.c1);
let exp_r_c1_c0_c0 = bn254_fq_to_biguint(r_fq12.c1.c0.c0);
let exp_r_c1_c0_c1 = bn254_fq_to_biguint(r_fq12.c1.c0.c1);
let exp_r_c1_c1_c0 = bn254_fq_to_biguint(r_fq12.c1.c1.c0);
let exp_r_c1_c1_c1 = bn254_fq_to_biguint(r_fq12.c1.c1.c1);
let exp_r_c1_c2_c0 = bn254_fq_to_biguint(r_fq12.c1.c2.c0);
let exp_r_c1_c2_c1 = bn254_fq_to_biguint(r_fq12.c1.c2.c1);

assert_eq!(r_c0, exp_r_c0_c0_c0);
assert_eq!(r_c1, exp_r_c0_c0_c1);
Expand Down
28 changes: 14 additions & 14 deletions circuits/ecc/src/field_extension/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ mod tests {

fn two_fp2_input(x: &Fq2, y: &Fq2) -> Vec<BigUint> {
vec![
bn254_fq_to_biguint(&x.c0),
bn254_fq_to_biguint(&x.c1),
bn254_fq_to_biguint(&y.c0),
bn254_fq_to_biguint(&y.c1),
bn254_fq_to_biguint(x.c0),
bn254_fq_to_biguint(x.c1),
bn254_fq_to_biguint(y.c0),
bn254_fq_to_biguint(y.c1),
]
}

Expand Down Expand Up @@ -207,8 +207,8 @@ mod tests {
assert_eq!(vars.len(), 2);
let r_c0 = evaluate_biguint(&vars[0], LIMB_BITS);
let r_c1 = evaluate_biguint(&vars[1], LIMB_BITS);
let expected_c0 = bn254_fq_to_biguint(&r_fp2.c0);
let expected_c1 = bn254_fq_to_biguint(&r_fp2.c1);
let expected_c0 = bn254_fq_to_biguint(r_fp2.c0);
let expected_c1 = bn254_fq_to_biguint(r_fp2.c1);
assert_eq!(r_c0, expected_c0);
assert_eq!(r_c1, expected_c1);

Expand Down Expand Up @@ -260,12 +260,12 @@ mod tests {
let z_fp2 = bn254_fq2_random(95);
let r_fp2 = z_fp2.invert().unwrap() * x_fp2 * y_fp2;
let inputs = vec![
bn254_fq_to_biguint(&x_fp2.c0),
bn254_fq_to_biguint(&x_fp2.c1),
bn254_fq_to_biguint(&y_fp2.c0),
bn254_fq_to_biguint(&y_fp2.c1),
bn254_fq_to_biguint(&z_fp2.c0),
bn254_fq_to_biguint(&z_fp2.c1),
bn254_fq_to_biguint(x_fp2.c0),
bn254_fq_to_biguint(x_fp2.c1),
bn254_fq_to_biguint(y_fp2.c0),
bn254_fq_to_biguint(y_fp2.c1),
bn254_fq_to_biguint(z_fp2.c0),
bn254_fq_to_biguint(z_fp2.c1),
];
let mut row = vec![BabyBear::zero(); width];
air.generate_subrow((&range_checker, inputs, vec![]), &mut row);
Expand All @@ -275,8 +275,8 @@ mod tests {
assert_eq!(vars.len(), 2);
let r_c0 = evaluate_biguint(&vars[0], LIMB_BITS);
let r_c1 = evaluate_biguint(&vars[1], LIMB_BITS);
let expected_c0 = bn254_fq_to_biguint(&r_fp2.c0);
let expected_c1 = bn254_fq_to_biguint(&r_fp2.c1);
let expected_c0 = bn254_fq_to_biguint(r_fp2.c0);
let expected_c1 = bn254_fq_to_biguint(r_fp2.c1);
assert_eq!(r_c0, expected_c0);
assert_eq!(r_c1, expected_c1);

Expand Down
117 changes: 0 additions & 117 deletions circuits/ecc/src/test_utils.rs

This file was deleted.

38 changes: 38 additions & 0 deletions circuits/ecc/src/test_utils/bls12381.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use ax_stark_sdk::utils::create_seeded_rng_with_seed;
use halo2curves_axiom::{
bls12_381::{Fq, Fq12, Fq2},
ff::Field,
};
use num_bigint_dig::BigUint;

pub fn bls12381_fq_to_biguint(fq: Fq) -> BigUint {
let bytes = fq.to_bytes();
BigUint::from_bytes_le(&bytes)
}

pub fn bls12381_fq2_to_biguint_vec(x: Fq2) -> Vec<BigUint> {
vec![bls12381_fq_to_biguint(x.c0), bls12381_fq_to_biguint(x.c1)]
}

pub fn bls12381_fq12_to_biguint_vec(x: Fq12) -> Vec<BigUint> {
vec![
bls12381_fq_to_biguint(x.c0.c0.c0),
bls12381_fq_to_biguint(x.c0.c0.c1),
bls12381_fq_to_biguint(x.c0.c1.c0),
bls12381_fq_to_biguint(x.c0.c1.c1),
bls12381_fq_to_biguint(x.c0.c2.c0),
bls12381_fq_to_biguint(x.c0.c2.c1),
bls12381_fq_to_biguint(x.c1.c0.c0),
bls12381_fq_to_biguint(x.c1.c0.c1),
bls12381_fq_to_biguint(x.c1.c1.c0),
bls12381_fq_to_biguint(x.c1.c1.c1),
bls12381_fq_to_biguint(x.c1.c2.c0),
bls12381_fq_to_biguint(x.c1.c2.c1),
]
}

pub fn bls12381_fq12_random(seed: u64) -> Vec<BigUint> {
let seed = create_seeded_rng_with_seed(seed);
let fq = Fq12::random(seed);
bls12381_fq12_to_biguint_vec(fq)
}
42 changes: 42 additions & 0 deletions circuits/ecc/src/test_utils/bn254.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use ax_stark_sdk::utils::create_seeded_rng_with_seed;
use halo2curves_axiom::{
bn256::{Fq, Fq12, Fq2},
ff::Field,
};
use num_bigint_dig::BigUint;

pub fn bn254_fq_to_biguint(fq: Fq) -> BigUint {
let bytes = fq.to_bytes();
BigUint::from_bytes_le(&bytes)
}

pub fn bn254_fq2_to_biguint_vec(x: Fq2) -> Vec<BigUint> {
vec![bn254_fq_to_biguint(x.c0), bn254_fq_to_biguint(x.c1)]
}

pub fn bn254_fq12_to_biguint_vec(x: Fq12) -> Vec<BigUint> {
vec![
bn254_fq_to_biguint(x.c0.c0.c0),
bn254_fq_to_biguint(x.c0.c0.c1),
bn254_fq_to_biguint(x.c0.c1.c0),
bn254_fq_to_biguint(x.c0.c1.c1),
bn254_fq_to_biguint(x.c0.c2.c0),
bn254_fq_to_biguint(x.c0.c2.c1),
bn254_fq_to_biguint(x.c1.c0.c0),
bn254_fq_to_biguint(x.c1.c0.c1),
bn254_fq_to_biguint(x.c1.c1.c0),
bn254_fq_to_biguint(x.c1.c1.c1),
bn254_fq_to_biguint(x.c1.c2.c0),
bn254_fq_to_biguint(x.c1.c2.c1),
]
}

pub fn bn254_fq2_random(seed: u64) -> Fq2 {
let seed = create_seeded_rng_with_seed(seed);
Fq2::random(seed)
}

pub fn bn254_fq12_random(seed: u64) -> Fq12 {
let seed = create_seeded_rng_with_seed(seed);
Fq12::random(seed)
}
Loading
Loading