Skip to content

Commit

Permalink
fix: namespace C methods (#148)
Browse files Browse the repository at this point in the history
* add namespace to c methods

* add eth_kzg prefix to csharp methods

* modify golang code

* update nim bindings

* add missing prefix

* fix comment

* update header

* update method call
  • Loading branch information
kevaundray committed Aug 13, 2024
1 parent 5524142 commit e175ccd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 78 deletions.
29 changes: 19 additions & 10 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ pub use rust_eth_kzg::constants::{
};
use rust_eth_kzg::Error;

/*
* Note: All methods in this file have been prefixed with `eth_kzg`.
* This is so that when they are imported into languages such as nim,
* they will have a separate namespace to other c libraries.
*
* ie Nim will take two c libraries and put their methods in the same
* namespace.
*/

// This is a wrapper around the DASContext from the eip7594 library.
// We need to wrap it as some bindgen tools cannot pick up items
// not defined in this file.
Expand All @@ -39,7 +48,7 @@ impl DASContext {
/// To avoid memory leaks, one should ensure that the pointer is freed after use
/// by calling `das_context_free`.
#[no_mangle]
pub extern "C" fn das_context_new() -> *mut DASContext {
pub extern "C" fn eth_kzg_das_context_new() -> *mut DASContext {
let ctx = Box::<DASContext>::default();
Box::into_raw(ctx)
}
Expand All @@ -59,7 +68,7 @@ pub extern "C" fn das_context_new() -> *mut DASContext {
/// a pointer that was not created by `das_context_new`.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
pub extern "C" fn das_context_free(ctx: *mut DASContext) {
pub extern "C" fn eth_kzg_das_context_free(ctx: *mut DASContext) {
if ctx.is_null() {
return;
}
Expand Down Expand Up @@ -120,7 +129,7 @@ impl CResult {
/// - The caller must ensure that the pointer is valid. If the pointer is null, this method will return early.
/// - The caller should also avoid a double-free by setting the pointer to null after calling this method.
#[no_mangle]
pub unsafe extern "C" fn free_error_message(c_message: *mut std::os::raw::c_char) {
pub unsafe extern "C" fn eth_kzg_free_error_message(c_message: *mut std::os::raw::c_char) {
// check if the pointer is null
if c_message.is_null() {
return;
Expand All @@ -145,7 +154,7 @@ pub unsafe extern "C" fn free_error_message(c_message: *mut std::os::raw::c_char
/// If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
#[no_mangle]
#[must_use]
pub extern "C" fn blob_to_kzg_commitment(
pub extern "C" fn eth_kzg_blob_to_kzg_commitment(
ctx: *const DASContext,

blob: *const u8,
Expand Down Expand Up @@ -175,7 +184,7 @@ pub extern "C" fn blob_to_kzg_commitment(
/// If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
#[no_mangle]
#[must_use]
pub extern "C" fn compute_cells_and_kzg_proofs(
pub extern "C" fn eth_kzg_compute_cells_and_kzg_proofs(
ctx: *const DASContext,

blob: *const u8,
Expand Down Expand Up @@ -230,7 +239,7 @@ fn verification_result_to_bool_cresult(
/// If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
#[no_mangle]
#[must_use]
pub extern "C" fn verify_cell_kzg_proof_batch(
pub extern "C" fn eth_kzg_verify_cell_kzg_proof_batch(
ctx: *const DASContext,

commitments_length: u64,
Expand Down Expand Up @@ -288,7 +297,7 @@ pub extern "C" fn verify_cell_kzg_proof_batch(
/// If the other arguments are null, this method will dereference a null pointer and result in undefined behavior.
#[no_mangle]
#[must_use]
pub extern "C" fn recover_cells_and_proofs(
pub extern "C" fn eth_kzg_recover_cells_and_proofs(
ctx: *const DASContext,

cells_length: u64,
Expand Down Expand Up @@ -317,14 +326,14 @@ pub extern "C" fn recover_cells_and_proofs(
// Expose the constants to the C API so that languages that have to define them
// manually can use them in tests.
#[no_mangle]
pub extern "C" fn constant_bytes_per_cell() -> u64 {
pub extern "C" fn eth_kzg_constant_bytes_per_cell() -> u64 {
BYTES_PER_CELL as u64
}
#[no_mangle]
pub extern "C" fn constant_bytes_per_proof() -> u64 {
pub extern "C" fn eth_kzg_constant_bytes_per_proof() -> u64 {
BYTES_PER_COMMITMENT as u64
}
#[no_mangle]
pub extern "C" fn constant_cells_per_ext_blob() -> u64 {
pub extern "C" fn eth_kzg_constant_cells_per_ext_blob() -> u64 {
CELLS_PER_EXT_BLOB as u64
}
14 changes: 7 additions & 7 deletions bindings/csharp/csharp_code/EthKZG.bindings/ethkzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public sealed unsafe class EthKZG : IDisposable

public EthKZG()
{
_context = das_context_new();
_context = eth_kzg_das_context_new();
}

public void Dispose()
{
if (_context != null)
{
das_context_free(_context);
eth_kzg_das_context_free(_context);
_context = null;
}
}
Expand All @@ -56,7 +56,7 @@ public unsafe byte[] BlobToKzgCommitment(byte[] blob)
fixed (byte* blobPtr = blob)
fixed (byte* commitmentPtr = commitment)
{
CResult result = blob_to_kzg_commitment(_context, blobPtr, commitmentPtr);
CResult result = eth_kzg_blob_to_kzg_commitment(_context, blobPtr, commitmentPtr);

ThrowOnError(result);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public unsafe (byte[][], byte[][]) ComputeCellsAndKZGProofs(byte[] blob)
}
}

CResult result = compute_cells_and_kzg_proofs(_context, blobPtr, outCellsPtrPtr, outProofsPtrPtr);
CResult result = eth_kzg_compute_cells_and_kzg_proofs(_context, blobPtr, outCellsPtrPtr, outProofsPtrPtr);
ThrowOnError(result);
}
return (outCells, outProofs);
Expand Down Expand Up @@ -182,7 +182,7 @@ public bool VerifyCellKZGProofBatch(byte[][] commitments, ulong[] cellIndices, b
}
}

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);
CResult result = eth_kzg_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 Expand Up @@ -244,7 +244,7 @@ public bool VerifyCellKZGProofBatch(byte[][] commitments, ulong[] cellIndices, b
}
}

CResult result = recover_cells_and_proofs(_context, Convert.ToUInt64(numInputCells), inputCellsPtrPtr, Convert.ToUInt64(cellIds.Length), cellIdsPtr, outCellsPtrPtr, outProofsPtrPtr);
CResult result = eth_kzg_recover_cells_and_proofs(_context, Convert.ToUInt64(numInputCells), inputCellsPtrPtr, Convert.ToUInt64(cellIds.Length), cellIdsPtr, outCellsPtrPtr, outProofsPtrPtr);
ThrowOnError(result);
}

Expand All @@ -261,7 +261,7 @@ private static void ThrowOnError(CResult result)
if (errorMessage != null)
{
// Free the error message that we allocated on the rust side
free_error_message(result.error_msg);
eth_kzg_free_error_message(result.error_msg);
throw new ArgumentException($"an error occurred from the bindings: {errorMessage}");
}
else
Expand Down
40 changes: 20 additions & 20 deletions bindings/csharp/csharp_code/EthKZG.bindings/native_methods.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal static unsafe partial class NativeMethods
/// To avoid memory leaks, one should ensure that the pointer is freed after use
/// by calling `das_context_free`.
/// </summary>
[DllImport(__DllName, EntryPoint = "das_context_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern DASContext* das_context_new();
[DllImport(__DllName, EntryPoint = "eth_kzg_das_context_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern DASContext* eth_kzg_das_context_new();

/// <summary>
/// # Safety
Expand All @@ -42,8 +42,8 @@ internal static unsafe partial class NativeMethods
/// - Since the `ctx` is created in Rust, we can only get undefined behavior, if the caller passes in
/// a pointer that was not created by `das_context_new`.
/// </summary>
[DllImport(__DllName, EntryPoint = "das_context_free", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void das_context_free(DASContext* ctx);
[DllImport(__DllName, EntryPoint = "eth_kzg_das_context_free", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void eth_kzg_das_context_free(DASContext* ctx);

/// <summary>
/// Free the memory allocated for the error message.
Expand All @@ -53,8 +53,8 @@ internal static unsafe partial class NativeMethods
/// - The caller must ensure that the pointer is valid. If the pointer is null, this method will return early.
/// - The caller should also avoid a double-free by setting the pointer to null after calling this method.
/// </summary>
[DllImport(__DllName, EntryPoint = "free_error_message", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void free_error_message(byte* c_message);
[DllImport(__DllName, EntryPoint = "eth_kzg_free_error_message", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern void eth_kzg_free_error_message(byte* c_message);

/// <summary>
/// Compute a commitment from a Blob
Expand All @@ -70,8 +70,8 @@ internal static unsafe partial class NativeMethods
/// - 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.
/// </summary>
[DllImport(__DllName, EntryPoint = "blob_to_kzg_commitment", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult blob_to_kzg_commitment(DASContext* ctx, byte* blob, byte* @out);
[DllImport(__DllName, EntryPoint = "eth_kzg_blob_to_kzg_commitment", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult eth_kzg_blob_to_kzg_commitment(DASContext* ctx, byte* blob, byte* @out);

/// <summary>
/// Computes the cells and KZG proofs for a given blob.
Expand All @@ -90,8 +90,8 @@ internal static unsafe partial class NativeMethods
/// - 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.
/// </summary>
[DllImport(__DllName, EntryPoint = "compute_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult compute_cells_and_kzg_proofs(DASContext* ctx, byte* blob, byte** out_cells, byte** out_proofs);
[DllImport(__DllName, EntryPoint = "eth_kzg_compute_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult eth_kzg_compute_cells_and_kzg_proofs(DASContext* ctx, byte* blob, byte** out_cells, byte** out_proofs);

/// <summary>
/// Verifies a batch of cells and their KZG proofs.
Expand Down Expand Up @@ -120,8 +120,8 @@ internal static unsafe partial class NativeMethods
/// - 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.
/// </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 commitments_length, byte** commitments, ulong cell_indices_length, ulong* cell_indices, ulong cells_length, byte** cells, ulong proofs_length, byte** proofs, bool* verified);
[DllImport(__DllName, EntryPoint = "eth_kzg_verify_cell_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult eth_kzg_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 All @@ -147,17 +147,17 @@ internal static unsafe partial class NativeMethods
/// - 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.
/// </summary>
[DllImport(__DllName, EntryPoint = "recover_cells_and_proofs", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult recover_cells_and_proofs(DASContext* ctx, ulong cells_length, byte** cells, ulong cell_indices_length, ulong* cell_indices, byte** out_cells, byte** out_proofs);
[DllImport(__DllName, EntryPoint = "eth_kzg_recover_cells_and_proofs", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern CResult eth_kzg_recover_cells_and_proofs(DASContext* ctx, ulong cells_length, byte** cells, ulong cell_indices_length, ulong* cell_indices, byte** out_cells, byte** out_proofs);

[DllImport(__DllName, EntryPoint = "constant_bytes_per_cell", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong constant_bytes_per_cell();
[DllImport(__DllName, EntryPoint = "eth_kzg_constant_bytes_per_cell", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong eth_kzg_constant_bytes_per_cell();

[DllImport(__DllName, EntryPoint = "constant_bytes_per_proof", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong constant_bytes_per_proof();
[DllImport(__DllName, EntryPoint = "eth_kzg_constant_bytes_per_proof", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong eth_kzg_constant_bytes_per_proof();

[DllImport(__DllName, EntryPoint = "constant_cells_per_ext_blob", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong constant_cells_per_ext_blob();
[DllImport(__DllName, EntryPoint = "eth_kzg_constant_cells_per_ext_blob", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern ulong eth_kzg_constant_cells_per_ext_blob();


}
Expand Down
6 changes: 3 additions & 3 deletions bindings/golang/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ type DASContext struct {
}

func NewProverContext() *DASContext {
self := &DASContext{_inner: C.das_context_new()}
self := &DASContext{_inner: C.eth_kzg_das_context_new()}

runtime.SetFinalizer(self, func(self *DASContext) {
C.das_context_free(self.inner())
C.eth_kzg_das_context_free(self.inner())
})

return self
Expand All @@ -60,7 +60,7 @@ func (prover *DASContext) BlobToKZGCommitment(blob []byte) ([]byte, error) {
return nil, errors.New("invalid blob size")
}
out := make([]byte, 48)
C.blob_to_kzg_commitment(prover.inner(), (*C.uint8_t)(&blob[0]), (*C.uint8_t)(&out[0]))
C.eth_kzg_blob_to_kzg_commitment(prover.inner(), (*C.uint8_t)(&blob[0]), (*C.uint8_t)(&out[0]))
return out, nil
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub extern "system" fn Java_ethereum_cryptography_LibEthKZG_DASContextNew(
_class: JClass,
) -> jlong {
// TODO: Switch to using the Rust DASContext object
c_eth_kzg::das_context_new() as jlong
c_eth_kzg::eth_kzg_das_context_new() as jlong
}

#[no_mangle]
Expand All @@ -23,7 +23,7 @@ pub extern "system" fn Java_ethereum_cryptography_LibEthKZG_DASContextDestroy(
ctx_ptr: jlong,
) {
// TODO: Switch to using the Rust DASContext object
c_eth_kzg::das_context_free(ctx_ptr as *mut DASContext);
c_eth_kzg::eth_kzg_das_context_free(ctx_ptr as *mut DASContext);
}

#[no_mangle]
Expand Down
Loading

0 comments on commit e175ccd

Please sign in to comment.