Skip to content

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
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
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
16 changes: 16 additions & 0 deletions lookup/Cargo.toml
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"}

12 changes: 12 additions & 0 deletions lookup/src/commitment.rs
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;
}
8 changes: 8 additions & 0 deletions lookup/src/lib.rs
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;
14 changes: 14 additions & 0 deletions lookup/src/lookup.rs
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;
}

1 change: 1 addition & 0 deletions lookup/src/multiset/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod multiset;
34 changes: 34 additions & 0 deletions lookup/src/multiset/multiset.rs
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()
}


}
3 changes: 3 additions & 0 deletions lookup/src/plookup/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait PlookupError {

}
32 changes: 32 additions & 0 deletions lookup/src/plookup/lookup_element.rs
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!()
}
}
70 changes: 70 additions & 0 deletions lookup/src/plookup/lookup_vector.rs
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());
}
}
4 changes: 4 additions & 0 deletions lookup/src/plookup/mod.rs
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;
3 changes: 3 additions & 0 deletions lookup/src/plookup/proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct PlookupProof {

}
34 changes: 34 additions & 0 deletions lookup/src/tests.rs
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)
}
}
}
38 changes: 38 additions & 0 deletions lookup/src/transcript.rs
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";
}

4 changes: 4 additions & 0 deletions lookup/src/types.rs
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>;
16 changes: 16 additions & 0 deletions lookup/src/utils/fold.rs
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;
}
1 change: 1 addition & 0 deletions lookup/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod fold;
21 changes: 21 additions & 0 deletions lookup/tests/plookup.rs
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)]);
}