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

nim-bindings: Fix GCC-14 [-Wincompatible-pointer-types] issue #430

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions bindings/nim/config.nims
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
when fileExists("nimble.paths"):
include "nimble.paths"

import strutils
from os import DirSep

Expand Down
24 changes: 12 additions & 12 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ proc toCommitment*(ctx: KzgCtx,
blob: KzgBlob):
Result[KzgCommitment, string] {.gcsafe.} =
var ret: KzgCommitment
let res = blob_to_kzg_commitment(ret, blob, ctx.val)
let res = blob_to_kzg_commitment(ret, blob.getPtr, ctx.val)
verify(res, ret)

proc computeProof*(ctx: KzgCtx,
Expand All @@ -140,8 +140,8 @@ proc computeProof*(ctx: KzgCtx,
let res = compute_kzg_proof(
ret.proof,
ret.y,
blob,
z,
blob.getPtr,
z.getPtr,
ctx.val)
verify(res, ret)

Expand All @@ -151,8 +151,8 @@ proc computeProof*(ctx: KzgCtx,
var proof: KzgProof
let res = compute_blob_kzg_proof(
proof,
blob,
commitmentBytes,
blob.getPtr,
commitmentBytes.getPtr,
ctx.val)
verify(res, proof)

Expand All @@ -164,10 +164,10 @@ proc verifyProof*(ctx: KzgCtx,
var valid: bool
let res = verify_kzg_proof(
valid,
commitment,
z,
y,
proof,
commitment.getPtr,
z.getPtr,
y.getPtr,
proof.getPtr,
ctx.val)
verify(res, valid)

Expand All @@ -178,9 +178,9 @@ proc verifyProof*(ctx: KzgCtx,
var valid: bool
let res = verify_blob_kzg_proof(
valid,
blob,
commitment,
proof,
blob.getPtr,
commitment.getPtr,
proof.getPtr,
ctx.val)
verify(res, valid)

Expand Down
36 changes: 21 additions & 15 deletions bindings/nim/kzg_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ type
header: "c_kzg_4844.h", byref.} = object

# A basic blob data.
KzgBlob* = array[KzgBlobSize, byte]
KzgBlob* {.importc: "Blob",
header: "c_kzg_4844.h".} = object
bytes*: array[KzgBlobSize, uint8]

# An array of 48 bytes. Represents an untrusted
# (potentially invalid) commitment/proof.
KzgBytes48* = array[48, byte]
KzgBytes48* {.importc: "Bytes48",
header: "c_kzg_4844.h".} = object
bytes*: array[48, uint8]

# An array of 32 bytes. Represents an untrusted
# (potentially invalid) field element.
KzgBytes32* = array[32, byte]
KzgBytes32* {.importc: "Bytes32",
header: "c_kzg_4844.h".} = object
bytes*: array[32, uint8]

# A trusted (valid) KZG commitment.
KzgCommitment* = KzgBytes48
Expand All @@ -87,31 +93,31 @@ proc load_trusted_setup_file*(res: KzgSettings,
proc free_trusted_setup*(s: KzgSettings) {.kzg_abi.}

proc blob_to_kzg_commitment*(res: var KzgCommitment,
blob: KzgBlob,
blob: ptr KzgBlob,
s: KzgSettings): KZG_RET {.kzg_abi.}

proc compute_kzg_proof*(res: var KzgProof,
yOut: var KzgBytes32,
blob: KzgBlob,
zBytes: KzgBytes32,
blob: ptr KzgBlob,
zBytes: ptr KzgBytes32,
s: KzgSettings): KZG_RET {.kzg_abi.}

proc compute_blob_kzg_proof*(res: var KzgProof,
blob: KzgBlob,
commitmentBytes: KzgBytes48,
blob: ptr KzgBlob,
commitmentBytes: ptr KzgBytes48,
s: KzgSettings): KZG_RET {.kzg_abi.}

proc verify_kzg_proof*(res: var bool,
commitmentBytes: KzgBytes48,
zBytes: KzgBytes32,
yBytes: KzgBytes32,
proofBytes: KzgBytes48,
commitmentBytes: ptr KzgBytes48,
zBytes: ptr KzgBytes32,
yBytes: ptr KzgBytes32,
proofBytes: ptr KzgBytes48,
s: KzgSettings): KZG_RET {.kzg_abi.}

proc verify_blob_kzg_proof*(res: var bool,
blob: KzgBlob,
commitmentsBytes: KzgBytes48,
proofBytes: KzgBytes48,
blob: ptr KzgBlob,
commitmentsBytes: ptr KzgBytes48,
proofBytes: ptr KzgBytes48,
s: KzgSettings): KZG_RET {.kzg_abi.}

proc verify_blob_kzg_proof_batch*(res: var bool,
Expand Down
24 changes: 12 additions & 12 deletions bindings/nim/tests/test_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ proc readSetup(filename: string): KzgSettings =
proc createKateBlobs(s: KzgSettings, n: int): KateBlobs =
for i in 0..<n:
var blob: KzgBlob
discard urandom(blob)
for i in 0..<len(blob):
discard urandom(blob.bytes)
for i in 0..<blob.bytes.len:
# don't overflow modulus
if blob[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob[i] = MAX_TOP_BYTE
if blob.bytes[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob.bytes[i] = MAX_TOP_BYTE
result.blobs.add(blob)

for i in 0..<n:
var kate: KzgCommitment
doAssert blob_to_kzg_commitment(kate, result.blobs[i], s) == KZG_OK
doAssert blob_to_kzg_commitment(kate, result.blobs[i].addr, s) == KZG_OK
result.kates.add(kate)

let
Expand All @@ -69,7 +69,7 @@ suite "verify proof (abi)":
var kp: array[nblobs, KzgProof]

for i in 0..<nblobs:
let res = compute_blob_kzg_proof(kp[i], kb.blobs[i], kb.kates[i], kzgs)
let res = compute_blob_kzg_proof(kp[i], kb.blobs[i].addr, kb.kates[i].addr, kzgs)
check res == KZG_OK

var ok: bool
Expand All @@ -87,12 +87,12 @@ suite "verify proof (abi)":
var kp: array[nblobs, KzgProof]

for i in 0..<nblobs:
let res = compute_blob_kzg_proof(kp[i], kb.blobs[i], kb.kates[i], kzgs)
let res = compute_blob_kzg_proof(kp[i], kb.blobs[i].addr, kb.kates[i].addr, kzgs)
check res == KZG_OK

var other = kzgs.createKateBlobs(nblobs)
for i in 0..<nblobs:
let res = compute_blob_kzg_proof(kp[i], other.blobs[i], other.kates[i], kzgs)
let res = compute_blob_kzg_proof(kp[i], other.blobs[i].addr, other.kates[i].addr, kzgs)
check res == KZG_OK

var ok: bool
Expand All @@ -107,24 +107,24 @@ suite "verify proof (abi)":

test "verify blob proof":
var kp: KzgProof
var res = compute_blob_kzg_proof(kp, blob, commitment, kzgs)
var res = compute_blob_kzg_proof(kp, blob.addr, commitment.addr, kzgs)
check res == KZG_OK

var ok: bool
res = verify_blob_kzg_proof(ok, blob, commitment, kp, kzgs)
res = verify_blob_kzg_proof(ok, blob.addr, commitment.addr, kp.addr, kzgs)
check res == KZG_OK
check ok

test "verify proof":
var kp: KzgProof
var ky: KzgBytes32
var res = compute_kzg_proof(kp, ky, blob, inputPoint, kzgs)
var res = compute_kzg_proof(kp, ky, blob.addr, inputPoint.addr, kzgs)
check res == KZG_OK
check kp == proof
check ky == claimedValue

var ok: bool
res = verify_kzg_proof(ok, commitment, inputPoint, claimedValue, kp, kzgs)
res = verify_kzg_proof(ok, commitment.addr, inputPoint.addr, claimedValue.addr, kp.addr, kzgs)
check res == KZG_OK
check ok

Expand Down
8 changes: 4 additions & 4 deletions bindings/nim/tests/test_kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import
proc createKateBlobs(ctx: KzgCtx, n: int): KateBlobs =
var blob: KzgBlob
for i in 0..<n:
discard urandom(blob)
for i in 0..<len(blob):
discard urandom(blob.bytes)
for i in 0..<blob.bytes.len:
# don't overflow modulus
if blob[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob[i] = MAX_TOP_BYTE
if blob.bytes[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob.bytes[i] = MAX_TOP_BYTE
result.blobs.add(blob)

for i in 0..<n:
Expand Down
8 changes: 4 additions & 4 deletions bindings/nim/tests/test_kzg_ex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import
proc createKateBlobs(n: int): KateBlobs =
var blob: KzgBlob
for i in 0..<n:
discard urandom(blob)
for i in 0..<len(blob):
discard urandom(blob.bytes)
for i in 0..<blob.bytes.len:
# don't overflow modulus
if blob[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob[i] = MAX_TOP_BYTE
if blob.bytes[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 0:
blob.bytes[i] = MAX_TOP_BYTE
result.blobs.add(blob)

for i in 0..<n:
Expand Down
4 changes: 2 additions & 2 deletions bindings/nim/tests/test_yaml.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ proc loadYaml(filename: string): YamlNode =
s.close()

proc fromHex(T: type, x: string): T =
if (x.len - 2) div 2 > sizeof(T):
if (x.len - 2) div 2 > sizeof(result.bytes):
raise newException(ValueError, "invalid hex")
hexToByteArray(x, sizeof(T))
result.bytes = hexToByteArray(x, sizeof(result.bytes))

proc fromHex(T: type, x: YamlNode): T =
T.fromHex(x.content)
Expand Down
17 changes: 12 additions & 5 deletions bindings/nim/tests/types.nim

Large diffs are not rendered by default.

Loading