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

feat: VerifyCellKZGProofBatch now takes duplicated commitments #113

Merged
merged 3 commits into from
Jul 25, 2024
Merged
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
25 changes: 10 additions & 15 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ fn verification_result_to_bool_cresult(
/// will be created.
///
/// - The caller must ensure that the pointers are valid.
/// - The caller must ensure that `row_commitments` points to a region of memory that is at least `row_commitments_length` commitments
/// - The caller must ensure that `commitments` points to a region of memory that is at least `commitments_length` commitments
/// and that each commitment is at least `BYTES_PER_COMMITMENT` bytes.
/// - The caller must ensure that `row_indices` points to a region of memory that is at least `num_cells` elements
/// and that each element is 8 bytes.
/// - The caller must ensure that `column_indices` points to a region of memory that is at least `num_cells` elements
/// - The caller must ensure that `cell_indices` points to a region of memory that is at least `num_cells` elements
/// and that each element is 8 bytes.
/// - The caller must ensure that `cells` points to a region of memory that is at least `cells_length` proof and
/// that each cell is at least `BYTES_PER_CELL` bytes
Expand All @@ -233,14 +233,11 @@ fn verification_result_to_bool_cresult(
pub extern "C" fn verify_cell_kzg_proof_batch(
ctx: *const DASContext,

row_commitments_length: u64,
row_commitments: *const *const u8,
commitments_length: u64,
commitments: *const *const u8,

row_indices_length: u64,
row_indices: *const u64,

column_indices_length: u64,
column_indices: *const u64,
cell_indices_length: u64,
cell_indices: *const u64,

cells_length: u64,
cells: *const *const u8,
Expand All @@ -252,12 +249,10 @@ pub extern "C" fn verify_cell_kzg_proof_batch(
) -> CResult {
match _verify_cell_kzg_proof_batch(
ctx,
row_commitments_length,
row_commitments,
row_indices_length,
row_indices,
column_indices_length,
column_indices,
commitments_length,
commitments,
cell_indices_length,
cell_indices,
cells_length,
cells,
proofs_length,
Expand Down
30 changes: 10 additions & 20 deletions bindings/c/src/verify_cells_and_kzg_proofs_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ use rust_eth_kzg::constants::{BYTES_PER_CELL, BYTES_PER_COMMITMENT};
pub(crate) fn _verify_cell_kzg_proof_batch(
ctx: *const DASContext,

row_commitments_length: u64,
row_commitments: *const *const u8,
commitments_length: u64,
commitments: *const *const u8,

row_indices_length: u64,
row_indices: *const u64,

column_indices_length: u64,
column_indices: *const u64,
cell_indices_length: u64,
cell_indices: *const u64,

cells_length: u64,
cells: *const *const u8,
Expand Down Expand Up @@ -44,26 +41,19 @@ pub(crate) fn _verify_cell_kzg_proof_batch(
// Dereference the input pointers
//
let ctx = deref_const(ctx).inner();
let row_commitments = ptr_ptr_to_vec_slice_const::<BYTES_PER_COMMITMENT>(
row_commitments,
row_commitments_length as usize,
let commitments = ptr_ptr_to_vec_slice_const::<BYTES_PER_COMMITMENT>(
commitments,
commitments_length as usize,
);
let row_indices = create_slice_view(row_indices, row_indices_length as usize);
let column_indices = create_slice_view(column_indices, column_indices_length as usize);
let cell_indices = create_slice_view(cell_indices, cell_indices_length as usize);
let cells = ptr_ptr_to_vec_slice_const::<BYTES_PER_CELL>(cells, cells_length as usize);
let proofs = ptr_ptr_to_vec_slice_const::<BYTES_PER_COMMITMENT>(proofs, proofs_length as usize);
let verified = deref_mut(verified);

// Computation
//
let verification_result = ctx.verify_cell_kzg_proof_batch(
row_commitments,
// TODO: conversion to a vector should not be needed
row_indices.to_vec(),
column_indices.to_vec(),
cells,
proofs,
);
let verification_result =
ctx.verify_cell_kzg_proof_batch(commitments, cell_indices.to_vec(), cells, proofs);

// Write to output
let proof_is_valid = verification_result_to_bool_cresult(verification_result)?;
Expand Down
19 changes: 9 additions & 10 deletions bindings/csharp/csharp_code/EthKZG.bindings/ethkzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public unsafe (byte[][], byte[][]) ComputeCellsAndKZGProofs(byte[] blob)
return (outCells, outProofs);
}

public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices, ulong[] columnIndices, byte[][] cells, byte[][] proofs)
public bool VerifyCellKZGProofBatch(byte[][] commitments, ulong[] cellIndices, byte[][] cells, byte[][] proofs)
{

// Length checks
Expand All @@ -131,19 +131,19 @@ public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices,
}
}

for (int i = 0; i < rowCommitments.Length; i++)
for (int i = 0; i < commitments.Length; i++)
{
if (rowCommitments[i].Length != BytesPerCommitment)
if (commitments[i].Length != BytesPerCommitment)
{
throw new ArgumentException($"commitments at index {i} has an invalid length");
}
}

int numCells = cells.Length;
int numProofs = proofs.Length;
int numRowCommitments = rowCommitments.Length;
int numCommitments = commitments.Length;

byte*[] commPtrs = new byte*[numRowCommitments];
byte*[] commPtrs = new byte*[numCommitments];
byte*[] cellsPtrs = new byte*[numCells];
byte*[] proofsPtrs = new byte*[numProofs];

Expand All @@ -153,8 +153,7 @@ public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices,
fixed (byte** commitmentPtrPtr = commPtrs)
fixed (byte** cellsPtrPtr = cellsPtrs)
fixed (byte** proofsPtrPtr = proofsPtrs)
fixed (ulong* rowIndicesPtr = rowIndices)
fixed (ulong* columnIndicesPtr = columnIndices)
fixed (ulong* cellIndicesPtr = cellIndices)
{
// Get the pointer for each cell
for (int i = 0; i < numCells; i++)
Expand All @@ -166,9 +165,9 @@ public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices,
}

// Get the pointer for each commitment
for (int i = 0; i < numRowCommitments; i++)
for (int i = 0; i < numCommitments; i++)
{
fixed (byte* commPtr = rowCommitments[i])
fixed (byte* commPtr = commitments[i])
{
commitmentPtrPtr[i] = commPtr;
}
Expand All @@ -183,7 +182,7 @@ public bool VerifyCellKZGProofBatch(byte[][] rowCommitments, ulong[] rowIndices,
}
}

CResult result = verify_cell_kzg_proof_batch(_context, Convert.ToUInt64(rowCommitments.Length), commitmentPtrPtr, Convert.ToUInt64(rowIndices.Length), rowIndicesPtr, Convert.ToUInt64(columnIndices.Length), columnIndicesPtr, Convert.ToUInt64(cells.Length), cellsPtrPtr, Convert.ToUInt64(proofs.Length), proofsPtrPtr, verifiedPtr);
CResult result = verify_cell_kzg_proof_batch(_context, Convert.ToUInt64(commitments.Length), commitmentPtrPtr, Convert.ToUInt64(cellIndices.Length), cellIndicesPtr, Convert.ToUInt64(cells.Length), cellsPtrPtr, Convert.ToUInt64(proofs.Length), proofsPtrPtr, verifiedPtr);
ThrowOnError(result);
}
return verified;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ internal static unsafe partial class NativeMethods
/// will be created.
///
/// - The caller must ensure that the pointers are valid.
/// - The caller must ensure that `row_commitments` points to a region of memory that is at least `row_commitments_length` commitments
/// - The caller must ensure that `commitments` points to a region of memory that is at least `commitments_length` commitments
/// and that each commitment is at least `BYTES_PER_COMMITMENT` bytes.
/// - The caller must ensure that `row_indices` points to a region of memory that is at least `num_cells` elements
/// and that each element is 8 bytes.
/// - The caller must ensure that `column_indices` points to a region of memory that is at least `num_cells` elements
/// - The caller must ensure that `cell_indices` points to a region of memory that is at least `num_cells` elements
/// and that each element is 8 bytes.
/// - The caller must ensure that `cells` points to a region of memory that is at least `cells_length` proof and
/// that each cell is at least `BYTES_PER_CELL` bytes
Expand All @@ -122,7 +122,7 @@ internal static unsafe partial class NativeMethods
/// If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
/// </summary>
[DllImport(__DllName, EntryPoint = "verify_cell_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult verify_cell_kzg_proof_batch(DASContext* ctx, ulong row_commitments_length, byte** row_commitments, ulong row_indices_length, ulong* row_indices, ulong column_indices_length, ulong* column_indices, ulong cells_length, byte** cells, ulong proofs_length, byte** proofs, bool* verified);
internal static extern CResult verify_cell_kzg_proof_batch(DASContext* ctx, ulong commitments_length, byte** commitments, ulong cell_indices_length, ulong* cell_indices, ulong cells_length, byte** cells, ulong proofs_length, byte** proofs, bool* verified);

/// <summary>
/// Recovers all cells and their KZG proofs from the given cell indices and cells
Expand Down
12 changes: 5 additions & 7 deletions bindings/csharp/csharp_code/EthKZG.test/RefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ public void TestComputeCellsAndKzgProofs()

private class VerifyCellKzgProofBatchInput
{
public List<string> RowCommitments { get; set; } = null!;
public List<ulong> RowIndices { get; set; } = null!;
public List<ulong> ColumnIndices { get; set; } = null!;
public List<string> Commitments { get; set; } = null!;
public List<ulong> CellIndices { get; set; } = null!;
public List<string> Cells { get; set; } = null!;
public List<string> Proofs { get; set; } = null!;
}
Expand All @@ -181,15 +180,14 @@ public void TestVerifyCellKzgProofBatch()
VerifyCellKzgProofBatchTest test = _deserializerUnderscoreNaming.Deserialize<VerifyCellKzgProofBatchTest>(yaml);
Assert.That(test, Is.Not.EqualTo(null));

byte[][] rowCommitments = GetByteArrays(test.Input.RowCommitments);
ulong[] rowIndices = test.Input.RowIndices.ToArray();
ulong[] columnIndices = test.Input.ColumnIndices.ToArray();
byte[][] commitments = GetByteArrays(test.Input.Commitments);
ulong[] cellIndices = test.Input.CellIndices.ToArray();
byte[][] cells = GetByteArrays(test.Input.Cells);
byte[][] proofs = GetByteArrays(test.Input.Proofs);

try
{
bool isCorrect = _context.VerifyCellKZGProofBatch(rowCommitments, rowIndices, columnIndices, cells, proofs);
bool isCorrect = _context.VerifyCellKZGProofBatch(commitments, cellIndices, cells, proofs);
Assert.That(isCorrect, Is.EqualTo(test.Output));
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public CellsAndProofs computeCellsAndKZGProofs(byte[] blob) {
return cellsAndProofs;
}

public boolean verifyCellKZGProofBatch(byte[][] commitmentsArr, long[] rowIndices, long[] columnIndices, byte[][] cellsArr,
public boolean verifyCellKZGProofBatch(byte[][] commitmentsArr, long[] cellIndices, byte[][] cellsArr,
byte[][] proofsArr) {
checkContextHasNotBeenFreed();
return verifyCellKZGProofBatch(contextPtr, commitmentsArr, rowIndices, columnIndices, cellsArr, proofsArr);
return verifyCellKZGProofBatch(contextPtr, commitmentsArr, cellIndices, cellsArr, proofsArr);
}

public CellsAndProofs recoverCellsAndProofs(long[] cellIDs, byte[][] cellsArr) {
Expand All @@ -103,7 +103,7 @@ public CellsAndProofs recoverCellsAndProofs(long[] cellIDs, byte[][] cellsArr) {
private static native byte[] blobToKZGCommitment(long context_ptr, byte[] blob);

private static native boolean verifyCellKZGProofBatch(
long context_ptr, byte[][] commitments, long[] rowIndices, long[] columnIndices, byte[][] cells, byte[][] proofs);
long context_ptr, byte[][] commitments, long[] cellIndices, byte[][] cells, byte[][] proofs);

private static native CellsAndProofs recoverCellsAndProof(long context_ptr, long[] cellIDs, byte[][] cells);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ public void recoverCellsAndKzgProofsTests(final RecoverCellsAndKzgProofsTest tes
public void verifyCellKzgProofBatchTests(final VerifyCellKzgProofBatchTest test) {
try {
boolean valid = context.verifyCellKZGProofBatch(
test.getInput().getRowCommitments(),
test.getInput().getRowIndices(),
test.getInput().getColumnIndices(),
test.getInput().getCommitments(),
test.getInput().getCellIndices(),
test.getInput().getCells(),
test.getInput().getProofs());
assertEquals(test.getOutput(), valid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,26 @@

public class VerifyCellKzgProofBatchTest {
public static class Input {
@JsonProperty("row_commitments")
private List<String> rowCommitments;
@JsonProperty("commitments")
private List<String> commitments;

@JsonProperty("row_indices")
private List<Long> rowIndices;

@JsonProperty("column_indices")
private List<Long> columnIndices;
@JsonProperty("cell_indices")
private List<Long> cellIndices;

private List<String> cells;
private List<String> proofs;

public byte[][] getRowCommitments() {
return rowCommitments.stream()
public byte[][] getCommitments() {
return commitments.stream()
.map(Bytes::fromHexString)
.map(Bytes::toArray)
.collect(Collectors.toList())
.toArray(byte[][]::new);
}

public long[] getRowIndices() {
return rowIndices.stream().mapToLong(Long::longValue).toArray();
}

public long[] getColumnIndices() {
return columnIndices.stream().mapToLong(Long::longValue).toArray();
public long[] getCellIndices() {
return cellIndices.stream().mapToLong(Long::longValue).toArray();
}

public byte[][] getCells() {
Expand Down
4 changes: 2 additions & 2 deletions bindings/java/rust_code/ethereum_cryptography_LibEthKZG.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 8 additions & 22 deletions bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,13 @@ pub extern "system" fn Java_ethereum_cryptography_LibEthKZG_verifyCellKZGProofBa
_class: JClass,
ctx_ptr: jlong,
commitment: JObjectArray<'local>,
row_indices: JLongArray,
column_indices: JLongArray,
cell_indices: JLongArray,
cells: JObjectArray<'local>,
proofs: JObjectArray<'local>,
) -> jboolean {
let ctx = unsafe { &*(ctx_ptr as *const DASContext) };

match verify_cell_kzg_proof_batch(
&mut env,
ctx,
commitment,
row_indices,
column_indices,
cells,
proofs,
) {
match verify_cell_kzg_proof_batch(&mut env, ctx, commitment, cell_indices, cells, proofs) {
Ok(result) => result,
Err(err) => {
throw_on_error(&mut env, err, "verifyCellKZGProofBatch");
Expand All @@ -116,14 +107,12 @@ fn verify_cell_kzg_proof_batch<'local>(
env: &mut JNIEnv,
ctx: &DASContext,
commitment: JObjectArray<'local>,
row_indices: JLongArray,
column_indices: JLongArray,
cell_indices: JLongArray,
cells: JObjectArray<'local>,
proofs: JObjectArray<'local>,
) -> Result<jboolean, Error> {
let commitment = jobject_array_to_2d_byte_array(env, commitment)?;
let row_indices = jlongarray_to_vec_u64(env, row_indices)?;
let column_indices = jlongarray_to_vec_u64(env, column_indices)?;
let cell_indices = jlongarray_to_vec_u64(env, cell_indices)?;
let cells = jobject_array_to_2d_byte_array(env, cells)?;
let proofs = jobject_array_to_2d_byte_array(env, proofs)?;

Expand All @@ -140,13 +129,10 @@ fn verify_cell_kzg_proof_batch<'local>(
.map(|proof| slice_to_array_ref(proof, "proof"))
.collect::<Result<_, _>>()?;

match ctx.inner().verify_cell_kzg_proof_batch(
commitments,
row_indices,
column_indices,
cells,
proofs,
) {
match ctx
.inner()
.verify_cell_kzg_proof_batch(commitments, cell_indices, cells, proofs)
{
Ok(_) => Ok(jboolean::from(true)),
Err(x) if x.invalid_proof() => Ok(jboolean::from(false)),
Err(err) => Err(Error::Cryptography(err)),
Expand Down
14 changes: 6 additions & 8 deletions bindings/nim/nim_code/nim_eth_kzg/header.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ proc compute_cells_and_kzg_proofs*(ctx: ptr DASContext,
# will be created.
#
# - The caller must ensure that the pointers are valid.
# - The caller must ensure that `row_commitments` points to a region of memory that is at least `row_commitments_length` commitments
# - The caller must ensure that `commitments` points to a region of memory that is at least `commitments_length` commitments
# and that each commitment is at least `BYTES_PER_COMMITMENT` bytes.
# - The caller must ensure that `row_indices` points to a region of memory that is at least `num_cells` elements
# and that each element is 8 bytes.
# - The caller must ensure that `column_indices` points to a region of memory that is at least `num_cells` elements
# - The caller must ensure that `cell_indices` points to a region of memory that is at least `num_cells` elements
# and that each element is 8 bytes.
# - The caller must ensure that `cells` points to a region of memory that is at least `cells_length` proof and
# that each cell is at least `BYTES_PER_CELL` bytes
Expand All @@ -109,12 +109,10 @@ proc compute_cells_and_kzg_proofs*(ctx: ptr DASContext,
# - This implementation will check if the ctx pointer is null, but it will not check if the other arguments are null.
# If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
proc verify_cell_kzg_proof_batch*(ctx: ptr DASContext,
row_commitments_length: uint64,
row_commitments: ptr pointer,
row_indices_length: uint64,
row_indices: pointer,
column_indices_length: uint64,
column_indices: pointer,
commitments_length: uint64,
commitments: ptr pointer,
cell_indices_length: uint64,
cell_indices: pointer,
cells_length: uint64,
cells: ptr pointer,
proofs_length: uint64,
Expand Down
Loading
Loading