2023-03-08 13:04:30 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-03-15 14:39:19 +00:00
|
|
|
std/[os, sequtils, strutils, streams],
|
2023-03-08 13:04:30 +00:00
|
|
|
unittest2, yaml,
|
|
|
|
../kzg,
|
|
|
|
./types
|
|
|
|
|
2023-03-09 04:04:23 +00:00
|
|
|
# we want to use our own fromHex
|
|
|
|
import
|
|
|
|
stew/byteutils except fromHex
|
|
|
|
|
2023-03-08 13:04:30 +00:00
|
|
|
const
|
|
|
|
testBase = kzgPath & "tests/"
|
|
|
|
BLOB_TO_KZG_COMMITMENT_TESTS = testBase & "blob_to_kzg_commitment"
|
|
|
|
COMPUTE_KZG_PROOF_TESTS = testBase & "compute_kzg_proof"
|
|
|
|
COMPUTE_BLOB_KZG_PROOF_TESTS = testBase & "compute_blob_kzg_proof"
|
|
|
|
VERIFY_KZG_PROOF_TESTS = testBase & "verify_kzg_proof"
|
|
|
|
VERIFY_BLOB_KZG_PROOF_TESTS = testBase & "verify_blob_kzg_proof"
|
|
|
|
VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = testBase & "verify_blob_kzg_proof_batch"
|
|
|
|
|
|
|
|
proc toTestName(x: string): string =
|
|
|
|
let parts = x.split(DirSep)
|
|
|
|
parts[^2]
|
|
|
|
|
|
|
|
proc loadYaml(filename: string): YamlNode =
|
|
|
|
var s = newFileStream(filename)
|
|
|
|
load(s, result)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
proc fromHex(T: type, x: string): T =
|
2024-05-29 14:09:41 +00:00
|
|
|
if (x.len - 2) div 2 > sizeof(result.bytes):
|
2023-03-08 13:04:30 +00:00
|
|
|
raise newException(ValueError, "invalid hex")
|
2024-05-29 14:09:41 +00:00
|
|
|
result.bytes = hexToByteArray(x, sizeof(result.bytes))
|
2023-03-08 13:04:30 +00:00
|
|
|
|
|
|
|
proc fromHex(T: type, x: YamlNode): T =
|
|
|
|
T.fromHex(x.content)
|
|
|
|
|
|
|
|
proc fromHexList(T: type, xList: YamlNode): seq[T] =
|
|
|
|
for x in xList:
|
|
|
|
result.add(T.fromHex(x.content))
|
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
template runTests(folder: string, body: untyped) =
|
2023-03-15 14:39:19 +00:00
|
|
|
let test_files = walkDirRec(folder).toSeq()
|
|
|
|
check test_files.len > 0
|
|
|
|
for test_file in test_files:
|
|
|
|
test toTestName(test_file):
|
2023-03-09 20:15:05 +00:00
|
|
|
# nim template is hygienic, {.inject.} will allow body to
|
|
|
|
# access injected symbol in current scope
|
2023-03-15 14:39:19 +00:00
|
|
|
let n {.inject.} = loadYaml(test_file)
|
2023-03-09 04:04:23 +00:00
|
|
|
try:
|
2023-03-09 20:15:05 +00:00
|
|
|
body
|
2023-03-09 04:04:23 +00:00
|
|
|
except ValueError:
|
2023-03-08 13:04:30 +00:00
|
|
|
check n["output"].content == "null"
|
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
template checkRes(res, body: untyped) =
|
|
|
|
if res.isErr:
|
|
|
|
check n["output"].content == "null"
|
|
|
|
else:
|
|
|
|
body
|
2023-03-08 13:04:30 +00:00
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
template checkBool(res: untyped) =
|
|
|
|
checkRes(res):
|
|
|
|
check n["output"].content == $res.get
|
2023-03-08 13:04:30 +00:00
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
template checkBytes48(res: untyped) =
|
|
|
|
checkRes(res):
|
|
|
|
let bytes = KzgBytes48.fromHex(n["output"])
|
|
|
|
check bytes == res.get
|
2023-03-08 13:04:30 +00:00
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
suite "yaml tests":
|
|
|
|
var ctx: KzgCtx
|
2023-03-08 13:04:30 +00:00
|
|
|
|
2023-03-09 20:15:05 +00:00
|
|
|
test "load trusted setup from string":
|
|
|
|
let res = loadTrustedSetupFromString(trustedSetup)
|
|
|
|
check res.isOk
|
|
|
|
ctx = res.get
|
|
|
|
|
|
|
|
runTests(BLOB_TO_KZG_COMMITMENT_TESTS):
|
|
|
|
let
|
|
|
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
|
|
|
res = ctx.toCommitment(blob)
|
|
|
|
checkBytes48(res)
|
|
|
|
|
|
|
|
runTests(COMPUTE_KZG_PROOF_TESTS):
|
|
|
|
let
|
|
|
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
|
|
|
zBytes = KzgBytes32.fromHex(n["input"]["z"])
|
|
|
|
res = ctx.computeProof(blob, zBytes)
|
|
|
|
|
|
|
|
checkRes(res):
|
|
|
|
let proof = KzgProof.fromHex(n["output"][0])
|
|
|
|
check proof == res.get.proof
|
|
|
|
let y = KzgBytes32.fromHex(n["output"][1])
|
|
|
|
check y == res.get.y
|
|
|
|
|
|
|
|
runTests(COMPUTE_BLOB_KZG_PROOF_TESTS):
|
|
|
|
let
|
|
|
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
|
|
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
|
|
|
res = ctx.computeProof(blob, commitment)
|
|
|
|
checkBytes48(res)
|
|
|
|
|
|
|
|
runTests(VERIFY_KZG_PROOF_TESTS):
|
|
|
|
let
|
|
|
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
|
|
|
z = KzgBytes32.fromHex(n["input"]["z"])
|
|
|
|
y = KzgBytes32.fromHex(n["input"]["y"])
|
|
|
|
proof = KzgProof.fromHex(n["input"]["proof"])
|
|
|
|
res = ctx.verifyProof(commitment, z, y, proof)
|
|
|
|
checkBool(res)
|
|
|
|
|
|
|
|
runTests(VERIFY_BLOB_KZG_PROOF_TESTS):
|
|
|
|
let
|
|
|
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
|
|
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
|
|
|
proof = KzgProof.fromHex(n["input"]["proof"])
|
|
|
|
res = ctx.verifyProof(blob, commitment, proof)
|
|
|
|
checkBool(res)
|
|
|
|
|
|
|
|
runTests(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS):
|
|
|
|
let
|
|
|
|
blobs = KzgBlob.fromHexList(n["input"]["blobs"])
|
|
|
|
commitments = KzgCommitment.fromHexList(n["input"]["commitments"])
|
|
|
|
proofs = KzgProof.fromHexList(n["input"]["proofs"])
|
|
|
|
res = ctx.verifyProofs(blobs, commitments, proofs)
|
|
|
|
checkBool(res)
|