Skip to content

Commit

Permalink
pass compute_blob_kzg_proof tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Sep 14, 2023
1 parent 9a51eb4 commit 8cdd4f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
45 changes: 42 additions & 3 deletions constantine/ethereum_eip4844_kzg_polynomial_commitments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ func fiatShamirChallenge(dst: var Fr[BLS12_381], blob: Blob, commitmentBytes: ar

transcript.update(FIAT_SHAMIR_PROTOCOL_DOMAIN)

# Append the degree of polynomial as a domain separator
transcript.update(FIELD_ELEMENTS_PER_BLOB.uint64.toBytes(bigEndian))
# Append the degree of polynomial as 16-byte big-endian integer as a domain separator
transcript.update(default(array[16-sizeof(uint64), byte]))
transcript.update(FIELD_ELEMENTS_PER_BLOB.uint64.toBytes(bigEndian))

transcript.update(blob)
transcript.update(commitmentBytes)
Expand Down Expand Up @@ -315,7 +315,7 @@ func compute_kzg_proof*(
proof, y,
poly[], ctx.domain,
z, ctx.srs_lagrange_g1,
bitreversedDomain = true)
isBitReversedDomain = true)

discard proof_bytes.serialize_g1_compressed(proof) # cannot fail
y_bytes.marshal(y, bigEndian) # cannot fail
Expand Down Expand Up @@ -349,6 +349,45 @@ func verify_kzg_proof*(
else:
return cttEthKZG_VerificationFailure

func compute_blob_kzg_proof*(
ctx: ptr EthereumKZGContext,
proof_bytes: var array[48, byte],
blob: ptr Blob,
commitment_bytes: array[48, byte]): CttEthKzgStatus =
## Given a blob, return the KZG proof that is used to verify it against the commitment.
## This method does not verify that the commitment is correct with respect to `blob`.

var commitment {.noInit.}: KZGCommitment
check commitment.bytes_to_kzg_commitment(commitment_bytes)

# Blob -> Polynomial
let poly = allocHeapAligned(PolynomialEval[FIELD_ELEMENTS_PER_BLOB, Fr[BLS12_381]], 64)
var status = poly.blob_to_field_polynomial(blob)
if status == cttCodecScalar_ScalarLargerThanCurveOrder:
freeHeap(poly)
return cttEthKZG_ScalarLargerThanCurveOrder
elif status != cttCodecScalar_Success:
debugEcho "Unreachable status in compute_kzg_proof: ", status
debugEcho "Panicking ..."
quit 1

var challenge {.noInit.}: Fr[BLS12_381]
challenge.fiatShamirChallenge(blob[], commitment_bytes)

var y {.noInit.}: Fr[BLS12_381] # y = p(z), eval at challenge z
var proof {.noInit.}: ECP_ShortW_Aff[Fp[BLS12_381], G1] # [proof]₁ = [(p(τ) - p(z)) / (τ-z)]₁

kzg_prove(
proof, y,
poly[], ctx.domain,
challenge, ctx.srs_lagrange_g1,
isBitReversedDomain = true)

discard proof_bytes.serialize_g1_compressed(proof) # cannot fail

freeHeap(poly)
return cttEthKZG_Success

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

Expand Down
23 changes: 22 additions & 1 deletion tests/t_ethereum_eip4844_deneb_kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ template testGen*(name, testData: untyped, body: untyped): untyped {.dirty.} =
var skipped = 0
const testdir = TestVectorsDir / astToStr(name)/"small"
for dir, file in walkTests(testdir, skipped):
stdout.write(" " & astToStr(name) & " test: " & alignLeft(file, 70))
stdout.write(" " & alignLeft(astToStr(name) & " test:", 36) & alignLeft(file, 90))
let testData = loadVectors(dir/file)

body
Expand Down Expand Up @@ -142,6 +142,24 @@ testGen(verify_kzg_proof, testVector):
else:
doAssert testVector["output"].content == "null"

testGen(compute_blob_kzg_proof, testVector):
parseAssign(blob, 32*4096, testVector["input"]["blob"].content)
parseAssign(commitment, 48, testVector["input"]["commitment"].content)

var proof: array[48, byte]

let status = compute_blob_kzg_proof(ctx, proof, blob[].addr, commitment[])
stdout.write "[" & $status & "]\n"

if status == cttEthKZG_Success:
parseAssign(expectedProof, 48, testVector["output"].content)

doAssert bool(proof == expectedProof[]), block:
"\nproof: " & proof.toHex() &
"\nexpected: " & expectedProof[].toHex() & "\n"
else:
doAssert testVector["output"].content == "null"

block:
suite "Ethereum Deneb Hardfork / EIP-4844 / Proto-Danksharding / KZG Polynomial Commitments":
let ctx = load_ethereum_kzg_test_trusted_setup_mainnet()
Expand All @@ -155,4 +173,7 @@ block:
test "verify_kzg_proof(commitment: array[48, byte], z, y: array[32, byte], proof: array[48, byte]) -> bool":
ctx.test_verify_kzg_proof()

test "compute_blob_kzg_proof(proof: var array[48, byte], blob: ptr array[4096, byte], commitment: array[48, byte])":
ctx.test_compute_blob_kzg_proof()

ctx.delete()

0 comments on commit 8cdd4f9

Please sign in to comment.