Skip to content

Commit

Permalink
update lookuprangecheck configure
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoGalteland committed Apr 16, 2024
1 parent f78bd8b commit bfe5fba
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 100 deletions.
4 changes: 2 additions & 2 deletions halo2_gadgets/src/sinsemilla/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ where
F: FixedPoints<pallas::Affine>,
Commit: CommitDomains<pallas::Affine, F, Hash>,
{
/// A type that holds any general chip state that needs to be loaded at the start of
/// [`Circuit::synthesize`]. This might simply be `()` for some chips.
/// A type that holds any general chip state that needs to be loaded initially.
/// This might simply be `()` for some chips.
type Loaded;
/// The `RangeCheckConfigType` associated type defines the configuration type used by
/// the implementing structure for lookup range check operations.
Expand Down
1 change: 1 addition & 0 deletions halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub mod tests {

use crate::sinsemilla::chip::SinsemillaChipProps;
use crate::utilities::lookup_range_check::LookupRangeCheck;

Check warning on line 199 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `crate::utilities::lookup_range_check::LookupRangeCheck`

warning: unused import: `crate::utilities::lookup_range_check::LookupRangeCheck` --> halo2_gadgets/src/sinsemilla/merkle.rs:199:9 | 199 | use crate::utilities::lookup_range_check::LookupRangeCheck; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check failure on line 199 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `crate::utilities::lookup_range_check::LookupRangeCheck`

error: unused import: `crate::utilities::lookup_range_check::LookupRangeCheck` --> halo2_gadgets/src/sinsemilla/merkle.rs:199:9 | 199 | use crate::utilities::lookup_range_check::LookupRangeCheck; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 199 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with nightly features

unused import: `crate::utilities::lookup_range_check::LookupRangeCheck`
use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized;

Check warning on line 200 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

warning: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/sinsemilla/merkle.rs:200:9 | 200 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check failure on line 200 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

error: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/sinsemilla/merkle.rs:200:9 | 200 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 200 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with nightly features

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`
use rand::{rngs::OsRng, RngCore};
use std::{convert::TryInto, iter};

Check warning on line 202 in halo2_gadgets/src/sinsemilla/merkle.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `TryInto` is imported redundantly

warning: the item `TryInto` is imported redundantly --> halo2_gadgets/src/sinsemilla/merkle.rs:202:15 | 202 | use std::{convert::TryInto, iter}; | ^^^^^^^^^^^^^^^^ --> /rustc/6fd1912922a7b468b1875d16571bb7a00d5df93f/library/std/src/prelude/mod.rs:129:13 | = note: the item `TryInto` is already defined here

Expand Down
5 changes: 3 additions & 2 deletions halo2_gadgets/src/sinsemilla_opt/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ pub mod tests {
meta.lookup_table_column(),
);

let range_check =
LookupRangeCheckConfigOptimized::configure(meta, advices[9], lookup.0);
let range_check = LookupRangeCheckConfigOptimized::configure_with_tag(
meta, advices[9], lookup.0, lookup.3,
);

let sinsemilla_config_1 = SinsemillaChipOptimized::configure(
meta,
Expand Down
167 changes: 81 additions & 86 deletions halo2_gadgets/src/utilities/lookup_range_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Make use of a K-bit lookup table to decompose a field element into K-bit
//! words.

use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized;

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

warning: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/utilities/lookup_range_check.rs:4:5 | 4 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

warning: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/utilities/lookup_range_check.rs:4:5 | 4 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Book tests

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Book tests

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check failure on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

error: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/utilities/lookup_range_check.rs:4:5 | 4 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check failure on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

error: unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized` --> halo2_gadgets/src/utilities/lookup_range_check.rs:4:5 | 4 | use crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with beta features

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with beta features

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with nightly features

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`

Check warning on line 4 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Test on ubuntu-latest with nightly features

unused import: `crate::utilities_opt::lookup_range_check::LookupRangeCheckConfigOptimized`
use ff::PrimeFieldBits;

Check warning on line 5 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `PrimeFieldBits` is imported redundantly

warning: the item `PrimeFieldBits` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:5:5 | 5 | use ff::PrimeFieldBits; | ^^^^^^^^^^^^^^^^^^ ... 13 | use super::*; | -------- the item `PrimeFieldBits` is already imported here

Check warning on line 5 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `PrimeFieldBits` is imported redundantly

warning: the item `PrimeFieldBits` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:5:5 | 5 | use ff::PrimeFieldBits; | ^^^^^^^^^^^^^^^^^^ ... 13 | use super::*; | -------- the item `PrimeFieldBits` is already imported here
use halo2_proofs::{
circuit::{AssignedCell, Layouter, Region},

Check warning on line 7 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `Layouter` is imported redundantly

warning: the item `Layouter` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:7:29 | 7 | circuit::{AssignedCell, Layouter, Region}, | ^^^^^^^^ ... 13 | use super::*; | -------- the item `Layouter` is already imported here

Check warning on line 7 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `AssignedCell` is imported redundantly

warning: the item `AssignedCell` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:7:15 | 7 | circuit::{AssignedCell, Layouter, Region}, | ^^^^^^^^^^^^ ... 13 | use super::*; | -------- the item `AssignedCell` is already imported here

Check warning on line 7 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `Layouter` is imported redundantly

warning: the item `Layouter` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:7:29 | 7 | circuit::{AssignedCell, Layouter, Region}, | ^^^^^^^^ ... 13 | use super::*; | -------- the item `Layouter` is already imported here

Check warning on line 7 in halo2_gadgets/src/utilities/lookup_range_check.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the item `AssignedCell` is imported redundantly

warning: the item `AssignedCell` is imported redundantly --> halo2_gadgets/src/utilities/lookup_range_check.rs:7:15 | 7 | circuit::{AssignedCell, Layouter, Region}, | ^^^^^^^^^^^^ ... 13 | use super::*; | -------- the item `AssignedCell` is already imported here
Expand Down Expand Up @@ -61,11 +62,7 @@ pub struct LookupRangeCheckConfig<F: PrimeFieldBits, const K: usize> {
pub(crate) _marker: PhantomData<F>,
}

/// Trait that provides common methods for a lookup range check.
pub trait LookupRangeCheck<F: PrimeFieldBits, const K: usize> {
/// Returns a reference to the `LookupRangeCheckConfig` instance.
fn base(&self) -> &LookupRangeCheckConfig<F, K>;

impl<F: PrimeFieldBits, const K: usize> LookupRangeCheckConfig<F, K> {
/// The `running_sum` advice column breaks the field element into `K`-bit
/// words. It is used to construct the input expression to the lookup
/// argument.
Expand All @@ -77,13 +74,88 @@ pub trait LookupRangeCheck<F: PrimeFieldBits, const K: usize> {
/// # Side-effects
///
/// Both the `running_sum` and `constants` columns will be equality-enabled.
fn configure(
pub(crate) fn configure(
meta: &mut ConstraintSystem<F>,
running_sum: Column<Advice>,
table_idx: TableColumn,
) -> Self
where
Self: Sized;
) -> Self {
meta.enable_equality(running_sum);

let q_lookup = meta.complex_selector();
let q_running = meta.complex_selector();
let q_bitshift = meta.selector();

// if the order of the creation makes a difference
let config = LookupRangeCheckConfig {
q_lookup,
q_running,
q_bitshift,
running_sum,
table_idx,
_marker: PhantomData,
};

// https://p.z.cash/halo2-0.1:decompose-combined-lookup
meta.lookup(|meta| {
let q_lookup = meta.query_selector(config.q_lookup);
let q_running = meta.query_selector(config.q_running);
// if the order of the creation makes a difference
let z_cur = meta.query_advice(config.running_sum, Rotation::cur());
let one = Expression::Constant(F::ONE);

// In the case of a running sum decomposition, we recover the word from
// the difference of the running sums:
// z_i = 2^{K}⋅z_{i + 1} + a_i
// => a_i = z_i - 2^{K}⋅z_{i + 1}
let running_sum_lookup = {
let running_sum_word = {
let z_next = meta.query_advice(config.running_sum, Rotation::next());
z_cur.clone() - z_next * F::from(1 << K)
};

q_running.clone() * running_sum_word
};

// In the short range check, the word is directly witnessed.
let short_lookup = {
let short_word = z_cur;
let q_short = one - q_running;

q_short * short_word
};

vec![(
q_lookup * (running_sum_lookup + short_lookup),
config.table_idx,
)]
});

// For short lookups, check that the word has been shifted by the correct number of bits.
// https://p.z.cash/halo2-0.1:decompose-short-lookup
meta.create_gate("Short lookup bitshift", |meta| {
let q_bitshift = meta.query_selector(config.q_bitshift);
let word = meta.query_advice(config.running_sum, Rotation::prev());
let shifted_word = meta.query_advice(config.running_sum, Rotation::cur());
let inv_two_pow_s = meta.query_advice(config.running_sum, Rotation::next());

let two_pow_k = F::from(1 << K);

// shifted_word = word * 2^{K-s}
// = word * 2^K * inv_two_pow_s
Constraints::with_selector(
q_bitshift,
Some(word * two_pow_k * inv_two_pow_s - shifted_word),
)
});

config
}
}

/// Trait that provides common methods for a lookup range check.
pub trait LookupRangeCheck<F: PrimeFieldBits, const K: usize> {
/// Returns a reference to the `LookupRangeCheckConfig` instance.
fn base(&self) -> &LookupRangeCheckConfig<F, K>;

#[cfg(test)]
// Fill `table_idx` and `table_range_check_tag`.
Expand Down Expand Up @@ -284,83 +356,6 @@ impl<F: PrimeFieldBits, const K: usize> LookupRangeCheck<F, K> for LookupRangeCh
self
}

fn configure(
meta: &mut ConstraintSystem<F>,
running_sum: Column<Advice>,
table_idx: TableColumn,
) -> Self {
meta.enable_equality(running_sum);

let q_lookup = meta.complex_selector();
let q_running = meta.complex_selector();
let q_bitshift = meta.selector();

// if the order of the creation makes a difference
let config = LookupRangeCheckConfig {
q_lookup,
q_running,
q_bitshift,
running_sum,
table_idx,
_marker: PhantomData,
};

// https://p.z.cash/halo2-0.1:decompose-combined-lookup
meta.lookup(|meta| {
let q_lookup = meta.query_selector(config.q_lookup);
let q_running = meta.query_selector(config.q_running);
// if the order of the creation makes a difference
let z_cur = meta.query_advice(config.running_sum, Rotation::cur());
let one = Expression::Constant(F::ONE);

// In the case of a running sum decomposition, we recover the word from
// the difference of the running sums:
// z_i = 2^{K}⋅z_{i + 1} + a_i
// => a_i = z_i - 2^{K}⋅z_{i + 1}
let running_sum_lookup = {
let running_sum_word = {
let z_next = meta.query_advice(config.running_sum, Rotation::next());
z_cur.clone() - z_next * F::from(1 << K)
};

q_running.clone() * running_sum_word
};

// In the short range check, the word is directly witnessed.
let short_lookup = {
let short_word = z_cur;
let q_short = one - q_running;

q_short * short_word
};

vec![(
q_lookup * (running_sum_lookup + short_lookup),
config.table_idx,
)]
});

// For short lookups, check that the word has been shifted by the correct number of bits.
// https://p.z.cash/halo2-0.1:decompose-short-lookup
meta.create_gate("Short lookup bitshift", |meta| {
let q_bitshift = meta.query_selector(config.q_bitshift);
let word = meta.query_advice(config.running_sum, Rotation::prev());
let shifted_word = meta.query_advice(config.running_sum, Rotation::cur());
let inv_two_pow_s = meta.query_advice(config.running_sum, Rotation::next());

let two_pow_k = F::from(1 << K);

// shifted_word = word * 2^{K-s}
// = word * 2^K * inv_two_pow_s
Constraints::with_selector(
q_bitshift,
Some(word * two_pow_k * inv_two_pow_s - shifted_word),
)
});

config
}

#[cfg(test)]
// Fill `table_idx` and `table_range_check_tag`.
// This is only used in testing for now, since the Sinsemilla chip provides a pre-loaded table
Expand Down
11 changes: 1 addition & 10 deletions halo2_gadgets/src/utilities_opt/lookup_range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<F: PrimeFieldBits, const K: usize> LookupRangeCheckConfigOptimized<F, K> {
/// # Side-effects
///
/// Both the `running_sum` and `constants` columns will be equality-enabled.
fn configure_with_tag(
pub(crate) fn configure_with_tag(
meta: &mut ConstraintSystem<F>,
running_sum: Column<Advice>,
table_idx: TableColumn,
Expand Down Expand Up @@ -164,15 +164,6 @@ impl<F: PrimeFieldBits, const K: usize> LookupRangeCheck<F, K>
&self.base
}

fn configure(
meta: &mut ConstraintSystem<F>,
running_sum: Column<Advice>,
table_idx: TableColumn,
) -> Self {
let table_range_check_tag = meta.lookup_table_column();
Self::configure_with_tag(meta, running_sum, table_idx, table_range_check_tag)
}

#[cfg(test)]
// Fill `table_idx` and `table_range_check_tag`.
// This is only used in testing for now, since the Sinsemilla chip provides a pre-loaded table
Expand Down

0 comments on commit bfe5fba

Please sign in to comment.