From d197ed1451eb3d441438da5bc6d8b27d9e6c5776 Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Mon, 26 Sep 2022 18:57:00 +0300 Subject: [PATCH] EIP4844: Introduce bytes_to_bls_field() helper Improves separation between BLS cryptography and Ethereum SSZ logic. Now the BLS library just implements bytes_to_bls_field(). Then hash_to_bls_field() does the Ethereum SSZ magic and calls bytes_to_bls_field(). --- specs/eip4844/polynomial-commitments.md | 11 +++++++++++ specs/eip4844/validator.md | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/specs/eip4844/polynomial-commitments.md b/specs/eip4844/polynomial-commitments.md index 66a2e9e11..45f942b0f 100644 --- a/specs/eip4844/polynomial-commitments.md +++ b/specs/eip4844/polynomial-commitments.md @@ -17,6 +17,7 @@ - [`reverse_bits`](#reverse_bits) - [`bit_reversal_permutation`](#bit_reversal_permutation) - [BLS12-381 helpers](#bls12-381-helpers) + - [`bytes_to_bls_field`](#bytes_to_bls_field) - [`bls_modular_inverse`](#bls_modular_inverse) - [`div`](#div) - [`g1_lincomb`](#g1_lincomb) @@ -111,6 +112,16 @@ def bit_reversal_permutation(l: Sequence[T]) -> Sequence[T]: ### BLS12-381 helpers +#### `bytes_to_bls_field` + +```python +def bytes_to_bls_field(b: Bytes32) -> BLSFieldElement: + """ + Convert bytes to a BLS field scalar. The output is not uniform over the BLS field. + """ + return int.from_bytes(b, "little") % BLS_MODULUS +``` + #### `bls_modular_inverse` ```python diff --git a/specs/eip4844/validator.md b/specs/eip4844/validator.md index f624c5157..a1faffa25 100644 --- a/specs/eip4844/validator.md +++ b/specs/eip4844/validator.md @@ -96,7 +96,7 @@ def hash_to_bls_field(x: Container) -> BLSFieldElement: Compute 32-byte hash of serialized container and convert it to BLS field. The output is not uniform over the BLS field. """ - return int.from_bytes(hash(ssz_serialize(x)), "little") % BLS_MODULUS + return bytes_to_bls_field(hash(ssz_serialize(x))) ``` ### `compute_powers`