Skip to content

Commit

Permalink
Revert "Compatability changes to work with arb nitro"
Browse files Browse the repository at this point in the history
  • Loading branch information
afkbyte committed May 31, 2024
1 parent 5667c66 commit 3a03a68
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 377 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ num-traits = "0.2"
byteorder = "1.4"
ark-poly = "0.4.2"
crossbeam-channel = "0.5"
num_cpus = "1.13.0"
num_cpus = "^1.16.0"

[dev-dependencies]
criterion = "0.5"
lazy_static = "1.4"
tracing = { version = "^0.1.34", features = ["log"] }
tracing-subscriber = "0.3.18"

[[test]]
name = "kzg"
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_kzg_commit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg, polynomial::PolynomialFormat};
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};
use std::time::Duration;

fn bench_kzg_commit(c: &mut Criterion) {
Expand All @@ -17,7 +17,7 @@ fn bench_kzg_commit(c: &mut Criterion) {
c.bench_function("bench_kzg_commit_10000", |b| {
let random_blob: Vec<u8> = (0..10000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
b.iter(|| kzg.commit(&input_poly).unwrap());
Expand All @@ -26,7 +26,7 @@ fn bench_kzg_commit(c: &mut Criterion) {
c.bench_function("bench_kzg_commit_30000", |b| {
let random_blob: Vec<u8> = (0..30000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
b.iter(|| kzg.commit(&input_poly).unwrap());
Expand All @@ -35,7 +35,7 @@ fn bench_kzg_commit(c: &mut Criterion) {
c.bench_function("bench_kzg_commit_50000", |b| {
let random_blob: Vec<u8> = (0..50000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
b.iter(|| kzg.commit(&input_poly).unwrap());
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_kzg_proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg, polynomial::PolynomialFormat};
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};
use std::time::Duration;

fn bench_kzg_proof(c: &mut Criterion) {
Expand All @@ -17,7 +17,7 @@ fn bench_kzg_proof(c: &mut Criterion) {
c.bench_function("bench_kzg_proof_10000", |b| {
let random_blob: Vec<u8> = (0..10000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand All @@ -31,7 +31,7 @@ fn bench_kzg_proof(c: &mut Criterion) {
c.bench_function("bench_kzg_proof_30000", |b| {
let random_blob: Vec<u8> = (0..30000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand All @@ -45,7 +45,7 @@ fn bench_kzg_proof(c: &mut Criterion) {
c.bench_function("bench_kzg_proof_50000", |b| {
let random_blob: Vec<u8> = (0..50000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_kzg_verify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rand::Rng;
use rust_kzg_bn254::{blob::Blob, kzg::Kzg, polynomial::PolynomialFormat};
use rust_kzg_bn254::{blob::Blob, kzg::Kzg};
use std::time::Duration;

fn bench_kzg_verify(c: &mut Criterion) {
Expand All @@ -17,7 +17,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
c.bench_function("bench_kzg_verify_10000", |b| {
let random_blob: Vec<u8> = (0..10000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand All @@ -34,7 +34,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
c.bench_function("bench_kzg_verify_30000", |b| {
let random_blob: Vec<u8> = (0..30000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand All @@ -51,7 +51,7 @@ fn bench_kzg_verify(c: &mut Criterion) {
c.bench_function("bench_kzg_verify_50000", |b| {
let random_blob: Vec<u8> = (0..50000).map(|_| rng.gen_range(32..=126) as u8).collect();
let input = Blob::from_bytes_and_pad(&random_blob);
let input_poly = input.to_polynomial(PolynomialFormat::InCoefficientForm).unwrap();
let input_poly = input.to_polynomial().unwrap();
kzg.data_setup_custom(1, input.len().try_into().unwrap())
.unwrap();
let index =
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = '1.74'
channel = 'nightly-2024-04-29'
profile = 'minimal'
components = ['clippy', 'rustfmt']
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "wasm32-unknown-unknown", "aarch64-apple-darwin"]
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "wasm32-unknown-unknown"]
34 changes: 4 additions & 30 deletions src/blob.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::{
errors::BlobError,
helpers,
polynomial::{Polynomial, PolynomialFormat},
};
use crate::{errors::BlobError, helpers, polynomial::Polynomial};

/// A blob which is Eigen DA spec aligned.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -43,22 +39,6 @@ impl Blob {
}
}

/// Creates a new `Blob` from the provided byte slice and assumes it's
/// already padded according to DA specs.
/// WARNING: This function does not check if the bytes are modulo bn254
/// if the data has 32 byte segments exceeding the modulo of the field
/// then the bytes will be modded by the order of the field and the data
/// will be transformed incorrectly
pub fn from_padded_bytes_unchecked(input: &[u8]) -> Self {
let length_after_padding = input.len();

Blob {
blob_data: input.to_vec(),
is_padded: true,
length_after_padding,
}
}

/// Returns the blob data
pub fn get_blob_data(&self) -> Vec<u8> {
self.blob_data.clone()
Expand All @@ -69,11 +49,6 @@ impl Blob {
self.blob_data.len()
}

/// Checks if the blob data is empty.
pub fn is_empty(&self) -> bool {
self.blob_data.is_empty()
}

/// Pads the blob data in-place if it is not already padded.
pub fn pad_data(&mut self) -> Result<(), BlobError> {
if self.is_padded {
Expand All @@ -91,21 +66,20 @@ impl Blob {
if !self.is_padded {
Err(BlobError::NotPaddedError)
} else {
self.blob_data =
helpers::remove_empty_byte_from_padded_bytes_unchecked(&self.blob_data);
self.blob_data = helpers::remove_empty_byte_from_padded_bytes(&self.blob_data);
self.is_padded = false;
self.length_after_padding = 0;
Ok(())
}
}

/// Converts the blob data to a `Polynomial` if the data is padded.
pub fn to_polynomial(&self, form: PolynomialFormat) -> Result<Polynomial, BlobError> {
pub fn to_polynomial(&self) -> Result<Polynomial, BlobError> {
if !self.is_padded {
Err(BlobError::NotPaddedError)
} else {
let fr_vec = helpers::to_fr_array(&self.blob_data);
let poly = Polynomial::new(&fr_vec, self.length_after_padding, form)
let poly = Polynomial::new(&fr_vec, self.length_after_padding)
.map_err(|err| BlobError::GenericError(err.to_string()))?;
Ok(poly)
}
Expand Down
6 changes: 0 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub enum PolynomialError {
SerializationFromStringError,
CommitError(String),
GenericError(String),
FFTError(String),
IncorrectFormError(String),
}

impl fmt::Display for PolynomialError {
Expand All @@ -35,11 +33,7 @@ impl fmt::Display for PolynomialError {
write!(f, "couldn't load string to fr vector")
},
PolynomialError::CommitError(ref msg) => write!(f, "Commitment error: {}", msg),
PolynomialError::FFTError(ref msg) => write!(f, "FFT error: {}", msg),
PolynomialError::GenericError(ref msg) => write!(f, "generic error: {}", msg),
PolynomialError::IncorrectFormError(ref msg) => {
write!(f, "Incorrect form error: {}", msg)
},
}
}
}
Expand Down
45 changes: 24 additions & 21 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
traits::ReadPointFromBytes,
};

pub fn blob_to_polynomial(blob: &[u8]) -> Vec<Fr> {
pub fn blob_to_polynomial(blob: &Vec<u8>) -> Vec<Fr> {
to_fr_array(blob)
}

Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn convert_by_padding_empty_byte(data: &[u8]) -> Vec<u8> {
valid_data
}

pub fn remove_empty_byte_from_padded_bytes_unchecked(data: &[u8]) -> Vec<u8> {
pub fn remove_empty_byte_from_padded_bytes(data: &[u8]) -> Vec<u8> {
let data_size = data.len();
let parse_size = BYTES_PER_FIELD_ELEMENT;
let data_len = (data_size + parse_size - 1) / parse_size;
Expand Down Expand Up @@ -94,15 +94,15 @@ pub fn to_fr_array(data: &[u8]) -> Vec<Fr> {
let num_ele = get_num_element(data.len(), BYTES_PER_FIELD_ELEMENT);
let mut eles = vec![Fr::zero(); num_ele]; // Initialize with zero elements

for (i, element) in eles.iter_mut().enumerate().take(num_ele) {
for i in 0..num_ele {
let start = i * BYTES_PER_FIELD_ELEMENT;
let end = (i + 1) * BYTES_PER_FIELD_ELEMENT;
if end > data.len() {
let mut padded = vec![0u8; BYTES_PER_FIELD_ELEMENT];
padded[..data.len() - start].copy_from_slice(&data[start..]);
*element = set_bytes_canonical(&padded);
eles[i] = set_bytes_canonical(&padded);
} else {
*element = set_bytes_canonical(&data[start..end]);
eles[i] = set_bytes_canonical(&data[start..end]);
}
}
eles
Expand All @@ -113,8 +113,8 @@ pub fn to_byte_array(data_fr: &[Fr], max_data_size: usize) -> Vec<u8> {
let data_size = cmp::min(n * BYTES_PER_FIELD_ELEMENT, max_data_size);
let mut data = vec![0u8; data_size];

for (i, element) in data_fr.iter().enumerate().take(n) {
let v: Vec<u8> = element.into_bigint().to_bytes_be();
for i in 0..n {
let v: Vec<u8> = data_fr[i].into_bigint().to_bytes_be();

let start = i * BYTES_PER_FIELD_ELEMENT;
let end = (i + 1) * BYTES_PER_FIELD_ELEMENT;
Expand All @@ -136,8 +136,8 @@ pub fn is_zeroed(first_byte: u8, buf: Vec<u8>) -> bool {
return false;
}

for byte in &buf {
if *byte != 0 {
for i in 0..buf.len() {
if buf[i] != 0 {
return false;
}
}
Expand All @@ -147,13 +147,13 @@ pub fn is_zeroed(first_byte: u8, buf: Vec<u8>) -> bool {
pub fn str_vec_to_fr_vec(input: Vec<&str>) -> Result<Vec<Fr>, &str> {
let mut output: Vec<Fr> = Vec::<Fr>::with_capacity(input.len());

for element in &input {
if *element == "-1" {
for i in 0..input.len() {
if input[i] == "-1" {
let mut test = Fr::one();
test.neg_in_place();
output.push(test);
} else {
let fr_data = Fr::from_str(element).expect("could not load string to Fr");
let fr_data = Fr::from_str(input[i]).expect("could not load string to Fr");
output.push(fr_data);
}
}
Expand Down Expand Up @@ -232,11 +232,13 @@ pub fn read_g2_point_from_bytes_be(g2_bytes_be: &Vec<u8>) -> Result<G2Affine, &s

let mut y_sqrt = added_result.sqrt().ok_or("no square root found").unwrap();

let lexicographical_check_result = if y_sqrt.c1.0.is_zero() {
lexicographically_largest(&y_sqrt.c0)
let mut lexicographical_check_result = false;

if y_sqrt.c1.0.is_zero() {
lexicographical_check_result = lexicographically_largest(&y_sqrt.c0);
} else {
lexicographically_largest(&y_sqrt.c1)
};
lexicographical_check_result = lexicographically_largest(&y_sqrt.c1);
}

if lexicographical_check_result {
if m_data == m_compressed_smallest {
Expand Down Expand Up @@ -303,11 +305,12 @@ where
{
receiver
.iter()
.map(|(chunk, position)| {
let point =
T::read_point_from_bytes_be(&chunk).expect("Failed to read point from bytes");
(point, position)
})
.filter_map(
|(chunk, position)| match T::read_point_from_bytes_be(&chunk) {
Ok(point) => Some((point, position)),
Err(err) => panic!("{}", err),
},
)
.collect()
}

Expand Down
Loading

0 comments on commit 3a03a68

Please sign in to comment.