Skip to content

Commit

Permalink
Merge pull request #398 from JohnLCaron/jsonR
Browse files Browse the repository at this point in the history
First pass reading json output from eg-rust.
  • Loading branch information
JohnLCaron authored Oct 15, 2023
2 parents 5b5dc3c + e1c4c20 commit 3e3c9bd
Show file tree
Hide file tree
Showing 47 changed files with 1,403 additions and 14,679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class ElectionConfig(
val metadata: Map<String, String> = emptyMap(),
) {
init {
require(numberOfGuardians > 0) { "numberOfGuardians ${numberOfGuardians} <= 0" }
require(numberOfGuardians > 0) { "numberOfGuardians ${numberOfGuardians} <= 0" }
require(numberOfGuardians >= quorum) { "numberOfGuardians ${numberOfGuardians} != $quorum" }
}

Expand Down Expand Up @@ -66,6 +66,12 @@ data class ElectionConfig(
return result
}

fun show(): String = buildString {
appendLine("ElectionConfig '${configVersion}' numberOfGuardians=$numberOfGuardians, quorum=$quorum chainConfirmationCodes=$chainConfirmationCodes")
appendLine(" parameterBaseHash ${parameterBaseHash}")
appendLine(" electionBaseHash ${electionBaseHash}")
appendLine(" ElectionConstants ${constants}")
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ data class ElectionInitialized(
) {
init {
require(guardians.isNotEmpty()) { "empty guardians" }
require(guardians.size == this.config.numberOfGuardians) { "nguardians ${guardians.size} != ${this.config.numberOfGuardians}" }
require(guardians.size == this.config.numberOfGuardians) {
"ElectionInitialized nguardians ${guardians.size} does not match config= ${this.config.numberOfGuardians}"
}
}

fun jointPublicKey(): ElGamalPublicKey {
Expand All @@ -31,4 +33,14 @@ data class ElectionInitialized(
pairs.forEach { added[it.first] = it.second }
return this.copy(metadata = added)
}

fun show(): String = buildString {
appendLine("ElectionInitialized")
append("${config.show()}")
appendLine(" jointPublicKey ${jointPublicKey}")
appendLine(" extendedBaseHash ${extendedBaseHash}")
guardians.forEach {
appendLine(" ${it}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import electionguard.core.SchnorrProof
data class Guardian(
val guardianId: String,
val xCoordinate: Int, // use sequential numbering starting at 1; = i of T_i, K_i
val coefficientProofs: List<SchnorrProof> // the order is the same as coordinates, size = quorum
val coefficientProofs: List<SchnorrProof> // for each coordinate in polynomial Pi(x)
) {
init {
require(guardianId.isNotEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ interface ElementModQ : Element, Comparable<ElementModQ> {
/** Computes the additive inverse */
operator fun unaryMinus(): ElementModQ

/** Computes b^e mod q */
/** Computes b^e mod q. LOOK seems to be not ever used */
infix fun powQ(e: ElementModQ): ElementModQ

/** Finds the multiplicative inverse */
Expand Down
14 changes: 7 additions & 7 deletions egklib/src/commonMain/kotlin/electionguard/core/UInt256.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ fun ByteArray.toUInt256(): UInt256 {
return UInt256(this.normalize(32))
}

/** Make ByteArray have exactly [want] bytes by zero padding or removing leading zeros.
/** Make ByteArray have exactly [nbytes] bytes by zero padding or removing leading zeros.
* Throws an [IllegalArgumentException] if not possible. */
fun ByteArray.normalize(want: Int): ByteArray {
return if (size == want) {
fun ByteArray.normalize(nbytes: Int): ByteArray {
return if (size == nbytes) {
this
} else if (size > want) {
} else if (size > nbytes) {
// BigInteger sometimes has leading zeroes, so remove them
val leading = size - want
val leading = size - nbytes
for (idx in 0 until leading) {
if (this[idx].compareTo(0) != 0) {
throw IllegalArgumentException("ByteArray.normalize has $size bytes, only want $want, leading zeroes stop at $idx")
throw IllegalArgumentException("ByteArray.normalize has $size bytes, only want $nbytes, leading zeroes stop at $idx")
}
}
this.copyOfRange(leading, this.size)
} else {
val leftPad = ByteArray(want - size) { 0 }
val leftPad = ByteArray(nbytes - size) { 0 }
leftPad + this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class DecryptingTrusteeDoerre(
val group = compatibleContextOrFail(publicKey, keyShare)
// problem with this is now theres state; if trustee goes down it cant come back up and continue, if there are
// PartialDecryption not yet challenged, since there will be a different randomConstantNonce.
val randomConstantNonce = group.randomElementModQ(2) // random value u in Zq
private val randomConstantNonce = group.randomElementModQ(2) // random value u in Zq

init {
require(xCoordinate > 0)
Expand Down
44 changes: 44 additions & 0 deletions egklib/src/commonMain/kotlin/electionguard/json/ElectionHashes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package electionguard.json

import electionguard.core.UInt256
import kotlinx.serialization.Serializable

/* hashes.json
{
"h_p": "H(BAD5EEBFE2C98C9031BA8C36E7E4FB76DAC20665FD3621DF33F3F666BEC9AC0D)",
"h_m": "H(2FE7EA3C2E3C42F88647B4727254F960F1BB7B0D00A6A60C21D2F8984F5090B7)",
"h_b": "H(3800F226CB90A422D293BDC0F35D3C6767C2945D8C957F29CD0EAC2EAC6394B1)"
}
*/

@Serializable
data class ElectionHashesJsonR(
val h_p : UInt256JsonR,
val h_m : UInt256JsonR,
val h_b : UInt256JsonR,
)

data class ElectionHashes(
val Hp : UInt256,
val Hm : UInt256,
val Hb : UInt256,
)

fun ElectionHashesJsonR.import() = ElectionHashes(
this.h_p.import(),
this.h_m.import(),
this.h_b.import(),
)

/* hashes_ext.json
{
"h_e": "H(B1321998DB34667B3BD17021665D38274510BE53975496DA2EF3CBB38E799869)"
}
*/

@Serializable
data class ElectionHashesExtJsonR(
val h_e : UInt256JsonR,
)

fun ElectionHashesExtJsonR.import() = this.h_e.import()
114 changes: 114 additions & 0 deletions egklib/src/commonMain/kotlin/electionguard/json/ElectionManifest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package electionguard.json

import kotlinx.serialization.Serializable

/* election_manifest.jcon
{
"label": "General Election - The United Realms of Imaginaria",
"contests": [
{
"label": "For President and Vice President of The United Realms of Imaginaria",
"selection_limit": 1,
"options": [
{
"label": "Thündéroak, Vâlêriana D.\nËverbright, Ålistair R. Jr.\n(Ætherwïng)"
},
{
"label": "Stârførge, Cássánder A.\nMøonfire, Célestïa L.\n(Crystâlheärt)"
}
]
},
{
"label": "Minister of Arcane Sciences",
"selection_limit": 1,
"options": [
{
"label": "Élyria Moonshadow\n(Crystâlheärt)"
},
{
"label": "Archímedes Darkstone\n(Ætherwïng)"
},
{
"label": "Seraphína Stormbinder\n(Independent)"
},
{
"label": "Gávrïel Runëbørne\n(Stärsky)"
}
]
},
],
"ballot_styles": [
{
"label": "Smoothstone County Ballot",
"contests": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
},
{
"label": "Silvërspîre County Ballot",
"contests": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
11
]
}
]
}
*/

@Serializable
data class ElectionManifestJsonR(
val label : String,
val contests : List<ManifestContestJsonR>,
val ballot_styles : List<BallotStyleJsonR>,
) {
override fun toString(): String {
return "ElectionManifestJsonR(label='$label'\n contests=$contests\n ballot_styles=$ballot_styles)"
}
}

@Serializable
data class ManifestContestJsonR(
val label : String,
val selection_limit: Int,
val options : List<ManifestOptionJsonR>,
) {
override fun toString(): String {
return "\n ManifestContestJsonR(label='$label', selection_limit=$selection_limit, options=$options)\n"
}
}

@Serializable
data class ManifestOptionJsonR(
val label : String,
) {
override fun toString(): String {
return "\n ManifestOptionJsonR(label='$label')"
}
}

@Serializable
data class BallotStyleJsonR(
val label : String,
val contests : List<Int>,
) {
override fun toString(): String {
return "\n BallotStyleJsonR(label='$label', contests=$contests)\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package electionguard.json

import electionguard.ballot.ElectionConstants
import electionguard.core.Base64.fromBase64
import kotlinx.serialization.Serializable

/* election_parameters.jcon
{
"fixed_parameters": {
"ElectionGuard_Design_Specification": {
"Official": {
"version": [
2,
0
],
"release": "Release"
}
},
"generation_parameters": {
"q_bits_total": 256,
"p_bits_total": 4096,
"p_bits_msb_fixed_1": 256,
"p_middle_bits_source": "ln_2",
"p_bits_lsb_fixed_1": 256
},
"p": "base64://////////////////////////////////////////+xchf30c95q8njs5gD8vavQPNDJnKYti2KDRdbi6r6K+e4diBt66yYVZVS+0r6GxDtLq41wTghRCdXOykRabglPpbKFiJK6MUay9oRMXw4frnqm8OxNmA7JW+g7HZX90tyzoexnWVIyvXfpr04MDJIZV+hhy8g46LaLZfFDz/Vxgf0yhH7W/uQYQ0w+I/lUaLuVp1t/B76FX0uI94UALOWF0YHdduJkOXJQ+cytHnNM8zMTmWTk1FMTBoeC9HWCV0lZpszNWSjN2qcf4peFI6CB022AVz+eqMMSApUFzUNLJVdUXmx4XudrjE822xgbLEHj3NdGy2zG19QtRhQZMGLTRYts7NlhT11mKGVGuJz7lVwtsaPlpg0ltTm0zCviJtEoCVUcxzcjqFyk9Eiik75jW9Rd/vPB1UmilwflTi5gmGv/URrHKPPXpIiuIxm08VCIYPtyZQhCQu7Fvrz2UnyNuArIM7ohrkFwSjVPQvS+WITYxlq9QMCAGDkmQg5GgxXM5uivrp9BSrFthzE6SB87y8M4tc3OVjXYiZYkERXRPtfLaS3UQBYktNWiQ3v6crZudS3E+BhYqLY/dDfL9YI//////////////////////////////////////////8=",
"q": "base64://///////////////////////////////////////0M=",
"r": "base64:AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC8sXIX99HPeavJ47OYA/L2r0DzQyZymLYtig0XW4urhXro9CgWVBiAbGKw6jY1Wjpz4MdBmFv2oOMTAXm/LwtD4zrYYpI4YbjJ92jEFpUZYAutBgk/lksn4C2GgxIxqRYN5I9NpT2KteaeOGtpS+wa5yLUdXkknVQkdnxcM7kVHgfFwR0QasRG0zC0fbWdNS5HpTFX3gRGGQD2/jYNuJffUxbYfJSucdrQvoS2R8S8+BjCOi1Ou1PHAqXIBi0Z9em1AzqU9/9zL1QSlxKGnZe4yWxBKSGp2GeXcPSZoEHCl8/3nUyRSetsr2e56j3FY9ll86rRN3/yLenD5iBo3Q7WFRw3tPdGNMK9CdqRL9WZ9DM6jSzABWJ9yje61D5ko5YxGcC/40gQoh7nz8Qh1TOYy8epWzv1heWgS3kOL+H+m8Jk/agQn2RUoIL177LzfqI3qinfMg1uqGDEGpBUzNJIdsYlP2Z7+wE5tVMf8wGJlhIC/SsNVadScsf9czQ/eJm8oLNqTEcKZKAJJEyE53zrySQX1bsTvxgWfYAz62xN14ef1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kf1Kg==",
"g": "base64:NgNv7SFPO1DcVm06MS/kEx/uHCvObQLqObR3rAX3+IXzjP53p+Raz0ApEUxNepv+BYvy+ZXSR5092mGP/ZENPEI2qyz914OlAW90Zc9Zu/RdJKIvEw8tBP6TstWLucHR0n/JoX0q9Jp3nz/73KIpAMFCAu5smWFgNL41y83T57t5lq3+U0tjzKQeIf9dx3jrsbhsU7++mZh9euoHViN/tAkiE5+Qpi8qqNmtNN/3meM8hXpkaNABrPO2gduH3EJCdV4qxaUCfbgZhPAzxNF4Nx8nPbtPzqHmKMI+UnWbx3ZXKANc6ia0TEmmVmaImCCkXDPdN+pKHQDLYjBc1UG+HoqSaFoHASsaIKdGw1kaLbOBUADSqsz+Q9xJ6CjB7XOHRmr9jkvxk1WTsqRC7sJxxQrTn3M3l6HqEYAqJVeRZTRmKmt+mp5EmiTIz/gJ55pNgG62gRGTMObFeYXjmyALSJNjn9/epJ92rRrNmX66E2V1QeeexXQ35QTtqd0BEGFRbGQ/sw1tWK/M0otz/top7BKwGl64Y5mlk6nV9FDeOcuSlixexpJTSNtU0Sj9mcFLRX+IPsIBEqdaagWB09gKO07wnshvlVL/2hZT8TOqJTSYOm8xsO5Gl5Naax6i91uF5+uhUbpIYJTWhyKwVGM/7FHKPymzHnfjF7F4trnYrg8="
},
"varying_parameters": {
"n": 5,
"k": 3,
"date": "11/29/2023",
"info": "info",
"ballot_chaining": "Prohibited"
}
}
*/

@Serializable
data class ElectionParametersJsonR(
val fixed_parameters : FixedParametersJsonR,
val varying_parameters : VaryingParameters,
)

fun ElectionParametersJsonR.import() = ElectionParameters(
this.fixed_parameters.import(),
this.varying_parameters,
)

@Serializable
data class FixedParametersJsonR(
val p: String,
val q: String,
val r: String,
val g: String,
)

fun FixedParametersJsonR.import() : ElectionConstants {
return ElectionConstants( "FixedParametersJsonR",
this.p.substring(7).fromBase64()!!, // strip off "base64:"
this.q.substring(7).fromBase64()!!,
this.r.substring(7).fromBase64()!!,
this.g.substring(7).fromBase64()!!,
)
}

@Serializable
data class VaryingParameters(
val n: Int,
val k: Int,
val date: String,
val info: String,
val ballot_chaining: String,
)

data class ElectionParameters(
val electionConstants : ElectionConstants,
val varyingParameters : VaryingParameters,
)
Loading

0 comments on commit 3e3c9bd

Please sign in to comment.