Hacker News new | ask | show | jobs
by Someone 1339 days ago
Relevant code (from https://gitlab.com/swisspost-evoting/crypto-primitives/crypt...):

  ๐‘ ๐‘ โ† 8530092
     โ–ท I.e. 0b100000100010100010101100: for all primes ๐‘ โ‰ค 23,
       TestBit(๐‘ ๐‘, ๐‘) = true
  if ๐‘› โ‰ค 23 then
    โ–ท Quick check for small values, simpler and faster
      than testing equality on each option
    return TestBit(๐‘ ๐‘, ๐‘›)
  end if
  if ยฌTestBit(๐‘›, 0) then return โŠฅ
    โ–ท I.e. ๐‘› is even and, as per line 2, ๐‘› > 2 thus composite
  end if
And it indeed is a bug. The function guarantees to return false โ€œif the number can be determined to be compositeโ€ and true for all primes, so it should err in only one way.

I would further improve the code by having it shortcut for all primes smaller than 31 (adding 29 and 31) or 63.

2 comments

Specifically a bug in that constant sp, which should have a 1 in bit 19 like it does for the other primes โ‰ค 23.

This is step 1 of the algorithm they're using: https://en.wikipedia.org/wiki/Baillie%E2%80%93PSW_primality_...

Let me repeat if I understand it right:

* The function will return True for all primes.

* The function will return False if the number is detected as a composite by some tests.

* The function can return True or False for all other numbers.

* For numbers<=23 they implemented a shortcut using a lookup table which is implemented as bits in a number.

* The bit for number 19 is wrong. It returns false for a prime number which violates "return True for all prime numbers".

This is indeed quite shoddy programming for such an essential and easily testable piece of software.

Itโ€™s not easily testable if itโ€™a pseudocode that hasnโ€™t been implemented yet.