Skip to content

Commit

Permalink
Add test skeleton of verify KZG proof
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Aug 13, 2023
1 parent 8aa9fa9 commit 60809ff
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
30 changes: 29 additions & 1 deletion constantine/ethereum_kzg_polynomial_commitments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import
serialization/[codecs_bls12_381, endians],
trusted_setups/ethereum_kzg_srs

export loadTrustedSetup
export loadTrustedSetup, TrustedSetupStatus, EthereumKZGContext

## ############################################################
##
Expand Down Expand Up @@ -278,3 +278,31 @@ func verify_kzg_proof*(
return cttEthKZG_Success
else:
return cttEthKZG_VerificationFailure

# Ethereum Trusted Setup
# ------------------------------------------------------------

# Temporary workaround, hardcoding the testing trusted setups

# To be removed, no modules that use heap allocation are used at runtime
import std/[os, strutils]

const TrustedSetupMainnet =
currentSourcePath.rsplit(DirSep, 1)[0] /
"trusted_setups" /
"trusted_setup_ethereum_kzg_test_mainnet.tsif"

proc load_ethereum_kzg_test_trusted_setup_mainnet*(): ptr EthereumKZGContext =
## 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.

let ctx = allocHeapAligned(EthereumKZGContext, alignment = 64)

let tsStatus = ctx.loadTrustedSetup(TrustedSetupMainnet)
doAssert tsStatus == tsSuccess, "\n[Trusted Setup Error] " & $tsStatus

echo "Trusted Setup loaded successfully"
return ctx

proc delete*(ctx: ptr EthereumKZGContext) =
freeHeapAligned(ctx)
66 changes: 66 additions & 0 deletions tests/t_ethereum_deneb_kzg.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Constantine
# Copyright (c) 2018-2019 Status Research & Development GmbH
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import
# Standard library
std/[os, strutils, streams],
# 3rd party
pkg/yaml,
# Internals
../constantine/serialization/codecs,
../constantine/ethereum_kzg_polynomial_commitments

# Organization
#
# We choose not to use a type schema here, unlike with the other json-based tests
# like:
# - t_ethereum_bls_signatures
# - t_ethereum_evem_precompiles
#
# They'll add a lot of verbosity due to all the KZG types
# and failure modes (subgroups, ...)
# https://nimyaml.org/serialization.html

const
TestVectorsDir =
currentSourcePath.rsplit(DirSep, 1)[0] / "protocol_ethereum_deneb_kzg"
VerifyKzgTestDir =
TestVectorsDir / "verify_kzg_proof" / "small"

proc loadVectors(filename: string): YamlNode =
var s = filename.openFileStream()
defer: s.close()
load(s, result)

proc testVerifyKzgProof(ctx: ptr EthereumKZGContext, filename: string) =
let tv = loadVectors(VerifyKzgTestDir / filename / "data.yaml")

let
commitment = array[48, byte].fromHex(tv["input"]["commitment"].content)
z = array[32, byte].fromHex(tv["input"]["z"].content)
y = array[32, byte].fromHex(tv["input"]["y"].content)
proof = array[48, byte].fromHex(tv["input"]["proof"].content)

stdout.write(" " & "verify_kzg_proof" & " test: " & alignLeft(filename, 70))
let status = ctx.verify_kzg_proof(commitment, z, y, proof)
stdout.write "[" & $status & "]\n"

if status == cttEthKZG_Success:
doAssert tv["output"].content == "true"
elif status == cttEthKZG_VerificationFailure:
doAssert tv["output"].content == "false"
else:
doAssert tv["output"].content == "null"

block:
let ctx = load_ethereum_kzg_test_trusted_setup_mainnet()

ctx.testVerifyKzgProof("verify_kzg_proof_case_correct_proof_0b16242de3e9c686")
ctx.testVerifyKzgProof("verify_kzg_proof_case_incorrect_proof_0b16242de3e9c686")

ctx.delete()
2 changes: 1 addition & 1 deletion tests/t_ethereum_evm_precompiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import
# Standard library
std/[times, os, strutils, macros],
std/[os, strutils],
# 3rd party
pkg/jsony,
# Internals
Expand Down

0 comments on commit 60809ff

Please sign in to comment.