-
Notifications
You must be signed in to change notification settings - Fork 496
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
Implement Dynamic lookups #660
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
184d7a4
Implement dynamic lookups
moreSocratic 55e2b91
Clippy
moreSocratic d9f8c76
Make DynamicTable: Copy
moreSocratic a70123f
Merge branch 'main' of github.com:zcash/halo2 into dynamic-lookups
moreSocratic 86bf070
Fix bad merge
moreSocratic 4593699
Fix assert_satisfied for dynamic lookups
moreSocratic 8bcc3f2
Fix commit
moreSocratic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use halo2_proofs::{ | ||
circuit::{Layouter, SimpleFloorPlanner, Value}, | ||
dev::MockProver, | ||
pasta::Fp, | ||
plonk::{ | ||
create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Circuit, Column, | ||
ConstraintSystem, DynamicTable, DynamicTableMap, Error, Selector, SingleVerifier, | ||
}, | ||
poly::{commitment::Params, Rotation}, | ||
transcript::{Blake2bRead, Blake2bWrite}, | ||
}; | ||
use pasta_curves::{vesta, EqAffine}; | ||
use rand_core::OsRng; | ||
|
||
#[derive(Clone)] | ||
struct EvenOddCircuitConfig { | ||
is_even: Selector, | ||
is_odd: Selector, | ||
a: Column<Advice>, | ||
// starts at zero to use as default | ||
table_vals: Column<Advice>, | ||
even: DynamicTable, | ||
odd: DynamicTable, | ||
} | ||
|
||
struct DynLookupCircuit {} | ||
impl Circuit<Fp> for DynLookupCircuit { | ||
type Config = EvenOddCircuitConfig; | ||
type FloorPlanner = SimpleFloorPlanner; | ||
|
||
fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config { | ||
let a = meta.advice_column(); | ||
let table_vals = meta.advice_column(); | ||
let is_even = meta.complex_selector(); | ||
let is_odd = meta.complex_selector(); | ||
let even = meta.create_dynamic_table("even", &[], &[table_vals]); | ||
let odd = meta.create_dynamic_table("odd", &[], &[table_vals]); | ||
|
||
meta.lookup_dynamic(&even, |cells| { | ||
let a = cells.query_advice(a, Rotation::cur()); | ||
let is_even = cells.query_selector(is_even); | ||
|
||
DynamicTableMap { | ||
selector: is_even, | ||
table_map: vec![(a, table_vals.into())], | ||
} | ||
}); | ||
|
||
meta.lookup_dynamic(&odd, |cells| { | ||
let a = cells.query_advice(a, Rotation::cur()); | ||
let is_odd = cells.query_selector(is_odd); | ||
|
||
DynamicTableMap { | ||
selector: is_odd, | ||
table_map: vec![(a, table_vals.into())], | ||
} | ||
}); | ||
|
||
EvenOddCircuitConfig { | ||
a, | ||
table_vals, | ||
is_even, | ||
is_odd, | ||
even, | ||
odd, | ||
} | ||
} | ||
|
||
fn without_witnesses(&self) -> Self { | ||
Self {} | ||
} | ||
|
||
fn synthesize( | ||
&self, | ||
config: Self::Config, | ||
mut layouter: impl Layouter<Fp>, | ||
) -> Result<(), Error> { | ||
for i in 0..=5 { | ||
layouter.assign_region( | ||
|| format!("lookup: {}", i), | ||
|mut region| { | ||
// Enable the lookup on rows | ||
if i % 2 == 0 { | ||
config.is_even.enable(&mut region, 0)?; | ||
} else { | ||
config.is_odd.enable(&mut region, 0)?; | ||
}; | ||
|
||
region.assign_advice(|| "", config.a, 0, || Value::known(Fp::from(i as u64))) | ||
}, | ||
)?; | ||
} | ||
layouter.assign_region( | ||
|| "table", | ||
|mut region| { | ||
for i in 0..=5 { | ||
region.assign_advice( | ||
|| "", | ||
config.table_vals, | ||
i, | ||
|| Value::known(Fp::from(i as u64)), | ||
)?; | ||
|
||
let table = if i % 2 == 0 { | ||
&config.even | ||
} else { | ||
&config.odd | ||
}; | ||
table.add_row(&mut region, i)?; | ||
} | ||
Ok(()) | ||
}, | ||
)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
let k = 5; | ||
|
||
MockProver::run(k, &DynLookupCircuit {}, vec![]) | ||
.unwrap() | ||
.verify() | ||
.unwrap(); | ||
|
||
let params: Params<EqAffine> = Params::new(k); | ||
let verifier = SingleVerifier::new(¶ms); | ||
let vk = keygen_vk(¶ms, &DynLookupCircuit {}).unwrap(); | ||
let pk = keygen_pk(¶ms, vk, &DynLookupCircuit {}).unwrap(); | ||
let mut transcript = Blake2bWrite::<_, vesta::Affine, _>::init(vec![]); | ||
create_proof( | ||
¶ms, | ||
&pk, | ||
&[DynLookupCircuit {}], | ||
&[&[]], | ||
&mut OsRng, | ||
&mut transcript, | ||
) | ||
.expect("Failed to create proof"); | ||
|
||
let proof: Vec<u8> = transcript.finalize(); | ||
|
||
let mut transcript = Blake2bRead::init(&proof[..]); | ||
verify_proof(¶ms, pk.get_vk(), verifier, &[&[]], &mut transcript) | ||
.expect("could not verify_proof"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we always mean to be calling
enable
andassign_advice
at offset 0?