-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Bignum: Montgomery multiplication from bignum prototype #6083
Changes from 1 commit
82d3f1e
71f4b0d
90c426b
7e655f7
268f96b
0cc7865
2a65b85
659c84a
79b70f6
f334d96
9384284
40d2294
4641ec6
f88b47e
2523791
958fd3d
f0ffb15
7259463
b2c06f4
ecbb124
d932de8
b496486
5dd97e6
f0c8a8c
9354990
ed43c6c
630110a
5eefc3d
f0b2231
9339f05
b0fb17a
1b2947a
eceb4cc
a043aeb
42dfac6
1135b20
67c9247
2b17792
1feb5ac
50c477b
be7209d
e2159f2
359feb0
818d992
2701dea
b7438d1
17f1fdc
dbc1561
c71ca0c
5c0e810
3bd7bc3
f2b3818
ea45c1d
b0b77e1
4782823
c573882
119eae2
4386ead
6da3a3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,6 +245,14 @@ mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N ); | |
/** | ||
* \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) | ||
* | ||
* \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs == | ||
* \p B_limbs) but may not overlap any parameters otherwise. | ||
* | ||
* \p A, \p B and \p N must not alias or overlap each other in any way, even | ||
* if \p AN_limbs == \p B_limbs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a strange requirement: why would it be a problem that an input overlaps an input? (Unless we plan to add Here I'd expect Note: if we allow We could state that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, you are right - I must have misread the code when determining the restrictions. I've updated the text, and added a test for A aliased to B and X (which covers A and B only - it doesn't seem worth having yet another test for that) |
||
* | ||
* \p A and \p B must be in canonical form: that is, <= \p N. | ||
* | ||
* \param[out] X The destination MPI, as a little-endian array of | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* length \p AN_limbs. | ||
* On successful completion, X contains the result of | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably
The documentation of N states that it may not alias X. Our general rule is that outputs may not alias moduli.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the function does actually work if X aliases N - there are already test cases for that. So in this case the documentation for N is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the documentation should state what we are promising. The implementation might actually do more than that, but that shouldn't be a problem.
In this case, I think it makes sense not to promise that X can alias N as it doesn't make much sense. (eg. in the modulus structure, the value of the modulus is const. Also, the post condition on X is that it is less than N, which in the case of aliasing is impossible in a tight interpretation and still confusing when we try to interpret it well.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an output parameter to alias to an input parameter means the storage of the input parameter can be overwritten to be used for the output - the input value doesn't matter (whereas for two input parameters to alias, their values must be identical).
As long as
core
is just used internally, I agree with you, that it's unlikely that we will want to aliasX
andN
. However, there's nothing to stop a library user calling these functions, and they may have their own reasons to minimise memory use and overwriteN
with the output value, so I see no harm in documenting that this is safe (and, of course, testing it!).Given that a temporary working area is supplied to this function, this kind of "over promise" (beyond what we need and currently expect) is unlikely to constrain any future implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is: it's an internal function. Only the legacy bignum API is public and must obey its promises until the next major version change.
But because it's an internal function, we can make high-reaching promises if we want: we can rescind them at any time, we just need to check that it doesn't break our own code.