Skip to content

Commit

Permalink
feat: pick rest changes of halo2_proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
han0110 committed Aug 29, 2023
1 parent 05ab478 commit 296c6a5
Show file tree
Hide file tree
Showing 55 changed files with 256 additions and 1,060 deletions.
1 change: 1 addition & 0 deletions halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test-dev-graph = [
"plotters",
"plotters/bitmap_backend",
"plotters/bitmap_encoder",
"plotters/ttf",
]
circuit-params = ["halo2_proofs/circuit-params"]
test-dependencies = ["proptest"]
Expand Down
9 changes: 6 additions & 3 deletions halo2_gadgets/benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2curves::pasta::{pallas, EqAffine};
use rand::rngs::OsRng;

use std::{
fs::File,
fs::{create_dir_all, File},
io::{prelude::*, BufReader},
path::Path,
};
Expand Down Expand Up @@ -88,6 +88,9 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
}
}

// Create parent directory for assets
create_dir_all("./benches/sha256_assets").expect("Failed to create sha256_assets directory");

// Initialize the polynomial commitment parameters
let params_path = Path::new("./benches/sha256_assets/sha256_params");
if File::open(params_path).is_err() {
Expand Down Expand Up @@ -134,7 +137,7 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
&params,
&pk,
&[circuit],
&[],
&[&[]],
OsRng,
&mut transcript,
)
Expand All @@ -159,7 +162,7 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
&params,
pk.get_vk(),
strategy,
&[],
&[&[]],
&mut transcript,
)
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions halo2_gadgets/src/ecc/chip/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fn compute_window_table<C: CurveAffine>(base: C, num_windows: usize) -> Vec<[C;
(0..H)
.map(|k| {
// scalar = (k+2)*(8^w)
let scalar = C::Scalar::from(k as u64 + 2)
* C::Scalar::from(H as u64).pow([w as u64, 0, 0, 0]);
let scalar =
C::Scalar::from(k as u64 + 2) * C::Scalar::from(H as u64).pow([w as u64]);
(base * scalar).to_affine()
})
.collect::<ArrayVec<C, H>>()
Expand All @@ -62,14 +62,14 @@ fn compute_window_table<C: CurveAffine>(base: C, num_windows: usize) -> Vec<[C;
// For the last window, we compute [k * (2^3)^w - sum]B, where sum is defined
// as sum = \sum_{j = 0}^{`num_windows - 2`} 2^{3j+1}
let sum = (0..(num_windows - 1)).fold(C::Scalar::ZERO, |acc, j| {
acc + C::Scalar::from(2).pow([FIXED_BASE_WINDOW_SIZE as u64 * j as u64 + 1, 0, 0, 0])
acc + C::Scalar::from(2).pow([FIXED_BASE_WINDOW_SIZE as u64 * j as u64 + 1])
});
window_table.push(
(0..H)
.map(|k| {
// scalar = k * (2^3)^w - sum, where w = `num_windows - 1`
let scalar = C::Scalar::from(k as u64)
* C::Scalar::from(H as u64).pow([(num_windows - 1) as u64, 0, 0, 0])
* C::Scalar::from(H as u64).pow([(num_windows - 1) as u64])
- sum;
(base * scalar).to_affine()
})
Expand Down Expand Up @@ -197,7 +197,7 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
// Compute the actual x-coordinate of the multiple [(k+2)*(8^w)]B.
let point = base
* C::Scalar::from(bits as u64 + 2)
* C::Scalar::from(H as u64).pow([idx as u64, 0, 0, 0]);
* C::Scalar::from(H as u64).pow([idx as u64]);
let x = *point.to_affine().coordinates().unwrap().x();

// Check that the interpolated x-coordinate matches the actual one.
Expand All @@ -214,10 +214,10 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
// Compute the actual x-coordinate of the multiple [k * (8^84) - offset]B,
// where offset = \sum_{j = 0}^{83} 2^{3j+1}
let offset = (0..(num_windows - 1)).fold(C::Scalar::ZERO, |acc, w| {
acc + C::Scalar::from(2).pow([FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1, 0, 0, 0])
acc + C::Scalar::from(2).pow([FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1])
});
let scalar = C::Scalar::from(bits as u64)
* C::Scalar::from(H as u64).pow([(num_windows - 1) as u64, 0, 0, 0])
* C::Scalar::from(H as u64).pow([(num_windows - 1) as u64])
- offset;
let point = base * scalar;
let x = *point.to_affine().coordinates().unwrap().x();
Expand Down
6 changes: 3 additions & 3 deletions halo2_gadgets/src/ecc/chip/mul_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> Config<FixedPoints> {
base: &F,
) -> Result<NonIdentityEccPoint, Error> {
// `scalar = [(k_w + 2) ⋅ 8^w]
let scalar = k.map(|k| (k + *TWO_SCALAR) * (*H_SCALAR).pow([w as u64, 0, 0, 0]));
let scalar = k.map(|k| (k + *TWO_SCALAR) * (*H_SCALAR).pow([w as u64]));

self.process_window::<_, NUM_WINDOWS>(region, offset, w, k_usize, scalar, base)
}
Expand All @@ -389,12 +389,12 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> Config<FixedPoints> {

// offset_acc = \sum_{j = 0}^{NUM_WINDOWS - 2} 2^{FIXED_BASE_WINDOW_SIZE*j + 1}
let offset_acc = (0..(NUM_WINDOWS - 1)).fold(pallas::Scalar::zero(), |acc, w| {
acc + (*TWO_SCALAR).pow([FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1, 0, 0, 0])
acc + (*TWO_SCALAR).pow([FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1])
});

// `scalar = [k * 8^(NUM_WINDOWS - 1) - offset_acc]`.
let scalar = scalar.windows_field()[scalar.windows_field().len() - 1]
.map(|k| k * (*H_SCALAR).pow([(NUM_WINDOWS - 1) as u64, 0, 0, 0]) - offset_acc);
.map(|k| k * (*H_SCALAR).pow([(NUM_WINDOWS - 1) as u64]) - offset_acc);

self.process_window::<_, NUM_WINDOWS>(
region,
Expand Down
1 change: 1 addition & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ test-dev-graph = [
"dev-graph",
"plotters/bitmap_backend",
"plotters/bitmap_encoder",
"plotters/ttf",
]
gadget-traces = ["backtrace"]
thread-safe-region = []
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn multiexp_serial<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut

let mut tmp = u64::from_le_bytes(v);
tmp >>= skip_bits - (skip_bytes * 8);
tmp = tmp % (1 << c);
tmp %= 1 << c;

tmp as usize
}
Expand Down Expand Up @@ -110,7 +110,7 @@ fn multiexp_serial<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut
let mut running_sum = C::Curve::identity();
for exp in buckets.into_iter().rev() {
running_sum = exp.add(running_sum);
*acc = *acc + &running_sum;
*acc += &running_sum;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits and structs for implementing circuit components.

use std::{convert::TryInto, fmt, marker::PhantomData};
use std::{fmt, marker::PhantomData};

use ff::Field;

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/circuit/layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ff::Field;

pub use super::table_layouter::TableLayouter;
use super::{Cell, RegionIndex, Value};
use crate::plonk::{Advice, Any, Assigned, Column, Error, Fixed, Instance, Selector, TableColumn};
use crate::plonk::{Advice, Any, Assigned, Column, Error, Fixed, Instance, Selector};

/// Intermediate trait requirements for [`RegionLayouter`] when thread-safe regions are enabled.
#[cfg(feature = "thread-safe-region")]
Expand Down
11 changes: 2 additions & 9 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
//! Tools for developing circuits.

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::iter;
use std::ops::{Add, Mul, Neg, Range};
use std::time::{Duration, Instant};

use blake2b_simd::blake2b;
use ff::Field;
use ff::FromUniformBytes;
use group::Group;

use crate::circuit::layouter::SyncDeps;
use crate::multicore::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
ParallelSliceMut,
Expand All @@ -24,11 +19,9 @@ use crate::{
plonk::{
permutation,
sealed::{self, SealedPhase},
Advice, Any, Assigned, Assignment, Challenge, Circuit, Column, ColumnType,
ConstraintSystem, Error, Expression, FirstPhase, Fixed, FloorPlanner, Instance, Phase,
Selector, VirtualCell,
Advice, Any, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error,
Expression, FirstPhase, Fixed, FloorPlanner, Instance, Phase, Selector,
},
poly::Rotation,
};

pub mod metadata;
Expand Down
1 change: 0 additions & 1 deletion halo2_proofs/src/dev/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::dev::metadata::Constraint;
use crate::{
dev::{Instance, Value},
plonk::{Any, Column, ConstraintSystem, Expression, Gate},
poly::Rotation,
};

mod emitter;
Expand Down
5 changes: 1 addition & 4 deletions halo2_proofs/src/dev/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use ff::PrimeField;

use crate::{
dev::util,
plonk::{
sealed::{self, SealedPhase},
Circuit, ConstraintSystem, FirstPhase,
},
plonk::{sealed::SealedPhase, Circuit, ConstraintSystem, FirstPhase},
};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::poly::Polynomial;
use ff::PrimeField;
use halo2curves::{pairing::Engine, serde::SerdeObject, CurveAffine};
use halo2curves::{serde::SerdeObject, CurveAffine};
use std::io;

/// This enum specifies how various types are serialized and deserialized.
Expand Down
21 changes: 2 additions & 19 deletions halo2_proofs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
//! # halo2_proofs

#![cfg_attr(docsrs, feature(doc_cfg))]
// Build without warnings on stable 1.51 and later.
#![allow(unknown_lints)]
// Disable old lint warnings until our MSRV is at least 1.51.
#![allow(renamed_and_removed_lints)]
// Use the old lint name to build without warnings until our MSRV is at least 1.51.
#![allow(clippy::unknown_clippy_lints)]
// The actual lints we want to disable.
#![allow(
clippy::op_ref,
clippy::assign_op_pattern,
clippy::too_many_arguments,
clippy::suspicious_arithmetic_impl,
clippy::many_single_char_names,
clippy::same_item_push,
clippy::upper_case_acronyms
)]
#![deny(broken_intra_doc_links)]
#![allow(clippy::op_ref, clippy::many_single_char_names)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(unsafe_code)]
// Remove this once we update pasta_curves
#![allow(unused_imports)]
#![allow(clippy::derive_partial_eq_without_eq)]

pub mod arithmetic;
pub mod circuit;
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::helpers::{
SerdePrimeField,
};
use crate::poly::{
commitment::Params, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff,
PinnedEvaluationDomain, Polynomial,
Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, PinnedEvaluationDomain,
Polynomial,
};
use crate::transcript::{ChallengeScalar, EncodedChallenge, Transcript};
use crate::SerdeFormat;
Expand Down
1 change: 0 additions & 1 deletion halo2_proofs/src/plonk/assigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ mod tests {
mod proptests {
use std::{
cmp,
convert::TryFrom,
ops::{Add, Mul, Neg, Sub},
};

Expand Down
5 changes: 3 additions & 2 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use core::cmp::max;
use core::ops::{Add, Mul};
use ff::Field;
use sealed::SealedPhase;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::fmt::Debug;
use std::{
convert::TryFrom,
ops::{Neg, Sub},
Expand Down Expand Up @@ -873,6 +872,7 @@ impl<F: Field> Expression<F> {

/// Evaluate the polynomial using the provided closures to perform the
/// operations.
#[allow(clippy::too_many_arguments)]
pub fn evaluate<T>(
&self,
constant: &impl Fn(F) -> T,
Expand Down Expand Up @@ -982,6 +982,7 @@ impl<F: Field> Expression<F> {

/// Evaluate the polynomial lazily using the provided closures to perform the
/// operations.
#[allow(clippy::too_many_arguments)]
pub fn evaluate_lazy<T: PartialEq>(
&self,
constant: &impl Fn(F) -> T,
Expand Down
1 change: 0 additions & 1 deletion halo2_proofs/src/plonk/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cmp;
use std::error;
use std::fmt;
use std::io;
Expand Down
31 changes: 8 additions & 23 deletions halo2_proofs/src/plonk/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
use crate::multicore;
use crate::plonk::lookup::prover::Committed;
use crate::plonk::permutation::Argument;
use crate::plonk::{lookup, permutation, AdviceQuery, Any, FixedQuery, InstanceQuery, ProvingKey};
use crate::plonk::{lookup, permutation, Any, ProvingKey};
use crate::poly::Basis;
use crate::{
arithmetic::{eval_polynomial, parallelize, CurveAffine},
poly::{
commitment::Params, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff,
Polynomial, ProverQuery, Rotation,
},
transcript::{EncodedChallenge, TranscriptWrite},
};
use group::prime::PrimeCurve;
use group::{
ff::{BatchInvert, Field, PrimeField, WithSmallOrderMulGroup},
Curve,
};
use std::any::TypeId;
use std::convert::TryInto;
use std::num::ParseIntError;
use std::slice;
use std::{
collections::BTreeMap,
iter,
ops::{Index, Mul, MulAssign},
arithmetic::{parallelize, CurveAffine},
poly::{Coeff, ExtendedLagrangeCoeff, Polynomial, Rotation},
};
use group::ff::{Field, PrimeField, WithSmallOrderMulGroup};

use super::{shuffle, ConstraintSystem, Expression};

Expand Down Expand Up @@ -68,6 +49,7 @@ impl Default for ValueSource {

impl ValueSource {
/// Get the value for this source
#[allow(clippy::too_many_arguments)]
pub fn get<F: Field, B: Basis>(
&self,
rotations: &[usize],
Expand Down Expand Up @@ -128,6 +110,7 @@ pub enum Calculation {

impl Calculation {
/// Get the resulting value of this calculation
#[allow(clippy::too_many_arguments)]
pub fn evaluate<F: Field, B: Basis>(
&self,
rotations: &[usize],
Expand Down Expand Up @@ -312,6 +295,7 @@ impl<C: CurveAffine> Evaluator<C> {
}

/// Evaluate h poly
#[allow(clippy::too_many_arguments)]
pub(in crate::plonk) fn evaluate_h(
&self,
pk: &ProvingKey<C>,
Expand Down Expand Up @@ -796,6 +780,7 @@ impl<C: CurveAffine> GraphEvaluator<C> {
}
}

#[allow(clippy::too_many_arguments)]
pub fn evaluate<B: Basis>(
&self,
data: &mut EvaluationData<C>,
Expand Down
7 changes: 3 additions & 4 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use super::{
Selector,
},
evaluation::Evaluator,
permutation, Assigned, Challenge, Error, Expression, LagrangeCoeff, Polynomial, ProvingKey,
VerifyingKey,
permutation, Assigned, Challenge, Error, LagrangeCoeff, Polynomial, ProvingKey, VerifyingKey,
};
use crate::{
arithmetic::{parallelize, CurveAffine},
circuit::{layouter::SyncDeps, Value},
circuit::Value,
poly::{
batch_invert_assigned,
commitment::{Blind, Params, MSM},
commitment::{Blind, Params},
EvaluationDomain,
},
};
Expand Down
Loading

0 comments on commit 296c6a5

Please sign in to comment.