Add lookup to circuit and test

This commit is contained in:
therealyingtong 2020-12-02 03:48:19 +08:00
parent 0c81e9adab
commit 0a85e93714
2 changed files with 84 additions and 6 deletions

View file

@ -121,7 +121,7 @@ fn test_proving() {
use crate::poly::commitment::{Blind, Params}; use crate::poly::commitment::{Blind, Params};
use crate::transcript::DummyHash; use crate::transcript::DummyHash;
use crate::tweedle::{EqAffine, Fp, Fq}; use crate::tweedle::{EqAffine, Fp, Fq};
use circuit::{Advice, Column, Fixed}; use circuit::{Advice, Any, Column, Fixed};
use std::marker::PhantomData; use std::marker::PhantomData;
const K: u32 = 5; const K: u32 = 5;
@ -144,6 +144,8 @@ fn test_proving() {
sc: Column<Fixed>, sc: Column<Fixed>,
sm: Column<Fixed>, sm: Column<Fixed>,
sp: Column<Fixed>, sp: Column<Fixed>,
sl: Column<Fixed>,
sl2: Column<Fixed>,
perm: usize, perm: usize,
perm2: usize, perm2: usize,
@ -160,10 +162,12 @@ fn test_proving() {
fn public_input<F>(&mut self, f: F) -> Result<Variable, Error> fn public_input<F>(&mut self, f: F) -> Result<Variable, Error>
where where
F: FnOnce() -> Result<FF, Error>; F: FnOnce() -> Result<FF, Error>;
fn lookup_table(&mut self, values: &[Vec<FF>]) -> Result<(), Error>;
} }
struct MyCircuit<F: FieldExt> { struct MyCircuit<F: FieldExt> {
a: Option<F>, a: Option<F>,
lookup_tables: Vec<Vec<F>>,
} }
struct StandardPLONK<'a, F: FieldExt, CS: Assignment<F> + 'a> { struct StandardPLONK<'a, F: FieldExt, CS: Assignment<F> + 'a> {
@ -297,6 +301,18 @@ fn test_proving() {
Ok(Variable(self.config.a, index)) Ok(Variable(self.config.a, index))
} }
fn lookup_table(&mut self, values: &[Vec<FF>]) -> Result<(), Error> {
for (&value_0, &value_1) in values[0].iter().zip(values[1].iter()) {
let index = self.current_gate;
self.current_gate += 1;
self.cs
.assign_fixed(self.config.sl, index, || Ok(value_0))?;
self.cs
.assign_fixed(self.config.sl2, index, || Ok(value_1))?;
}
Ok(())
}
} }
impl<F: FieldExt> Circuit<F> for MyCircuit<F> { impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
@ -319,6 +335,30 @@ fn test_proving() {
let sb = meta.fixed_column(); let sb = meta.fixed_column();
let sc = meta.fixed_column(); let sc = meta.fixed_column();
let sp = meta.fixed_column(); let sp = meta.fixed_column();
let sl = meta.fixed_column();
let sl2 = meta.fixed_column();
/*
* A B ... sl sl2
* [
* aux 0 ... 0 0
* a a ... 0 0
* a a^2 ... 0 0
* a a ... 0 0
* a a^2 ... 0 0
* ... ... ... ... ...
* ... ... ... aux 0
* ... ... ... a a
* ... ... ... a a^2
* ... ... ... 0 0
*
* ]
*/
meta.lookup(&[Column::<Any>::from(a)], &[Column::<Any>::from(sl)]);
meta.lookup(
&[Column::<Any>::from(a), Column::<Any>::from(b)],
&[Column::<Any>::from(sl), Column::<Any>::from(sl2)],
);
meta.create_gate(|meta| { meta.create_gate(|meta| {
let d = meta.query_advice(d, 1); let d = meta.query_advice(d, 1);
@ -355,6 +395,8 @@ fn test_proving() {
sc, sc,
sm, sm,
sp, sp,
sl,
sl2,
perm, perm,
perm2, perm2,
} }
@ -391,22 +433,33 @@ fn test_proving() {
cs.copy(b1, c0)?; cs.copy(b1, c0)?;
} }
cs.lookup_table(&self.lookup_tables)?;
Ok(()) Ok(())
} }
} }
let circuit: MyCircuit<Fp> = MyCircuit { let a = Fp::rand();
a: Some(Fp::rand()), let a_squared = a * &a;
let aux = Fp::one() + Fp::one();
let lookup_table = vec![aux, a, a, Fp::zero()];
let lookup_table_2 = vec![Fp::zero(), a, a_squared, Fp::zero()];
let empty_circuit: MyCircuit<Fp> = MyCircuit {
a: None,
lookup_tables: vec![lookup_table.clone(), lookup_table_2.clone()],
}; };
let empty_circuit: MyCircuit<Fp> = MyCircuit { a: None }; let circuit: MyCircuit<Fp> = MyCircuit {
a: Some(a),
lookup_tables: vec![lookup_table, lookup_table_2],
};
// Initialize the proving key // Initialize the proving key
let pk = keygen(&params, &empty_circuit).expect("keygen should not fail"); let pk = keygen(&params, &empty_circuit).expect("keygen should not fail");
let mut pubinputs = pk.get_vk().get_domain().empty_lagrange(); let mut pubinputs = pk.get_vk().get_domain().empty_lagrange();
pubinputs[0] = Fp::one(); pubinputs[0] = aux;
pubinputs[0] += Fp::one();
let pubinput = params let pubinput = params
.commit_lagrange(&pubinputs, Blind::default()) .commit_lagrange(&pubinputs, Blind::default())
.to_affine(); .to_affine();

View file

@ -358,6 +358,31 @@ impl<F: Field> ConstraintSystem<F> {
index index
} }
/// Add a lookup argument for some input columns and table columns.
pub fn lookup(
&mut self,
input_columns: &[Column<Any>],
table_columns: &[Column<Any>],
) -> usize {
let index = self.lookups.len();
if self.lookups.is_empty() {
let at = Rotation(-1);
let len = self.rotations.len();
self.rotations.entry(at).or_insert(PointIndex(len));
}
for input in input_columns {
self.query_any_index(*input, 0);
}
for table in table_columns {
self.query_any_index(*table, 0);
}
self.lookups
.push(lookup::Argument::new(input_columns, table_columns));
index
}
fn query_fixed_index(&mut self, column: Column<Fixed>, at: i32) -> usize { fn query_fixed_index(&mut self, column: Column<Fixed>, at: i32) -> usize {
let at = Rotation(at); let at = Rotation(at);
{ {