Skip to content

Commit

Permalink
Make some constants public again
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Jul 18, 2024
1 parent 7a99c25 commit 0c96ac1
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 64 deletions.
12 changes: 4 additions & 8 deletions bindings/csharp/Ckzg.Bindings/Ckzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ namespace Ckzg;

public static partial class Ckzg
{

// Private constants.
private const int BytesPerFieldElement = 32;
private const int CellsPerExtBlob = 128;
private const int FieldElementsPerBlob = 4096;
private const int FieldElementsPerCell = 64;

// Public constants.
public const int BytesPerBlob = FieldElementsPerBlob * BytesPerFieldElement;
public const int BytesPerCell = FieldElementsPerCell * BytesPerFieldElement;
public const int BytesPerCommitment = 48;
public const int BytesPerFieldElement = 32;
public const int BytesPerProof = 48;
public const int CellsPerExtBlob = 128;
public const int FieldElementsPerBlob = 4096;
public const int FieldElementsPerCell = 64;

/// <summary>
/// Loads trusted setup settings from file.
Expand Down
37 changes: 17 additions & 20 deletions bindings/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ import (
)

const (
BytesPerBlob = C.BYTES_PER_BLOB
BytesPerCommitment = C.BYTES_PER_COMMITMENT
BytesPerProof = C.BYTES_PER_PROOF
BytesPerCell = C.BYTES_PER_CELL

// Only used in testing, shouldn't be exposed.
bytesPerFieldElement = C.BYTES_PER_FIELD_ELEMENT

// Used to define return types, but clients should use the NUMBER_OF_COLUMNS
// constant from the consensus specs. These will be the same.
cellsPerExtBlob = C.CELLS_PER_EXT_BLOB
BytesPerBlob = C.BYTES_PER_BLOB
BytesPerCell = C.BYTES_PER_CELL
BytesPerCommitment = C.BYTES_PER_COMMITMENT
BytesPerFieldElement = C.BYTES_PER_FIELD_ELEMENT
BytesPerProof = C.BYTES_PER_PROOF
CellsPerExtBlob = C.CELLS_PER_EXT_BLOB
FieldElementsPerBlob = C.FIELD_ELEMENTS_PER_BLOB
FieldElementsPerCell = C.FIELD_ELEMENTS_PER_CELL
)

type (
Expand Down Expand Up @@ -411,21 +408,21 @@ ComputeCellsAndKZGProofs is the binding for:
const Blob *blob,
const KZGSettings *s);
*/
func ComputeCellsAndKZGProofs(blob *Blob) ([cellsPerExtBlob]Cell, [cellsPerExtBlob]KZGProof, error) {
func ComputeCellsAndKZGProofs(blob *Blob) ([CellsPerExtBlob]Cell, [CellsPerExtBlob]KZGProof, error) {
if !loaded {
panic("trusted setup isn't loaded")
}

cells := [cellsPerExtBlob]Cell{}
proofs := [cellsPerExtBlob]KZGProof{}
cells := [CellsPerExtBlob]Cell{}
proofs := [CellsPerExtBlob]KZGProof{}
ret := C.compute_cells_and_kzg_proofs(
(*C.Cell)(unsafe.Pointer(&cells)),
(*C.KZGProof)(unsafe.Pointer(&proofs)),
(*C.Blob)(unsafe.Pointer(blob)),
&settings)

if ret != C.C_KZG_OK {
return [cellsPerExtBlob]Cell{}, [cellsPerExtBlob]KZGProof{}, makeErrorFromRet(ret)
return [CellsPerExtBlob]Cell{}, [CellsPerExtBlob]KZGProof{}, makeErrorFromRet(ret)
}
return cells, proofs, nil
}
Expand All @@ -441,16 +438,16 @@ RecoverCellsAndKZGProofs is the binding for:
size_t num_cells,
const KZGSettings *s);
*/
func RecoverCellsAndKZGProofs(cellIndices []uint64, cells []Cell) ([cellsPerExtBlob]Cell, [cellsPerExtBlob]KZGProof, error) {
func RecoverCellsAndKZGProofs(cellIndices []uint64, cells []Cell) ([CellsPerExtBlob]Cell, [CellsPerExtBlob]KZGProof, error) {
if !loaded {
panic("trusted setup isn't loaded")
}
if len(cellIndices) != len(cells) {
return [cellsPerExtBlob]Cell{}, [cellsPerExtBlob]KZGProof{}, ErrBadArgs
return [CellsPerExtBlob]Cell{}, [CellsPerExtBlob]KZGProof{}, ErrBadArgs
}

recoveredCells := [cellsPerExtBlob]Cell{}
recoveredProofs := [cellsPerExtBlob]KZGProof{}
recoveredCells := [CellsPerExtBlob]Cell{}
recoveredProofs := [CellsPerExtBlob]KZGProof{}
ret := C.recover_cells_and_kzg_proofs(
(*C.Cell)(unsafe.Pointer(&recoveredCells)),
(*C.KZGProof)(unsafe.Pointer(&recoveredProofs)),
Expand All @@ -460,7 +457,7 @@ func RecoverCellsAndKZGProofs(cellIndices []uint64, cells []Cell) ([cellsPerExtB
&settings)

if ret != C.C_KZG_OK {
return [cellsPerExtBlob]Cell{}, [cellsPerExtBlob]KZGProof{}, makeErrorFromRet(ret)
return [CellsPerExtBlob]Cell{}, [CellsPerExtBlob]KZGProof{}, makeErrorFromRet(ret)
}
return recoveredCells, recoveredProofs, nil
}
Expand Down
22 changes: 11 additions & 11 deletions bindings/go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func getRandFieldElement(seed int64) Bytes32 {
}

func fillBlobRandom(blob *Blob, seed int64) {
for i := 0; i < BytesPerBlob; i += bytesPerFieldElement {
for i := 0; i < BytesPerBlob; i += BytesPerFieldElement {
fieldElementBytes := getRandFieldElement(seed + int64(i))
copy(blob[i:i+bytesPerFieldElement], fieldElementBytes[:])
copy(blob[i:i+BytesPerFieldElement], fieldElementBytes[:])
}
}

func getPartialCells(cells [cellsPerExtBlob]Cell, i int) ([]uint64, []Cell) {
func getPartialCells(cells [CellsPerExtBlob]Cell, i int) ([]uint64, []Cell) {
cellIndices := []uint64{}
partialCells := []Cell{}
for j := range cells {
Expand All @@ -61,7 +61,7 @@ func getPartialCells(cells [cellsPerExtBlob]Cell, i int) ([]uint64, []Cell) {
return cellIndices, partialCells
}

func getColumns(blobCommitments []Bytes48, cellRows [][cellsPerExtBlob]Cell, proofRows [][cellsPerExtBlob]Bytes48, numCols int) ([]Bytes48, []uint64, []Cell, []Bytes48) {
func getColumns(blobCommitments []Bytes48, cellRows [][CellsPerExtBlob]Cell, proofRows [][CellsPerExtBlob]Bytes48, numCols int) ([]Bytes48, []uint64, []Cell, []Bytes48) {
var cellCommitments []Bytes48
var cellIndices []uint64
var cells []Cell
Expand Down Expand Up @@ -621,7 +621,7 @@ func TestPartialRecover(t *testing.T) {
require.NoError(t, err)

for i := 1; i <= 5; i++ {
mod := divideRoundUp(cellsPerExtBlob, i)
mod := divideRoundUp(CellsPerExtBlob, i)
cellIndices, partialCells := getPartialCells(cells, mod)
recoveredCells, recoveredProofs, err := RecoverCellsAndKZGProofs(cellIndices, partialCells)
require.NoError(t, err)
Expand All @@ -640,8 +640,8 @@ func Benchmark(b *testing.B) {
commitments := [length]Bytes48{}
proofs := [length]Bytes48{}
fields := [length]Bytes32{}
blobCells := [length][cellsPerExtBlob]Cell{}
blobCellProofs := [length][cellsPerExtBlob]Bytes48{}
blobCells := [length][CellsPerExtBlob]Cell{}
blobCellProofs := [length][CellsPerExtBlob]Bytes48{}

for i := 0; i < length; i++ {
var blob Blob
Expand All @@ -654,10 +654,10 @@ func Benchmark(b *testing.B) {
require.NoError(b, err)
proofs[i] = Bytes48(proof)

tProofs := [cellsPerExtBlob]KZGProof{}
tProofs := [CellsPerExtBlob]KZGProof{}
blobCells[i], tProofs, err = ComputeCellsAndKZGProofs(&blobs[i])
require.NoError(b, err)
blobCellProofs[i] = [cellsPerExtBlob]Bytes48{}
blobCellProofs[i] = [CellsPerExtBlob]Bytes48{}
for j, p := range tProofs {
blobCellProofs[i][j] = Bytes48(p)
}
Expand Down Expand Up @@ -775,7 +775,7 @@ func Benchmark(b *testing.B) {
}

for i := 1; i <= 5; i++ {
mod := divideRoundUp(cellsPerExtBlob, i)
mod := divideRoundUp(CellsPerExtBlob, i)
cellIndices, partialCells := getPartialCells(blobCells[0], mod)
b.Run(fmt.Sprintf("RecoverCellsAndKZGProofs(missing=%v)", i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
Expand Down Expand Up @@ -832,7 +832,7 @@ func Benchmark(b *testing.B) {
})
}

for i := 1; i <= cellsPerExtBlob; i *= 2 {
for i := 1; i <= CellsPerExtBlob; i *= 2 {
cellCommitments, cellIndices, cells, cellProofs := getColumns(commitments[:], blobCells[:], blobCellProofs[:], i)
b.Run(fmt.Sprintf("VerifyColumns(count=%v)", i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
Expand Down
15 changes: 7 additions & 8 deletions bindings/java/src/main/java/ethereum/ckzg4844/CKZG4844JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,23 @@ public static void loadNativeLibrary() {
}

/** Scalar field modulus of BLS12-381. */
protected static final BigInteger BLS_MODULUS =
public static final BigInteger BLS_MODULUS =
new BigInteger(
"52435875175126190479447740508185965837690552500527637822603658699938581184513");
/** The number of bytes in a g1 point. */
protected static final int BYTES_PER_G1 = 48;
/** The number of bytes in a g2 point. */
protected static final int BYTES_PER_G2 = 96;
/** The number of bytes in a BLS scalar field element. */
protected static final int BYTES_PER_FIELD_ELEMENT = 32;
public static final int BYTES_PER_FIELD_ELEMENT = 32;
/** The number of bits in a BLS scalar field element. */
protected static final int BITS_PER_FIELD_ELEMENT = 255;
/** The number of field elements in a blob. */
protected static final int FIELD_ELEMENTS_PER_BLOB = 4096;
public static final int FIELD_ELEMENTS_PER_BLOB = 4096;
/** The number of field elements in an extended blob. */
protected static final int FIELD_ELEMENTS_PER_EXT_BLOB = FIELD_ELEMENTS_PER_BLOB * 2;
/** The number of field elements in a cell. */
protected static final int FIELD_ELEMENTS_PER_CELL = 64;
/** The number of cells in an extended blob. */
protected static final int CELLS_PER_EXT_BLOB =
FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL;

public static final int FIELD_ELEMENTS_PER_CELL = 64;
/** The number of bytes in a KZG commitment. */
public static final int BYTES_PER_COMMITMENT = 48;
/** The number of bytes in a KZG proof. */
Expand All @@ -73,6 +69,9 @@ public static void loadNativeLibrary() {
public static final int BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
/** The number of bytes in a single cell. */
public static final int BYTES_PER_CELL = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_CELL;
/** The number of cells in an extended blob. */
public static final int CELLS_PER_EXT_BLOB =
FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL;

private CKZG4844JNI() {}

Expand Down
5 changes: 0 additions & 5 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ export
results,
kzg_abi

# Private constants
const
FIELD_ELEMENTS_PER_BLOB = 4096
CELLS_PER_EXT_BLOB = 128

type
KzgCtx* = ref object
valFreed: bool
Expand Down
11 changes: 4 additions & 7 deletions bindings/nim/kzg_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ when not defined(kzgExternalBlst):
{.passc: "-I" & escape(bindingsPath) .}
{.passc: "-I" & escape(srcPath) .}

# Private constants
const
FIELD_ELEMENTS_PER_BLOB = 4096
FIELD_ELEMENTS_PER_CELL = 64
BYTES_PER_FIELD_ELEMENT = 32

# Public constants
const
FIELD_ELEMENTS_PER_BLOB* = 4096
FIELD_ELEMENTS_PER_CELL* = 64
BYTES_PER_FIELD_ELEMENT* = 32
BYTES_PER_BLOB* = FIELD_ELEMENTS_PER_BLOB*BYTES_PER_FIELD_ELEMENT
BYTES_PER_CELL* = FIELD_ELEMENTS_PER_CELL*BYTES_PER_FIELD_ELEMENT
CELLS_PER_EXT_BLOB* = 128

type
KZG_RET* = distinct cint
Expand Down
4 changes: 4 additions & 0 deletions bindings/node.js/lib/kzg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ export interface TrustedSetupJson {

export const BYTES_PER_BLOB: number;
export const BYTES_PER_COMMITMENT: number;
export const BYTES_PER_FIELD_ELEMENT: number;
export const BYTES_PER_PROOF: number;
export const BYTES_PER_CELL: number;
export const FIELD_ELEMENTS_PER_BLOB: number;
export const FIELD_ELEMENTS_PER_CELL: number;
export const CELLS_PER_EXT_BLOB: number;

/**
* Initialize the library with a trusted setup file.
Expand Down
10 changes: 10 additions & 0 deletions bindings/node.js/src/kzg.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,18 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["BYTES_PER_COMMITMENT"] = Napi::Number::New(
env, BYTES_PER_COMMITMENT
);
exports["BYTES_PER_FIELD_ELEMENT"] = Napi::Number::New(
env, BYTES_PER_FIELD_ELEMENT
);
exports["BYTES_PER_PROOF"] = Napi::Number::New(env, BYTES_PER_PROOF);
exports["BYTES_PER_CELL"] = Napi::Number::New(env, BYTES_PER_CELL);
exports["FIELD_ELEMENTS_PER_BLOB"] = Napi::Number::New(
env, FIELD_ELEMENTS_PER_BLOB
);
exports["FIELD_ELEMENTS_PER_CELL"] = Napi::Number::New(
env, FIELD_ELEMENTS_PER_CELL
);
exports["CELLS_PER_EXT_BLOB"] = Napi::Number::New(env, CELLS_PER_EXT_BLOB);

return exports;
}
Expand Down
3 changes: 0 additions & 3 deletions bindings/rust/benches/kzg_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use rand::{rngs::ThreadRng, Rng};
use std::path::Path;
use std::sync::Arc;

const BYTES_PER_FIELD_ELEMENT: usize = 32;
const FIELD_ELEMENTS_PER_BLOB: usize = 4096;

fn generate_random_field_element(rng: &mut ThreadRng) -> Bytes32 {
let mut arr = [0u8; BYTES_PER_FIELD_ELEMENT];
rng.fill(&mut arr[..]);
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const BYTES_PER_G1_POINT: usize = 48;
const BYTES_PER_G2_POINT: usize = 96;

/// Number of G1 points required for the kzg trusted setup.
pub const NUM_G1_POINTS: usize = 4096;
const NUM_G1_POINTS: usize = 4096;

/// Number of G2 points required for the kzg trusted setup.
/// 65 is fixed and is used for providing multiproofs up to 64 field elements.
Expand Down
5 changes: 4 additions & 1 deletion bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub use bindings::{
pub use ethereum_kzg_settings::{ethereum_kzg_settings, ethereum_kzg_settings_arc};

// Expose the constants.
pub use bindings::{BYTES_PER_BLOB, BYTES_PER_CELL, BYTES_PER_COMMITMENT, BYTES_PER_PROOF};
pub use bindings::{
BYTES_PER_BLOB, BYTES_PER_CELL, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_PROOF,
CELLS_PER_EXT_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_CELL,
};
// Expose the remaining relevant types.
pub use bindings::{Blob, Bytes32, Bytes48, Cell, Error};

0 comments on commit 0c96ac1

Please sign in to comment.