# SPDX-License-Identifier: Apache-2.0 # Copyright 2026 Scienting # # Source Code: https://git.scient.ing/papers/paperspec # See the LICENSE.md file for full license terms. """The ECVRF of RFC 9381 over Edwards25519 (paperspec.vrf).""" import pytest from hypothesis import given, settings, strategies as st from conftest import vector_cases from helpers_lib2 import PROPS from paperspec import crypto, vrf CASES = vector_cases("vrf") PROVABLE = [c for c in CASES if "secret" in c] BASE_POINT = bytes.fromhex("58" + "66" * 31) IDENTITY_ENCODING = bytes([1]) + bytes(31) def hexes(case): """The public key, input and proof of a case as bytes.""" return ( bytes.fromhex(case["public"]), bytes.fromhex(case["alpha"]), bytes.fromhex(case["proof"]), ) @pytest.mark.parametrize("case", PROVABLE, ids=lambda c: c["id"]) def test_prove_reproduces_the_vector_proof(case): secret = bytes.fromhex(case["secret"]) public, alpha, proof = hexes(case) assert vrf.public_key(secret) == public == crypto.ed25519_public(secret) assert vrf.prove(secret, alpha) == proof @pytest.mark.parametrize("case", CASES, ids=lambda c: c["id"]) def test_verify_returns_the_vector_output_or_nothing(case): public, alpha, proof = hexes(case) beta = vrf.verify(public, alpha, proof) assert (beta.hex() if beta else None) == case["beta"] @pytest.mark.parametrize("case", [c for c in CASES if c["beta"]], ids=lambda c: c["id"]) def test_proof_to_hash_gives_the_vector_output(case): assert vrf.proof_to_hash(bytes.fromhex(case["proof"])).hex() == case["beta"] def test_rfc_9381_example_16_is_reproduced(): case = next(c for c in CASES if c["id"] == "rfc9381-example-16") assert case["proof"].startswith("8657106690b5526245a92b003bb079cc") assert case["beta"].startswith("90cf1df3b703cce59e2a35b925d41116") def test_the_vectors_include_proofs_that_must_not_verify(): assert {c["id"] for c in CASES if c["beta"] is None} == { "tampered-challenge", "other-input", } def test_verify_rejects_a_proof_for_another_input_or_key(): case = PROVABLE[0] public, alpha, proof = hexes(case) assert vrf.verify(public, alpha + b"x", proof) is None assert vrf.verify(vrf.public_key(bytes(32)), alpha, proof) is None @pytest.mark.parametrize("position", [0, 20, 31, 32, 47, 48, 70, 79]) def test_verify_rejects_a_proof_with_any_byte_changed(position): public, alpha, proof = hexes(PROVABLE[0]) bad = bytearray(proof) bad[position] ^= 0x01 assert vrf.verify(public, alpha, bytes(bad)) is None @pytest.mark.parametrize("length", [0, 79, 81]) def test_verify_rejects_a_proof_of_the_wrong_length(length): public, alpha, proof = hexes(PROVABLE[0]) assert vrf.verify(public, alpha, (proof + bytes(2))[:length]) is None assert vrf.proof_to_hash((proof + bytes(2))[:length]) is None @pytest.mark.parametrize( "public", [b"", bytes(31), b"\xff" * 32, IDENTITY_ENCODING, bytes(32)], ids=["empty", "short", "no-point", "identity", "order-four-point"], ) def test_verify_rejects_a_public_key_that_is_no_usable_point(public): _, alpha, proof = hexes(PROVABLE[0]) assert vrf.verify(public, alpha, proof) is None @pytest.mark.parametrize( "args", [(None, b"", bytes(80)), (bytes(32), None, bytes(80)), (bytes(32), b"", None)], ) def test_verify_never_raises_on_wrong_types(args): assert vrf.verify(*args) is None def test_proof_whose_scalar_is_not_below_the_group_order_is_malformed(): _, _, proof = hexes(PROVABLE[0]) too_big = proof[:48] + vrf.Q.to_bytes(32, "little") assert vrf.proof_to_hash(too_big) is None assert vrf.decode_proof(too_big) is None def test_proof_whose_point_is_invalid_is_malformed(): _, _, proof = hexes(PROVABLE[0]) assert vrf.decode_proof(b"\xff" * 32 + proof[32:]) is None def test_decode_proof_splits_point_challenge_and_scalar(): _, _, proof = hexes(PROVABLE[0]) gamma, c, s = vrf.decode_proof(proof) assert vrf.point_to_string(gamma) == proof[:32] assert c == int.from_bytes(proof[32:48], "little") assert s == int.from_bytes(proof[48:], "little") < vrf.Q def test_the_base_point_encodes_as_in_rfc_8032(): assert vrf.point_to_string(vrf.G) == BASE_POINT assert vrf.string_to_point(BASE_POINT) is not None assert vrf.public_key(bytes(32)) == crypto.ed25519_public(bytes(32)) def test_string_to_point_refuses_wrong_lengths_and_noncanonical_coordinates(): assert vrf.string_to_point(bytes(31)) is None assert vrf.string_to_point(bytes(33)) is None p_as_y = vrf.P.to_bytes(32, "little") assert vrf.string_to_point(p_as_y) is None def test_string_to_point_refuses_the_sign_bit_of_a_zero_coordinate(): positive = vrf.string_to_point(IDENTITY_ENCODING) assert positive is not None assert positive[0] == 0 negative = bytes([1]) + bytes(30) + bytes([0x80]) assert vrf.string_to_point(negative) is None def test_about_half_of_all_y_coordinates_encode_no_point(): found = [ vrf.string_to_point(y.to_bytes(32, "little")) is not None for y in range(2, 66) ] assert 16 < sum(found) < 48 @given(st.binary(min_size=32, max_size=32)) @PROPS def test_a_decoded_point_encodes_back_to_the_same_bytes(encoded): point = vrf.string_to_point(encoded) if point is not None: assert vrf.point_to_string(point) == encoded @given(st.binary(min_size=32, max_size=32), st.binary(max_size=40)) @settings(PROPS, max_examples=12) def test_a_proof_verifies_to_its_hash_and_is_deterministic(secret, alpha): public = vrf.public_key(secret) proof = vrf.prove(secret, alpha) beta = vrf.verify(public, alpha, proof) assert len(proof) == 80 assert beta is not None assert len(beta) == 64 assert beta == vrf.proof_to_hash(proof) assert proof == vrf.prove(secret, alpha) assert vrf.verify(public, alpha + b"\x00", proof) is None def test_encode_to_curve_is_deterministic_and_depends_on_the_input(): key = vrf.public_key(bytes(32)) first = vrf.encode_to_curve(key, b"input") assert vrf.point_to_string(first) == vrf.point_to_string( vrf.encode_to_curve(key, b"input") ) assert vrf.point_to_string(first) != vrf.point_to_string( vrf.encode_to_curve(key, b"other") ) assert vrf.point_to_string(first) != vrf.point_to_string( vrf.encode_to_curve(vrf.public_key(bytes(31) + b"\x01"), b"input") ) def test_encode_to_curve_gives_up_after_256_attempts(monkeypatch): monkeypatch.setattr(vrf, "string_to_point", lambda s: None) with pytest.raises(ValueError, match="no curve point found"): vrf.encode_to_curve(bytes(32), b"") def test_encode_to_curve_skips_points_the_cofactor_sends_to_the_identity(monkeypatch): monkeypatch.setattr(vrf, "string_to_point", lambda s: vrf.IDENTITY) with pytest.raises(ValueError, match="no curve point found"): vrf.encode_to_curve(bytes(32), b"")