Closed JohnLCaron closed 1 year ago
What do you think, @danwallach ?
Not sure if I know what all the values in
egklib/src/commonMain/kotlin/electionguard/core/GroupCommon.kt
should be.
What do you think, @danwallach ?
I suppose we could change from base64 to base16 for the constants. The reason I decided to go with base64 was that I thought base16 was wasteful particularly when you're doing JSON and you've got a million ballots. Getting to 6 bits per byte rather than 4 bits per byte is a significant savings.
For purposes of making it easier to cut-and-paste from the standard docs into GroupCommon.kt
, it makes sense to go with base16.
BTW, there's something else going on here worth noting: We've got a bunch of pre-computed constants. Things like b64Production4096P256MinusQ
is just $2^{256} - Q$ (i.e., the additive inverse of Q), which comes in handy for some equations. Similar things are going on for the Montgomery transformation, where I'm hanging on to $i-1$ so I can avoid having to recompute it. I know this smacks of premature optimization, but I was aiming to make as few HACL calls as possible, and these things helped with that.
Also worth noting here, we've got our own Base16.kt
and Base64.kt
classes which define extension functions on ByteArray
. At least hypothetically, this gives us identical behavior between native and JVM. If you wanted to change leading-zero behavior for a base-16 conversion, you'd do it in Base16.kt
.
Honestly, though, I thought one of the outcomes of the hashing discussion was that we'd want to hash the raw bytes without any conversion prior to hashing. That suggests that the only thing to do is to have a ByteArray method that strips leading zeros.
I have the failing test in ConstantsTest commented out for now. Waiting for 2.0 spec, and hopefullly some sample election records to test against. Also see Issue #210
this has been fixed in 2.0, see Primes4096 and Primes3072.
The productionGroup().constants.smallPrime and largePrime have leading 0's in their byte arrays when they are formed from the base64 string. (this is for JVM, havent tested on native).
Theres no problem with the mathematics, but the serialization (both proto and JSON) retain the leading 0's, which can make the verifier fail when testing that the constants are correct.
There are a number of ways to fix this, but I think a simple one is to use the base16 string instead of the base64. This has the added advantage of matching the spec, which specifies them in base16.
See electionguard/core/ConstantsTest.kt testConstants(). Output is: