2023-02-20 16:00:13 +00:00
|
|
|
import glob
|
2023-03-04 18:24:03 +00:00
|
|
|
import yaml
|
2023-02-20 16:00:13 +00:00
|
|
|
|
2022-09-18 18:50:49 +00:00
|
|
|
import ckzg
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
###############################################################################
|
|
|
|
# Constants
|
|
|
|
###############################################################################
|
|
|
|
|
2023-03-04 18:24:03 +00:00
|
|
|
BLOB_TO_KZG_COMMITMENT_TESTS = "../../tests/blob_to_kzg_commitment/*/*/data.yaml"
|
|
|
|
COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/*/*/data.yaml"
|
|
|
|
COMPUTE_BLOB_KZG_PROOF_TESTS = "../../tests/compute_blob_kzg_proof/*/*/data.yaml"
|
|
|
|
VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/*/*/data.yaml"
|
|
|
|
VERIFY_BLOB_KZG_PROOF_TESTS = "../../tests/verify_blob_kzg_proof/*/*/data.yaml"
|
|
|
|
VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = "../../tests/verify_blob_kzg_proof_batch/*/*/data.yaml"
|
2023-02-20 16:00:13 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Helper Functions
|
|
|
|
###############################################################################
|
|
|
|
|
2023-03-04 18:24:03 +00:00
|
|
|
def bytes_from_hex(hexstring):
|
|
|
|
return bytes.fromhex(hexstring.replace("0x", ""))
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
###############################################################################
|
|
|
|
# Tests
|
|
|
|
###############################################################################
|
2022-09-19 08:43:18 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_blob_to_kzg_commitment(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(BLOB_TO_KZG_COMMITMENT_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
blob = bytes_from_hex(test["input"]["blob"])
|
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
|
|
|
commitment = ckzg.blob_to_kzg_commitment(blob, ts)
|
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
expected_commitment = bytes_from_hex(test["output"])
|
|
|
|
assert commitment == expected_commitment, f"{test_file}\n{commitment.hex()=}\n{expected_commitment.hex()=}"
|
|
|
|
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_compute_kzg_proof(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(COMPUTE_KZG_PROOF_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
blob = bytes_from_hex(test["input"]["blob"])
|
|
|
|
z = bytes_from_hex(test["input"]["z"])
|
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
2023-03-07 19:51:43 +00:00
|
|
|
proof, y = ckzg.compute_kzg_proof(blob, z, ts)
|
2023-02-20 16:00:13 +00:00
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
2023-03-07 19:51:43 +00:00
|
|
|
expected_proof = bytes_from_hex(test["output"][0])
|
2023-03-04 18:24:03 +00:00
|
|
|
assert proof == expected_proof, f"{test_file}\n{proof.hex()=}\n{expected_proof.hex()=}"
|
|
|
|
|
2023-03-07 19:51:43 +00:00
|
|
|
expected_y = bytes_from_hex(test["output"][1])
|
|
|
|
assert y == expected_y, f"{test_file}\n{y.hex()=}\n{expected_y.hex()=}"
|
|
|
|
|
2022-09-19 08:43:18 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_compute_blob_kzg_proof(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(COMPUTE_BLOB_KZG_PROOF_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
blob = bytes_from_hex(test["input"]["blob"])
|
2023-03-07 19:51:43 +00:00
|
|
|
commitment = bytes_from_hex(test["input"]["commitment"])
|
2023-03-04 18:24:03 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
2023-03-07 19:51:43 +00:00
|
|
|
proof = ckzg.compute_blob_kzg_proof(blob, commitment, ts)
|
2023-02-20 16:00:13 +00:00
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
expected_proof = bytes_from_hex(test["output"])
|
|
|
|
assert proof == expected_proof, f"{test_file}\n{proof.hex()=}\n{expected_proof.hex()=}"
|
|
|
|
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_verify_kzg_proof(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(VERIFY_KZG_PROOF_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
commitment = bytes_from_hex(test["input"]["commitment"])
|
|
|
|
z = bytes_from_hex(test["input"]["z"])
|
|
|
|
y = bytes_from_hex(test["input"]["y"])
|
|
|
|
proof = bytes_from_hex(test["input"]["proof"])
|
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
2023-03-04 18:24:03 +00:00
|
|
|
valid = ckzg.verify_kzg_proof(commitment, z, y, proof, ts)
|
2023-02-20 16:00:13 +00:00
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
expected_valid = test["output"]
|
|
|
|
assert valid == expected_valid, f"{test_file}\n{valid=}\n{expected_valid=}"
|
|
|
|
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_verify_blob_kzg_proof(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(VERIFY_BLOB_KZG_PROOF_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
blob = bytes_from_hex(test["input"]["blob"])
|
|
|
|
commitment = bytes_from_hex(test["input"]["commitment"])
|
|
|
|
proof = bytes_from_hex(test["input"]["proof"])
|
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
2023-03-04 18:24:03 +00:00
|
|
|
valid = ckzg.verify_blob_kzg_proof(blob, commitment, proof, ts)
|
2023-02-20 16:00:13 +00:00
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
expected_valid = test["output"]
|
|
|
|
assert valid == expected_valid, f"{test_file}\n{valid=}\n{expected_valid=}"
|
|
|
|
|
2022-09-19 08:43:18 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
def test_verify_blob_kzg_proof_batch(ts):
|
2023-03-04 18:24:03 +00:00
|
|
|
for test_file in glob.glob(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS):
|
|
|
|
with open(test_file, "r") as f:
|
|
|
|
test = yaml.safe_load(f)
|
|
|
|
|
|
|
|
blobs = b"".join(map(bytes_from_hex, test["input"]["blobs"]))
|
|
|
|
commitments = b"".join(map(bytes_from_hex, test["input"]["commitments"]))
|
|
|
|
proofs = b"".join(map(bytes_from_hex, test["input"]["proofs"]))
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
try:
|
2023-03-04 18:24:03 +00:00
|
|
|
valid = ckzg.verify_blob_kzg_proof_batch(blobs, commitments, proofs, ts)
|
2023-02-20 16:00:13 +00:00
|
|
|
except:
|
2023-03-04 18:24:03 +00:00
|
|
|
assert test["output"] is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
expected_valid = test["output"]
|
|
|
|
assert valid == expected_valid, f"{test_file}\n{valid=}\n{expected_valid=}"
|
|
|
|
|
2022-09-19 08:43:18 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
###############################################################################
|
|
|
|
# Main Logic
|
|
|
|
###############################################################################
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt")
|
2022-09-19 08:43:18 +00:00
|
|
|
|
2023-02-20 16:00:13 +00:00
|
|
|
test_blob_to_kzg_commitment(ts)
|
|
|
|
test_compute_kzg_proof(ts)
|
|
|
|
test_compute_blob_kzg_proof(ts)
|
|
|
|
test_verify_kzg_proof(ts)
|
|
|
|
test_verify_blob_kzg_proof(ts)
|
|
|
|
test_verify_blob_kzg_proof_batch(ts)
|
2022-09-18 22:16:16 +00:00
|
|
|
|
2023-03-04 18:24:03 +00:00
|
|
|
print("tests passed")
|