Skip to content

Commit

Permalink
feat: public cells to allow for implementations of custom Layouter (#…
Browse files Browse the repository at this point in the history
…192)

* feat: public cells

* Update mds.rs

* Update mds.rs

* Update single_pass.rs

Co-authored-by: Han <[email protected]>

* bump toolchain to resolve errors

* fix clippy errors for CI run

* rustfmt post clippy

* plz let it be the last lint

* patch clippy lints in gadgets

* clippy lints for sha256 bench

* patch halo2proof benches

* Update assigned.rs

* Update halo2_gadgets/src/poseidon/primitives/mds.rs

Co-authored-by: Han <[email protected]>

* Update halo2_gadgets/src/poseidon/primitives/mds.rs

Co-authored-by: Han <[email protected]>

---------

Co-authored-by: Han <[email protected]>
  • Loading branch information
alexander-camuto and han0110 authored Aug 25, 2023
1 parent f348757 commit 0f00047
Show file tree
Hide file tree
Showing 28 changed files with 115 additions and 138 deletions.
2 changes: 1 addition & 1 deletion halo2_gadgets/benches/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<const WIDTH: usize, const RATE: usize> Spec<Fp, WIDTH, RATE> for MySpec<WID
}

fn sbox(val: Fp) -> Fp {
val.pow_vartime(&[5])
val.pow_vartime([5])
}

fn secure_mds() -> usize {
Expand Down
12 changes: 6 additions & 6 deletions halo2_gadgets/benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {

// Initialize the polynomial commitment parameters
let params_path = Path::new("./benches/sha256_assets/sha256_params");
if File::open(&params_path).is_err() {
if File::open(params_path).is_err() {
let params: ParamsIPA<EqAffine> = ParamsIPA::new(k);
let mut buf = Vec::new();

params.write(&mut buf).expect("Failed to write params");
let mut file = File::create(&params_path).expect("Failed to create sha256_params");
let mut file = File::create(params_path).expect("Failed to create sha256_params");

file.write_all(&buf[..])
.expect("Failed to write params to file");
}

let params_fs = File::open(&params_path).expect("couldn't load sha256_params");
let params_fs = File::open(params_path).expect("couldn't load sha256_params");
let params: ParamsIPA<EqAffine> =
ParamsIPA::read::<_>(&mut BufReader::new(params_fs)).expect("Failed to read params");

Expand All @@ -128,7 +128,7 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {

// Create a proof
let proof_path = Path::new("./benches/sha256_assets/sha256_proof");
if File::open(&proof_path).is_err() {
if File::open(proof_path).is_err() {
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<_>, ProverIPA<_>, _, _, _, _>(
&params,
Expand All @@ -140,11 +140,11 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
)
.expect("proof generation should not fail");
let proof: Vec<u8> = transcript.finalize();
let mut file = File::create(&proof_path).expect("Failed to create sha256_proof");
let mut file = File::create(proof_path).expect("Failed to create sha256_proof");
file.write_all(&proof[..]).expect("Failed to write proof");
}

let mut proof_fs = File::open(&proof_path).expect("couldn't load sha256_proof");
let mut proof_fs = File::open(proof_path).expect("couldn't load sha256_proof");
let mut proof = Vec::<u8>::new();
proof_fs
.read_to_end(&mut proof)
Expand Down
12 changes: 6 additions & 6 deletions halo2_gadgets/src/ecc/chip/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn compute_window_table<C: CurveAffine>(base: C, num_windows: usize) -> Vec<[C;
.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]);
* C::Scalar::from(H as u64).pow([w as u64, 0, 0, 0]);
(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, 0, 0, 0])
});
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, 0, 0, 0])
- 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, 0, 0, 0]);
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, 0, 0, 0])
});
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, 0, 0, 0])
- offset;
let point = base * scalar;
let x = *point.to_affine().coordinates().unwrap().x();
Expand Down
8 changes: 4 additions & 4 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, 0, 0, 0]));

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, 0, 0, 0])
});

// `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, 0, 0, 0]) - offset_acc);

self.process_window::<_, NUM_WINDOWS>(
region,
Expand Down Expand Up @@ -490,7 +490,7 @@ impl ScalarFixed {
.by_vals()
.take(FIXED_BASE_WINDOW_SIZE)
.rev()
.fold(0, |acc, b| 2 * acc + if b { 1 } else { 0 })
.fold(0, |acc, b| 2 * acc + usize::from(b))
})
})
.collect::<Vec<_>>()
Expand Down
6 changes: 3 additions & 3 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
.value()
.map(|v| *v + config.round_constants[round][idx])
});
let r: Value<Vec<F>> = q.map(|q| q.map(|q| q.pow(&config.alpha))).collect();
let r: Value<Vec<F>> = q.map(|q| q.map(|q| q.pow(config.alpha))).collect();
let m = &config.m_reg;
let state = m.iter().map(|m_i| {
r.as_ref().map(|r| {
Expand All @@ -470,7 +470,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
let p: Value<Vec<_>> = self.0.iter().map(|word| word.0.value().cloned()).collect();

let r: Value<Vec<_>> = p.map(|p| {
let r_0 = (p[0] + config.round_constants[round][0]).pow(&config.alpha);
let r_0 = (p[0] + config.round_constants[round][0]).pow(config.alpha);
let r_i = p[1..]
.iter()
.enumerate()
Expand Down Expand Up @@ -510,7 +510,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
}

let r_mid: Value<Vec<_>> = p_mid.map(|p| {
let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(&config.alpha);
let r_0 = (p[0] + config.round_constants[round + 1][0]).pow(config.alpha);
let r_i = p[1..]
.iter()
.enumerate()
Expand Down
4 changes: 2 additions & 2 deletions halo2_gadgets/src/poseidon/primitives/p128pow5t3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Spec<Fp, 3, 2> for P128Pow5T3 {
}

fn sbox(val: Fp) -> Fp {
val.pow_vartime(&[5])
val.pow_vartime([5])
}

fn secure_mds() -> usize {
Expand All @@ -48,7 +48,7 @@ impl Spec<Fq, 3, 2> for P128Pow5T3 {
}

fn sbox(val: Fq) -> Fq {
val.pow_vartime(&[5])
val.pow_vartime([5])
}

fn secure_mds() -> usize {
Expand Down
3 changes: 1 addition & 2 deletions halo2_proofs/benches/commit_zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ fn rand_poly_par(mut rng: ChaCha20Rng, domain: usize) -> Vec<Scalar> {
// Sample a random polynomial of degree n - 1
let n_threads = current_num_threads();
let n = 1usize << domain;
let n_chunks = n_threads + if n % n_threads != 0 { 1 } else { 0 };
let n_chunks = n_threads + usize::from(n % n_threads != 0);
let mut rand_vec = vec![Scalar::zero(); n];

let mut thread_seeds: Vec<ChaCha20Rng> = (0..n_chunks)
.into_iter()
.map(|_| {
let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed);
Expand Down
10 changes: 5 additions & 5 deletions halo2_proofs/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn best_fft<Scalar: Field, G: FftGroup<Scalar>>(a: &mut [G], omega: Scalar,

let threads = multicore::current_num_threads();
let log_threads = log2_floor(threads);
let n = a.len() as usize;
let n = a.len();
assert_eq!(n, 1 << log_n);

for k in 0..n {
Expand All @@ -206,7 +206,7 @@ pub fn best_fft<Scalar: Field, G: FftGroup<Scalar>>(a: &mut [G], omega: Scalar,
}

// precompute twiddle factors
let twiddles: Vec<_> = (0..(n / 2) as usize)
let twiddles: Vec<_> = (0..(n / 2))
.scan(Scalar::ONE, |w, _| {
let tw = *w;
*w *= &omega;
Expand All @@ -216,7 +216,7 @@ pub fn best_fft<Scalar: Field, G: FftGroup<Scalar>>(a: &mut [G], omega: Scalar,

if log_n <= log_threads {
let mut chunk = 2_usize;
let mut twiddle_chunk = (n / 2) as usize;
let mut twiddle_chunk = n / 2;
for _ in 0..log_n {
a.chunks_mut(chunk).for_each(|coeffs| {
let (left, right) = coeffs.split_at_mut(chunk / 2);
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn recursive_butterfly_arithmetic<Scalar: Field, G: FftGroup<Scalar>>(

/// Convert coefficient bases group elements to lagrange basis by inverse FFT.
pub fn g_to_lagrange<C: CurveAffine>(g_projective: Vec<C::Curve>, k: u32) -> Vec<C> {
let n_inv = C::Scalar::TWO_INV.pow_vartime(&[k as u64, 0, 0, 0]);
let n_inv = C::Scalar::TWO_INV.pow_vartime([k as u64, 0, 0, 0]);
let mut omega_inv = C::Scalar::ROOT_OF_UNITY_INV;
for _ in k..C::Scalar::S {
omega_inv = omega_inv.square();
Expand Down Expand Up @@ -335,7 +335,7 @@ pub fn eval_polynomial<F: Field>(poly: &[F], point: F) -> F {
{
scope.spawn(move |_| {
let start = chunk_idx * chunk_size;
out[0] = evaluate(poly, point) * point.pow_vartime(&[start as u64, 0, 0, 0]);
out[0] = evaluate(poly, point) * point.pow_vartime([start as u64, 0, 0, 0]);
});
}
});
Expand Down
7 changes: 4 additions & 3 deletions halo2_proofs/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use value::Value;

pub mod floor_planner;
pub use floor_planner::single_pass::SimpleFloorPlanner;
pub use floor_planner::single_pass::SimpleTableLayouter;

pub mod layouter;

Expand Down Expand Up @@ -87,11 +88,11 @@ impl std::ops::Deref for RegionStart {
#[derive(Clone, Copy, Debug)]
pub struct Cell {
/// Identifies the region in which this cell resides.
region_index: RegionIndex,
pub region_index: RegionIndex,
/// The relative offset of this cell within its region.
row_offset: usize,
pub row_offset: usize,
/// The column of this cell.
column: Column<Any>,
pub column: Column<Any>,
}

/// An assigned cell.
Expand Down
10 changes: 6 additions & 4 deletions halo2_proofs/src/circuit/floor_planner/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,12 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
/// witnesses or not.
type DefaultTableValue<F> = Option<Value<Assigned<F>>>;

pub(crate) struct SimpleTableLayouter<'r, 'a, F: Field, CS: Assignment<F> + 'a> {
/// A table layouter that can be used to assign values to a table.
pub struct SimpleTableLayouter<'r, 'a, F: Field, CS: Assignment<F> + 'a> {
cs: &'a mut CS,
used_columns: &'r [TableColumn],
// maps from a fixed column to a pair (default value, vector saying which rows are assigned)
pub(crate) default_and_assigned: HashMap<TableColumn, (DefaultTableValue<F>, Vec<bool>)>,
/// maps from a fixed column to a pair (default value, vector saying which rows are assigned)
pub default_and_assigned: HashMap<TableColumn, (DefaultTableValue<F>, Vec<bool>)>,
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug for SimpleTableLayouter<'r, 'a, F, CS> {
Expand All @@ -415,7 +416,8 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug for SimpleTableLayoute
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> SimpleTableLayouter<'r, 'a, F, CS> {
pub(crate) fn new(cs: &'a mut CS, used_columns: &'r [TableColumn]) -> Self {
/// Returns a new SimpleTableLayouter
pub fn new(cs: &'a mut CS, used_columns: &'r [TableColumn]) -> Self {
SimpleTableLayouter {
cs,
used_columns,
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/circuit/floor_planner/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ impl FloorPlanner for V1 {

// - Determine how many rows our planned circuit will require.
let first_unassigned_row = column_allocations
.iter()
.map(|(_, a)| a.unbounded_interval_start())
.values()
.map(|a| a.unbounded_interval_start())
.max()
.unwrap_or(0);

Expand Down
9 changes: 1 addition & 8 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,7 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
.flat_map(|(gate_index, gate)| {
let blinding_rows =
(self.n as usize - (self.cs.blinding_factors() + 1))..(self.n as usize);
(gate_row_ids
.clone()
.into_iter()
.chain(blinding_rows.into_iter()))
.flat_map(move |row| {
(gate_row_ids.clone().chain(blinding_rows.into_iter())).flat_map(move |row| {
let row = row as i32 + n;
gate.polynomials().iter().enumerate().filter_map(
move |(poly_index, poly)| match poly.evaluate_lazy(
Expand Down Expand Up @@ -955,7 +951,6 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {

let mut inputs: Vec<(Vec<_>, usize)> = lookup_input_row_ids
.clone()
.into_iter()
.filter_map(|input_row| {
let t = lookup
.input_expressions
Expand Down Expand Up @@ -1026,7 +1021,6 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
let mut input_rows: Vec<(Vec<Value<F>>, usize)> = self
.usable_rows
.clone()
.into_iter()
.map(|input_row| {
let t = shuffle
.input_expressions
Expand Down Expand Up @@ -1449,7 +1443,6 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
let mut input_rows: Vec<(Vec<Value<F>>, usize)> = self
.usable_rows
.clone()
.into_iter()
.map(|input_row| {
let t = shuffle
.input_expressions
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/dev/graph/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl CircuitLayout {

root.draw(&Rectangle::new(
[(0, 0), (total_columns, view_bottom)],
&BLACK,
BLACK,
))?;

let draw_region = |root: &DrawingArea<_, _>, top_left, bottom_right| {
Expand All @@ -200,7 +200,7 @@ impl CircuitLayout {
[top_left, bottom_right],
ShapeStyle::from(&GREEN.mix(0.2)).filled(),
))?;
root.draw(&Rectangle::new([top_left, bottom_right], &BLACK))?;
root.draw(&Rectangle::new([top_left, bottom_right], BLACK))?;
Ok(())
};

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
.map(|mut selector| {
let mut selector_bytes = vec![0u8; (selector.len() + 7) / 8];
reader.read_exact(&mut selector_bytes)?;
for (bits, byte) in selector.chunks_mut(8).into_iter().zip(selector_bytes) {
for (bits, byte) in selector.chunks_mut(8).zip(selector_bytes) {
crate::helpers::unpack(byte, bits);
}
Ok(selector)
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/assigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ mod proptests {
// Ensure that:
// - we have at least one value to apply unary operators to.
// - we can apply every binary operator pairwise sequentially.
cmp::max(if num_unary > 0 { 1 } else { 0 }, num_binary + 1)),
cmp::max(usize::from(num_unary > 0), num_binary + 1)),
operations in arb_operators(num_unary, num_binary).prop_shuffle(),
) -> (Vec<Assigned<Fp>>, Vec<Operator>) {
(values, operations)
Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ pub struct TableColumn {
}

impl TableColumn {
pub(crate) fn inner(&self) -> Column<Fixed> {
/// Returns inner column
pub fn inner(&self) -> Column<Fixed> {
self.inner
}
}
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ impl<C: CurveAffine> Evaluator<C> {

// Permutation constraints
parallelize(&mut values, |values, start| {
let mut beta_term = extended_omega.pow_vartime(&[start as u64, 0, 0, 0]);
let mut beta_term = extended_omega.pow_vartime([start as u64, 0, 0, 0]);
for (i, value) in values.iter_mut().enumerate() {
let idx = start + i;
let r_next = get_rotation_idx(idx, 1, rot_scale, isize);
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/lookup/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ fn permute_expression_pair<'params, C: CurveAffine, P: Params<'params, C>, R: Rn
// Populate permuted table at unfilled rows with leftover table elements
for (coeff, count) in leftover_table_map.iter() {
for _ in 0..*count {
permuted_table_coeffs[repeated_input_rows.pop().unwrap() as usize] = *coeff;
permuted_table_coeffs[repeated_input_rows.pop().unwrap()] = *coeff;
}
}
assert!(repeated_input_rows.is_empty());
Expand Down
Loading

0 comments on commit 0f00047

Please sign in to comment.