-
Notifications
You must be signed in to change notification settings - Fork 1
SOT-133: Implement Plookup #11
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
Open
hduoc2003
wants to merge
6
commits into
main
Choose a base branch
from
sot-133-plookup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec22002
sot-133-plookup: add basic code structure
hduoc2003 ae234dc
SOT-154: Implement batch_verify function for KZG
hduoc2003 37a2a98
Merge branch 'sot-154-agg-kzg' of https://github.com/sota-zk-labs/zkp…
hduoc2003 e3b42d1
SOT-133: Complete coding and writing document
hduoc2003 3536d94
Add additive homomorphic PCS constraint
hduoc2003 96260c7
Refine
hduoc2003 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[workspace] | ||
members = ["plonk", "kzg"] | ||
members = ["plonk", "kzg", "lookup"] | ||
resolver = "2" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
This file contains hidden or 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,16 @@ | ||
[package] | ||
name = "lookup" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
merlin = "3.0.0" | ||
ark-ff = "0.4.2" | ||
ark-poly = "0.4.2" | ||
|
||
[dev-dependencies] | ||
ark-bls12-381 = "0.4.0" | ||
kzg = {path = "../kzg"} | ||
|
This file contains hidden or 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,12 @@ | ||
use ark_ff::PrimeField; | ||
|
||
use crate::types::Poly; | ||
|
||
pub trait PCS<F: PrimeField> { | ||
type Commitment; | ||
type Opening; | ||
|
||
fn new() -> Self; | ||
fn commit(&self, poly: &Poly<F>) -> Self::Commitment; | ||
fn open(&self, poly: &Poly<F>, z: F) -> Self::Opening; | ||
} |
This file contains hidden or 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,8 @@ | ||
pub mod transcript; | ||
pub mod commitment; | ||
pub mod types; | ||
pub mod plookup; | ||
pub mod multiset; | ||
pub mod lookup; | ||
pub mod utils; | ||
pub mod tests; |
This file contains hidden or 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,14 @@ | ||
use ark_ff::PrimeField; | ||
|
||
use crate::commitment::PCS; | ||
use crate::transcript::TranscriptProtocol; | ||
|
||
pub trait Lookup<F: PrimeField, P: PCS<F>> { | ||
type Proof; | ||
type Element; | ||
fn new(table: Vec<Self::Element>) -> Self; | ||
fn prove(&self, transcript: &mut TranscriptProtocol<F>, pcs: &P) -> Self::Proof; | ||
|
||
fn add_witness(&mut self, witness: Self::Element) -> bool; | ||
} | ||
|
This file contains hidden or 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 @@ | ||
pub mod multiset; | ||
This file contains hidden or 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,34 @@ | ||
use ark_ff::PrimeField; | ||
|
||
pub struct Multiset<F: PrimeField>(Vec<F>); | ||
|
||
impl<F: PrimeField> Multiset<F> { | ||
|
||
pub fn get_ref(&self) -> &Vec<F> { | ||
&self.0 | ||
} | ||
/// Creates an empty Multiset | ||
pub fn new() -> Self { | ||
Multiset(vec![]) | ||
} | ||
|
||
|
||
/// Pushes a value into the end of the set | ||
pub fn push(&mut self, val: F) { | ||
self.0.push(val); | ||
} | ||
|
||
/// Pushes `n` identical values into the set | ||
pub fn extend(&mut self, n: usize, val: F) { | ||
let elements = vec![val; n]; | ||
self.0.extend(elements); | ||
} | ||
|
||
/// Fetches last element in multiset | ||
/// | ||
/// Panics if there are no elements | ||
pub fn last(&self) -> F { | ||
*self.0.last().unwrap() | ||
} | ||
|
||
|
||
} |
This file contains hidden or 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,3 @@ | ||
pub trait PlookupError { | ||
|
||
} |
This file contains hidden or 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,32 @@ | ||
use std::marker::PhantomData; | ||
|
||
use ark_ff::PrimeField; | ||
|
||
use crate::commitment::PCS; | ||
use crate::lookup::Lookup; | ||
use crate::multiset::multiset::Multiset; | ||
use crate::plookup::proof::PlookupProof; | ||
use crate::transcript::TranscriptProtocol; | ||
|
||
pub struct PLookupElement<F: PrimeField, P: PCS<F>> { | ||
|
||
f: Multiset<F>, | ||
|
||
t: Multiset<F>, | ||
|
||
phantom: PhantomData<P> | ||
} | ||
|
||
impl<F: PrimeField, P: PCS<F>> Lookup<F, P> for PLookupElement<F, P> { | ||
type Proof = PlookupProof; | ||
type Element = F; | ||
|
||
fn new(table: Vec<Self::Element>) -> Self { | ||
|
||
todo!() | ||
} | ||
|
||
fn prove(&self, transcript: &mut TranscriptProtocol<F>, pcs: &P) -> Self::Proof { | ||
|
||
todo!() | ||
} | ||
|
||
fn add_witness(&mut self, witness: Self::Element) -> bool { | ||
|
||
todo!() | ||
} | ||
} |
This file contains hidden or 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,70 @@ | ||
use std::collections::{BTreeSet, HashSet}; | ||
|
||
use std::marker::PhantomData; | ||
|
||
use ark_ff::PrimeField; | ||
|
||
use crate::commitment::PCS; | ||
use crate::lookup::Lookup; | ||
use crate::plookup::proof::PlookupProof; | ||
use crate::transcript::TranscriptProtocol; | ||
|
||
pub struct PLookupVector<F: PrimeField, P: PCS<F>> { | ||
f: HashSet<Vec<F>>, | ||
t: HashSet<Vec<F>>, | ||
phantom: PhantomData<P> | ||
} | ||
|
||
impl<F: PrimeField, P: PCS<F>> Lookup<F, P> for PLookupVector<F, P> { | ||
type Proof = PlookupProof; | ||
type Element = Vec<F>; | ||
|
||
fn new(table: Vec<Self::Element>) -> Self { | ||
let mut t = HashSet::new(); | ||
for e in table { | ||
t.insert(e); | ||
} | ||
Self { | ||
f: HashSet::new(), | ||
t, | ||
phantom: PhantomData | ||
} | ||
} | ||
|
||
fn prove(&self, transcript: &mut TranscriptProtocol<F>, pcs: &P) -> Self::Proof { | ||
|
||
todo!() | ||
} | ||
|
||
fn add_witness(&mut self, witness: Self::Element) -> bool { | ||
if !self.t.contains(&witness) { | ||
return false; | ||
} | ||
if self.f.contains(&witness) { | ||
return false; | ||
} | ||
self.f.insert(witness); | ||
return true; | ||
|
||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::lookup::Lookup; | ||
use crate::plookup::lookup_vector::PLookupVector; | ||
use crate::tests::tests::{F, KZG12_381}; | ||
|
||
#[test] | ||
fn test_add_witness() { | ||
const BIT: i32 = 4; | ||
let mut v: Vec<Vec<F>> = vec![]; | ||
for a in 0..(1<<BIT) { | ||
for b in 0..(1<<BIT) { | ||
v.push(vec![F::from(a), F::from(b), F::from(a ^ b)]) | ||
} | ||
} | ||
let mut lookup = PLookupVector::<F, KZG12_381>::new(v); | ||
assert!(lookup.add_witness(vec![F::from(6), F::from(5), F::from(5^6)])); | ||
assert!(!lookup.add_witness(vec![F::from(6), F::from(5), F::from(5^6)])); | ||
assert!(lookup.add_witness(vec![F::from(3651), F::from(5), F::from(5^6)]);) | ||
// assert!(v.contains()); | ||
} | ||
} |
This file contains hidden or 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,4 @@ | ||
pub mod lookup_vector; | ||
pub mod lookup_element; | ||
pub mod proof; | ||
mod errors; |
This file contains hidden or 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,3 @@ | ||
pub struct PlookupProof { | ||
|
||
} |
This file contains hidden or 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,34 @@ | ||
#[cfg(test)] | ||
pub mod tests { | ||
use ark_bls12_381::Fr; | ||
use kzg::commitment::KzgCommitment; | ||
use kzg::opening::KzgOpening; | ||
use kzg::scheme::KzgScheme; | ||
use kzg::srs::Srs; | ||
use crate::commitment::PCS; | ||
use crate::types::Poly; | ||
|
||
pub type F = ark_bls12_381::Fr; | ||
pub struct KZG12_381 { | ||
scheme: KzgScheme | ||
} | ||
|
||
impl PCS<F> for KZG12_381 { | ||
type Commitment = KzgCommitment; | ||
type Opening = KzgOpening; | ||
|
||
fn new() -> Self { | ||
Self { | ||
scheme: KzgScheme::new(Srs::new(10)), | ||
} | ||
} | ||
|
||
fn commit(&self, poly: &Poly<Fr>) -> KzgCommitment { | ||
self.scheme.commit(poly) | ||
} | ||
|
||
fn open(&self, poly: &Poly<Fr>, z: Fr) -> KzgOpening { | ||
self.scheme.open(poly.clone(), z) | ||
} | ||
} | ||
} |
This file contains hidden or 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,38 @@ | ||
use std::marker::PhantomData; | ||
|
||
use ark_ff::{BigInteger, PrimeField}; | ||
use merlin::Transcript; | ||
|
||
pub struct TranscriptProtocol<F: PrimeField>(Transcript, PhantomData<F>); | ||
|
||
impl<F: PrimeField> TranscriptProtocol<F> { | ||
|
||
pub fn new(name: &'static [u8]) -> Self { | ||
Self(Transcript::new(name), PhantomData) | ||
} | ||
|
||
pub fn append_commitment(&mut self, label: &'static [u8], comm: &F) { | ||
|
||
// self.0.append_message(label, &comm.into().to_bytes_le()); | ||
} | ||
|
||
pub fn append_scalar(&mut self, label: &'static [u8], s: &F) { | ||
self.0.append_message(label, &s.into_bigint().to_bytes_le()) | ||
} | ||
|
||
pub fn challenge_scalar(&mut self, label: &'static [u8]) -> F { | ||
let mut buf = [0u8; 64]; | ||
self.0.challenge_bytes(label, &mut buf); | ||
|
||
// let mut rng = test_rng(); | ||
// Fr::rand(&mut rng) | ||
F::from_le_bytes_mod_order(&buf) | ||
} | ||
} | ||
|
||
pub struct TranscriptLabel; | ||
|
||
impl TranscriptLabel { | ||
pub const NAME: &'static [u8] = b"lookup"; | ||
pub const ZETA: &'static [u8] = b"zeta"; | ||
} | ||
|
This file contains hidden or 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,4 @@ | ||
use ark_ff::PrimeField; | ||
use ark_poly::univariate::DensePolynomial; | ||
|
||
pub type Poly<F: PrimeField> = DensePolynomial<F>; | ||
|
This file contains hidden or 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,16 @@ | ||
use ark_ff::PrimeField; | ||
|
||
/// Fold a vector into one element using a random challenge | ||
/// | ||
/// Eg. for `v = [A,B,C]` and a random challenge `k` | ||
/// | ||
/// The aggregate is k^0 *A + k^1 * B + k^2 * C | ||
pub fn fold<F: PrimeField>(v: &Vec<F>, zeta: F) -> F { | ||
let mut pow = F::one(); | ||
let mut res = F::zero(); | ||
for e in v { | ||
res += *e * pow; | ||
pow *= zeta; | ||
} | ||
return res; | ||
|
||
} |
This file contains hidden or 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 @@ | ||
pub mod fold; |
This file contains hidden or 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,21 @@ | ||
use lookup::commitment::PCS; | ||
use lookup::lookup::Lookup; | ||
use lookup::plookup::lookup_vector::PLookupVector; | ||
use lookup::tests::tests::{F, KZG12_381}; | ||
use lookup::transcript::{TranscriptLabel, TranscriptProtocol}; | ||
|
||
#[test] | ||
fn test() { | ||
const BIT: i32 = 4; | ||
let mut v: Vec<Vec<F>> = vec![]; | ||
for a in 0..(1<<BIT) { | ||
for b in 0..(1<<BIT) { | ||
v.push(vec![F::from(a), F::from(b), F::from(a ^ b)]) | ||
} | ||
} | ||
let mut transcript = TranscriptProtocol::<F>::new(TranscriptLabel::NAME); | ||
let zeta = transcript.challenge_scalar(TranscriptLabel::ZETA); | ||
let mut lookup = PLookupVector::<F, KZG12_381>::new(v); | ||
|
||
// lookup.add_witness(vec![F::from(5), F::from(6), F::from(5 ^ 6)]); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.