top | item 33200271

Swiss evoting system – IsProbablePrime is incorrect for input 19

36 points| herr_gurke | 3 years ago |gitlab.com

48 comments

order

Someone|3 years 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.

threatripper|3 years ago

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.

nairboon|3 years ago

For context: Switzerland doesn't have evoting. This is just some big company trying to re-sell a evoting system to the government. It has been in the news a few times, due to software and cryptographic quality issues.

greyw|3 years ago

It's a state owned company so it is kinda selling to itself.

crisbal_|3 years ago

> big company

Isn't it the Swiss post?

benevol|3 years ago

> Switzerland doesn't have evoting.

And given what they show off, they should abstain from it for the next 1000 years.

tromp|3 years ago

> since 19 is a prime number, if I am not mistaken.

That is one modest reviewer!

OJFord|3 years ago

Well, it would be embarrassing to have a slow morning and get that wrong. Always build in a way out!

raverbashing|3 years ago

I would be very weary of changing such constants as this

19 should have been tested by a lookup table, there's no need to apply such heavy test to it

However, by changing that constant (if not properly verified) I'd worry it might change the primality test for some classes of numbers. Where this might be later manipulated to produce a weak key

mkl|3 years ago

I think you're misunderstanding the constant: it is only used for small numbers (≀ 23), which are tested with a lookup table, and the constant is itself that lookup table. The test for small numbers is literally just checking a bit, so is not heavy at all.

They intended to do 2^2+2^3+2^5+2^7+2^11+2^13+2^17+2^19+2^23 = 9054380, but they accidentally left out 2^19, and got 2^2+2^3+2^5+2^7+2^11+2^13+2^17+2^23 = 8530092.

You can see the problematic Algorithm 4.16 on p31 here: https://gitlab.com/swisspost-evoting/crypto-primitives/crypt...

omega3|3 years ago

Could someone explain why they've used a constant sp instead of hardcoding the primes? How was it created, did someone manually hardcoded the bits then converted into a number?

gsk22|3 years ago

Can someone explain how such a simple and easily-testable bug existed in a seemingly-important system like this?

I don't know much about Swiss e-voting, but seems even the most brain-dead unit test of the IsProbablePrime function should have caught this.

sschueller|3 years ago

It is brain dead but this is old code from what I recall. We do not have e-voting at this time although the Post which outsourced this first nightmare of a version is still trying to push for it.

I highly doubt we will see e-voting here until there is a verifiable proof that it is verifiable. There was a lot of bad press about this and people don't trust this crap.

They tried a similar thing for E-ID which was supposed to be built by some private cooperation and run on some centralized servers. The people voted against it and now the government has done the right thing and is building an E-ID system that is decentralized and government run. It still has some quirks but it's going in the right direction.

The sad thing is for the governments first version (outsource to private industry) their claim at the poles was that it would take many years for an E-ID if we don't do it this way. Now only 1 year later we have a very good proposal. There is too much corporate interest pushing around pawns in Government at this time even in a direct democracy like Switzerland.

herr_gurke|3 years ago

Im not sure if really the simplest test would catch it. You would need to go over n primes and check them, but you might always finish too early.

There is also a question of impact - i think that 19 does not really cause any harm there.

mkl|3 years ago

It's not really easily testable, because the bug is in pseudocode which cannot be executed.

simonmales|3 years ago

As humans we do our best, and try to learn from our mistakes.

amelius|3 years ago

Perhaps the word "probable" has something to do with it?