Allow invalid length inputs in nim tests (#190)
This commit is contained in:
parent
ff4e99579d
commit
fc11addf50
|
@ -3,10 +3,13 @@
|
||||||
import
|
import
|
||||||
std/[os, strutils, streams],
|
std/[os, strutils, streams],
|
||||||
unittest2, yaml,
|
unittest2, yaml,
|
||||||
stew/byteutils,
|
|
||||||
../kzg,
|
../kzg,
|
||||||
./types
|
./types
|
||||||
|
|
||||||
|
# we want to use our own fromHex
|
||||||
|
import
|
||||||
|
stew/byteutils except fromHex
|
||||||
|
|
||||||
const
|
const
|
||||||
testBase = kzgPath & "tests/"
|
testBase = kzgPath & "tests/"
|
||||||
BLOB_TO_KZG_COMMITMENT_TESTS = testBase & "blob_to_kzg_commitment"
|
BLOB_TO_KZG_COMMITMENT_TESTS = testBase & "blob_to_kzg_commitment"
|
||||||
|
@ -28,7 +31,7 @@ proc loadYaml(filename: string): YamlNode =
|
||||||
proc fromHex(T: type, x: string): T =
|
proc fromHex(T: type, x: string): T =
|
||||||
if (x.len - 2) div 2 > sizeof(T):
|
if (x.len - 2) div 2 > sizeof(T):
|
||||||
raise newException(ValueError, "invalid hex")
|
raise newException(ValueError, "invalid hex")
|
||||||
hexToByteArray[sizeof(T)](x)
|
hexToByteArray(x, sizeof(T))
|
||||||
|
|
||||||
proc fromHex(T: type, x: YamlNode): T =
|
proc fromHex(T: type, x: YamlNode): T =
|
||||||
T.fromHex(x.content)
|
T.fromHex(x.content)
|
||||||
|
@ -47,86 +50,104 @@ suite "yaml tests":
|
||||||
|
|
||||||
for filename in walkDirRec(BLOB_TO_KZG_COMMITMENT_TESTS):
|
for filename in walkDirRec(BLOB_TO_KZG_COMMITMENT_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
blob = KzgBlob.fromHex(n["input"]["blob"])
|
let
|
||||||
res = ctx.toCommitment(blob)
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
||||||
|
res = ctx.toCommitment(blob)
|
||||||
|
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
let kate = KzgCommitment.fromHex(n["output"])
|
||||||
|
check kate == res.get
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
let kate = KzgCommitment.fromHex(n["output"])
|
|
||||||
check kate == res.get
|
|
||||||
|
|
||||||
for filename in walkDirRec(COMPUTE_KZG_PROOF_TESTS):
|
for filename in walkDirRec(COMPUTE_KZG_PROOF_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
blob = KzgBlob.fromHex(n["input"]["blob"])
|
let
|
||||||
zBytes = KzgBytes32.fromHex(n["input"]["z"])
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
||||||
res = ctx.computeProof(blob, zBytes)
|
zBytes = KzgBytes32.fromHex(n["input"]["z"])
|
||||||
|
res = ctx.computeProof(blob, zBytes)
|
||||||
|
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
let proof = KzgProof.fromHex(n["output"][0])
|
||||||
|
check proof == res.get.proof
|
||||||
|
let y = KzgBytes32.fromHex(n["output"][1])
|
||||||
|
check y == res.get.y
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
let proof = KzgProof.fromHex(n["output"][0])
|
|
||||||
check proof == res.get.proof
|
|
||||||
let y = KzgBytes32.fromHex(n["output"][1])
|
|
||||||
check y == res.get.y
|
|
||||||
|
|
||||||
for filename in walkDirRec(COMPUTE_BLOB_KZG_PROOF_TESTS):
|
for filename in walkDirRec(COMPUTE_BLOB_KZG_PROOF_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
blob = KzgBlob.fromHex(n["input"]["blob"])
|
let
|
||||||
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
||||||
res = ctx.computeProof(blob, commitment)
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
||||||
|
res = ctx.computeProof(blob, commitment)
|
||||||
|
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
let proof = KzgProof.fromHex(n["output"])
|
||||||
|
check proof == res.get
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
let proof = KzgProof.fromHex(n["output"])
|
|
||||||
check proof == res.get
|
|
||||||
|
|
||||||
for filename in walkDirRec(VERIFY_KZG_PROOF_TESTS):
|
for filename in walkDirRec(VERIFY_KZG_PROOF_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
let
|
||||||
z = KzgBytes32.fromHex(n["input"]["z"])
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
||||||
y = KzgBytes32.fromHex(n["input"]["y"])
|
z = KzgBytes32.fromHex(n["input"]["z"])
|
||||||
proof = KzgProof.fromHex(n["input"]["proof"])
|
y = KzgBytes32.fromHex(n["input"]["y"])
|
||||||
|
proof = KzgProof.fromHex(n["input"]["proof"])
|
||||||
|
res = ctx.verifyProof(commitment, z, y, proof)
|
||||||
|
|
||||||
let res = ctx.verifyProof(commitment, z, y, proof)
|
if res.isErr:
|
||||||
if res.isErr:
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
check n["output"].content == $res.get
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
check n["output"].content == $res.get
|
|
||||||
|
|
||||||
for filename in walkDirRec(VERIFY_BLOB_KZG_PROOF_TESTS):
|
for filename in walkDirRec(VERIFY_BLOB_KZG_PROOF_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
blob = KzgBlob.fromHex(n["input"]["blob"])
|
let
|
||||||
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
blob = KzgBlob.fromHex(n["input"]["blob"])
|
||||||
proof = KzgProof.fromHex(n["input"]["proof"])
|
commitment = KzgCommitment.fromHex(n["input"]["commitment"])
|
||||||
|
proof = KzgProof.fromHex(n["input"]["proof"])
|
||||||
|
res = ctx.verifyProof(blob, commitment, proof)
|
||||||
|
|
||||||
let res = ctx.verifyProof(blob, commitment, proof)
|
if res.isErr:
|
||||||
if res.isErr:
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
check n["output"].content == $res.get
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
check n["output"].content == $res.get
|
|
||||||
|
|
||||||
for filename in walkDirRec(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS):
|
for filename in walkDirRec(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS):
|
||||||
test toTestName(filename):
|
test toTestName(filename):
|
||||||
let
|
let n = loadYaml(filename)
|
||||||
n = loadYaml(filename)
|
try:
|
||||||
blobs = KzgBlob.fromHexList(n["input"]["blobs"])
|
let
|
||||||
commitments = KzgCommitment.fromHexList(n["input"]["commitments"])
|
blobs = KzgBlob.fromHexList(n["input"]["blobs"])
|
||||||
proofs = KzgProof.fromHexList(n["input"]["proofs"])
|
commitments = KzgCommitment.fromHexList(n["input"]["commitments"])
|
||||||
res = ctx.verifyProofs(blobs, commitments, proofs)
|
proofs = KzgProof.fromHexList(n["input"]["proofs"])
|
||||||
|
res = ctx.verifyProofs(blobs, commitments, proofs)
|
||||||
|
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
|
check n["output"].content == "null"
|
||||||
|
else:
|
||||||
|
check n["output"].content == $res.get
|
||||||
|
except ValueError:
|
||||||
check n["output"].content == "null"
|
check n["output"].content == "null"
|
||||||
else:
|
|
||||||
check n["output"].content == $res.get
|
|
||||||
|
|
Loading…
Reference in New Issue