Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a LookupRangeCheck trait and minor modifications in preparation for ZSA #823

Open
wants to merge 102 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
35c815d
Add variable-base sign-scalar multiplication
ConstanceBeguier Mar 21, 2023
731bc10
Add CommitDomain creation from two personalizations
ConstanceBeguier Apr 19, 2023
b1e397f
Add commit_from_hash_point
ConstanceBeguier Apr 19, 2023
8cfe0ae
updated naming
PaulLaux Apr 20, 2023
54697b2
Add hash and blinding_factor functions
ConstanceBeguier Apr 17, 2023
4ce262d
Add some functionalities for MuxChip
ConstanceBeguier Apr 18, 2023
475f54d
Add Point::new_from_constant method (#17)
ConstanceBeguier Jun 23, 2023
4c3c00b
Optimized short range check on 4 and 5 bits (#21)
ConstanceBeguier Oct 16, 2023
f51eebe
Add multiplexer chip (#23)
ConstanceBeguier Oct 17, 2023
cba30b1
Add functions to evaluate a Sinsemilla hash from an initial private p…
ConstanceBeguier Oct 18, 2023
87464d4
Update comments
ConstanceBeguier Nov 23, 2023
d76d231
Reactivate i686 CI test
ConstanceBeguier Nov 23, 2023
ac7a90d
Some minor changes
ConstanceBeguier Nov 28, 2023
622875e
Remove q_sinsemilla4_private
ConstanceBeguier Nov 30, 2023
5f436dc
Move mux functionality into CondSwap chip
ConstanceBeguier Nov 30, 2023
80dfd60
add initial doc
Apr 23, 2024
d3a6f42
clean up code
Apr 24, 2024
86bd33f
remove cfg[test]
Apr 24, 2024
989f29b
remove generator table trait, fix hash_to_point
Apr 25, 2024
507a53c
update code for hash
Apr 29, 2024
2e31e97
fmt
Apr 29, 2024
b02478a
fix run tests error
Apr 29, 2024
e6d1dbe
minor update
Apr 29, 2024
23a2951
test error
Apr 29, 2024
301e7d8
Revert "test error"
Apr 29, 2024
c324006
re-naming structs and functions
Apr 30, 2024
af29df4
fix complex type
Apr 30, 2024
1ea04bb
add round trip test to verify that the pinned verification key (repre…
May 6, 2024
684c13e
update serialized_proof_test_case
May 6, 2024
a25a6ad
update
May 6, 2024
42f1b0a
update short lookup test
May 6, 2024
2b89fae
cargo fmt
May 6, 2024
8552c97
fix Clippy error
May 6, 2024
f35cbe9
clean up code
May 7, 2024
f65d5a3
add initial files
May 10, 2024
7e50d0b
minor update
May 10, 2024
58f8b3f
remove pub(crate) in ecc
May 13, 2024
738eabe
add a line after cargo.toml
May 13, 2024
0fb6dd4
add a line after cargo.toml
May 13, 2024
e2c9c30
slide lines in base_field_elem
May 13, 2024
5322c81
slide lines in base_field_elem
May 13, 2024
46773e3
adjuct function orders for configure in sinsemilla/chip
May 13, 2024
3621e0e
adjuct function orders for hash_message
May 13, 2024
a30596b
adjuct function orders for hash_message
May 13, 2024
b3cb453
cleanup code
May 13, 2024
cf2bd37
cleanup code
May 13, 2024
da85c71
cleanup code
May 13, 2024
a5bf5ee
resolve review
May 14, 2024
8d27dd7
Introduce conditionally_save_circuit_to_disk function to reuse the pr…
dmidem May 14, 2024
48f941e
Rename conditionally_save_circuit_to_disk function to conditionally_s…
dmidem May 14, 2024
58b7eff
Introduce test_serialized_proof_with_vk and test_serialized_proof fun…
dmidem May 14, 2024
756bb78
Fix cargo clippy error
dmidem May 14, 2024
6814fc2
rename
May 14, 2024
64a8ac2
fix git error
May 14, 2024
704d3f6
Move all test .bin files and test_circuit.rs module to new tests fold…
dmidem May 14, 2024
d0d3a6f
Add missed files
dmidem May 14, 2024
35f161d
Move all .bin and vk_ test data files into src/tests folder
dmidem May 14, 2024
497c290
add tests for short range check
May 14, 2024
d3a9d4e
Add fixed_verification_key_test_with_circuit function to generalize V…
dmidem May 15, 2024
cf8aca0
Avoid duplicating test data folder name src/tests in every file name,…
dmidem May 15, 2024
b7c7261
Remove 'else' in fixed_verification_key_test_with_circuit function, t…
dmidem May 15, 2024
d3c7eb0
Use fs::read, fs::write instad of read_test_case and write_test_case
dmidem May 15, 2024
af5fd9c
Merge branch 'improve-backward-compatability-without-zsa' of github.c…
May 15, 2024
5248a0a
revert .github/workflows/ci.yml
May 15, 2024
8a68152
rename circuit.rs
May 15, 2024
8dbb582
add a line
May 15, 2024
32842df
fix ci.yml
May 15, 2024
3324348
Rename PallasLookupRC LookupRangeCheck trait alias to PallasLookup. A…
dmidem May 15, 2024
92922be
Move circuit test data to tests/circuit_reference_data folder, fix na…
dmidem May 15, 2024
6c7713f
Rename circuit_reference_data to circuit_data
dmidem May 15, 2024
8922ed6
Fix cargo fmt issues
dmidem May 15, 2024
f75b054
Try to increase cargo-tarpaulin timeout in ci.yml from 600 to 1800 to…
dmidem May 16, 2024
5414d95
Add default generic arguments for PallasLookup to improve backward co…
dmidem May 17, 2024
396bbde
Try to increase cargo-tarpaulin timeout in ci.yml from to 3600
dmidem May 22, 2024
1ae0a31
update
May 28, 2024
adf4d0d
cargo fmt
May 28, 2024
807304d
Merge branch 'improve-backward-compatability-without-zsa-default-gene…
dmidem May 28, 2024
1bb0b84
add index to proofs
May 28, 2024
923293b
rename PallasLookupRC10b to PallasLookupRCConfig
May 30, 2024
9ecbd65
resolve some comments
May 30, 2024
5800687
Fix error in ci.yml
Jun 4, 2024
236dfc5
Merge branch 'improve-backward-compatability-without-zsa' of github.c…
Jun 4, 2024
3ed08fb
Cleanup code and some renamings
ConstanceBeguier Jun 11, 2024
65b3ae4
rust fmt
ConstanceBeguier Jun 11, 2024
80dd7a1
Put together test_against_stored_vk/proof into test_against_stored_ci…
ConstanceBeguier Jul 2, 2024
e0822c5
Refactor
ConstanceBeguier Jul 2, 2024
aef19cf
Remove MerkleSinsemillaInstructions
ConstanceBeguier Jul 2, 2024
5a517a5
Remove small functions in SinsemillaChip
ConstanceBeguier Jul 2, 2024
372c216
Refactor in lookup_range_check
ConstanceBeguier Jul 2, 2024
1fe4680
Rename PallasLookupRC(Config)
ConstanceBeguier Jul 2, 2024
b241533
Add Lookup generic argument in ecc tests
ConstanceBeguier Jul 5, 2024
4165304
Add Lookup generic argument in Sinsemilla tests
ConstanceBeguier Jul 5, 2024
c9e62f3
Refactor
ConstanceBeguier Jul 5, 2024
023551b
Add Lookup generic argument in Lookup tests
ConstanceBeguier Jul 5, 2024
20907a8
Add Lookup generic argument into mul_fixed/short tests
ConstanceBeguier Jul 7, 2024
632313c
Add Lookup generic argument into Merkle tests
ConstanceBeguier Jul 7, 2024
194162f
Refactor
ConstanceBeguier Jul 7, 2024
f64d62f
Add generic Lookup argument in short_range_check tests
ConstanceBeguier Jul 8, 2024
1bb738a
Update EccPointQ enum
ConstanceBeguier Jul 9, 2024
3c03e46
Update EccPointQ enum
ConstanceBeguier Jul 9, 2024
552df1a
Take into account Pablo's review
ConstanceBeguier Jul 16, 2024
7ed444a
Remove unnecessary changes in CI
ConstanceBeguier Aug 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: tarpaulin
# Extend the timeout to 3600 to ensure the code coverage test pass
args: >
${{ steps.prepare.outputs.feature-flags }}
--timeout 600
--timeout 3600
--out Xml
- name: Upload coverage to Codecov
uses: codecov/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

43 changes: 34 additions & 9 deletions halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ impl<C: CurveAffine, EccChip: EccInstructions<C>> FixedPointShort<C, EccChip> {
pub(crate) mod tests {
use ff::PrimeField;
use group::{prime::PrimeCurveAffine, Curve, Group};
use std::marker::PhantomData;

use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
Expand All @@ -595,7 +596,10 @@ pub(crate) mod tests {
},
FixedPoints,
};
use crate::utilities::lookup_range_check::LookupRangeCheckConfig;
use crate::{
tests::test_utils::test_against_stored_circuit,
utilities::lookup_range_check::{PallasLookupRangeCheck, PallasLookupRangeCheckConfig},
};

#[derive(Debug, Eq, PartialEq, Clone)]
pub(crate) struct TestFixedBases;
Expand Down Expand Up @@ -723,17 +727,27 @@ pub(crate) mod tests {
type Base = BaseField;
}

struct MyCircuit {
struct MyCircuit<Lookup: PallasLookupRangeCheck> {
test_errors: bool,
_lookup_marker: PhantomData<Lookup>,
}

impl<Lookup: PallasLookupRangeCheck> MyCircuit<Lookup> {
fn new(test_errors: bool) -> Self {
Self {
test_errors,
_lookup_marker: PhantomData,
}
}
}

#[allow(non_snake_case)]
impl Circuit<pallas::Base> for MyCircuit {
type Config = EccConfig<TestFixedBases>;
impl<Lookup: PallasLookupRangeCheck> Circuit<pallas::Base> for MyCircuit<Lookup> {
type Config = EccConfig<TestFixedBases, Lookup>;
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
MyCircuit { test_errors: false }
MyCircuit::new(false)
}

fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self::Config {
Expand Down Expand Up @@ -764,8 +778,13 @@ pub(crate) mod tests {
let constants = meta.fixed_column();
meta.enable_constant(constants);

let range_check = LookupRangeCheckConfig::configure(meta, advices[9], lookup_table);
EccChip::<TestFixedBases>::configure(meta, advices, lagrange_coeffs, range_check)
let range_check = Lookup::configure(meta, advices[9], lookup_table);
EccChip::<TestFixedBases, Lookup>::configure(
meta,
advices,
lagrange_coeffs,
range_check,
)
}

fn synthesize(
Expand Down Expand Up @@ -896,11 +915,17 @@ pub(crate) mod tests {
#[test]
fn ecc_chip() {
let k = 13;
let circuit = MyCircuit { test_errors: true };
let circuit = MyCircuit::<PallasLookupRangeCheckConfig>::new(true);
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()))
}

#[test]
fn test_ecc_chip_against_stored_circuit() {
let circuit = MyCircuit::<PallasLookupRangeCheckConfig>::new(false);
test_against_stored_circuit(circuit, "ecc_chip", 3872);
}

#[cfg(feature = "test-dev-graph")]
#[test]
fn print_ecc_chip() {
Expand All @@ -910,7 +935,7 @@ pub(crate) mod tests {
root.fill(&WHITE).unwrap();
let root = root.titled("Ecc Chip Layout", ("sans-serif", 60)).unwrap();

let circuit = MyCircuit { test_errors: false };
let circuit = MyCircuit::<PallasLookupRangeCheckConfig>::new(false);
halo2_proofs::dev::CircuitLayout::default()
.render(13, &circuit, &root)
.unwrap();
Expand Down
60 changes: 36 additions & 24 deletions halo2_gadgets/src/ecc/chip.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Chip implementations for the ECC gadgets.

use super::{BaseFitsInScalarInstructions, EccInstructions, FixedPoints};
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{lookup_range_check::LookupRangeCheckConfig, UtilitiesInstructions},
use crate::utilities::{
lookup_range_check::{PallasLookupRangeCheck, PallasLookupRangeCheckConfig},
UtilitiesInstructions,
};
use arrayvec::ArrayVec;

Expand Down Expand Up @@ -137,7 +137,10 @@ impl From<NonIdentityEccPoint> for EccPoint {
/// Configuration for [`EccChip`].
#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(non_snake_case)]
pub struct EccConfig<FixedPoints: super::FixedPoints<pallas::Affine>> {
pub struct EccConfig<
FixedPoints: super::FixedPoints<pallas::Affine>,
Lookup: PallasLookupRangeCheck = PallasLookupRangeCheckConfig,
> {
/// Advice columns needed by instructions in the ECC chip.
pub advices: [Column<Advice>; 10],

Expand All @@ -148,20 +151,20 @@ pub struct EccConfig<FixedPoints: super::FixedPoints<pallas::Affine>> {
add: add::Config,

/// Variable-base scalar multiplication
mul: mul::Config,
mul: mul::Config<Lookup>,

/// Fixed-base full-width scalar multiplication
mul_fixed_full: mul_fixed::full_width::Config<FixedPoints>,
/// Fixed-base signed short scalar multiplication
mul_fixed_short: mul_fixed::short::Config<FixedPoints>,
/// Fixed-base mul using a base field element as a scalar
mul_fixed_base_field: mul_fixed::base_field_elem::Config<FixedPoints>,
mul_fixed_base_field: mul_fixed::base_field_elem::Config<FixedPoints, Lookup>,

/// Witness point
witness_point: witness_point::Config,

/// Lookup range check using 10-bit lookup table
pub lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
pub lookup_config: Lookup,
}

/// A trait representing the kind of scalar used with a particular `FixedPoint`.
Expand Down Expand Up @@ -227,12 +230,17 @@ pub trait FixedPoint<C: CurveAffine>: std::fmt::Debug + Eq + Clone {

/// An [`EccInstructions`] chip that uses 10 advice columns.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EccChip<FixedPoints: super::FixedPoints<pallas::Affine>> {
config: EccConfig<FixedPoints>,
pub struct EccChip<
FixedPoints: super::FixedPoints<pallas::Affine>,
Lookup: PallasLookupRangeCheck = PallasLookupRangeCheckConfig,
> {
config: EccConfig<FixedPoints, Lookup>,
}

impl<FixedPoints: super::FixedPoints<pallas::Affine>> Chip<pallas::Base> for EccChip<FixedPoints> {
type Config = EccConfig<FixedPoints>;
impl<FixedPoints: super::FixedPoints<pallas::Affine>, Lookup: PallasLookupRangeCheck>
Chip<pallas::Base> for EccChip<FixedPoints, Lookup>
{
type Config = EccConfig<FixedPoints, Lookup>;
type Loaded = ();

fn config(&self) -> &Self::Config {
Expand All @@ -244,13 +252,15 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> Chip<pallas::Base> for Ecc
}
}

impl<Fixed: super::FixedPoints<pallas::Affine>> UtilitiesInstructions<pallas::Base>
for EccChip<Fixed>
impl<Fixed: super::FixedPoints<pallas::Affine>, Lookup: PallasLookupRangeCheck>
UtilitiesInstructions<pallas::Base> for EccChip<Fixed, Lookup>
{
type Var = AssignedCell<pallas::Base, pallas::Base>;
}

impl<FixedPoints: super::FixedPoints<pallas::Affine>> EccChip<FixedPoints> {
impl<FixedPoints: super::FixedPoints<pallas::Affine>, Lookup: PallasLookupRangeCheck>
EccChip<FixedPoints, Lookup>
{
/// Reconstructs this chip from the given config.
pub fn construct(config: <Self as Chip<pallas::Base>>::Config) -> Self {
Self { config }
Expand All @@ -264,7 +274,7 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> EccChip<FixedPoints> {
meta: &mut ConstraintSystem<pallas::Base>,
advices: [Column<Advice>; 10],
lagrange_coeffs: [Column<Fixed>; 8],
range_check: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
range_check: Lookup,
) -> <Self as Chip<pallas::Base>>::Config {
// Create witness point gate
let witness_point = witness_point::Config::configure(meta, advices[0], advices[1]);
Expand Down Expand Up @@ -301,12 +311,13 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> EccChip<FixedPoints> {
mul_fixed::short::Config::<FixedPoints>::configure(meta, mul_fixed.clone());

// Create gate that is only used in fixed-base mul using a base field element.
let mul_fixed_base_field = mul_fixed::base_field_elem::Config::<FixedPoints>::configure(
meta,
advices[6..9].try_into().unwrap(),
range_check,
mul_fixed,
);
let mul_fixed_base_field =
mul_fixed::base_field_elem::Config::<FixedPoints, Lookup>::configure(
meta,
advices[6..9].try_into().unwrap(),
range_check,
mul_fixed,
);

EccConfig {
advices,
Expand Down Expand Up @@ -407,7 +418,8 @@ pub enum ScalarVar {
FullWidth,
}

impl<Fixed: FixedPoints<pallas::Affine>> EccInstructions<pallas::Affine> for EccChip<Fixed>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: PallasLookupRangeCheck>
EccInstructions<pallas::Affine> for EccChip<Fixed, Lookup>
where
<Fixed as FixedPoints<pallas::Affine>>::Base:
FixedPoint<pallas::Affine, FixedScalarKind = BaseFieldElem>,
Expand Down Expand Up @@ -594,8 +606,8 @@ where
}
}

impl<Fixed: FixedPoints<pallas::Affine>> BaseFitsInScalarInstructions<pallas::Affine>
for EccChip<Fixed>
impl<Fixed: FixedPoints<pallas::Affine>, Lookup: PallasLookupRangeCheck>
BaseFitsInScalarInstructions<pallas::Affine> for EccChip<Fixed, Lookup>
where
<Fixed as FixedPoints<pallas::Affine>>::Base:
FixedPoint<pallas::Affine, FixedScalarKind = BaseFieldElem>,
Expand Down
22 changes: 11 additions & 11 deletions halo2_gadgets/src/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{add, EccPoint, NonIdentityEccPoint, ScalarVar, T_Q};
use crate::{
sinsemilla::primitives as sinsemilla,
utilities::{bool_check, lookup_range_check::LookupRangeCheckConfig, ternary},
use crate::utilities::{
lookup_range_check::{PallasLookupRangeCheck, PallasLookupRangeCheckConfig},
{bool_check, ternary},
};
use std::{
convert::TryInto,
Expand Down Expand Up @@ -46,7 +46,7 @@ const INCOMPLETE_LO_LEN: usize = INCOMPLETE_LEN - INCOMPLETE_HI_LEN;
const COMPLETE_RANGE: Range<usize> = INCOMPLETE_LEN..(INCOMPLETE_LEN + NUM_COMPLETE_BITS);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Config {
pub struct Config<Lookup: PallasLookupRangeCheck = PallasLookupRangeCheckConfig> {
// Selector used to check switching logic on LSB
q_mul_lsb: Selector,
// Configuration used in complete addition
Expand All @@ -58,14 +58,14 @@ pub struct Config {
// Configuration used for complete addition part of double-and-add algorithm
complete_config: complete::Config,
// Configuration used to check for overflow
overflow_config: overflow::Config,
overflow_config: overflow::Config<Lookup>,
}

impl Config {
impl<Lookup: PallasLookupRangeCheck> Config<Lookup> {
pub(super) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
add_config: add::Config,
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: Lookup,
advices: [Column<Advice>; 10],
) -> Self {
let hi_config = incomplete::Config::configure(
Expand Down Expand Up @@ -473,13 +473,13 @@ pub mod tests {
tests::TestFixedBases,
EccInstructions, NonIdentityPoint, Point, ScalarVar,
},
utilities::UtilitiesInstructions,
utilities::{lookup_range_check::PallasLookupRangeCheck, UtilitiesInstructions},
};

pub(crate) fn test_mul(
chip: EccChip<TestFixedBases>,
pub(crate) fn test_mul<Lookup: PallasLookupRangeCheck>(
chip: EccChip<TestFixedBases, Lookup>,
mut layouter: impl Layouter<pallas::Base>,
p: &NonIdentityPoint<pallas::Affine, EccChip<TestFixedBases>>,
p: &NonIdentityPoint<pallas::Affine, EccChip<TestFixedBases, Lookup>>,
p_val: pallas::Affine,
) -> Result<(), Error> {
let column = chip.config().advices[0];
Expand Down
11 changes: 6 additions & 5 deletions halo2_gadgets/src/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{T_Q, Z};
use crate::{
sinsemilla::primitives as sinsemilla, utilities::lookup_range_check::LookupRangeCheckConfig,
sinsemilla::primitives as sinsemilla,
utilities::lookup_range_check::{PallasLookupRangeCheck, PallasLookupRangeCheckConfig},
};

use group::ff::PrimeField;
Expand All @@ -15,19 +16,19 @@ use pasta_curves::pallas;
use std::iter;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Config {
pub struct Config<Lookup: PallasLookupRangeCheck = PallasLookupRangeCheckConfig> {
// Selector to check z_0 = alpha + t_q (mod p)
q_mul_overflow: Selector,
// 10-bit lookup table
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: Lookup,
// Advice columns
advices: [Column<Advice>; 3],
}

impl Config {
impl<Lookup: PallasLookupRangeCheck> Config<Lookup> {
pub(super) fn configure(
meta: &mut ConstraintSystem<pallas::Base>,
lookup_config: LookupRangeCheckConfig<pallas::Base, { sinsemilla::K }>,
lookup_config: Lookup,
advices: [Column<Advice>; 3],
) -> Self {
for advice in advices.iter() {
Expand Down
Loading