hyperledger-labs / agora-glass_pumpkin

agora-glass_pumpkin

Home Page:https://wiki.hyperledger.org/display/labs/Agora

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Intermittent 'attempt to subtract with overflow' panic

jnichols-pdx opened this issue · comments

https://github.com/mikelodder7/glass_pumpkin/blob/412a51b8235187c980a0194b6eae44ff02ac89aa/src/common.rs#L176

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():

https://github.com/mikelodder7/glass_pumpkin/blob/412a51b8235187c980a0194b6eae44ff02ac89aa/src/common.rs#L192-L202

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