Skip to content
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

chore: Test stackoverflow edge case #110

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions .github/workflows/test-rust-stackoverflow-test-release-mode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test Stackoverflow test case

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: cargo test --release stack_overflow_poc -- --show-output
6 changes: 3 additions & 3 deletions bindings/c/src/compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub(crate) fn _compute_cells_and_kzg_proofs(
let (cells, proofs) = ctx
.compute_cells_and_kzg_proofs(blob)
.map_err(|err| CResult::with_error(&format!("{:?}", err)))?;
let cells_unboxed = cells.map(|cell| cell.to_vec());
let cells_unboxed: Vec<_> = cells.into_iter().map(|cell| cell.to_vec()).collect();

// Write to output
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_cells, cells_unboxed);
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_proofs, proofs);
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_cells, cells_unboxed.try_into().unwrap());
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_proofs, proofs.try_into().unwrap());

Ok(())
}
12 changes: 9 additions & 3 deletions bindings/c/src/recover_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ pub(crate) fn _recover_cells_and_proofs(
let (recovered_cells, recovered_proofs) = ctx
.recover_cells_and_proofs(cell_indices.to_vec(), cells)
.map_err(|err| CResult::with_error(&format!("{:?}", err)))?;
let recovered_cells_unboxed = recovered_cells.map(|cell| cell.to_vec());
let recovered_cells_unboxed: Vec<_> = recovered_cells
.into_iter()
.map(|cell| cell.to_vec())
.collect();

// Write to output
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_cells, recovered_cells_unboxed);
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_proofs, recovered_proofs);
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(
out_cells,
recovered_cells_unboxed.try_into().unwrap(),
);
write_to_2d_slice::<_, CELLS_PER_EXT_BLOB>(out_proofs, recovered_proofs.try_into().unwrap());

Ok(())
}
8 changes: 4 additions & 4 deletions bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn compute_cells_and_kzg_proofs<'local>(
let blob = slice_to_array_ref(&blob, "blob")?;

let (cells, proofs) = ctx.inner().compute_cells_and_kzg_proofs(blob)?;
let cells = cells.map(|cell| *cell);
cells_and_proofs_to_jobject(env, &cells, &proofs).map_err(Error::from)
let cells: Vec<_> = cells.into_iter().map(|cell| *cell).collect();
cells_and_proofs_to_jobject(env, &cells, &*proofs).map_err(Error::from)
}

#[no_mangle]
Expand Down Expand Up @@ -186,8 +186,8 @@ fn recover_cells_and_kzg_proofs<'local>(

let (recovered_cells, recovered_proofs) =
ctx.inner().recover_cells_and_proofs(cell_ids, cells)?;
let recovered_cells = recovered_cells.map(|cell| *cell);
cells_and_proofs_to_jobject(env, &recovered_cells, &recovered_proofs).map_err(Error::from)
let recovered_cells: Vec<_> = recovered_cells.into_iter().map(|cell| *cell).collect();
cells_and_proofs_to_jobject(env, &recovered_cells, &*recovered_proofs).map_err(Error::from)
}

/// Converts a JLongArray to a Vec<u64>
Expand Down
41 changes: 41 additions & 0 deletions eip7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,44 @@ impl DASContext {
&self.verifier_ctx
}
}

#[cfg(test)]
mod tests {
use super::*;
use bls12_381::Scalar;
#[test]
fn stack_overflow_poc() {
use rayon::prelude::*;

fn compute_blob(offset: u64) -> Vec<u8> {
let poly: Vec<_> = (0..4096).map(|i| -Scalar::from(i + offset)).collect();
let blob: Vec<_> = poly
.into_iter()
.flat_map(|scalar| scalar.to_bytes_be())
.collect();
blob
}

const NUM_BLOBS: u64 = 100;
let blobs = (0..NUM_BLOBS).map(compute_blob).collect::<Vec<_>>();

let trusted_setup = TrustedSetup::default();

let ctx = DASContext::with_threads(&trusted_setup, 8);
let blob_cells_and_proofs_vec: Vec<_> = blobs
.par_iter()
.map(|blob_vec| {
// let mut blob = [0; BYTES_PER_BLOB];
// blob.copy_from_slice(&blob_vec);
// let cells_and_proofs = ctx.compute_cells_and_kzg_proofs(&blob);

let cells_and_proofs =
ctx.compute_cells_and_kzg_proofs(blob_vec.as_slice().try_into().unwrap());

cells_and_proofs
})
.collect();

std::hint::black_box(blob_cells_and_proofs_vec);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! This is handy!

}
}
11 changes: 4 additions & 7 deletions eip7594/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ use kzg_multi_open::{
};

use crate::{
constants::{
CELLS_PER_EXT_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_CELL,
FIELD_ELEMENTS_PER_EXT_BLOB,
},
constants::{FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_CELL, FIELD_ELEMENTS_PER_EXT_BLOB},
errors::Error,
serialization::{
deserialize_blob_to_scalars, serialize_cells_and_proofs, serialize_g1_compressed,
},
trusted_setup::TrustedSetup,
BlobRef, Cell, CellIndex, CellRef, KZGCommitment, KZGProof, DASContext,
BlobRef, Cell, CellIndex, CellRef, DASContext, KZGCommitment, KZGProof,
};

/// Context object that is used to call functions in the prover API.
Expand Down Expand Up @@ -79,7 +76,7 @@ impl DASContext {
pub fn compute_cells_and_kzg_proofs(
&self,
blob: BlobRef,
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), Error> {
) -> Result<(Vec<Cell>, Vec<KZGProof>), Error> {
self.thread_pool.install(|| {
// Deserialization
//
Expand Down Expand Up @@ -107,7 +104,7 @@ impl DASContext {
&self,
cell_indices: Vec<CellIndex>,
cells: Vec<CellRef>,
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), Error> {
) -> Result<(Vec<Cell>, Vec<KZGProof>), Error> {
self.thread_pool.install(|| {
// Recover polynomial
//
Expand Down
17 changes: 9 additions & 8 deletions eip7594/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) fn deserialize_cells(
/// Converts a set of scalars (evaluations) to the `Cell` type.
pub(crate) fn coset_evaluations_to_cells<T: AsRef<[Scalar]>>(
evaluations: impl Iterator<Item = T>,
) -> [Cell; CELLS_PER_EXT_BLOB] {
) -> Vec<Cell> {
let cells: Vec<Cell> = evaluations
.map(|eval| serialize_scalars_to_cell(eval.as_ref()))
.map(|cell| {
Expand All @@ -122,24 +122,25 @@ pub(crate) fn coset_evaluations_to_cells<T: AsRef<[Scalar]>>(
.expect("infallible: Vec<u8> should have length equal to BYTES_PER_CELL")
})
.collect();

cells
.try_into()
.unwrap_or_else(|_| panic!("expected {} number of cells", CELLS_PER_EXT_BLOB))
// cells
// .try_into()
// .unwrap_or_else(|_| panic!("expected {} number of cells", CELLS_PER_EXT_BLOB))
}

pub(crate) fn serialize_cells_and_proofs(
coset_evaluations: Vec<Vec<Scalar>>,
proofs: Vec<G1Point>,
) -> ([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]) {
) -> (Vec<Cell>, Vec<KZGProof>) {
// Serialize the evaluation sets into `Cell`s.
let cells = coset_evaluations_to_cells(coset_evaluations.into_iter());

// Serialize the proofs into `KZGProof` objects.
let proofs: Vec<_> = proofs.iter().map(serialize_g1_compressed).collect();
let proofs: [KZGProof; CELLS_PER_EXT_BLOB] = proofs
.try_into()
.unwrap_or_else(|_| panic!("expected {} number of proofs", CELLS_PER_EXT_BLOB));
// let proofs: Box<[KZGProof; CELLS_PER_EXT_BLOB]> = proofs
// .into_boxed_slice()
// .try_into()
// .unwrap_or_else(|_| panic!("expected {} number of proofs", CELLS_PER_EXT_BLOB));

(cells, proofs)
}
Loading