Skip to content

Commit

Permalink
fix previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Velaciela committed Oct 9, 2023
1 parent 233dbc6 commit 0ba9621
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 113 deletions.
73 changes: 21 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ blake2b_simd = "1"
sha3 = "0.9.1"
subtle = "2.3"
cfg-if = "0.1"
poseidon = { git = "https://github.com/scroll-tech/poseidon.git", branch = "scroll-dev-0220" }
poseidon = { git = "https://github.com/privacy-scaling-explorations/poseidon.git", tag = "v2023_04_20" }
num-integer = "0.1"
num-bigint = { version = "0.4", features = ["rand"] }

Expand Down
30 changes: 20 additions & 10 deletions halo2_proofs/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn serial_split_fft<Scalar: Field, G: FftGroup<Scalar>>(

let mut k = 0;
while k < n {
let mut w = G::Scalar::ONE;
let mut w = Scalar::ONE;
for j in 0..m {
let mut t = a[(k + j + m) as usize];
t *= &w;
Expand Down Expand Up @@ -293,10 +293,15 @@ fn split_radix_fft<Scalar: Field, G: FftGroup<Scalar>>(

// we use out-place bitreverse here, split_m <= num_threads, so the buffer spase is small
// and it's is good for data locality
let mut t1 = vec![G::Scalar::ZERO; split_m];
// COPY `a` to init temp buffer,
// it's a workaround for G: FftGroup,
// used to be: vec![G::identity; split_m];
// let mut t1 = a.clone();
// if unsafe code is allowed, a 10% performance improvement can be achieved
// let mut t1: Vec<G> = Vec::with_capacity(split_m as usize);
// unsafe{ t1.set_len(split_m as usize); }
let mut t1: Vec<G> = Vec::with_capacity(split_m as usize);
unsafe {
t1.set_len(split_m as usize);
}
for i in 0..split_m {
t1[bitreverse(i, log_split)] = a[(i * sub_n + sub_fft_offset)];
}
Expand All @@ -310,7 +315,7 @@ fn split_radix_fft<Scalar: Field, G: FftGroup<Scalar>>(
if high_idx > 0 {
omega = omega * twiddle_lut[(1 << sparse_degree) + high_idx];
}
let mut w_m = G::Scalar::ONE;
let mut w_m = Scalar::ONE;
for i in 0..split_m {
t1[i] *= &w_m;
tmp[i] = t1[i];
Expand All @@ -329,7 +334,7 @@ pub fn generate_twiddle_lookup_table<F: Field>(

// dense
if is_lut_len_large {
let mut twiddle_lut = vec![F::zero(); (1 << log_n) as usize];
let mut twiddle_lut = vec![F::ZERO; (1 << log_n) as usize];
parallelize(&mut twiddle_lut, |twiddle_lut, start| {
let mut w_n = omega.pow_vartime([start as u64, 0, 0, 0]);
for twiddle_lut in twiddle_lut.iter_mut() {
Expand All @@ -343,7 +348,7 @@ pub fn generate_twiddle_lookup_table<F: Field>(
// sparse
let low_degree_lut_len = 1 << sparse_degree;
let high_degree_lut_len = 1 << (log_n - sparse_degree - without_last_level as u32);
let mut twiddle_lut = vec![F::zero(); (low_degree_lut_len + high_degree_lut_len) as usize];
let mut twiddle_lut = vec![F::ZERO; (low_degree_lut_len + high_degree_lut_len) as usize];
parallelize(
&mut twiddle_lut[..low_degree_lut_len],
|twiddle_lut, start| {
Expand Down Expand Up @@ -378,10 +383,15 @@ pub fn parallel_fft<Scalar: Field, G: FftGroup<Scalar>>(a: &mut [G], omega: Scal
let twiddle_lut = generate_twiddle_lookup_table(omega, log_n, SPARSE_TWIDDLE_DEGREE, true);

// split fft
let mut tmp = vec![G::Scalar::ZERO; n];
// COPY `a` to init temp buffer,
// it's a workaround for G: FftGroup,
// used to be: vec![G::identity; n];
// let mut tmp = a.clone();
// if unsafe code is allowed, a 10% performance improvement can be achieved
// let mut tmp: Vec<G> = Vec::with_capacity(n);
// unsafe{ tmp.set_len(n); }
let mut tmp: Vec<G> = Vec::with_capacity(n);
unsafe {
tmp.set_len(n);
}
multicore::scope(|scope| {
let a = &*a;
let twiddle_lut = &*twiddle_lut;
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Region {
}

/// The value of a particular cell within the circuit.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Eq)]
pub enum CellValue<F: Field> {
/// An unassigned cell.
Unassigned,
Expand All @@ -110,7 +110,7 @@ pub enum CellValue<F: Field> {
Poison(usize),
}

impl<F: Group + Field> PartialEq for CellValue<F> {
impl<F: Field> PartialEq for CellValue<F> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unassigned, Self::Unassigned) => true,
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/dev/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(super) fn load<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Copy>(
}
*/

pub(super) fn load_slice<'a, F: FieldExt, T: ColumnType, Q: Into<AnyQuery> + Copy>(
pub(super) fn load_slice<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Copy>(
n: i32,
row: i32,
queries: &'a [(Column<T>, Rotation)],
Expand Down
62 changes: 34 additions & 28 deletions halo2_proofs/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::plonk::{Any, Column};
use crate::poly::Polynomial;
use ff::Field;
use ff::FromUniformBytes;
use ff::PrimeField;
use halo2curves::FieldExt;
use halo2curves::{pairing::Engine, serde::SerdeObject, CurveAffine};
use num_bigint::BigUint;
use std::io;
Expand Down Expand Up @@ -42,24 +41,30 @@ pub(crate) trait CurveRead: CurveAffine {
}
impl<C: CurveAffine> CurveRead for C {}

pub fn field_to_bn<F: FieldExt>(f: &F) -> BigUint {
pub fn field_to_bn<F: PrimeField>(f: &F) -> BigUint {
BigUint::from_bytes_le(f.to_repr().as_ref())
}

/// Input a big integer `bn`, compute a field element `f`
/// such that `f == bn % F::MODULUS`.
pub fn bn_to_field<F: FieldExt>(bn: &BigUint) -> F {
pub fn bn_to_field<F: PrimeField>(bn: &BigUint) -> F
where
F: FromUniformBytes<64>,
{
let mut buf = bn.to_bytes_le();
buf.resize(64, 0u8);

let mut buf_array = [0u8; 64];
buf_array.copy_from_slice(buf.as_ref());
F::from_bytes_wide(&buf_array)
F::from_uniform_bytes(&buf_array)
}

/// Input a base field element `b`, output a scalar field
/// element `s` s.t. `s == b % ScalarField::MODULUS`
pub(crate) fn base_to_scalar<C: CurveAffine>(base: &C::Base) -> C::Scalar {
pub(crate) fn base_to_scalar<C: CurveAffine>(base: &C::Base) -> C::Scalar
where
C::Scalar: FromUniformBytes<64>,
{
let bn = field_to_bn(base);
// bn_to_field will perform a mod reduction
bn_to_field(&bn)
Expand All @@ -81,28 +86,29 @@ macro_rules! two_dim_vec_to_vec_of_slice {
};
}

#[cfg(test)]
mod test {
use super::*;
use halo2curves::bn256::{Fq, G1Affine};
use rand_core::OsRng;
#[test]
fn test_conversion() {
// random numbers
for _ in 0..100 {
let b = Fq::random(OsRng);
let bi = field_to_bn(&b);
let b_rec = bn_to_field(&bi);
assert_eq!(b, b_rec);

let s = base_to_scalar::<G1Affine>(&b);
let si = field_to_bn(&s);
// TODO: fixme -- this test has a small probability to fail
// because |base field| > |scalar field|
assert_eq!(si, bi);
}
}
}
// #[cfg(test)]
// mod test {
// use super::*;
// use halo2curves::bn256::{Fq, G1Affine};
// use rand_core::OsRng;
// #[test]
// fn test_conversion() {
// // random numbers
// for _ in 0..100 {
// let b = Fq::random(OsRng);
// let bi = field_to_bn(&b);
// let b_rec = bn_to_field(&bi);
// assert_eq!(b, b_rec);

// let s = base_to_scalar::<G1Affine>(&b);
// let si = field_to_bn(&s);
// // TODO: fixme -- this test has a small probability to fail
// // because |base field| > |scalar field|
// assert_eq!(si, bi);
// }
// }
// }

pub trait SerdeCurveAffine: CurveAffine + SerdeObject {
/// Reads an element from the buffer and parses it according to the `format`:
/// - `Processed`: Reads a compressed curve element and decompress it
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
<C as CurveAffine>::ScalarExt: FromUniformBytes<64>,
{
keygen_pk_impl(params, None, circuit)
}
Expand All @@ -438,6 +439,7 @@ where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
<C as CurveAffine>::ScalarExt: FromUniformBytes<64>,
{
keygen_pk_impl(params, Some(vk), circuit)
}
Expand All @@ -452,6 +454,7 @@ where
C: CurveAffine,
P: Params<'params, C>,
ConcreteCircuit: Circuit<C::Scalar>,
<C as CurveAffine>::ScalarExt: FromUniformBytes<64>,
{
let (domain, cs, config) = create_domain::<C, ConcreteCircuit>(params.k());

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ where
//*cell = C::Scalar::one();
//}
let idx = advice_values.len() - 1;
advice_values[idx] = Scheme::Scalar::one();
advice_values[idx] = Scheme::Scalar::ONE;
}

// Compute commitments to advice column polynomials
Expand Down
Loading

0 comments on commit 0ba9621

Please sign in to comment.