Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject zero inputs in bls_modular_inverse() #3299

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions specs/deneb/polynomial-commitments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down