Skip to content

Commit

Permalink
Replace size_t with uint64_t (#478)
Browse files Browse the repository at this point in the history
* Replace size_t with uint64_t in API methods

* Replace ulong with UInt64 in C# bindings

* Do the same thing for C# tests
  • Loading branch information
jtraglia committed Aug 16, 2024
1 parent e3ef368 commit 344188f
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 95 deletions.
8 changes: 4 additions & 4 deletions bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static IntPtr LoadNativeLibrary(Assembly _, string path)
}

[DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")]
private static extern IntPtr InternalLoadTrustedSetup(string filename, ulong precompute);
private static extern IntPtr InternalLoadTrustedSetup(string filename, UInt64 precompute);

[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
private static extern void InternalFreeTrustedSetup(IntPtr ts);
Expand All @@ -58,18 +58,18 @@ private static extern unsafe KzgResult VerifyBlobKzgProof(out bool result, byte*

[DllImport("ckzg", EntryPoint = "verify_blob_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult VerifyBlobKzgProofBatch(out bool result, byte* blobs, byte* commitments,
byte* proofs, ulong count, IntPtr ts);
byte* proofs, UInt64 count, IntPtr ts);

[DllImport("ckzg", EntryPoint = "compute_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult ComputeCellsAndKzgProofs(byte* cells, byte* proofs, byte* blob, IntPtr ts);

[DllImport("ckzg", EntryPoint = "recover_cells_and_kzg_proofs", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult RecoverCellsAndKzgProofs(byte* recovered_cells, byte* recovered_proofs,
ulong* cell_indices, byte* cells, ulong num_cells, IntPtr ts);
UInt64* cell_indices, byte* cells, UInt64 num_cells, IntPtr ts);

[DllImport("ckzg", EntryPoint = "verify_cell_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult VerifyCellKzgProofBatch(out bool result, byte* commitments,
ulong* cell_indices, byte* cells, byte* proofs, ulong num_cells, IntPtr ts);
UInt64* cell_indices, byte* cells, byte* proofs, UInt64 num_cells, IntPtr ts);

private enum KzgResult
{
Expand Down
2 changes: 2 additions & 0 deletions bindings/csharp/Ckzg.Bindings/Ckzg.Bindings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<RepositoryUrl>https://github.com/ethereum/c-kzg-4844</RepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>0.1.0.3</Version>
<!-- Disable the warnings about using UInt64-->
<NoWarn>$(NoWarn);IDE0049</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions bindings/csharp/Ckzg.Bindings/Ckzg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IntPtr LoadTrustedSetup(string filepath, int precompute)
{
if (!File.Exists(filepath)) throw new ArgumentException("Trusted setup file does not exist", nameof(filepath));

IntPtr ckzgSetup = InternalLoadTrustedSetup(filepath, (ulong)precompute);
IntPtr ckzgSetup = InternalLoadTrustedSetup(filepath, (UInt64)precompute);

if (ckzgSetup == IntPtr.Zero) throw new InvalidOperationException("Unable to load trusted setup");
return ckzgSetup;
Expand Down Expand Up @@ -194,7 +194,7 @@ public static unsafe bool VerifyBlobKzgProofBatch(ReadOnlySpan<byte> blobs, Read
fixed (byte* blobsPtr = blobs, commitmentsPtr = commitments, proofsPtr = proofs)
{
KzgResult kzgResult =
VerifyBlobKzgProofBatch(out var result, blobsPtr, commitmentsPtr, proofsPtr, (ulong)count, ckzgSetup);
VerifyBlobKzgProofBatch(out var result, blobsPtr, commitmentsPtr, proofsPtr, (UInt64)count, ckzgSetup);
ThrowOnError(kzgResult);
return result;
}
Expand Down Expand Up @@ -230,15 +230,15 @@ public static unsafe void ComputeCellsAndKzgProofs(Span<byte> cells, Span<byte>
/// </summary>
/// <param name="recoveredCells">Recovered cells as a flattened byte array</param>
/// <param name="recoveredProofs">Recovered proofs as a flattened byte array</param>
/// <param name="cellIndices">Cell indices as a flattened ulong array</param>
/// <param name="cellIndices">Cell indices as a flattened UInt64 array</param>
/// <param name="cells">Cells as a flattened byte array</param>
/// <param name="numCells">The number of cells provided</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
public static unsafe void RecoverCellsAndKzgProofs(Span<byte> recoveredCells, Span<byte> recoveredProofs,
ReadOnlySpan<ulong> cellIndices, ReadOnlySpan<byte> cells, int numCells, IntPtr ckzgSetup)
ReadOnlySpan<UInt64> cellIndices, ReadOnlySpan<byte> cells, int numCells, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(recoveredCells, nameof(recoveredCells), BytesPerCell * CellsPerExtBlob);
Expand All @@ -248,10 +248,10 @@ public static unsafe void RecoverCellsAndKzgProofs(Span<byte> recoveredCells, Sp

fixed (byte* recoveredCellsPtr = recoveredCells, recoveredProofsPtr = recoveredProofs, cellsPtr = cells)
{
fixed(ulong* cellIndicesPtr = cellIndices)
fixed(UInt64* cellIndicesPtr = cellIndices)
{
KzgResult result = RecoverCellsAndKzgProofs(recoveredCellsPtr, recoveredProofsPtr, cellIndicesPtr,
cellsPtr, (ulong)numCells, ckzgSetup);
cellsPtr, (UInt64)numCells, ckzgSetup);
ThrowOnError(result);
}
}
Expand All @@ -261,7 +261,7 @@ public static unsafe void RecoverCellsAndKzgProofs(Span<byte> recoveredCells, Sp
/// Given some cells, verify that their proofs are valid.
/// </summary>
/// <param name="commitmentsBytes">The commitments associated with the rows</param>
/// <param name="cellIndices">Cell indices as a flattened ulong array</param>
/// <param name="cellIndices">Cell indices as a flattened UInt64 array</param>
/// <param name="cells">Cells as a flattened byte array</param>
/// <param name="proofsBytes">Proofs as a flattened byte array</param>
/// <param name="numCells">The number of cells provided</param>
Expand All @@ -270,7 +270,7 @@ public static unsafe void RecoverCellsAndKzgProofs(Span<byte> recoveredCells, Sp
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
/// <returns>Verification result</returns>
public static unsafe bool VerifyCellKzgProofBatch(ReadOnlySpan<byte> commitments, ReadOnlySpan<ulong> cellIndices,
public static unsafe bool VerifyCellKzgProofBatch(ReadOnlySpan<byte> commitments, ReadOnlySpan<UInt64> cellIndices,
ReadOnlySpan<byte> cells, ReadOnlySpan<byte> proofs, int numCells, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
Expand All @@ -281,10 +281,10 @@ public static unsafe bool VerifyCellKzgProofBatch(ReadOnlySpan<byte> commitments

fixed (byte* commitmentsPtr = commitments, cellsPtr = cells, proofsPtr = proofs)
{
fixed (ulong* cellIndicesPtr = cellIndices)
fixed (UInt64* cellIndicesPtr = cellIndices)
{
KzgResult kzgResult = VerifyCellKzgProofBatch(out var result, commitmentsPtr,
cellIndicesPtr, cellsPtr, proofsPtr, (ulong)numCells, ckzgSetup);
cellIndicesPtr, cellsPtr, proofsPtr, (UInt64)numCells, ckzgSetup);
ThrowOnError(kzgResult);
return result;
}
Expand Down Expand Up @@ -317,7 +317,7 @@ private static void ThrowOnInvalidLength(ReadOnlySpan<byte> data, string fieldNa
throw new ArgumentException($"Invalid data size, got {data.Length}, expected {expectedLength}", fieldName);
}

private static void ThrowOnInvalidLength(ReadOnlySpan<ulong> data, string fieldName, int expectedLength)
private static void ThrowOnInvalidLength(ReadOnlySpan<UInt64> data, string fieldName, int expectedLength)
{
if (data.Length != expectedLength)
throw new ArgumentException($"Invalid data size, got {data.Length}, expected {expectedLength}", fieldName);
Expand Down
2 changes: 2 additions & 0 deletions bindings/csharp/Ckzg.Test/Ckzg.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<!-- Disable the warnings about using UInt64-->
<NoWarn>$(NoWarn);IDE0049</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions bindings/csharp/Ckzg.Test/ReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void TestComputeCellsAndKzgProofs(ComputeCellsAndKzgProofsTest test)

public class RecoverCellsAndKzgProofsInput
{
public List<ulong> CellIndices { get; set; } = null!;
public List<UInt64> CellIndices { get; set; } = null!;
public List<string> Cells { get; set; } = null!;
}

Expand Down Expand Up @@ -451,7 +451,7 @@ public void TestRecoverCellsAndKzgProofs(RecoverCellsAndKzgProofsTest test)
{
byte[] recoveredCells = new byte[CellsPerExtBlob * Ckzg.BytesPerCell];
byte[] recoveredProofs = new byte[CellsPerExtBlob * Ckzg.BytesPerProof];
ulong[] cellIndices = test.Input.CellIndices.ToArray();
UInt64[] cellIndices = test.Input.CellIndices.ToArray();
byte[] cells = GetFlatBytes(test.Input.Cells);
int numCells = cells.Length / Ckzg.BytesPerCell;

Expand All @@ -477,7 +477,7 @@ public void TestRecoverCellsAndKzgProofs(RecoverCellsAndKzgProofsTest test)
public class VerifyCellKzgProofBatchInput
{
public List<string> Commitments { get; set; } = null!;
public List<ulong> CellIndices { get; set; } = null!;
public List<UInt64> CellIndices { get; set; } = null!;
public List<string> Cells { get; set; } = null!;
public List<string> Proofs { get; set; } = null!;
}
Expand Down Expand Up @@ -506,7 +506,7 @@ private static IEnumerable<VerifyCellKzgProofBatchTest> GetVerifyCellKzgProofBat
public void TestVerifyCellKzgProofBatch(VerifyCellKzgProofBatchTest test)
{
byte[] commitments = GetFlatBytes(test.Input.Commitments);
ulong[] cellIndices = test.Input.CellIndices.ToArray();
UInt64[] cellIndices = test.Input.CellIndices.ToArray();
byte[] cells = GetFlatBytes(test.Input.Cells);
byte[] proofs = GetFlatBytes(test.Input.Proofs);
int numCells = cells.Length / Ckzg.BytesPerCell;
Expand Down
30 changes: 15 additions & 15 deletions bindings/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ LoadTrustedSetup is the binding for:
C_KZG_RET load_trusted_setup(
KZGSettings *out,
const uint8_t *g1_monomial_bytes,
size_t num_g1_monomial_bytes,
uint64_t num_g1_monomial_bytes,
const uint8_t *g1_lagrange_bytes,
size_t num_g1_lagrange_bytes,
uint64_t num_g1_lagrange_bytes,
const uint8_t *g2_monomial_bytes,
size_t num_g2_monomial_bytes,
size_t precompute);
uint64_t num_g2_monomial_bytes,
uint64_t precompute);
*/
func LoadTrustedSetup(g1MonomialBytes, g1LagrangeBytes, g2MonomialBytes []byte, precompute uint) error {
if loaded {
Expand All @@ -159,12 +159,12 @@ func LoadTrustedSetup(g1MonomialBytes, g1LagrangeBytes, g2MonomialBytes []byte,
ret := C.load_trusted_setup(
&settings,
*(**C.uint8_t)(unsafe.Pointer(&g1MonomialBytes)),
(C.size_t)(len(g1MonomialBytes)),
(C.uint64_t)(len(g1MonomialBytes)),
*(**C.uint8_t)(unsafe.Pointer(&g1LagrangeBytes)),
(C.size_t)(len(g1LagrangeBytes)),
(C.uint64_t)(len(g1LagrangeBytes)),
*(**C.uint8_t)(unsafe.Pointer(&g2MonomialBytes)),
(C.size_t)(len(g2MonomialBytes)),
(C.size_t)(precompute))
(C.uint64_t)(len(g2MonomialBytes)),
(C.uint64_t)(precompute))
if ret == C.C_KZG_OK {
loaded = true
return nil
Expand All @@ -178,7 +178,7 @@ LoadTrustedSetupFile is the binding for:
C_KZG_RET load_trusted_setup_file(
KZGSettings *out,
FILE *in,
size_t precompute);
uint64_t precompute);
*/
func LoadTrustedSetupFile(trustedSetupFile string, precompute uint) error {
if loaded {
Expand All @@ -192,7 +192,7 @@ func LoadTrustedSetupFile(trustedSetupFile string, precompute uint) error {
if fp == nil {
panic("error reading trusted setup")
}
ret := C.load_trusted_setup_file(&settings, fp, (C.size_t)(precompute))
ret := C.load_trusted_setup_file(&settings, fp, (C.uint64_t)(precompute))
C.fclose(fp)
if ret == C.C_KZG_OK {
loaded = true
Expand Down Expand Up @@ -390,7 +390,7 @@ func VerifyBlobKZGProofBatch(blobs []Blob, commitmentsBytes, proofsBytes []Bytes
*(**C.Blob)(unsafe.Pointer(&blobs)),
*(**C.Bytes48)(unsafe.Pointer(&commitmentsBytes)),
*(**C.Bytes48)(unsafe.Pointer(&proofsBytes)),
(C.size_t)(len(blobs)),
(C.uint64_t)(len(blobs)),
&settings)

if ret != C.C_KZG_OK {
Expand Down Expand Up @@ -435,7 +435,7 @@ RecoverCellsAndKZGProofs is the binding for:
KZGProof *recovered_proofs,
const uint64_t *cell_indices,
const Cell *cells,
size_t num_cells,
uint64_t num_cells,
const KZGSettings *s);
*/
func RecoverCellsAndKZGProofs(cellIndices []uint64, cells []Cell) ([CellsPerExtBlob]Cell, [CellsPerExtBlob]KZGProof, error) {
Expand All @@ -453,7 +453,7 @@ func RecoverCellsAndKZGProofs(cellIndices []uint64, cells []Cell) ([CellsPerExtB
(*C.KZGProof)(unsafe.Pointer(&recoveredProofs)),
*(**C.uint64_t)(unsafe.Pointer(&cellIndices)),
*(**C.Cell)(unsafe.Pointer(&cells)),
(C.size_t)(len(cells)),
(C.uint64_t)(len(cells)),
&settings)

if ret != C.C_KZG_OK {
Expand All @@ -471,7 +471,7 @@ VerifyCellKZGProofBatch is the binding for:
const uint64_t *cell_indices,
const Cell *cells,
const Bytes48 *proofs_bytes,
size_t num_cells,
uint64_t num_cells,
const KZGSettings *s);
*/
func VerifyCellKZGProofBatch(commitmentsBytes []Bytes48, cellIndices []uint64, cells []Cell, proofsBytes []Bytes48) (bool, error) {
Expand All @@ -489,7 +489,7 @@ func VerifyCellKZGProofBatch(commitmentsBytes []Bytes48, cellIndices []uint64, c
*(**C.uint64_t)(unsafe.Pointer(&cellIndices)),
*(**C.Cell)(unsafe.Pointer(&cells)),
*(**C.Bytes48)(unsafe.Pointer(&proofsBytes)),
(C.size_t)(len(cells)),
(C.uint64_t)(len(cells)),
&settings)

if ret != C.C_KZG_OK {
Expand Down
16 changes: 8 additions & 8 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ proc loadTrustedSetup*(input: File, precompute: Natural): Result[void, string] {
if gCtx.initialized:
return err(TrustedSetupAlreadyLoadedErr)
gCtx.settings = cast[ptr KzgSettings](alloc0(sizeof(KzgSettings)))
let res = load_trusted_setup_file(gCtx.settings, input, precompute.csize_t)
let res = load_trusted_setup_file(gCtx.settings, input, precompute.uint64)
if res != KZG_OK:
dealloc(gCtx.settings)
gCtx.settings = nil
Expand Down Expand Up @@ -100,12 +100,12 @@ proc loadTrustedSetup*(g1MonomialBytes: openArray[byte],
gCtx.settings = cast[ptr KzgSettings](alloc0(sizeof(KzgSettings)))
let res = load_trusted_setup(gCtx.settings,
g1MonomialBytes[0].getPtr,
g1MonomialBytes.len.csize_t,
g1MonomialBytes.len.uint64,
g1LagrangeBytes[0].getPtr,
g1LagrangeBytes.len.csize_t,
g1LagrangeBytes.len.uint64,
g2MonomialBytes[0].getPtr,
g2MonomialBytes.len.csize_t,
precompute.csize_t)
g2MonomialBytes.len.uint64,
precompute.uint64)
if res != KZG_OK:
dealloc(gCtx.settings)
gCtx.settings = nil
Expand Down Expand Up @@ -248,7 +248,7 @@ proc verifyBlobKzgProofBatch*(blobs: openArray[KzgBlob],
blobs[0].getPtr,
commitments[0].getPtr,
proofs[0].getPtr,
blobs.len.csize_t,
blobs.len.uint64,
gCtx.settings)
verify(res, valid)

Expand Down Expand Up @@ -282,7 +282,7 @@ proc recoverCellsAndKzgProofs*(cellIndices: openArray[uint64],
recoveredProofsPtr,
cellIndices[0].getPtr,
cells[0].getPtr,
cells.len.csize_t,
cells.len.uint64,
gCtx.settings)
verify(res, ret)

Expand All @@ -308,7 +308,7 @@ proc verifyCellKzgProofBatch*(commitments: openArray[KzgBytes48],
cellIndices[0].getPtr,
cells[0].getPtr,
proofs[0].getPtr,
cells.len.csize_t,
cells.len.uint64,
gCtx.settings)
verify(res, valid)

Expand Down
18 changes: 9 additions & 9 deletions bindings/nim/kzg_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ type

proc load_trusted_setup*(res: ptr KzgSettings,
g1MonomialBytes: ptr byte,
numG1MonomialBytes: csize_t,
numG1MonomialBytes: uint64,
g1LagrangeBytes: ptr byte,
numG1LagrangeBytes: csize_t,
numG1LagrangeBytes: uint64,
g2MonomialBytes: ptr byte,
numG2MonomialBytes: csize_t,
precompute: csize_t): KZG_RET {.kzg_abi.}
numG2MonomialBytes: uint64,
precompute: uint64): KZG_RET {.kzg_abi.}

proc load_trusted_setup_file*(res: ptr KzgSettings,
input: File,
precompute: csize_t): KZG_RET {.kzg_abi.}
precompute: uint64): KZG_RET {.kzg_abi.}

proc free_trusted_setup*(s: ptr KzgSettings) {.kzg_abi.}

Expand Down Expand Up @@ -136,7 +136,7 @@ proc verify_blob_kzg_proof_batch*(res: var bool,
blobs: ptr KzgBlob,
commitmentsBytes: ptr KzgBytes48,
proofBytes: ptr KzgBytes48,
n: csize_t,
n: uint64,
s: ptr KzgSettings): KZG_RET {.kzg_abi.}

proc compute_cells_and_kzg_proofs*(cellsOut: ptr KzgCell,
Expand All @@ -148,13 +148,13 @@ proc recover_cells_and_kzg_proofs*(recoveredOut: ptr KzgCell,
recoveredProofsOut: ptr KzgProof,
cellIndices: ptr uint64,
cells: ptr KzgCell,
numCells: csize_t,
numCells: uint64,
s: ptr KzgSettings): KZG_RET {.kzg_abi.}

proc verify_cell_kzg_proof_batch*(res: var bool,
commitments: ptr KzgBytes48,
cellIndices: ptr uint64,
cells: ptr KzgCell,
proofs: ptr KzgBytes48,
numCells: csize_t,
s: ptr KzgSettings): KZG_RET {.kzg_abi.}
numCells: uint64,
s: ptr KzgSettings): KZG_RET {.kzg_abi.}
Loading

0 comments on commit 344188f

Please sign in to comment.