Skip to content

Test Point Evaluation Precompile

Documentation for tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py.

Generate fixtures for these test cases for Cancun with:

Cancun only:

fill -v tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py --fork=Cancun --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Cancun:
fill -v tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py --until=Cancun --evm-bin=/path/to/evm-tool-dev-version

abstract: Tests point evaluation precompile for EIP-4844: Shard Blob Transactions

Test point evaluation precompile for [EIP-4844: Shard Blob Transactions](https://eips.ethereum.org/EIPS/eip-4844).

note: Adding a new test

Add a function that is named `test_<test_name>` and takes at least the following arguments:

- blockchain_test
- pre
- tx
- post

The following arguments *need* to be parametrized or the test will not be generated:

- versioned_hash
- kzg_commitment
- z
- y
- kzg_proof
- success

These values correspond to a single call of the precompile, and `success` refers to
whether the call should succeed or fail.

All other `pytest.fixture` fixtures can be parametrized to generate new combinations and test
cases.

test_valid_precompile_calls(blockchain_test, pre, tx, post)

Test valid sanity precompile calls that are expected to succeed.

  • kzg_commitment and kzg_proof are set to values such that p(z)==0 for all values of z, hence y is tested to be zero, and call to be successful.
Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
@pytest.mark.parametrize(
    "z,y,kzg_commitment,kzg_proof,versioned_hash",
    [
        pytest.param(Spec.BLS_MODULUS - 1, 0, INF_POINT, INF_POINT, auto, id="in_bounds_z"),
    ],
)
@pytest.mark.parametrize("success", [True])
@pytest.mark.valid_from("Cancun")
def test_valid_precompile_calls(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Test valid sanity precompile calls that are expected to succeed.

    - `kzg_commitment` and `kzg_proof` are set to values such that `p(z)==0` for all values of `z`,
    hence `y` is tested to be zero, and call to be successful.
    """
    blockchain_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=[tx])],
    )

test_invalid_precompile_calls(blockchain_test, pre, tx, post)

Test invalid precompile calls:

  • Out of bounds inputs z and y
  • Correct proof, commitment, z and y, but incorrect lengths
  • Null inputs
  • Zero inputs
  • Correct proof, commitment, z and y, but incorrect version versioned hash
Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
@pytest.mark.parametrize(
    "z,y,kzg_commitment,kzg_proof,versioned_hash",
    [
        (Spec.BLS_MODULUS, 0, INF_POINT, INF_POINT, auto),
        (0, Spec.BLS_MODULUS, INF_POINT, INF_POINT, auto),
        (Z, 0, INF_POINT, INF_POINT[:-1], auto),
        (Z, 0, INF_POINT, INF_POINT[0:1], auto),
        (Z, 0, INF_POINT, INF_POINT + bytes([0]), auto),
        (Z, 0, INF_POINT, INF_POINT + bytes([0] * 1023), auto),
        (bytes(), bytes(), bytes(), bytes(), bytes()),
        (0, 0, 0, 0, 0),
        (0, 0, 0, 0, auto),
        (Z, 0, INF_POINT, INF_POINT, Spec.kzg_to_versioned_hash(0xC0 << 376, 0x00)),
        (Z, 0, INF_POINT, INF_POINT, Spec.kzg_to_versioned_hash(0xC0 << 376, 0x02)),
        (Z, 0, INF_POINT, INF_POINT, Spec.kzg_to_versioned_hash(0xC0 << 376, 0xFF)),
    ],
    ids=[
        "out_of_bounds_z",
        "out_of_bounds_y",
        "correct_proof_1_input_too_short",
        "correct_proof_1_input_too_short_2",
        "correct_proof_1_input_too_long",
        "correct_proof_1_input_extra_long",
        "null_inputs",
        "zeros_inputs",
        "zeros_inputs_correct_versioned_hash",
        "correct_proof_1_incorrect_versioned_hash_version_0x00",
        "correct_proof_1_incorrect_versioned_hash_version_0x02",
        "correct_proof_1_incorrect_versioned_hash_version_0xff",
    ],
)
@pytest.mark.parametrize("success", [False])
@pytest.mark.valid_from("Cancun")
def test_invalid_precompile_calls(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Test invalid precompile calls:

    - Out of bounds inputs `z` and `y`
    - Correct proof, commitment, z and y, but incorrect lengths
    - Null inputs
    - Zero inputs
    - Correct proof, commitment, z and y, but incorrect version versioned hash
    """
    blockchain_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=[tx])],
    )

test_point_evaluation_precompile_external_vectors(blockchain_test, pre, tx, post)

Test precompile calls using external test vectors compiled from different sources:

  • go_kzg_4844_verify_kzg_proof.json: test vectors from the go-kzg-4844 repository.
Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
@pytest.mark.parametrize(
    "z,y,kzg_commitment,kzg_proof,success",
    all_external_vectors(),
)
@pytest.mark.parametrize("versioned_hash", [auto])
@pytest.mark.valid_from("Cancun")
def test_point_evaluation_precompile_external_vectors(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Test precompile calls using external test vectors compiled from different sources:

    - `go_kzg_4844_verify_kzg_proof.json`: test vectors from the
    [go-kzg-4844](https://github.com/crate-crypto/go-kzg-4844) repository.
    """
    blockchain_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=[tx])],
    )

test_point_evaluation_precompile_calls(blockchain_test, pre, tx, post)

Test calling the Point Evaluation Precompile with different call types, gas and parameter configuration:

  • Using CALL, DELEGATECALL, CALLCODE and STATICCALL.
  • Using correct and incorrect proofs
  • Using barely insufficient gas
Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
@pytest.mark.parametrize(
    "call_gas,y,success",
    [
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS, 0, True),
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS, 1, False),
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS - 1, 0, False),
    ],
    ids=["correct", "incorrect", "insufficient_gas"],
)
@pytest.mark.parametrize(
    "call_type",
    [
        Op.CALL,
        Op.DELEGATECALL,
        Op.CALLCODE,
        Op.STATICCALL,
    ],
)
@pytest.mark.parametrize(
    "z,kzg_commitment,kzg_proof,versioned_hash",
    [[Z, INF_POINT, INF_POINT, auto]],
    ids=[""],
)
@pytest.mark.valid_from("Cancun")
def test_point_evaluation_precompile_calls(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    tx: Transaction,
    post: Dict,
):
    """
    Test calling the Point Evaluation Precompile with different call types, gas
    and parameter configuration:

    - Using CALL, DELEGATECALL, CALLCODE and STATICCALL.
    - Using correct and incorrect proofs
    - Using barely insufficient gas
    """
    blockchain_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=[tx])],
    )

test_point_evaluation_precompile_gas_tx_to(blockchain_test, precompile_input, call_gas, proof_correct)

Test calling the Point Evaluation Precompile directly as transaction entry point, and measure the gas consumption.

  • Using gas_limit with exact necessary gas, insufficient gas and extra gas.
  • Using correct and incorrect proofs
Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
@pytest.mark.parametrize(
    "call_gas",
    [
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS),
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS + 1),
        (Spec.POINT_EVALUATION_PRECOMPILE_GAS - 1),
    ],
    ids=["exact_gas", "extra_gas", "insufficient_gas"],
)
@pytest.mark.parametrize(
    "z,y,kzg_commitment,kzg_proof,versioned_hash,proof_correct",
    [
        [Z, 0, INF_POINT, INF_POINT, auto, True],
        [Z, 1, INF_POINT, INF_POINT, auto, False],
    ],
    ids=["correct_proof", "incorrect_proof"],
)
@pytest.mark.valid_from("Cancun")
def test_point_evaluation_precompile_gas_tx_to(
    blockchain_test: BlockchainTestFiller,
    precompile_input: bytes,
    call_gas: int,
    proof_correct: bool,
):
    """
    Test calling the Point Evaluation Precompile directly as
    transaction entry point, and measure the gas consumption.

    - Using `gas_limit` with exact necessary gas, insufficient gas and extra gas.
    - Using correct and incorrect proofs
    """
    start_balance = 10**18
    pre = {
        TestAddress: Account(
            nonce=0,
            balance=start_balance,
        ),
    }

    # Gas is appended the intrinsic gas cost of the transaction
    intrinsic_gas_cost = 21_000 + eip_2028_transaction_data_cost(precompile_input)

    # Consumed gas will only be the precompile gas if the proof is correct and
    # the call gas is sufficient.
    # Otherwise, the call gas will be consumed in full.
    consumed_gas = (
        Spec.POINT_EVALUATION_PRECOMPILE_GAS
        if call_gas >= Spec.POINT_EVALUATION_PRECOMPILE_GAS and proof_correct
        else call_gas
    ) + intrinsic_gas_cost

    fee_per_gas = 7

    tx = Transaction(
        ty=2,
        nonce=0,
        data=precompile_input,
        to=to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS),
        value=0,
        gas_limit=call_gas + intrinsic_gas_cost,
        max_fee_per_gas=7,
        max_priority_fee_per_gas=0,
    )

    post = {
        TestAddress: Account(
            nonce=1,
            balance=start_balance - (consumed_gas * fee_per_gas),
        )
    }

    blockchain_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=[tx])],
    )

test_point_evaluation_precompile_before_fork(blockchain_test, pre, tx)

Test calling the Point Evaluation Precompile before the appropriate fork.

Source code in tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
@pytest.mark.parametrize(
    "z,y,kzg_commitment,kzg_proof,versioned_hash",
    [[Z, 0, INF_POINT, INF_POINT, auto]],
    ids=["correct_proof"],
)
@pytest.mark.valid_at_transition_to("Cancun")
def test_point_evaluation_precompile_before_fork(
    blockchain_test: BlockchainTestFiller,
    pre: Dict,
    tx: Transaction,
):
    """
    Test calling the Point Evaluation Precompile before the appropriate fork.
    """
    precompile_caller_code = Op.SSTORE(
        Op.NUMBER,
        Op.CALL(
            Op.GAS,
            Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS,
            1,  # Value
            0,  # Zero-length calldata
            0,
            0,  # Zero-length return
            0,
        ),
    )
    precompile_caller_address = to_address(0x100)

    pre = {
        TestAddress: Account(
            nonce=0,
            balance=0x10**18,
        ),
        precompile_caller_address: Account(
            nonce=0,
            code=precompile_caller_code,
            balance=0x10**18,
        ),
    }

    def tx_generator() -> Iterator[Transaction]:
        nonce = 0  # Initial value
        while True:
            yield tx.with_nonce(nonce)
            nonce = nonce + 1

    iter_tx = tx_generator()

    FORK_TIMESTAMP = 15_000
    PRE_FORK_BLOCK_RANGE = range(999, FORK_TIMESTAMP, 1_000)

    # Blocks before fork
    blocks = [Block(timestamp=t, txs=[next(iter_tx)]) for t in PRE_FORK_BLOCK_RANGE]
    # Block after fork
    blocks += [Block(timestamp=FORK_TIMESTAMP, txs=[next(iter_tx)])]

    post = {
        precompile_caller_address: Account(
            storage={b: 1 for b in range(1, len(PRE_FORK_BLOCK_RANGE) + 1)},
            # The tx in last block succeeds; storage 0 by default.
        ),
        to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account(
            balance=len(PRE_FORK_BLOCK_RANGE),
        ),
    }

    blockchain_test(
        tag="point_evaluation_precompile_before_fork",
        pre=pre,
        post=post,
        blocks=blocks,
    )