2022-06-22 12:13:41 +00:00
# EIP-4844 -- Polynomial Commitments
## Table of contents
<!-- TOC -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE - RUN doctoc TO UPDATE -->
- [Introduction ](#introduction )
- [Custom types ](#custom-types )
- [Constants ](#constants )
- [Preset ](#preset )
2022-11-03 15:01:32 +00:00
- [Blob ](#blob )
- [Crypto ](#crypto )
2022-06-22 12:13:41 +00:00
- [Trusted setup ](#trusted-setup )
- [Helper functions ](#helper-functions )
2022-09-26 13:39:16 +00:00
- [Bit-reversal permutation ](#bit-reversal-permutation )
- [`is_power_of_two` ](#is_power_of_two )
- [`reverse_bits` ](#reverse_bits )
- [`bit_reversal_permutation` ](#bit_reversal_permutation )
2022-06-22 12:13:41 +00:00
- [BLS12-381 helpers ](#bls12-381-helpers )
2022-09-26 15:57:00 +00:00
- [`bytes_to_bls_field` ](#bytes_to_bls_field )
2022-11-03 15:01:32 +00:00
- [`blob_to_polynomial` ](#blob_to_polynomial )
2022-11-23 14:52:47 +00:00
- [`compute_challenges` ](#compute_challenges )
2022-06-22 12:13:41 +00:00
- [`bls_modular_inverse` ](#bls_modular_inverse )
- [`div` ](#div )
2022-09-19 19:16:19 +00:00
- [`g1_lincomb` ](#g1_lincomb )
2022-11-03 15:01:32 +00:00
- [`poly_lincomb` ](#poly_lincomb )
- [`compute_powers` ](#compute_powers )
- [Polynomials ](#polynomials )
- [`evaluate_polynomial_in_evaluation_form` ](#evaluate_polynomial_in_evaluation_form )
2022-06-22 12:13:41 +00:00
- [KZG ](#kzg )
2022-07-13 10:13:30 +00:00
- [`blob_to_kzg_commitment` ](#blob_to_kzg_commitment )
2022-06-22 12:13:41 +00:00
- [`verify_kzg_proof` ](#verify_kzg_proof )
2022-11-09 12:01:47 +00:00
- [`verify_kzg_proof_impl` ](#verify_kzg_proof_impl )
2022-07-13 10:13:30 +00:00
- [`compute_kzg_proof` ](#compute_kzg_proof )
2022-11-03 15:01:32 +00:00
- [`compute_aggregated_poly_and_commitment` ](#compute_aggregated_poly_and_commitment )
- [`compute_aggregate_kzg_proof` ](#compute_aggregate_kzg_proof )
- [`verify_aggregate_kzg_proof` ](#verify_aggregate_kzg_proof )
2022-06-22 12:13:41 +00:00
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
This document specifies basic polynomial operations and KZG polynomial commitment operations as they are needed for the EIP-4844 specification. The implementations are not optimized for performance, but readability. All practical implementations should optimize the polynomial operations.
2022-11-09 12:02:56 +00:00
Functions flagged as "Public method" MUST be provided by the underlying KZG library as public functions. All other functions are private functions used internally by the KZG library.
2022-06-22 12:13:41 +00:00
## Custom types
| Name | SSZ equivalent | Description |
| - | - | - |
2022-07-13 10:12:31 +00:00
| `G1Point` | `Bytes48` | |
| `G2Point` | `Bytes96` | |
2022-06-22 12:13:41 +00:00
| `BLSFieldElement` | `uint256` | `x < BLS_MODULUS` |
| `KZGCommitment` | `Bytes48` | Same as BLS standard "is valid pubkey" check but also allows `0x00..00` for point-at-infinity |
| `KZGProof` | `Bytes48` | Same as for `KZGCommitment` |
2022-11-03 15:01:32 +00:00
| `Polynomial` | `Vector[BLSFieldElement, FIELD_ELEMENTS_PER_BLOB]` | a polynomial in evaluation form |
| `Blob` | `ByteVector[BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB]` | a basic blob data |
2022-06-22 12:13:41 +00:00
## Constants
| Name | Value | Notes |
| - | - | - |
| `BLS_MODULUS` | `52435875175126190479447740508185965837690552500527637822603658699938581184513` | Scalar field modulus of BLS12-381 |
2022-11-03 15:01:32 +00:00
| `BYTES_PER_FIELD_ELEMENT` | `uint64(32)` | Bytes used to encode a BLS scalar field element |
2022-06-22 12:13:41 +00:00
## Preset
2022-11-03 15:01:32 +00:00
### Blob
| Name | Value |
| - | - |
| `FIELD_ELEMENTS_PER_BLOB` | `uint64(4096)` |
| `FIAT_SHAMIR_PROTOCOL_DOMAIN` | `b'FSBLOBVERIFY_V1_'` |
### Crypto
| Name | Value | Notes |
| - | - | - |
| `ROOTS_OF_UNITY` | `Vector[BLSFieldElement, FIELD_ELEMENTS_PER_BLOB]` | Roots of unity of order FIELD_ELEMENTS_PER_BLOB over the BLS12-381 field |
2022-06-22 12:13:41 +00:00
### Trusted setup
The trusted setup is part of the preset: during testing a `minimal` insecure variant may be used,
but reusing the `mainnet` settings in public networks is a critical security requirement.
| Name | Value |
| - | - |
2022-07-13 10:12:31 +00:00
| `KZG_SETUP_G1` | `Vector[G1Point, FIELD_ELEMENTS_PER_BLOB]` , contents TBD |
2022-06-22 12:13:41 +00:00
| `KZG_SETUP_G2` | `Vector[G2Point, FIELD_ELEMENTS_PER_BLOB]` , contents TBD |
| `KZG_SETUP_LAGRANGE` | `Vector[KZGCommitment, FIELD_ELEMENTS_PER_BLOB]` , contents TBD |
## Helper functions
2022-09-26 13:39:16 +00:00
### Bit-reversal permutation
All polynomials (which are always given in Lagrange form) should be interpreted as being in
bit-reversal permutation. In practice, clients can implement this by storing the lists
`KZG_SETUP_LAGRANGE` and `ROOTS_OF_UNITY` in bit-reversal permutation, so these functions only
have to be called once at startup.
#### `is_power_of_two`
```python
def is_power_of_two(value: int) -> bool:
"""
Check if ``value`` is a power of two integer.
"""
return (value > 0) and (value & (value - 1) == 0)
```
#### `reverse_bits`
```python
def reverse_bits(n: int, order: int) -> int:
"""
2022-11-03 15:01:32 +00:00
Reverse the bit order of an integer ``n``.
2022-09-26 13:39:16 +00:00
"""
assert is_power_of_two(order)
# Convert n to binary with the same number of bits as "order" - 1, then reverse its bit order
return int(('{:0' + str(order.bit_length() - 1) + 'b}').format(n)[::-1], 2)
```
#### `bit_reversal_permutation`
```python
2022-09-28 04:22:53 +00:00
def bit_reversal_permutation(sequence: Sequence[T]) -> Sequence[T]:
2022-09-26 13:39:16 +00:00
"""
2022-09-27 11:13:56 +00:00
Return a copy with bit-reversed permutation. The permutation is an involution (inverts itself).
2022-09-26 13:39:16 +00:00
The input and output are a sequence of generic type ``T`` objects.
"""
2022-09-28 04:22:53 +00:00
return [sequence[reverse_bits(i, len(sequence))] for i in range(len(sequence))]
2022-09-26 13:39:16 +00:00
```
2022-06-22 12:13:41 +00:00
### BLS12-381 helpers
2022-09-26 15:57:00 +00:00
#### `bytes_to_bls_field`
```python
def bytes_to_bls_field(b: Bytes32) -> BLSFieldElement:
"""
2022-11-03 15:01:32 +00:00
Convert 32-byte value to a BLS field scalar. The output is not uniform over the BLS field.
"""
return int.from_bytes(b, ENDIANNESS) % BLS_MODULUS
```
#### `blob_to_polynomial`
```python
def blob_to_polynomial(blob: Blob) -> Polynomial:
"""
Convert a blob to list of BLS field scalars.
"""
polynomial = Polynomial()
for i in range(FIELD_ELEMENTS_PER_BLOB):
value = int.from_bytes(blob[i * BYTES_PER_FIELD_ELEMENT: (i + 1) * BYTES_PER_FIELD_ELEMENT], ENDIANNESS)
assert value < BLS_MODULUS
polynomial[i] = value
return polynomial
```
2022-11-23 14:52:47 +00:00
#### `compute_challenges`
2022-11-03 15:01:32 +00:00
```python
2022-11-23 14:52:47 +00:00
def compute_challenges(polynomials: Sequence[Polynomial],
commitments: Sequence[KZGCommitment]) -> BLSFieldElement:
2022-11-03 15:01:32 +00:00
"""
2022-11-23 14:52:47 +00:00
Return the Fiat-Shamir challenges required by the rest of the protocol.
The Fiat-Shamir logic works as per the following pseudocode:
hashed_data = hash(DOMAIN_SEPARATOR, polynomials, commitments)
r = hash(hashed_data, 0)
2022-11-24 13:11:46 +00:00
r_powers = [1, r, r**2, r**3, ...]
2022-11-23 14:52:47 +00:00
eval_challenge = hash(hashed_data, 1)
Then return `r_powers` and `eval_challenge` after converting them to BLS field elements.
The resulting field elements are not uniform over the BLS field.
2022-09-26 15:57:00 +00:00
"""
2022-11-03 15:01:32 +00:00
# Append the number of polynomials and the degree of each polynomial as a domain separator
2022-11-23 14:52:47 +00:00
num_polynomials = int.to_bytes(len(polynomials), 8, ENDIANNESS)
2022-11-03 15:01:32 +00:00
degree_poly = int.to_bytes(FIELD_ELEMENTS_PER_BLOB, 8, ENDIANNESS)
2022-11-23 14:52:47 +00:00
data = FIAT_SHAMIR_PROTOCOL_DOMAIN + degree_poly + num_polynomials
2022-11-03 15:01:32 +00:00
# Append each polynomial which is composed by field elements
2022-11-23 14:52:47 +00:00
for poly in polynomials:
2022-11-03 15:01:32 +00:00
for field_element in poly:
data += int.to_bytes(field_element, BYTES_PER_FIELD_ELEMENT, ENDIANNESS)
# Append serialized G1 points
2022-11-23 14:52:47 +00:00
for commitment in commitments:
2022-11-03 15:01:32 +00:00
data += commitment
2022-11-23 14:52:47 +00:00
# Transcript has been prepared: time to create the challenges
hashed_data = hash(data)
r = hash(hashed_data + b'\x00')
r_powers = compute_powers(bytes_to_bls_field(r), len(commitments))
eval_challenge = hash(hashed_data + b'\x01')
return r_powers, bytes_to_bls_field(eval_challenge)
2022-09-26 15:57:00 +00:00
```
2022-06-22 12:13:41 +00:00
#### `bls_modular_inverse`
```python
def bls_modular_inverse(x: BLSFieldElement) -> BLSFieldElement:
"""
2022-06-22 12:19:24 +00:00
Compute the modular inverse of x
2022-06-22 12:13:41 +00:00
i.e. return y such that x * y % BLS_MODULUS == 1 and return 0 for x == 0
"""
2022-06-22 12:19:24 +00:00
return pow(x, -1, BLS_MODULUS) if x != 0 else 0
2022-06-22 12:13:41 +00:00
```
#### `div`
```python
2022-06-23 10:40:09 +00:00
def div(x: BLSFieldElement, y: BLSFieldElement) -> BLSFieldElement:
2022-11-03 15:01:32 +00:00
"""
Divide two field elements: ``x`` by `y` `.
"""
2022-07-13 10:12:31 +00:00
return (int(x) * int(bls_modular_inverse(y))) % BLS_MODULUS
2022-06-22 12:13:41 +00:00
```
2022-09-19 19:16:19 +00:00
#### `g1_lincomb`
2022-06-22 12:13:41 +00:00
```python
2022-09-19 19:16:19 +00:00
def g1_lincomb(points: Sequence[KZGCommitment], scalars: Sequence[BLSFieldElement]) -> KZGCommitment:
2022-06-22 12:13:41 +00:00
"""
BLS multiscalar multiplication. This function can be optimized using Pippenger's algorithm and variants.
"""
2022-07-13 10:12:31 +00:00
assert len(points) == len(scalars)
result = bls.Z1
2022-06-22 12:13:41 +00:00
for x, a in zip(points, scalars):
2022-07-13 10:12:31 +00:00
result = bls.add(result, bls.multiply(bls.bytes48_to_G1(x), a))
return KZGCommitment(bls.G1_to_bytes48(result))
```
2022-11-03 15:01:32 +00:00
#### `poly_lincomb`
2022-07-13 10:12:31 +00:00
```python
2022-11-03 15:01:32 +00:00
def poly_lincomb(polys: Sequence[Polynomial],
scalars: Sequence[BLSFieldElement]) -> Polynomial:
2022-07-13 10:12:31 +00:00
"""
2022-11-03 15:01:32 +00:00
Given a list of ``polynomials``, interpret it as a 2D matrix and compute the linear combination
of each column with `scalars` : return the resulting polynomials.
2022-07-13 10:12:31 +00:00
"""
2022-11-23 14:52:47 +00:00
assert len(polys) == len(scalars)
result = [0] * FIELD_ELEMENTS_PER_BLOB
2022-11-03 15:01:32 +00:00
for v, s in zip(polys, scalars):
2022-07-13 10:12:31 +00:00
for i, x in enumerate(v):
result[i] = (result[i] + int(s) * int(x)) % BLS_MODULUS
return [BLSFieldElement(x) for x in result]
2022-06-22 12:13:41 +00:00
```
2022-11-03 15:01:32 +00:00
#### `compute_powers`
```python
def compute_powers(x: BLSFieldElement, n: uint64) -> Sequence[BLSFieldElement]:
"""
2022-11-24 13:11:46 +00:00
Return ``x`` to power of [0, n-1], if n > 0. When n==0, an empty array is returned.
2022-11-03 15:01:32 +00:00
"""
current_power = 1
powers = []
for _ in range(n):
powers.append(BLSFieldElement(current_power))
current_power = current_power * int(x) % BLS_MODULUS
return powers
```
2022-11-23 14:52:47 +00:00
2022-11-03 15:01:32 +00:00
### Polynomials
#### `evaluate_polynomial_in_evaluation_form`
```python
def evaluate_polynomial_in_evaluation_form(polynomial: Polynomial,
z: BLSFieldElement) -> BLSFieldElement:
"""
Evaluate a polynomial (in evaluation form) at an arbitrary point ``z``.
Uses the barycentric formula:
f(z) = (z**WIDTH - 1) / WIDTH * sum_(i=0)^WIDTH (f(DOMAIN[i]) * DOMAIN[i]) / (z - DOMAIN[i])
"""
width = len(polynomial)
assert width == FIELD_ELEMENTS_PER_BLOB
inverse_width = bls_modular_inverse(width)
# Make sure we won't divide by zero during division
assert z not in ROOTS_OF_UNITY
roots_of_unity_brp = bit_reversal_permutation(ROOTS_OF_UNITY)
result = 0
for i in range(width):
2022-11-11 20:57:41 +00:00
result += div(int(polynomial[i]) * int(roots_of_unity_brp[i]), (int(z) - int(roots_of_unity_brp[i])))
2022-11-03 15:01:32 +00:00
result = result * (pow(z, width, BLS_MODULUS) - 1) * inverse_width % BLS_MODULUS
return result
```
2022-06-22 12:13:41 +00:00
### KZG
KZG core functions. These are also defined in EIP-4844 execution specs.
2022-07-13 10:12:31 +00:00
#### `blob_to_kzg_commitment`
2022-06-22 12:13:41 +00:00
```python
2022-07-13 10:12:31 +00:00
def blob_to_kzg_commitment(blob: Blob) -> KZGCommitment:
2022-11-09 12:02:56 +00:00
"""
Public method.
"""
2022-11-03 15:01:32 +00:00
return g1_lincomb(bit_reversal_permutation(KZG_SETUP_LAGRANGE), blob_to_polynomial(blob))
2022-06-22 12:13:41 +00:00
```
#### `verify_kzg_proof`
```python
def verify_kzg_proof(polynomial_kzg: KZGCommitment,
2022-11-09 12:01:47 +00:00
z: Bytes32,
y: Bytes32,
2022-07-13 10:12:31 +00:00
kzg_proof: KZGProof) -> bool:
2022-06-23 10:40:09 +00:00
"""
2022-07-13 10:12:31 +00:00
Verify KZG proof that ``p(z) == y`` where ``p(z)`` is the polynomial represented by ``polynomial_kzg``.
2022-11-09 12:01:47 +00:00
Receives inputs as bytes.
Public method.
"""
return verify_kzg_proof_impl(polynomial_kzg, bytes_to_bls_field(z), bytes_to_bls_field(y), kzg_proof)
```
#### `verify_kzg_proof_impl`
```python
def verify_kzg_proof_impl(polynomial_kzg: KZGCommitment,
z: BLSFieldElement,
y: BLSFieldElement,
kzg_proof: KZGProof) -> bool:
"""
Verify KZG proof that ``p(z) == y`` where ``p(z)`` is the polynomial represented by ``polynomial_kzg``.
2022-06-23 10:40:09 +00:00
"""
2022-07-13 10:12:31 +00:00
# Verify: P - y = Q * (X - z)
X_minus_z = bls.add(bls.bytes96_to_G2(KZG_SETUP_G2[1]), bls.multiply(bls.G2, BLS_MODULUS - z))
P_minus_y = bls.add(bls.bytes48_to_G1(polynomial_kzg), bls.multiply(bls.G1, BLS_MODULUS - y))
2022-06-22 12:13:41 +00:00
return bls.pairing_check([
[P_minus_y, bls.neg(bls.G2)],
2022-07-13 10:12:31 +00:00
[bls.bytes48_to_G1(kzg_proof), X_minus_z]
2022-06-22 12:13:41 +00:00
])
```
2022-07-13 10:12:31 +00:00
#### `compute_kzg_proof`
```python
2022-11-03 15:01:32 +00:00
def compute_kzg_proof(polynomial: Polynomial, z: BLSFieldElement) -> KZGProof:
2022-09-26 13:39:16 +00:00
"""
Compute KZG proof at point `z` with `polynomial` being in evaluation form
2022-11-03 15:01:32 +00:00
Do this by computing the quotient polynomial in evaluation form: q(x) = (p(x) - p(z)) / (x - z)
2022-09-26 13:39:16 +00:00
"""
2022-07-13 10:12:31 +00:00
# To avoid SSZ overflow/underflow, convert element into int
polynomial = [int(i) for i in polynomial]
z = int(z)
y = evaluate_polynomial_in_evaluation_form(polynomial, z)
polynomial_shifted = [(p - int(y)) % BLS_MODULUS for p in polynomial]
# Make sure we won't divide by zero during division
assert z not in ROOTS_OF_UNITY
2022-09-27 09:50:03 +00:00
denominator_poly = [(int(x) - z) % BLS_MODULUS for x in bit_reversal_permutation(ROOTS_OF_UNITY)]
2022-07-13 10:12:31 +00:00
# Calculate quotient polynomial by doing point-by-point division
quotient_polynomial = [div(a, b) for a, b in zip(polynomial_shifted, denominator_poly)]
2022-09-26 13:39:16 +00:00
return KZGProof(g1_lincomb(bit_reversal_permutation(KZG_SETUP_LAGRANGE), quotient_polynomial))
2022-07-13 10:12:31 +00:00
```
2022-11-03 15:01:32 +00:00
#### `compute_aggregated_poly_and_commitment`
2022-06-22 12:13:41 +00:00
```python
2022-11-03 15:01:32 +00:00
def compute_aggregated_poly_and_commitment(
blobs: Sequence[Blob],
kzg_commitments: Sequence[KZGCommitment]) -> Tuple[Polynomial, KZGCommitment, BLSFieldElement]:
2022-06-22 12:13:41 +00:00
"""
2022-11-03 15:01:32 +00:00
Return (1) the aggregated polynomial, (2) the aggregated KZG commitment,
and (3) the polynomial evaluation random challenge.
2022-11-23 14:52:47 +00:00
This function should also work with blobs == [] and kzg_commitments == []
2022-06-22 12:13:41 +00:00
"""
2022-11-23 14:52:47 +00:00
assert len(blobs) == len(kzg_commitments)
2022-11-03 16:08:37 +00:00
# Convert blobs to polynomials
polynomials = [blob_to_polynomial(blob) for blob in blobs]
2022-11-23 14:52:47 +00:00
# Generate random linear combination and evaluation challenges
r_powers, evaluation_challenge = compute_challenges(polynomials, kzg_commitments)
2022-06-22 12:13:41 +00:00
2022-11-03 15:01:32 +00:00
# Create aggregated polynomial in evaluation form
2022-11-03 16:08:37 +00:00
aggregated_poly = Polynomial(poly_lincomb(polynomials, r_powers))
2022-06-22 12:13:41 +00:00
2022-11-03 15:01:32 +00:00
# Compute commitment to aggregated polynomial
aggregated_poly_commitment = KZGCommitment(g1_lincomb(kzg_commitments, r_powers))
2022-09-26 13:39:16 +00:00
2022-11-03 15:01:32 +00:00
return aggregated_poly, aggregated_poly_commitment, evaluation_challenge
```
#### `compute_aggregate_kzg_proof`
```python
def compute_aggregate_kzg_proof(blobs: Sequence[Blob]) -> KZGProof:
2022-11-09 12:02:56 +00:00
"""
2022-11-23 14:52:47 +00:00
Given a list of blobs, return the aggregated KZG proof that is used to verify them against their commitments.
2022-11-09 12:02:56 +00:00
Public method.
"""
2022-11-03 15:01:32 +00:00
commitments = [blob_to_kzg_commitment(blob) for blob in blobs]
aggregated_poly, aggregated_poly_commitment, evaluation_challenge = compute_aggregated_poly_and_commitment(
blobs,
commitments
)
return compute_kzg_proof(aggregated_poly, evaluation_challenge)
2022-06-22 12:13:41 +00:00
```
2022-07-13 10:12:31 +00:00
2022-11-03 15:01:32 +00:00
#### `verify_aggregate_kzg_proof`
```python
def verify_aggregate_kzg_proof(blobs: Sequence[Blob],
expected_kzg_commitments: Sequence[KZGCommitment],
2022-11-19 09:17:11 +00:00
kzg_aggregated_proof: KZGProof) -> bool:
2022-11-09 12:02:56 +00:00
"""
2022-11-23 14:52:47 +00:00
Given a list of blobs and an aggregated KZG proof, verify that they correspond to the provided commitments.
2022-11-09 12:02:56 +00:00
Public method.
"""
2022-11-03 15:01:32 +00:00
aggregated_poly, aggregated_poly_commitment, evaluation_challenge = compute_aggregated_poly_and_commitment(
blobs,
expected_kzg_commitments,
)
# Evaluate aggregated polynomial at `evaluation_challenge` (evaluation function checks for div-by-zero)
y = evaluate_polynomial_in_evaluation_form(aggregated_poly, evaluation_challenge)
# Verify aggregated proof
2022-11-09 12:01:47 +00:00
return verify_kzg_proof_impl(aggregated_poly_commitment, evaluation_challenge, y, kzg_aggregated_proof)
2022-11-03 15:01:32 +00:00
```