Skip to content

Commit

Permalink
Merge pull request #319 from danwallach/baux0
Browse files Browse the repository at this point in the history
Cast ballots, Baux0
  • Loading branch information
JohnLCaron committed Jul 8, 2023
2 parents ee2f5d1 + 1001abe commit c0be2ea
Show file tree
Hide file tree
Showing 221 changed files with 65,578 additions and 125,304 deletions.
27 changes: 21 additions & 6 deletions docs/JsonSerializationSpec1.9.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🗳 Election Record JSON serialization (proposed specification)

draft 6/26/2023
draft 7/8/2023

<!-- TOC -->
* [🗳 Election Record JSON serialization (proposed specification)](#-election-record-json-serialization-proposed-specification)
Expand Down Expand Up @@ -57,6 +57,9 @@ data class ElectionConfigJson(
val parameter_base_hash: UInt256Json, // Hp
val manifest_hash: UInt256Json, // Hm
val election_base_hash: UInt256Json, // Hb
val baux0: ByteArray, // B_aux,0 from eq 59,60
val device: String, // the device information from eq 61, and section 3.7
)
````

Expand All @@ -65,13 +68,15 @@ Example:
````
{
"config_version": "v2.0",
"number_of_guardians": 5,
"number_of_guardians": 3,
"quorum": 3,
"election_date": "date",
"jurisdiction_info": "juris",
"election_date": "2023-07-08T10:03:21.866816846",
"jurisdiction_info": "N/A",
"parameter_base_hash": "AB91D83C3DC3FEB76E57C2783CFE2CA85ADB4BC01FC5123EEAE3124CC3FB6CDE",
"manifest_hash": "837F1489FB799C6B3065A8A8411A5F672AD50700ED2B416C7E9789FA6143C818",
"election_base_hash": "2F43AF7A46973482884752A6D1B027087AD795027FC025094E4BAABBABE60F22"
"manifest_hash": "E69B574C48087CF4E4914C3C4CDBBCB9368255F9373EFCF9EE923AF6B23C7CD2",
"election_base_hash": "C7C5EC51E7CB411F4CDCEDF891203B1B6A18DEF7178B1584A30F8578C611801D",
"baux0": [ 100, 101, 118, 105, 99, 101, 32, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110 ],
"device": "device information"
}
````

Expand Down Expand Up @@ -377,6 +382,7 @@ Example:
data class EncryptedTallyJson(
val tally_id: String,
val contests: List<EncryptedTallyContestJson>,
val cast_ballot_ids: List<String>,
)
@Serializable
Expand Down Expand Up @@ -416,6 +422,15 @@ Example:
]
},
...
"cast_ballot_ids": [
"id1343738539",
"id-939995991",
"id-1698682164",
"id-1254440059",
"id287012309",
"id-712320552",
...
]
}
````

Expand Down
40 changes: 21 additions & 19 deletions docs/ProtoSerializationSpec1.9.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🗳 Election Record KMP serialization (proposed specification)

draft 6/25/2023
draft 7/8/2023

**Table of Contents**

Expand Down Expand Up @@ -218,19 +218,21 @@ draft 6/25/2023

#### message ElectionConfig

| Name | Type | Notes |
|---------------------|-----------------------|-----------|
| spec_version | string | "v2.0.0" |
| constants | ElectionConstants | |
| number_of_guardians | uint32 | n |
| quorum | uint32 | k |
| election_date | string | k |
| jurisdiction_info | string | k |
| parameter_base_hash | UInt256 | Hp |
| manifest_hash | UInt256 | Hm |
| election_base_hash | UInt256 | He |
| manifest_bytes | bytes | |
| metadata | map\<string, string\> | arbitrary |
| Name | Type | Notes |
|---------------------|-----------------------|----------------------------------------------------|
| spec_version | string | "v2.0.0" |
| constants | ElectionConstants | |
| number_of_guardians | uint32 | n |
| quorum | uint32 | k |
| election_date | string | k |
| jurisdiction_info | string | k |
| parameter_base_hash | UInt256 | Hp |
| manifest_hash | UInt256 | Hm |
| election_base_hash | UInt256 | He |
| manifest_bytes | bytes | |
| baux0 | bytes | B_aux,0 from eq 59,60 |
| device | string | the device information from eq 61, and section 3.7 |
| metadata | map\<string, string\> | arbitrary |

#### message ElectionConstants

Expand Down Expand Up @@ -266,7 +268,6 @@ draft 6/25/2023
|-----------------|-----------------------|---------------------|
| election_init | ElectionInitialized | |
| encrypted_tally | EncryptedTally | |
| ballot_ids | List\<string\> | included ballot ids |
| tally_ids | List\<string\> | included tally ids |
| metadata | map\<string, string\> | |

Expand Down Expand Up @@ -374,10 +375,11 @@ draft 6/25/2023

#### message EncryptedTally

| Name | Type | Notes |
|----------|-------------------------------|-------|
| tally_id | string | |
| contests | List\<EncryptedTallyContest\> | |
| Name | Type | Notes |
|-----------------|-------------------------------|---------------------|
| tally_id | string | |
| contests | List\<EncryptedTallyContest\> | |
| cast_ballot_ids | List\<string\> | included ballot ids |

#### message EncryptedTallyContest

Expand Down
3 changes: 1 addition & 2 deletions egklib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ kotlin {
}
}

val protoGenSource by
extra("build/generated/source/proto")
// val protoGenSource by extra("build/generated/source/proto")

/*
protoc --pbandk_out=./egklib/src/commonMain/kotlin/ --proto_path=./egklib/src/commonMain/proto \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ data class ElectionConfig(
/** info string used in hash */
val jurisdictionInfo : String,

/** may be calculated or passed in */
val parameterBaseHash : UInt256, // Hp
val manifestHash : UInt256, // Hm
val electionBaseHash : UInt256, // Hb

// keep a copy of this to make publishing easier
// the raw bytes of the manifest. You must regenerate the manifest from this.
// TODO may need to specify serialization form, or detect it.
val manifestBytes: ByteArray,

val baux0: ByteArray, // B_aux,0 from eq 59,60
val device: String, // the device information from eq 61, and section 3.7

/** arbitrary key/value metadata. */
val metadata: Map<String, String> = emptyMap(),
) {
Expand Down Expand Up @@ -168,6 +170,8 @@ fun makeElectionConfig(
electionDate: String,
jurisdictionInfo: String,
manifestBytes: ByteArray,
baux0: ByteArray, // B_aux,0 from eq 59,60
device: String, // the device information from eq 61, and section 3.7
metadata: Map<String, String> = emptyMap(),
): ElectionConfig {

Expand All @@ -186,6 +190,8 @@ fun makeElectionConfig(
manifestHash,
electionBaseHash,
manifestBytes,
baux0,
device,
metadata,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import electionguard.core.UInt256
data class TallyResult(
val electionInitialized: ElectionInitialized,
val encryptedTally: EncryptedTally,
val ballotIds: List<String>,
val tallyIds: List<String>,
val metadata: Map<String, String> = emptyMap(),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import electionguard.core.UInt256
/** The encrypted representation of the summed votes for a collection of ballots */
data class EncryptedTally(
val tallyId: String,
val contests: List<Contest>
val contests: List<Contest>,
val castBallotIds: List<String>,
) {
init {
require(contests.isNotEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import electionguard.ballot.protocolVersion
import electionguard.core.productionGroup
import electionguard.publish.makePublisher
import electionguard.publish.readAndCheckManifestBytes
import io.ktor.utils.io.core.*
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
Expand Down Expand Up @@ -51,6 +52,11 @@ fun createConfig(args: Array<String>) {
shortName = "info",
description = "jurisdictional information"
).default("N/A")
val device by parser.option(
ArgType.String,
shortName = "device",
description = "device information"
).required()
parser.parse(args)

val currentMoment : Instant = Clock.System.now()
Expand All @@ -64,7 +70,8 @@ fun createConfig(args: Array<String>) {
" output = $outputDir\n" +
" createdBy = $createdBy\n" +
" electionDate = $useDate\n" +
" info = $info"
" info = $info" +
" device = $device"
)

val group = productionGroup()
Expand All @@ -81,6 +88,8 @@ fun createConfig(args: Array<String>) {
useDate,
info,
manifestBytes,
device.toByteArray(),
device,
mapOf(
Pair("CreatedBy", createdBy),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,5 @@ private fun EncryptedBallot.convertToTally(): EncryptedTally {
contest.contestData,
)
}
return EncryptedTally(this.ballotId, contests)
return EncryptedTally(this.ballotId, contests, emptyList())
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fun runDecryptBallots(
decryptingTrustees,
)

// TODO you often want to put the decryption results in the same directory, but sinks now are append-only.
val publisher = makePublisher(outputDir, false, consumerIn.isJson())
val sink: DecryptedTallyOrBallotSinkIF = publisher.decryptedTallyOrBallotSink()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package electionguard.encrypt

import electionguard.ballot.*
import electionguard.core.ElGamalPublicKey
import electionguard.core.GroupContext
import electionguard.input.BallotInputValidation
import electionguard.input.ManifestInputValidation
import electionguard.publish.*
import io.ktor.utils.io.core.Closeable
import mu.KotlinLogging

private val logger = KotlinLogging.logger("RunEncryptBallot")

/** Encrypt a ballot and add to election record. Single threaded only. */
class AddEncryptedBallot(
val group: GroupContext,
val manifest: Manifest,
val electionInit: ElectionInitialized,
val outputDir: String, // write ballots here, must not have multiple writers to same directory
val invalidDir: String,
val isJson : Boolean,
val chainCodes : Boolean,
createNew : Boolean = false,
): Closeable {

val encryptor = Encryptor(
group,
manifest,
ElGamalPublicKey(electionInit.jointPublicKey),
electionInit.extendedBaseHash
)
val ballotValidator = BallotInputValidation(manifest)
val publisher = makePublisher(outputDir, createNew, isJson)
val sink: EncryptedBallotSinkIF = publisher.encryptedBallotSink()

var codeBaux: ByteArray = ByteArray(0)

init {
val manifestValidator = ManifestInputValidation(manifest)
val errors = manifestValidator.validate()
if (errors.hasErrors()) {
throw RuntimeException("ManifestInputValidation FAILED $errors")
}
}

fun encryptAndAdd(ballot: PlaintextBallot, state : EncryptedBallot.BallotState): Boolean {
val mess = ballotValidator.validate(ballot)
if (mess.hasErrors()) {
publisher.writePlaintextBallot(invalidDir, listOf(ballot)) // LOOK write just one ??
println(" wrote ${ballot.ballotId} invalid ballots to $invalidDir")
return false
}

val ciphertextBallot = if (chainCodes) {
encryptor.encrypt(ballot, null, null, codeBaux)
} else {
encryptor.encrypt(ballot, null)
}

codeBaux = ciphertextBallot.confirmationCode.bytes
val eballot = ciphertextBallot.submit(state)
sink.writeEncryptedBallot(eballot) // the sink must append
return true
}

override fun close() {
sink.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class RandomBallotProvider(val manifest: Manifest, val nballots: Int = 11, val a
return ballots
}

fun makeBallot(): PlaintextBallot {
val useStyle = manifest.ballotStyles[0].ballotStyleId
val ballotId = "id" + Random.nextInt()
return getFakeBallot(manifest, useStyle, ballotId)
}

fun getFakeBallot(manifest: Manifest, ballotStyleId: String, ballotId: String): PlaintextBallot {
val contests: MutableList<PlaintextBallot.Contest> = ArrayList()
for (contestp in manifest.contests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ data class ElectionConfigJson(
val parameter_base_hash: UInt256Json, // Hp
val manifest_hash: UInt256Json, // Hm
val election_base_hash: UInt256Json, // Hb

val baux0: ByteArray, // B_aux,0 from eq 59,60
val device: String, // the device information from eq 61, and section 3.7
)

fun ElectionConfig.publishJson() = ElectionConfigJson(
Expand All @@ -28,6 +31,8 @@ fun ElectionConfig.publishJson() = ElectionConfigJson(
this.parameterBaseHash.publishJson(),
this.manifestHash.publishJson(),
this.electionBaseHash.publishJson(),
this.baux0,
this.device,
)

fun ElectionConfigJson.import(constants: ElectionConstants, manifestBytes: ByteArray) : ElectionConfig {
Expand All @@ -42,6 +47,8 @@ fun ElectionConfigJson.import(constants: ElectionConstants, manifestBytes: ByteA
this.manifest_hash.import(),
this.election_base_hash.import(),
manifestBytes,
this.baux0,
this.device,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.Serializable
data class EncryptedTallyJson(
val tally_id: String,
val contests: List<EncryptedTallyContestJson>,
val cast_ballot_ids: List<String>,
)

@Serializable
Expand Down Expand Up @@ -38,7 +39,7 @@ fun EncryptedTally.publishJson(): EncryptedTallyJson {
)
})
}
return EncryptedTallyJson(this.tallyId, contests)
return EncryptedTallyJson(this.tallyId, contests, this.castBallotIds)
}

fun EncryptedTallyJson.import(group: GroupContext): EncryptedTally {
Expand All @@ -55,5 +56,5 @@ fun EncryptedTallyJson.import(group: GroupContext): EncryptedTally {
)
})
}
return EncryptedTally(this.tally_id, contests)
return EncryptedTally(this.tally_id, contests, this.cast_ballot_ids)
}
Loading

0 comments on commit c0be2ea

Please sign in to comment.