Skip to content

Commit

Permalink
stash: Update to Eth mainnet KZG trusted setup from ceremony [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Nov 30, 2023
1 parent aedd7b9 commit e9d5343
Show file tree
Hide file tree
Showing 347 changed files with 5,397 additions and 1,283 deletions.
18 changes: 9 additions & 9 deletions constantine/ethereum_eip4844_kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export loadTrustedSetup_tsif, TrustedSetupStatus, EthereumKZGContext
## - Audited reference implementation
## https://github.com/ethereum/c-kzg-4844

const prefix_ffi = "ctt_eth_kzg4844_"
const prefix_eth_kzg_4844 = "ctt_eth_kzg4844_"
import ./zoo_exports

# Constants
Expand All @@ -58,7 +58,7 @@ const BYTES_PER_FIELD_ELEMENT = 32
# Presets
# ------------------------------------------------------------

const FIELD_ELEMENTS_PER_BLOB {.intdefine.} = 4096
const FIELD_ELEMENTS_PER_BLOB = 4096
const FIAT_SHAMIR_PROTOCOL_DOMAIN = asBytes"FSBLOBVERIFY_V1_"
const RANDOM_CHALLENGE_KZG_BATCH_DOMAIN = asBytes"RCKZGBATCH___V1_"

Expand Down Expand Up @@ -269,7 +269,7 @@ template check(Section: untyped, evalExpr: CttCodecEccStatus): untyped {.dirty.}
func blob_to_kzg_commitment*(
ctx: ptr EthereumKZGContext,
dst: var array[48, byte],
blob: ptr Blob): CttEthKzgStatus {.libPrefix: prefix_ffi.} =
blob: ptr Blob): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844.} =
## Compute a commitment to the `blob`.
## The commitment can be verified without needing the full `blob`
##
Expand Down Expand Up @@ -306,7 +306,7 @@ func compute_kzg_proof*(
proof_bytes: var array[48, byte],
y_bytes: var array[32, byte],
blob: ptr Blob,
z_bytes: array[32, byte]): CttEthKzgStatus {.libPrefix: prefix_ffi, tags:[Alloca, HeapAlloc, Vartime].} =
z_bytes: array[32, byte]): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844, tags:[Alloca, HeapAlloc, Vartime].} =
## Generate:
## - A proof of correct evaluation.
## - y = p(z), the evaluation of p at the challenge z, with p being the Blob interpreted as a polynomial.
Expand Down Expand Up @@ -353,7 +353,7 @@ func verify_kzg_proof*(
commitment_bytes: array[48, byte],
z_bytes: array[32, byte],
y_bytes: array[32, byte],
proof_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_ffi, tags:[Alloca, Vartime].} =
proof_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844, tags:[Alloca, Vartime].} =
## Verify KZG proof that p(z) == y where p(z) is the polynomial represented by "polynomial_kzg"

var commitment {.noInit.}: KZGCommitment
Expand Down Expand Up @@ -381,7 +381,7 @@ func compute_blob_kzg_proof*(
ctx: ptr EthereumKZGContext,
proof_bytes: var array[48, byte],
blob: ptr Blob,
commitment_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_ffi, tags:[Alloca, HeapAlloc, Vartime].} =
commitment_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844, tags:[Alloca, HeapAlloc, Vartime].} =
## Given a blob, return the KZG proof that is used to verify it against the commitment.
## This method does not verify that the commitment is correct with respect to `blob`.

Expand Down Expand Up @@ -420,7 +420,7 @@ func verify_blob_kzg_proof*(
ctx: ptr EthereumKZGContext,
blob: ptr Blob,
commitment_bytes: array[48, byte],
proof_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_ffi, tags:[Alloca, HeapAlloc, Vartime].} =
proof_bytes: array[48, byte]): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844, tags:[Alloca, HeapAlloc, Vartime].} =
## Given a blob and a KZG proof, verify that the blob data corresponds to the provided commitment.

var commitment {.noInit.}: KZGCommitment
Expand Down Expand Up @@ -482,7 +482,7 @@ func verify_blob_kzg_proof_batch*(
commitments_bytes: ptr UncheckedArray[array[48, byte]],
proof_bytes: ptr UncheckedArray[array[48, byte]],
n: int,
secureRandomBytes: array[32, byte]): CttEthKzgStatus {.libPrefix: prefix_ffi, tags:[Alloca, HeapAlloc, Vartime].} =
secureRandomBytes: array[32, byte]): CttEthKzgStatus {.libPrefix: prefix_eth_kzg_4844, tags:[Alloca, HeapAlloc, Vartime].} =
## Verify `n` (blob, commitment, proof) sets efficiently
##
## `n` is the number of verifications set
Expand Down Expand Up @@ -593,7 +593,7 @@ const TrustedSetupMainnet =
"trusted_setups" /
"trusted_setup_ethereum_kzg_test_mainnet.tsif"

proc load_ethereum_kzg_test_trusted_setup_mainnet*(): ptr EthereumKZGContext {.libPrefix: prefix_ffi, raises: [OSError, IOError].} =
proc load_ethereum_kzg_test_trusted_setup_mainnet*(): ptr EthereumKZGContext {.libPrefix: prefix_eth_kzg_4844, raises: [OSError, IOError].} =
## This is a convenience function for the Ethereum mainnet testing trusted setups.
## It is insecure and will be replaced once the KZG ceremony is done.

Expand Down
32 changes: 24 additions & 8 deletions constantine/platforms/fileio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,46 @@ proc open*(f: var File, filepath: cstring, mode = kRead): bool =
# ------------------------------------------------------------

when defined(windows):
func getFilePosition*(f: File): int64 {.importc: "_ftelli64", header: "<stdio.h>".}
func setFilePosition*(f: File, offset: int64, relative = kAbsolute): cint {.importc: "_fseeki64", header: "<stdio.h>".}
proc getFilePosition*(f: File): int64 {.importc: "_ftelli64", header: "<stdio.h>", sideeffect.}
proc setFilePosition*(f: File, offset: int64, relative = kAbsolute): cint {.importc: "_fseeki64", header: "<stdio.h>", sideeffect.}
else:
func getFilePosition*(f: File): int64 {.importc: "ftello", header: "<stdio.h>".}
func setFilePosition*(f: File, offset: int64, relative = kAbsolute): cint {.importc: "fseeko", header: "<stdio.h>".}
proc getFilePosition*(f: File): int64 {.importc: "ftello", header: "<stdio.h>", sideeffect.}
proc setFilePosition*(f: File, offset: int64, relative = kAbsolute): cint {.importc: "fseeko", header: "<stdio.h>", sideeffect.}

# Reading files
# ------------------------------------------------------------

func c_fread(buffer: pointer, len, count: csize_t, f: File): csize_t {.importc: "fread", header: "<stdio.h>".}
proc c_fread(buffer: pointer, len, count: csize_t, f: File): csize_t {.importc: "fread", header: "<stdio.h>", sideeffect.}

func readInto*(f: File, buffer: pointer, len: int): int =
proc readInto*(f: File, buffer: pointer, len: int): int =
## Read data into buffer, return the number of bytes read
cast[int](c_fread(buffer, 1, cast[csize_t](len), f))

func readInto*[T](f: File, buf: var T): bool =
proc readInto*[T](f: File, buf: var T): bool =
## Read data into buffer,
## return true if the number of bytes read
## matches the output type size
return f.readInto(buf.addr, sizeof(buf)) == sizeof(T)

func read*(f: File, T: typedesc): T =
proc read*(f: File, T: typedesc): T =
## Interpret next bytes as type `T`
## Panics if the number of bytes read does not match
## the size of `T`
let ok = f.readInto(result)
doAssert ok, "Fatal error when reading '" & $T & "' from file."

# Parsing files
# ------------------------------------------------------------

proc c_fscanf*(f: File, format: cstring): cint{.importc:"fscanf", header: "<stdio.h>", sideeffect, varargs.}
## Note: The "format" parameter and followup arguments MUST NOT be forgotten
## to not be exposed to the "format string attacks"

# Formatted print
# ------------------------------------------------------------

proc c_printf*(fmt: cstring): cint {.sideeffect, importc: "printf", header: "<stdio.h>", varargs, discardable.}
func c_snprintf*(dst: cstring, maxLen: csize_t, format: cstring): cint {.importc:"snprintf", header: "<stdio.h>", varargs.}
## dst is really a `var` parameter, but Nim var are lowered to pointer hence unsuitable here.
## Note: The "format" parameter and followup arguments MUST NOT be forgotten
## to not be exposed to the "format string attacks"
Loading

0 comments on commit e9d5343

Please sign in to comment.