-
Notifications
You must be signed in to change notification settings - Fork 294
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
Some concerns regarding the overflows in modL function #187
Comments
Thanks for the analysis, I believe you're correct! It should be replaced with division. I'll fix it tomorrow morning. |
It seems to me that it array x maxes out at 20 bits due to this reduction of Line 778 in 1b61c87
But I'm half-asleep, so I'll recheck tomorrow (even if so, still worth fixing if modL is to be used externally). |
Here's a test with maximal values for each input:
The results are:
If you comment-out |
Hi Dmitry, Thanks for your quick replies, sorry for the delay. Very helpful of you to put together this test! How about this slightly modified variant of the test, here h is reduced from the start (i.e. reduce(h) does not change h), but some components of h are at max number of set bits (i.e. at 255): instead of line
do
then the output will be:
|
Oh, thanks! Also, my test missed that |
Looks good, thanks! Agree, for the rest of the modL it should be ok to have the binary operations. I will look through the modL a bit more, but hope it's all good now. |
Awesome, thanks a lot! I'm releasing the update soon. |
Hey Dmitry,
I have some troubles convincing myself that the
modL
function won't suffer from overflows. If you have any references to a more detailed description of modL that would be really helpful! In particular I am concerned with its use here:tweetnacl-js/nacl.js
Line 788 in 1b61c87
Two arrays h and d get multiplied to produce x.
Both h and d are
Uint8Array(64)
with elements at most 8 bits each and only the first 32 elements non-zero, both elements were reducedmod L
.The resulting x is of type
Float64Array(64)
.I believe, that the number of bits in each element of the array x is maxed at the following values:
[16, 17, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 20, 20, 20, 20, 20, 20, 20, 20, 19, 19, 19, 19, 18, 18, 17, 16, 0]
, i.e. the bit length|x[0]| <= 16
,|x[1]| <= 17
, etc.Now, the modL function is called on x. And in this line
tweetnacl-js/nacl.js
Line 729 in 1b61c87
|x[j]| > 32
, since|16 * x[i] * L[j - (i - 32)]| <= 4 + 21 + 8 = 33
.But during the bit operation on the next line:
tweetnacl-js/nacl.js
Line 730 in 1b61c87
(x[j] + 128)
will get converted to a 32-bits signed integer (according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators, Section Bitwise shift operators). This will act as expected only if the length of the number being shifted is at most 32 bits, but why now will it be the case?The text was updated successfully, but these errors were encountered: