Intermittent 'attempt to subtract with overflow' panic
jnichols-pdx opened this issue · comments
Encountered an intermittent panic when calling glass_pumpkin::prime::strong_check():
thread 'main' panicked at 'attempt to subtract with overflow', /home/jnichols/.cargo/registry/src/github.com-1ecc6299db9ec823/glass_pumpkin-0.5.0/src/common.rs:176:25
trials
may be zero, so the subtraction in the range definition can underflow.
If I understand the Miller Rabin algorithm properly, then I think a larger issue lies in rewrite():
I think rewrite()
is trying to find r
(aka trials) and d
from the following line in the algorithm, as given on wikipedia:
write n as 2^{r}·d + 1 with d odd (by factoring out powers of 2 from n − 1)
However the current rewrite()
function simply returns trials = 0
and d = candidate - 1
(making d
always positive) whenever candidate
is odd.
Thanks for filing this. Will investigate and push out a fix