ethereum.crypto.kzg
The KZG Implementation ^^^^^^^^^^^^^^^^^^^^^^
FQ
23 | FQ = Tuple[ |
---|---|
24 | optimized_bls12_381_FQ, optimized_bls12_381_FQ, optimized_bls12_381_FQ |
25 | ] |
FQ2
26 | FQ2 = Tuple[ |
---|---|
27 | optimized_bls12_381_FQ2, optimized_bls12_381_FQ2, optimized_bls12_381_FQ2 |
28 | ] |
VERSIONED_HASH_VERSION_KZG
31 | VERSIONED_HASH_VERSION_KZG = hex_to_bytes("0x01") |
---|
BYTES_PER_COMMITMENT
32 | BYTES_PER_COMMITMENT = 48 |
---|
BYTES_PER_PROOF
33 | BYTES_PER_PROOF = 48 |
---|
BYTES_PER_FIELD_ELEMENT
34 | BYTES_PER_FIELD_ELEMENT = 32 |
---|
G1_POINT_AT_INFINITY
35 | G1_POINT_AT_INFINITY = b"\xc0" + b"\x00" * 47 |
---|
BLS_MODULUS
36 | BLS_MODULUS = 52435875175126190479447740508185965837690552500527637822603658699938581184513 |
---|
KZG_SETUP_G2_LENGTH
37 | KZG_SETUP_G2_LENGTH = 65 |
---|
KZG_SETUP_G2_MONOMIAL_1
38 | KZG_SETUP_G2_MONOMIAL_1 = "0xb5bfd7dd8cdeb128843bc287230af38926187075cbfbefa81009a2ce615ac53d2914e5870cb452d2afaaab24f3499f72185cbfee53492714734429b7b38608e23926c911cceceac9a36851477ba4c60b087041de621000edc98edada20c1def2" |
---|
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:
G2Point
A point in G2.
class G2Point:
kzg_commitment_to_versioned_hash
Convert a KZG commitment to a versioned hash.
def kzg_commitment_to_versioned_hash(kzg_commitment: KZGCommitment) -> VersionedHash:
74 | """ |
---|---|
75 | Convert a KZG commitment to a versioned hash. |
76 | """ |
77 | return VersionedHash( |
78 | VERSIONED_HASH_VERSION_KZG |
79 | + Bytes32(sha256(kzg_commitment).digest())[1:] |
80 | ) |
validate_kzg_g1
Perform BLS validation required by the types KZGProof
and KZGCommitment
.
def validate_kzg_g1(b: Bytes48) -> None:
84 | """ |
---|---|
85 | Perform BLS validation required by the types `KZGProof` |
86 | and `KZGCommitment`. |
87 | """ |
88 | if b == G1_POINT_AT_INFINITY: |
89 | return |
90 | |
91 | 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:
95 | """ |
---|---|
96 | Convert untrusted bytes into a trusted and validated KZGCommitment. |
97 | """ |
98 | validate_kzg_g1(b) |
99 | 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:
103 | """ |
---|---|
104 | Convert untrusted bytes to a trusted and validated BLS scalar |
105 | field element. This function does not accept inputs greater than |
106 | the BLS modulus. |
107 | """ |
108 | field_element = int.from_bytes(b, "big") |
109 | assert field_element < BLS_MODULUS |
110 | 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:
114 | """ |
---|---|
115 | Convert untrusted bytes into a trusted and validated KZGProof. |
116 | """ |
117 | validate_kzg_g1(b) |
118 | return KZGProof(b) |
pairing_check
Check if the pairings are valid.
def pairing_check(values: Tuple[Tuple[FQ, FQ2], Tuple[FQ, FQ2]]) -> bool:
122 | """ |
---|---|
123 | Check if the pairings are valid. |
124 | """ |
125 | p_q_1, p_q_2 = values |
126 | final_exponentiation = final_exponentiate( |
127 | pairing(p_q_1[1], p_q_1[0], final_exponentiate=False) |
128 | * pairing(p_q_2[1], p_q_2[0], final_exponentiate=False) |
129 | ) |
130 | 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:
139 | """ |
---|---|
140 | Verify KZG proof that ``p(z) == y`` where ``p(z)`` |
141 | is the polynomial represented by ``polynomial_kzg``. |
142 | Receives inputs as bytes. |
143 | Public method. |
144 | """ |
145 | assert len(commitment_bytes) == BYTES_PER_COMMITMENT |
146 | assert len(z_bytes) == BYTES_PER_FIELD_ELEMENT |
147 | assert len(y_bytes) == BYTES_PER_FIELD_ELEMENT |
148 | assert len(proof_bytes) == BYTES_PER_PROOF |
149 | |
150 | return verify_kzg_proof_impl( |
151 | bytes_to_kzg_commitment(commitment_bytes), |
152 | bytes_to_bls_field(z_bytes), |
153 | bytes_to_bls_field(y_bytes), |
154 | bytes_to_kzg_proof(proof_bytes), |
155 | ) |
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:
164 | """ |
---|---|
165 | Verify KZG proof that ``p(z) == y`` where ``p(z)`` |
166 | is the polynomial represented by ``polynomial_kzg``. |
167 | """ |
168 | # Verify: P - y = Q * (X - z) |
169 | X_minus_z = add( |
170 | signature_to_G2(BLSSignature(hex_to_bytes(KZG_SETUP_G2_MONOMIAL_1))), |
171 | multiply(G2, (BLS_MODULUS - z) % BLS_MODULUS), |
172 | ) |
173 | P_minus_y = add( |
174 | pubkey_to_G1(BLSPubkey(commitment)), |
175 | multiply(G1, (BLS_MODULUS - y) % BLS_MODULUS), |
176 | ) |
177 | return pairing_check( |
178 | ( |
179 | (P_minus_y, neg(G2)), |
180 | (pubkey_to_G1(BLSPubkey(proof)), X_minus_z), |
181 | ) |
182 | ) |