2024-01-12 22:29:43 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-02-22 10:03:09 +00:00
|
|
|
{.push raises: [].}
|
2024-01-12 22:29:43 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/json,
|
|
|
|
yaml,
|
|
|
|
kzg4844/kzg_ex,
|
|
|
|
stew/[byteutils, results],
|
|
|
|
../testutil,
|
|
|
|
./fixtures_utils, ./os_ops
|
|
|
|
|
2024-01-15 06:14:38 +00:00
|
|
|
from std/sequtils import anyIt, mapIt, toSeq
|
2024-01-12 22:29:43 +00:00
|
|
|
from std/strutils import rsplit
|
|
|
|
|
2024-01-15 06:14:38 +00:00
|
|
|
func fromHex[N: static int](s: string): Opt[array[N, byte]] =
|
2024-01-19 10:59:07 +00:00
|
|
|
if s.len != 2*(N+1):
|
|
|
|
# 0x prefix
|
|
|
|
return Opt.none array[N, byte]
|
|
|
|
|
2024-01-12 22:29:43 +00:00
|
|
|
try:
|
2024-01-15 06:14:38 +00:00
|
|
|
Opt.some fromHex(array[N, byte], s)
|
2024-01-12 22:29:43 +00:00
|
|
|
except ValueError:
|
2024-01-15 06:14:38 +00:00
|
|
|
Opt.none array[N, byte]
|
2024-01-12 22:29:43 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
doAssert Kzg.loadTrustedSetup(
|
|
|
|
sourceDir &
|
|
|
|
"/../../vendor/nim-kzg4844/kzg4844/csources/src/trusted_setup.txt").isOk
|
|
|
|
|
2024-01-13 01:31:29 +00:00
|
|
|
proc runBlobToKzgCommitmentTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Blob to KZG commitment - " & relativePathComponent:
|
2024-01-13 01:31:29 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
blob = fromHex[131072](data["input"]["blob"].getStr)
|
2024-01-13 01:31:29 +00:00
|
|
|
|
2024-05-09 05:03:10 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/tests/formats/kzg/blob_to_kzg_commitment.md#condition
|
2024-01-13 01:31:29 +00:00
|
|
|
# If the blob is invalid (e.g. incorrect length or one of the 32-byte
|
|
|
|
# blocks does not represent a BLS field element) it should error, i.e. the
|
|
|
|
# output should be `null`.
|
|
|
|
if blob.isNone:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let commitment = blobToKzgCommitment(blob.get)
|
|
|
|
check:
|
|
|
|
if commitment.isErr:
|
|
|
|
output.kind == JNull
|
|
|
|
else:
|
2024-01-15 06:14:38 +00:00
|
|
|
commitment.get == fromHex[48](output.getStr).get
|
2024-01-13 01:31:29 +00:00
|
|
|
|
2024-01-12 22:29:43 +00:00
|
|
|
proc runVerifyKzgProofTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Verify KZG proof - " & relativePathComponent:
|
2024-01-12 22:29:43 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
commitment = fromHex[48](data["input"]["commitment"].getStr)
|
|
|
|
z = fromHex[32](data["input"]["z"].getStr)
|
|
|
|
y = fromHex[32](data["input"]["y"].getStr)
|
|
|
|
proof = fromHex[48](data["input"]["proof"].getStr)
|
2024-01-12 22:29:43 +00:00
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/tests/formats/kzg/verify_kzg_proof.md#condition
|
2024-01-12 22:29:43 +00:00
|
|
|
# "If the commitment or proof is invalid (e.g. not on the curve or not in
|
|
|
|
# the G1 subgroup of the BLS curve) or `z` or `y` are not a valid BLS
|
|
|
|
# field element, it should error, i.e. the output should be `null`."
|
|
|
|
if commitment.isNone or z.isNone or y.isNone or proof.isNone:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
2024-01-13 01:31:29 +00:00
|
|
|
let v = verifyProof(commitment.get, z.get, y.get, proof.get)
|
|
|
|
check:
|
|
|
|
if v.isErr:
|
|
|
|
output.kind == JNull
|
|
|
|
else:
|
|
|
|
v.get == output.getBool
|
|
|
|
|
|
|
|
proc runVerifyBlobKzgProofTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Verify blob KZG proof - " & relativePathComponent:
|
2024-01-13 01:31:29 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
blob = fromHex[131072](data["input"]["blob"].getStr)
|
|
|
|
commitment = fromHex[48](data["input"]["commitment"].getStr)
|
|
|
|
proof = fromHex[48](data["input"]["proof"].getStr)
|
2024-01-13 01:31:29 +00:00
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/tests/formats/kzg/verify_blob_kzg_proof.md#condition
|
2024-01-13 01:31:29 +00:00
|
|
|
# "If the commitment or proof is invalid (e.g. not on the curve or not in
|
|
|
|
# the G1 subgroup of the BLS curve) or `blob` is invalid (e.g. incorrect
|
|
|
|
# length or one of the 32-byte blocks does not represent a BLS field
|
|
|
|
# element), it should error, i.e. the output should be `null`."
|
|
|
|
if blob.isNone or commitment.isNone or proof.isNone:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let v = verifyBlobKzgProof(blob.get, commitment.get, proof.get)
|
|
|
|
check:
|
|
|
|
if v.isErr:
|
|
|
|
output.kind == JNull
|
|
|
|
else:
|
|
|
|
v.get == output.getBool
|
|
|
|
|
|
|
|
proc runVerifyBlobKzgProofBatchTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Verify blob KZG proof batch - " & relativePathComponent:
|
2024-01-13 01:31:29 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
blobs = data["input"]["blobs"].mapIt(fromHex[131072](it.getStr))
|
|
|
|
commitments = data["input"]["commitments"].mapIt(fromHex[48](it.getStr))
|
|
|
|
proofs = data["input"]["proofs"].mapIt(fromHex[48](it.getStr))
|
2024-01-13 01:31:29 +00:00
|
|
|
|
2024-05-09 05:03:10 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/tests/formats/kzg/verify_blob_kzg_proof_batch.md#condition
|
2024-01-13 01:31:29 +00:00
|
|
|
# "If any of the commitments or proofs are invalid (e.g. not on the curve or
|
|
|
|
# not in the G1 subgroup of the BLS curve) or any blob is invalid (e.g.
|
|
|
|
# incorrect length or one of the 32-byte blocks does not represent a BLS
|
|
|
|
# field element), it should error, i.e. the output should be null."
|
|
|
|
if blobs.anyIt(it.isNone) or commitments.anyIt(it.isNone) or
|
|
|
|
proofs.anyIt(it.isNone):
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let v = verifyBlobKzgProofBatch(
|
|
|
|
blobs.mapIt(it.get), commitments.mapIt(it.get), proofs.mapIt(it.get))
|
2024-01-12 22:29:43 +00:00
|
|
|
check:
|
2024-01-13 01:31:29 +00:00
|
|
|
if v.isErr:
|
2024-01-12 22:29:43 +00:00
|
|
|
output.kind == JNull
|
|
|
|
else:
|
2024-01-13 01:31:29 +00:00
|
|
|
v.get == output.getBool
|
2024-01-12 22:29:43 +00:00
|
|
|
|
|
|
|
proc runComputeKzgProofTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Compute KZG proof - " & relativePathComponent:
|
2024-01-12 22:29:43 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
blob = fromHex[131072](data["input"]["blob"].getStr)
|
|
|
|
z = fromHex[32](data["input"]["z"].getStr)
|
2024-01-12 22:29:43 +00:00
|
|
|
|
2024-05-09 05:03:10 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/tests/formats/kzg/compute_kzg_proof.md#condition
|
2024-01-12 22:29:43 +00:00
|
|
|
# "If the blob is invalid (e.g. incorrect length or one of the 32-byte
|
|
|
|
# blocks does not represent a BLS field element) or z is not a valid BLS
|
|
|
|
# field element, it should error, i.e. the output should be null."
|
|
|
|
if blob.isNone or z.isNone:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let p = computeKzgProof(blob.get, z.get)
|
|
|
|
if p.isErr:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let
|
2024-01-15 06:14:38 +00:00
|
|
|
proof = fromHex[48](output[0].getStr)
|
|
|
|
y = fromHex[32](output[1].getStr)
|
2024-01-12 22:29:43 +00:00
|
|
|
check:
|
|
|
|
p.get.proof == proof.get
|
|
|
|
p.get.y == y.get
|
|
|
|
|
2024-01-13 01:31:29 +00:00
|
|
|
proc runComputeBlobKzgProofTest(suiteName, suitePath, path: string) =
|
2024-02-22 10:03:09 +00:00
|
|
|
let relativePathComponent = path.relativeTestPathComponent(suitePath)
|
|
|
|
test "KZG - Compute blob KZG proof - " & relativePathComponent:
|
2024-01-13 01:31:29 +00:00
|
|
|
let
|
|
|
|
data = yaml.loadToJson(os_ops.readFile(path/"data.yaml"))[0]
|
|
|
|
output = data["output"]
|
2024-01-15 06:14:38 +00:00
|
|
|
blob = fromHex[131072](data["input"]["blob"].getStr)
|
|
|
|
commitment = fromHex[48](data["input"]["commitment"].getStr)
|
2024-01-13 01:31:29 +00:00
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/tests/formats/kzg/compute_blob_kzg_proof.md#condition
|
2024-01-13 01:31:29 +00:00
|
|
|
# If the blob is invalid (e.g. incorrect length or one of the 32-byte
|
|
|
|
# blocks does not represent a BLS field element) it should error, i.e. the
|
|
|
|
# output should be `null`.
|
|
|
|
if blob.isNone or commitment.isNone:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
|
|
|
let p = computeBlobKzgProof(blob.get, commitment.get)
|
|
|
|
if p.isErr:
|
|
|
|
check output.kind == JNull
|
|
|
|
else:
|
2024-01-15 06:14:38 +00:00
|
|
|
check p.get == fromHex[48](output.getStr).get
|
|
|
|
|
|
|
|
from std/algorithm import sorted
|
2024-01-13 01:31:29 +00:00
|
|
|
|
|
|
|
const suiteName = "EF - KZG"
|
|
|
|
|
|
|
|
suite suiteName:
|
2024-01-12 22:29:43 +00:00
|
|
|
const suitePath = SszTestsDir/"general"/"deneb"/"kzg"
|
2024-01-15 06:14:38 +00:00
|
|
|
|
|
|
|
# TODO also check that the only direct subdirectory of each is kzg-mainnet
|
|
|
|
doAssert sorted(mapIt(
|
|
|
|
toSeq(walkDir(suitePath, relative = true, checkDir = true)), it.path)) ==
|
|
|
|
["blob_to_kzg_commitment", "compute_blob_kzg_proof", "compute_kzg_proof",
|
|
|
|
"verify_blob_kzg_proof", "verify_blob_kzg_proof_batch",
|
|
|
|
"verify_kzg_proof"]
|
2024-01-13 01:31:29 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"blob_to_kzg_commitment"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
|
|
|
runBlobToKzgCommitmentTest(suiteName, testsDir, testsDir/path)
|
2024-01-12 22:29:43 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"verify_kzg_proof"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
2024-01-13 01:31:29 +00:00
|
|
|
runVerifyKzgProofTest(suiteName, testsDir, testsDir/path)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"verify_blob_kzg_proof"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
|
|
|
runVerifyBlobKzgProofTest(suiteName, testsDir, testsDir/path)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"verify_blob_kzg_proof_batch"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
|
|
|
runVerifyBlobKzgProofBatchTest(suiteName, testsDir, testsDir/path)
|
2024-01-12 22:29:43 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"compute_kzg_proof"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
2024-01-13 01:31:29 +00:00
|
|
|
runComputeKzgProofTest(suiteName, testsDir, testsDir / path)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let testsDir = suitePath/"compute_blob_kzg_proof"/"kzg-mainnet"
|
|
|
|
for kind, path in walkDir(testsDir, relative = true, checkDir = true):
|
|
|
|
runComputeBlobKzgProofTest(suiteName, testsDir, testsDir / path)
|
2024-01-12 22:29:43 +00:00
|
|
|
|
2024-04-17 03:51:16 +00:00
|
|
|
doAssert Kzg.freeTrustedSetup().isOk
|