Skip to content

Commit

Permalink
Merge branch 'master' into kw/thread-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Aug 16, 2024
2 parents 3af1f46 + ee42bf8 commit b08735b
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 21 deletions.
18 changes: 13 additions & 5 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,27 @@ impl Deref for DASContext {

/// Create a new DASContext and return a pointer to it.
///
/// `num_threads`: set to `0`` to indicate that the library should pick a sensible default.
///
/// # Memory faults
///
/// To avoid memory leaks, one should ensure that the pointer is freed after use
/// by calling `eth_kzg_das_context_free`.
#[no_mangle]
pub extern "C" fn eth_kzg_das_context_new() -> *mut DASContext {
pub extern "C" fn eth_kzg_das_context_new(use_precomp: bool, num_threads: u32) -> *mut DASContext {
let use_precomp = if use_precomp {
rust_eth_kzg::UsePrecomp::Yes {
width: RECOMMENDED_PRECOMP_WIDTH,
}
} else {
rust_eth_kzg::UsePrecomp::No
};

let ctx = Box::new(DASContext {
inner: rust_eth_kzg::DASContext::with_threads(
&rust_eth_kzg::TrustedSetup::default(),
ThreadCount::Multi(1),
rust_eth_kzg::UsePrecomp::Yes {
width: RECOMMENDED_PRECOMP_WIDTH,
},
ThreadCount::Multi(num_threads as usize),
use_precomp,
),
});
Box::into_raw(ctx)
Expand Down
4 changes: 2 additions & 2 deletions bindings/csharp/csharp_code/EthKZG.bindings/ethkzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed unsafe class EthKZG : IDisposable

private DASContext* _context;

public EthKZG()
public EthKZG(bool usePrecomp = true, uint numThreads = 1)
{
_context = eth_kzg_das_context_new();
_context = eth_kzg_das_context_new(usePrecomp, numThreads);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ internal static unsafe partial class NativeMethods
/// <summary>
/// Create a new DASContext and return a pointer to it.
///
/// `num_threads`: set to `0`` to indicate that the library should pick a sensible default.
///
/// # Memory faults
///
/// To avoid memory leaks, one should ensure that the pointer is freed after use
/// by calling `eth_kzg_das_context_free`.
/// </summary>
[DllImport(__DllName, EntryPoint = "eth_kzg_das_context_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
internal static extern DASContext* eth_kzg_das_context_new();
internal static extern DASContext* eth_kzg_das_context_new([MarshalAs(UnmanagedType.U1)] bool use_precomp, uint num_threads);

/// <summary>
/// # Safety
Expand Down
2 changes: 1 addition & 1 deletion bindings/golang/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type DASContext struct {
}

func NewProverContext() *DASContext {
self := &DASContext{_inner: C.eth_kzg_das_context_new()}
self := &DASContext{_inner: C.eth_kzg_das_context_new(C._Bool(true), C.uint32_t(1))}

runtime.SetFinalizer(self, func(self *DASContext) {
C.eth_kzg_das_context_free(self.inner())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public class LibEthKZG implements AutoCloseable{

public LibEthKZG() {
ensureLibraryLoaded();
this.contextPtr = DASContextNew();
boolean usePrecomp = true;
long numThreads = 1;
this.contextPtr = DASContextNew(usePrecomp, numThreads);
}

public LibEthKZG(boolean usePrecomp, long numThreads) {
ensureLibraryLoaded();
this.contextPtr = DASContextNew(usePrecomp, numThreads);
}

private static void ensureLibraryLoaded() {
Expand Down Expand Up @@ -87,7 +94,7 @@ public CellsAndProofs recoverCellsAndProofs(long[] cellIDs, byte[][] cellsArr) {
* library
*/

private static native long DASContextNew();
private static native long DASContextNew(boolean usePrecomp, long numThreads);

private static native void DASContextDestroy(long ctx_ptr);

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.

6 changes: 5 additions & 1 deletion bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ use errors::Error;
pub extern "system" fn Java_ethereum_cryptography_LibEthKZG_DASContextNew(
_env: JNIEnv,
_class: JClass,
use_precomp: jboolean,
num_threads: jlong,
) -> jlong {
c_eth_kzg::eth_kzg_das_context_new() as jlong
let use_precomp = use_precomp != 0;
let num_threads = (num_threads as u64) as u32;
c_eth_kzg::eth_kzg_das_context_new(use_precomp, num_threads) as jlong
}

#[no_mangle]
Expand Down
5 changes: 4 additions & 1 deletion bindings/nim/nim_code/nim_eth_kzg/header.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ type CResult* = object

## Create a new DASContext and return a pointer to it.
#
# `num_threads`: set to `0`` to indicate that the library should pick a sensible default.
#
# # Memory faults
#
# To avoid memory leaks, one should ensure that the pointer is freed after use
# by calling `eth_kzg_das_context_free`.
proc eth_kzg_das_context_new*(): ptr DASContext {.importc: "eth_kzg_das_context_new".}
proc eth_kzg_das_context_new*(use_precomp: bool,
num_threads: uint32): ptr DASContext {.importc: "eth_kzg_das_context_new".}

## # Safety
#
Expand Down
4 changes: 2 additions & 2 deletions bindings/nim/nim_code/nim_eth_kzg/nim_eth_kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ proc `=destroy`(x: typeof KZGCtx()[]) =
if x.ctx_ptr != nil:
eth_kzg_das_context_free(x.ctx_ptr)

proc newKZGCtx*(): KZGCtx =
proc newKZGCtx*(use_precomp: bool = true, num_threads: uint32 = 1): KZGCtx =
var kzgCtx = KZGCtx()
kzgCtx.ctx_ptr = eth_kzg_das_context_new()
kzgCtx.ctx_ptr = eth_kzg_das_context_new(use_precomp, num_threads)
return kzgCtx


Expand Down
5 changes: 5 additions & 0 deletions bindings/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ export const BYTES_PER_FIELD_ELEMENT: number
export const BYTES_PER_BLOB: number
export const MAX_NUM_COLUMNS: number
export const BYTES_PER_CELL: number
export interface DasContextOptions {
usePrecomp: boolean
numThreads: number
}
export class CellsAndProofs {
cells: Array<Uint8Array>
proofs: Array<Uint8Array>
}
export type DASContextJs = DasContextJs
export class DasContextJs {
constructor()
static create(options: DasContextOptions): DasContextJs
blobToKzgCommitment(blob: Uint8Array): Uint8Array
asyncBlobToKzgCommitment(blob: Uint8Array): Promise<Uint8Array>
computeCellsAndKzgProofs(blob: Uint8Array): CellsAndProofs
Expand Down
37 changes: 33 additions & 4 deletions bindings/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,46 @@ impl Default for DASContextJs {
}
}

#[napi(object)]
pub struct DASContextOptions {
pub use_precomp: bool,
pub num_threads: u32,
}

impl Default for DASContextOptions {
fn default() -> Self {
Self {
use_precomp: true,
num_threads: 1,
}
}
}

#[napi]
impl DASContextJs {
#[napi(constructor)]
pub fn new() -> Self {
Self::create(DASContextOptions::default())
}

#[napi(factory)]
pub fn create(options: DASContextOptions) -> Self {
let use_precomp = options.use_precomp;
let num_threads = options.num_threads;

let precomp = if use_precomp {
UsePrecomp::Yes {
width: RECOMMENDED_PRECOMP_WIDTH,
}
} else {
UsePrecomp::No
};

DASContextJs {
inner: Arc::new(DASContext::with_threads(
&TrustedSetup::default(),
ThreadCount::Single,
UsePrecomp::Yes {
width: RECOMMENDED_PRECOMP_WIDTH,
},
ThreadCount::Multi(num_threads as usize),
precomp,
)),
}
}
Expand Down

0 comments on commit b08735b

Please sign in to comment.