Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat-trait-compress
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobkaufmann committed Mar 7, 2024
2 parents 899affc + 2eb5170 commit ea8da06
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 350 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ serde_yaml = "0.9.25"
rand = "0.8.5"

[features]
default = ["rand"]
default = []
rand = ["dep:rand"]

[[bench]]
name = "kzg"
harness = false
harness = false
required-features = ["rand"]
14 changes: 7 additions & 7 deletions benches/kzg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use kateth::{
blob::Blob,
kzg::{Commitment, Proof, Setup},
kzg::{Bytes48, Setup},
};

use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput};
Expand All @@ -14,17 +14,17 @@ pub fn benchmark(c: &mut Criterion) {
let max_batch_size = *batch_sizes.last().unwrap();

let mut rng = thread_rng();
let blobs: Vec<Blob<4096>> = (0..max_batch_size)
.map(|_| Blob::random(&mut rng))
let blobs: Vec<Vec<u8>> = (0..max_batch_size)
.map(|_| Blob::<4096>::random(&mut rng).to_bytes())
.collect();
let commitments: Vec<Commitment> = blobs
let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| kzg.blob_to_commitment(blob))
.map(|blob| kzg.blob_to_commitment(blob).unwrap().serialize())
.collect();
let proofs: Vec<Proof> = blobs
let proofs: Vec<Bytes48> = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| kzg.blob_proof(blob, commitment))
.map(|(blob, commitment)| kzg.blob_proof(blob, commitment).unwrap().serialize())
.collect();

c.bench_function("blob to kzg commitment", |b| {
Expand Down
10 changes: 10 additions & 0 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
kzg::{Commitment, Polynomial, Proof, Setup},
};

#[derive(Clone, Copy, Debug)]
pub enum Error {
InvalidFieldElement,
InvalidLen,
Expand Down Expand Up @@ -35,6 +36,15 @@ impl<const N: usize> Blob<N> {
Ok(Self { elements })
}

pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(Self::BYTES);
for element in self.elements.iter() {
let element = element.to_be_bytes();
bytes.extend_from_slice(&element);
}
bytes
}

pub(crate) fn commitment<const G2: usize>(&self, setup: &Setup<N, G2>) -> Commitment {
let lincomb =
P1::lincomb_pippenger(setup.g1_lagrange_brp.as_slice(), self.elements.as_slice());
Expand Down
22 changes: 21 additions & 1 deletion src/kzg/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
use crate::bls;
use crate::{blob, bls};

mod poly;
mod setup;

#[cfg(test)]
mod spec;

pub type Proof = bls::P1;
pub type Commitment = bls::P1;

pub type Bytes32 = [u8; 32];
pub type Bytes48 = [u8; 48];

#[derive(Clone, Copy, Debug)]
pub enum Error {
Blob(blob::Error),
Bls(bls::Error),
}

impl From<blob::Error> for Error {
fn from(value: blob::Error) -> Self {
Self::Blob(value)
}
}

impl From<bls::Error> for Error {
fn from(value: bls::Error) -> Self {
Self::Bls(value)
}
}

pub(crate) use poly::Polynomial;

pub use setup::Setup;
Loading

0 comments on commit ea8da06

Please sign in to comment.