ethereum.crypto.kzg
The KZG Implementation.
FQ
| 22 | FQ = Tuple[ |
|---|---|
| 23 | optimized_bls12_381_FQ, optimized_bls12_381_FQ, optimized_bls12_381_FQ |
| 24 | ] |
FQ2
| 25 | FQ2 = Tuple[ |
|---|---|
| 26 | optimized_bls12_381_FQ2, optimized_bls12_381_FQ2, optimized_bls12_381_FQ2 |
| 27 | ] |
KZGCommitment
KZG commitment to a polynomial.
class KZGCommitment:
KZGProof
KZG proof.
class KZGProof:
BLSFieldElement
A field element in the BLS12-381 field.
class BLSFieldElement:
VersionedHash
A versioned hash.
class VersionedHash:
VERSIONED_HASH_VERSION_KZG
| 54 | VERSIONED_HASH_VERSION_KZG = hex_to_bytes("0x01") |
|---|
BYTES_PER_COMMITMENT
| 55 | BYTES_PER_COMMITMENT = 48 |
|---|
BYTES_PER_PROOF
| 56 | BYTES_PER_PROOF = 48 |
|---|
BYTES_PER_FIELD_ELEMENT
| 57 | BYTES_PER_FIELD_ELEMENT = 32 |
|---|
G1_POINT_AT_INFINITY
| 58 | G1_POINT_AT_INFINITY = b"\xc0" + b"\x00" * 47 |
|---|
BLS_MODULUS
| 59 | BLS_MODULUS = BLSFieldElement( |
|---|---|
| 60 | 52435875175126190479447740508185965837690552500527637822603658699938581184513 # noqa: E501 |
| 61 | ) |
KZG_SETUP_G2_MONOMIAL_1
| 62 | KZG_SETUP_G2_MONOMIAL_1 = "0xb5bfd7dd8cdeb128843bc287230af38926187075cbfbefa81009a2ce615ac53d2914e5870cb452d2afaaab24f3499f72185cbfee53492714734429b7b38608e23926c911cceceac9a36851477ba4c60b087041de621000edc98edada20c1def2" |
|---|
kzg_commitment_to_versioned_hash
Convert a KZG commitment to a versioned hash.
def kzg_commitment_to_versioned_hash(kzg_commitment: KZGCommitment) -> VersionedHash:
| 68 | """ |
|---|---|
| 69 | Convert a KZG commitment to a versioned hash. |
| 70 | """ |
| 71 | return VersionedHash( |
| 72 | VERSIONED_HASH_VERSION_KZG |
| 73 | + Bytes32(sha256(kzg_commitment).digest())[1:] |
| 74 | ) |
validate_kzg_g1
Perform BLS validation required by the types KZGProof
and KZGCommitment.
def validate_kzg_g1(b: Bytes48) -> None:
| 78 | """ |
|---|---|
| 79 | Perform BLS validation required by the types `KZGProof` |
| 80 | and `KZGCommitment`. |
| 81 | """ |
| 82 | if b == G1_POINT_AT_INFINITY: |
| 83 | return |
| 84 | |
| 85 | assert G2ProofOfPossession.KeyValidate(BLSPubkey(b)) |
bytes_to_kzg_commitment
Convert untrusted bytes into a trusted and validated KZGCommitment.
def bytes_to_kzg_commitment(b: Bytes48) -> KZGCommitment:
| 89 | """ |
|---|---|
| 90 | Convert untrusted bytes into a trusted and validated KZGCommitment. |
| 91 | """ |
| 92 | validate_kzg_g1(b) |
| 93 | return KZGCommitment(b) |
bytes_to_bls_field
Convert untrusted bytes to a trusted and validated BLS scalar field element. This function does not accept inputs greater than the BLS modulus.
def bytes_to_bls_field(b: Bytes32) -> BLSFieldElement:
| 97 | """ |
|---|---|
| 98 | Convert untrusted bytes to a trusted and validated BLS scalar |
| 99 | field element. This function does not accept inputs greater than |
| 100 | the BLS modulus. |
| 101 | """ |
| 102 | field_element = int.from_bytes(b, "big") |
| 103 | assert field_element < int(BLS_MODULUS) |
| 104 | return BLSFieldElement(field_element) |
bytes_to_kzg_proof
Convert untrusted bytes into a trusted and validated KZGProof.
def bytes_to_kzg_proof(b: Bytes48) -> KZGProof:
| 108 | """ |
|---|---|
| 109 | Convert untrusted bytes into a trusted and validated KZGProof. |
| 110 | """ |
| 111 | validate_kzg_g1(b) |
| 112 | return KZGProof(b) |
pairing_check
Check if the pairings are valid.
def pairing_check(values: Tuple[Tuple[FQ, FQ2], Tuple[FQ, FQ2]]) -> bool:
| 116 | """ |
|---|---|
| 117 | Check if the pairings are valid. |
| 118 | """ |
| 119 | p_q_1, p_q_2 = values |
| 120 | final_exponentiation = final_exponentiate( |
| 121 | pairing(p_q_1[1], p_q_1[0], final_exponentiate=False) |
| 122 | * pairing(p_q_2[1], p_q_2[0], final_exponentiate=False) |
| 123 | ) |
| 124 | return final_exponentiation == FQ12.one() |
verify_kzg_proof
Verify KZG proof that p(z) == y where p(z)
is the polynomial represented by polynomial_kzg.
Receives inputs as bytes.
Public method.
def verify_kzg_proof(commitment_bytes: Bytes48, z_bytes: Bytes32, y_bytes: Bytes32, proof_bytes: Bytes48) -> bool:
| 133 | """ |
|---|---|
| 134 | Verify KZG proof that ``p(z) == y`` where ``p(z)`` |
| 135 | is the polynomial represented by ``polynomial_kzg``. |
| 136 | Receives inputs as bytes. |
| 137 | Public method. |
| 138 | """ |
| 139 | assert len(commitment_bytes) == BYTES_PER_COMMITMENT |
| 140 | assert len(z_bytes) == BYTES_PER_FIELD_ELEMENT |
| 141 | assert len(y_bytes) == BYTES_PER_FIELD_ELEMENT |
| 142 | assert len(proof_bytes) == BYTES_PER_PROOF |
| 143 | |
| 144 | return verify_kzg_proof_impl( |
| 145 | bytes_to_kzg_commitment(commitment_bytes), |
| 146 | bytes_to_bls_field(z_bytes), |
| 147 | bytes_to_bls_field(y_bytes), |
| 148 | bytes_to_kzg_proof(proof_bytes), |
| 149 | ) |
verify_kzg_proof_impl
Verify KZG proof that p(z) == y where p(z)
is the polynomial represented by polynomial_kzg.
def verify_kzg_proof_impl(commitment: KZGCommitment, z: BLSFieldElement, y: BLSFieldElement, proof: KZGProof) -> bool:
| 158 | """ |
|---|---|
| 159 | Verify KZG proof that ``p(z) == y`` where ``p(z)`` |
| 160 | is the polynomial represented by ``polynomial_kzg``. |
| 161 | """ |
| 162 | # Verify: P - y = Q * (X - z) |
| 163 | X_minus_z = add( |
| 164 | signature_to_G2(BLSSignature(hex_to_bytes(KZG_SETUP_G2_MONOMIAL_1))), |
| 165 | multiply(G2, int((BLS_MODULUS - z) % BLS_MODULUS)), |
| 166 | ) |
| 167 | P_minus_y = add( |
| 168 | pubkey_to_G1(BLSPubkey(commitment)), |
| 169 | multiply(G1, int((BLS_MODULUS - y) % BLS_MODULUS)), |
| 170 | ) |
| 171 | return pairing_check( |
| 172 | ( |
| 173 | (P_minus_y, neg(G2)), |
| 174 | (pubkey_to_G1(BLSPubkey(proof)), X_minus_z), |
| 175 | ) |
| 176 | ) |