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: prevent Nim GC call free_trusted_setup if it already called manually #426

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all 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
12 changes: 9 additions & 3 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export

type
KzgCtx* = ref object
valFreed: bool
val: KzgSettings

KzgProofAndY* = object
Expand All @@ -32,7 +33,12 @@ else:
##############################################################

proc destroy*(x: KzgCtx) =
free_trusted_setup(x.val)
# Prevent Nim GC to call free_trusted_setup
# if user already done it before.
# Otherwise, the program will crash with segfault.
if not x.valFreed:
free_trusted_setup(x.val)
x.valFreed = true

proc newKzgCtx(): KzgCtx =
# Nim finalizer is still broken(v1.6)
Expand Down Expand Up @@ -209,8 +215,8 @@ template loadTrustedSetupFile*(input: File | string): untyped =
loadTrustedSetup(input)

template freeTrustedSetup*(ctx: KzgCtx) =
free_trusted_setup(ctx.val)
destroy(ctx)

template blobToKzgCommitment*(ctx: KzgCtx,
blob: KzgBlob): untyped =
toCommitment(ctx, blob)
Expand Down
Loading