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

Strengthen spec for montgomery_reduce() #292

Merged
merged 2 commits into from
Oct 31, 2024
Merged

Strengthen spec for montgomery_reduce() #292

merged 2 commits into from
Oct 31, 2024

Conversation

hanno-becker
Copy link
Contributor

@hanno-becker hanno-becker commented Oct 31, 2024

Based on: #289

Fixes #280

There are two uses of the generic Montgomery reduction montgomery_reduce() in the code base:

  • For modular multiplication with twiddles in fqmul()
  • For Montgomery reduction after accumulation in the base multiplication.

Those two uses require different specifications for montgomery_reduce(). However, there does not appear to be a mechanism in CBMC to register more than one contract.

To remedy, this commit moves the previous montgomery_reduce() into a static inline montgomery_reduce_generic(), and uses it in the definitions of montgomery_reduce() and fqmul(). The new montgomery_reduce() then makes bounds assumptions according to the use in base multiplication, while the call to montgomery_reduce_generic() provides stronger input bounds. Accordingly, the post-condition of montgomery_reduce() only guarantees coefficients < 3/2q, while fqmul() ensures < q.

The CBMC contracts an proofs for montgomery_reduce() and fqmul() are adjusted accordingly.

Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantically this looks good to me. Thanks for clearing this up.

Some docstring could be moved for consistency.

mlkem/reduce.c Show resolved Hide resolved
@rod-chapman
Copy link
Contributor

Question: why do montgomery_reduce() and fqmul() have the MLKEM_NAMESPACE macro applied to them? Surely, these functions are common to all parameter sets, so no need to adjust their name according to the value of MLKEM_K?

@hanno-becker
Copy link
Contributor Author

@rod-chapman Everything's prefixed by the K-dependent namespace to e.g. allow linking together multiple compilations of the lib with varying security levels. It's TBD if/how we want to avoid duplication of symbols for functions that don't depend on K.

@rod-chapman
Copy link
Contributor

To avoid symbol duplication, we might need to split the code into translation units that do depend on K, and those that don't. The latter would would be compiled exactly once.

There are two uses of the generic Montgomery reduction
`montgomery_reduce()` in the code base:
- For modular multiplication with twiddles in `fqmul()`
- For Montgomery reduction after accumulation in the base
  multiplication.

Those two uses require different specifications for
`montgomery_reduce()`. However, there does not appear
to be a mechanism in CBMC to register more than one contract.

To remedy, this commit moves the previous `montgomery_reduce()`
into a static inline `montgomery_reduce_generic()`, and uses it
in the definitions of `montgomery_reduce()` and `fqmul()`. The
new `montgomery_reduce()` then makes bounds assumptions according
to the use in base multiplication, while the call to
`fqmul()` provides stronger input bounds.
Accordingly, the post-condition of `montgomery_reduce()` only
guarantees coefficients < 3/2q, while `fqmul()` ensures < q.

The CBMC contracts an proofs for `montgomery_reduce()` and
`fqmul()` are adjusted accordingly.

Signed-off-by: Hanno Becker <[email protected]>
fqmul() is documented to require a 2nd input that's "< q/2"
in absolute value. This is the correct meaning when reading
q/2 as a _rational_ quotient, but it's not when q/2 is read
as round-down division: In that case, one would need to say
<= instead of <.

This commit avoids any confusion by rewriting the documentation
in terms of the integral quotient (q+1)/2.

Signed-off-by: Hanno Becker <[email protected]>
@hanno-becker
Copy link
Contributor Author

@rod-chapman We could do that, but it's a large restructuring that moves us further apart from the reference implementation. So unless we have a customer telling us about for simultaneous need for multiple security levels and deduplicated symbols, I don't think we should spend any time on this.

@hanno-becker hanno-becker added benchmark this PR should be benchmarked in CI and removed benchmark this PR should be benchmarked in CI labels Oct 31, 2024
@hanno-becker hanno-becker merged commit 10fd1dd into main Oct 31, 2024
86 checks passed
@hanno-becker hanno-becker deleted the fqmul_proof branch October 31, 2024 18:46
@rod-chapman
Copy link
Contributor

Agree on the above re: symbol duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
benchmark this PR should be benchmarked in CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CBMC: Strengthen spec for montgomery_reduce()
3 participants