Merge pull request #3299 from asn-d6/inv_no_inputs

Reject zero inputs in bls_modular_inverse()
This commit is contained in:
Hsiao-Wei Wang 2023-03-20 14:18:58 +08:00 committed by GitHub
commit 334601c032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -252,10 +252,11 @@ def compute_challenge(blob: Blob,
```python
def bls_modular_inverse(x: BLSFieldElement) -> BLSFieldElement:
"""
Compute the modular inverse of x
i.e. return y such that x * y % BLS_MODULUS == 1 and return 0 for x == 0
Compute the modular inverse of x (for x != 0)
i.e. return y such that x * y % BLS_MODULUS == 1
"""
return BLSFieldElement(pow(x, -1, BLS_MODULUS)) if x != 0 else BLSFieldElement(0)
assert (int(x) % BLS_MODULUS) != 0
return BLSFieldElement(pow(x, -1, BLS_MODULUS))
```
#### `div`

View File

@ -215,6 +215,29 @@ def test_verify_blob_kzg_proof_incorrect_proof(spec):
assert not spec.verify_blob_kzg_proof(blob, commitment, proof)
@with_deneb_and_later
@spec_test
@single_phase
def test_bls_modular_inverse(spec):
"""
Verify computation of multiplicative inverse
"""
rng = random.Random(5566)
# Should fail for x == 0
expect_assertion_error(lambda: spec.bls_modular_inverse(0))
expect_assertion_error(lambda: spec.bls_modular_inverse(spec.BLS_MODULUS))
expect_assertion_error(lambda: spec.bls_modular_inverse(2 * spec.BLS_MODULUS))
# Test a trivial inversion
assert 1 == int(spec.bls_modular_inverse(1))
# Test a random inversion
r = rng.randint(0, spec.BLS_MODULUS - 1)
r_inv = int(spec.bls_modular_inverse(r))
assert r * r_inv % BLS_MODULUS == 1
@with_deneb_and_later
@spec_test
@single_phase