Skip to content

Commit

Permalink
use separte halo2curves with halo2-pse feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltea committed Oct 12, 2023
1 parent 4c107de commit d92820e
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 21 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,5 @@ debug = true
halo2-base = { path = "../halo2-lib/halo2-base" }
halo2-ecc = { path = "../halo2-lib/halo2-ecc" }

[patch."https://github.com/privacy-scaling-explorations/halo2curves"]
halo2curves = { git = "https://github.com/timoftime/halo2curves", branch = "support_bls12-381" }

[patch."https://github.com/axiom-crypto/halo2curves"]
halo2curves = { git = "https://github.com/timoftime/halo2curves", branch = "support_bls12-381" }
2 changes: 1 addition & 1 deletion halo2-base/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub trait ScalarField: PrimeField + FromUniformBytes<64> + From<bool> + Hash + O

/// [ScalarField] that is ~256 bits long
#[cfg(feature = "halo2-pse")]
pub trait BigPrimeField = PrimeField<Repr = [u8; 32]> + ScalarField;
pub trait BigPrimeField = PrimeField + ScalarField;

/// Converts an [Iterator] of u64 digits into `number_of_limbs` limbs of `bit_len` bits returned as a [Vec].
///
Expand Down
5 changes: 4 additions & 1 deletion halo2-ecc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ rayon="1.6.1"
test-case="3.1.0"

halo2-base={ path="../halo2-base", default-features=false }
# Use additiona axiom-crypto halo2curves for BLS12-381 chips when feature = "halo2-pse" is on,
# because the PSE halo2curves does not support BLS12-381 chips and Halo2 depnds on lower major version so patchign it is not possible
halo2curves = { git = "https://github.com/axiom-crypto/halo2curves", optional=true }

[dev-dependencies]
ark-std={ version="0.3.0", features=["print-trace"] }
Expand All @@ -33,7 +36,7 @@ default=["jemallocator", "halo2-axiom", "display"]
dev-graph=["halo2-base/dev-graph"]
display=["halo2-base/display"]
asm=["halo2-base/asm"]
halo2-pse=["halo2-base/halo2-pse"]
halo2-pse=["halo2-base/halo2-pse", "halo2curves"]
halo2-axiom=["halo2-base/halo2-axiom"]
jemallocator=["halo2-base/jemallocator"]
mimalloc=["halo2-base/mimalloc"]
Expand Down
1 change: 0 additions & 1 deletion halo2-ecc/results/bls12_381/pairing_bench.csv

This file was deleted.

2 changes: 1 addition & 1 deletion halo2-ecc/src/bls12_381/final_exp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::XI_0;
use super::{Fp12Chip, Fp2Chip, FpChip, FqPoint};
use crate::halo2_proofs::halo2curves::bls12_381::{Fq, Fq12, Fq2, BLS_X, FROBENIUS_COEFF_FQ12_C1};
use super::{Fq, Fq12, Fq2, BLS_X, FROBENIUS_COEFF_FQ12_C1};
use crate::{
ecc::get_naf,
fields::{fp12::mul_no_carry_w6, vector::FieldVector, FieldChip},
Expand Down
10 changes: 9 additions & 1 deletion halo2-ecc/src/bls12_381/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use crate::bigint::ProperCrtUint;
use crate::fields::vector::FieldVector;
use crate::fields::{fp, fp12, fp2};
use crate::halo2_proofs::halo2curves::bls12_381::{Fq, Fq12, Fq2};

pub mod final_exp;
pub mod pairing;

#[cfg(feature = "halo2-axiom")]
pub(crate) use crate::halo2_proofs::halo2curves::bls12_381::{
Fq, Fq12, Fq2, G1Affine, G2Affine, BLS_X, BLS_X_IS_NEGATIVE, FROBENIUS_COEFF_FQ12_C1,
};
#[cfg(feature = "halo2-pse")]
pub(crate) use halo2curves::bls12_381::{
Fq, Fq12, Fq2, G1Affine, G2Affine, BLS_X, BLS_X_IS_NEGATIVE, FROBENIUS_COEFF_FQ12_C1,
};

pub(crate) const XI_0: i64 = 1;

pub type FpChip<'range, F> = fp::FpChip<'range, F, Fq>;
Expand Down
4 changes: 2 additions & 2 deletions halo2-ecc/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![allow(non_snake_case)]
use super::{Fp12Chip, Fp2Chip, FpChip, FpPoint, Fq, FqPoint, XI_0};
use super::{Fq12, G1Affine, G2Affine, BLS_X, BLS_X_IS_NEGATIVE};
use crate::fields::vector::FieldVector;
use crate::halo2_proofs::halo2curves::bls12_381::{Fq12, G1Affine, G2Affine, BLS_X};
use crate::{
ecc::{EcPoint, EccChip},
fields::fp12::mul_no_carry_w6,
fields::FieldChip,
};
use halo2_base::halo2_proofs::halo2curves::bls12_381::BLS_X_IS_NEGATIVE;

use halo2_base::utils::BigPrimeField;
use halo2_base::Context;

Expand Down
1 change: 0 additions & 1 deletion halo2-ecc/src/bls12_381/tests/ec_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::io::{BufRead, BufReader};

use super::*;
use crate::fields::{FieldChip, FpStrategy};
use crate::halo2_proofs::halo2curves::bls12_381::G2Affine;
use halo2_base::gates::RangeChip;
use halo2_base::utils::testing::base_test;
use halo2_base::utils::BigPrimeField;
Expand Down
10 changes: 6 additions & 4 deletions halo2-ecc/src/bls12_381/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
use super::pairing::PairingChip;
use super::*;
use crate::ecc::EccChip;
use crate::fields::FpStrategy;
use crate::group::Curve;
use crate::{
fields::FpStrategy,
halo2_proofs::halo2curves::bls12_381::{pairing, Fr as Scalar, G1Affine},
};
use halo2_base::utils::testing::base_test;
use rand::rngs::StdRng;
use rand_core::SeedableRng;
use serde::{Deserialize, Serialize};
use std::io::Write;

#[cfg(feature = "halo2-axiom")]
pub(crate) use crate::halo2_proofs::halo2curves::bls12_381::{pairing, Fr as Scalar};
#[cfg(feature = "halo2-pse")]
pub(crate) use halo2curves::bls12_381::{pairing, Fr as Scalar};

pub mod ec_add;
pub mod pairing;
5 changes: 1 addition & 4 deletions halo2-ecc/src/bls12_381/tests/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::fields::FieldChip;

use super::*;
use halo2_base::{
gates::RangeChip,
halo2_proofs::{arithmetic::Field, halo2curves::bls12_381::G2Affine},
utils::BigPrimeField,
Context,
gates::RangeChip, halo2_proofs::arithmetic::Field, utils::BigPrimeField, Context,
};
use serde::{Deserialize, Serialize};

Expand Down
4 changes: 2 additions & 2 deletions halo2-ecc/src/bn254/tests/bls_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use crate::{
};
use halo2_base::{
gates::RangeChip,
halo2_proofs::halo2curves::bn256::{multi_miller_loop, G2Prepared, Gt},
halo2_proofs::halo2curves::{bn256::{multi_miller_loop, G2Prepared, Gt}, pairing::MillerLoopResult},
utils::BigPrimeField,
Context,
};
extern crate pairing;
use pairing::{group::ff::Field, MillerLoopResult};
use crate::group::ff::Field;
use rand_core::OsRng;

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions halo2-ecc/src/fields/fp12.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::marker::PhantomData;

#[cfg(feature = "halo2-axiom")]
use crate::ff::PrimeField as _;
use crate::impl_field_ext_chip_common;

Expand Down Expand Up @@ -252,7 +253,10 @@ mod bn254 {

mod bls12_381 {
use crate::fields::FieldExtConstructor;
#[cfg(feature = "halo2-axiom")]
use crate::halo2_proofs::halo2curves::bls12_381::{Fq, Fq12, Fq2, Fq6};
#[cfg(feature = "halo2-pse")]
use halo2curves::bls12_381::{Fq, Fq12, Fq2, Fq6};
// This means we store an Fp12 point as `\sum_{i = 0}^6 (a_{i0} + a_{i1} * u) * w^i`
// This is encoded in an FqPoint of degree 12 as `(a_{00}, ..., a_{50}, a_{01}, ..., a_{51})`
impl FieldExtConstructor<Fq, 12> for Fq12 {
Expand Down
5 changes: 5 additions & 0 deletions halo2-ecc/src/fields/fp2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;
use std::marker::PhantomData;

#[cfg(feature = "halo2-axiom")]
use crate::ff::PrimeField as _;
use crate::impl_field_ext_chip_common;

Expand Down Expand Up @@ -133,7 +134,11 @@ mod bn254 {

mod bls12_381 {
use crate::fields::FieldExtConstructor;
#[cfg(feature = "halo2-axiom")]
use crate::halo2_proofs::halo2curves::bls12_381::{Fq, Fq2};
#[cfg(feature = "halo2-pse")]
use halo2curves::bls12_381::{Fq, Fq2};

impl FieldExtConstructor<Fq, 2> for Fq2 {
fn new(c: [Fq; 2]) -> Self {
Fq2 { c0: c[0], c1: c[1] }
Expand Down

0 comments on commit d92820e

Please sign in to comment.