-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RunAddEncryptedBallots CLI. Standardize CLI arguments. AddEncryptedBallot, RunEncryptBallot allows noDeviceNameInDir argument. Remove ballotOverrideDir: cannot write encrypted ballots to arbitrary place. Update DecryptedTallyOrBallot.compare(). RandomBallotProvider generates sns in (0, LONG.Max)
- Loading branch information
1 parent
566ce22
commit b1a2dbf
Showing
21 changed files
with
482 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/kotlin/org/cryptobiotic/eg/cli/RunAddEncryptedBallots.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.cryptobiotic.eg.cli | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.cli.ArgParser | ||
import kotlinx.cli.ArgType | ||
import kotlinx.cli.default | ||
import kotlinx.cli.required | ||
import org.cryptobiotic.eg.encrypt.AddEncryptedBallot | ||
import org.cryptobiotic.eg.input.BallotInputValidation | ||
import org.cryptobiotic.eg.input.ManifestInputValidation | ||
import org.cryptobiotic.eg.publish.readElectionRecord | ||
import org.cryptobiotic.util.ErrorMessages | ||
import kotlin.random.Random | ||
|
||
/** | ||
* This reads plaintext ballots from ballotDir and writes their encryptions into the specified election record. | ||
*/ | ||
class RunAddEncryptedBallots { | ||
|
||
companion object { | ||
private val logger = KotlinLogging.logger("RunAddEncryptedBallots") | ||
|
||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
val parser = ArgParser("RunAddEncryptedBallots") | ||
val inputDir by parser.option( | ||
ArgType.String, | ||
shortName = "in", | ||
description = "Directory containing input election record" | ||
).required() | ||
val ballotDir by parser.option( | ||
ArgType.String, | ||
shortName = "ballots", | ||
description = "Directory to read Plaintext ballots from" | ||
).required() | ||
val device by parser.option( | ||
ArgType.String, | ||
shortName = "device", | ||
description = "voting device name" | ||
).required() | ||
val outputDir by parser.option( | ||
ArgType.String, | ||
shortName = "out", | ||
description = "Directory to write output election record" | ||
).required() | ||
val challengePct by parser.option( | ||
ArgType.Int, | ||
shortName = "challenge", | ||
description = "Challenge percent of ballots" | ||
).default(0) | ||
|
||
parser.parse(args) | ||
|
||
logger.info { | ||
"starting\n inputDir= $inputDir\n ballotDir = $ballotDir\n device = $device\n" + | ||
" outputDir = $outputDir\n challengePct = $challengePct" | ||
} | ||
|
||
val electionRecord = readElectionRecord(inputDir) | ||
val electionInit = electionRecord.electionInit()!! | ||
val consumerIn = electionRecord.consumer() | ||
|
||
val manifest = consumerIn.makeManifest(electionInit.config.manifestBytes) | ||
val errors = ManifestInputValidation(manifest).validate() | ||
if (ManifestInputValidation(manifest).validate().hasErrors()) { | ||
logger.error { "ManifestInputValidation error ${errors}" } | ||
throw RuntimeException("ManifestInputValidation error $errors") | ||
} | ||
val chaining = electionInit.config.chainConfirmationCodes | ||
|
||
var allOk = true | ||
|
||
val encryptor = AddEncryptedBallot( | ||
electionRecord.manifest(), | ||
BallotInputValidation(electionRecord.manifest()), | ||
electionInit.config.chainConfirmationCodes, | ||
electionInit.config.configBaux0, | ||
electionInit.jointPublicKey, | ||
electionInit.extendedBaseHash, | ||
device, | ||
outputDir, | ||
"${outputDir}/invalidDir", | ||
noDeviceNameInDir = !chaining | ||
) | ||
|
||
var countChallenge = 0 | ||
consumerIn.iteratePlaintextBallots(ballotDir, null).forEach { pballot -> | ||
val errs = ErrorMessages("AddEncryptedBallot ${pballot.ballotId}") | ||
val encrypted = encryptor.encrypt(pballot, errs) | ||
if (encrypted == null) { | ||
logger.error{ "failed errors = $errs"} | ||
allOk = false | ||
} else { | ||
val challengeThisOne = (challengePct != 0) && (Random.nextInt(100) > (100 - challengePct)) | ||
if (challengeThisOne) { | ||
encryptor.challenge(encrypted.confirmationCode) | ||
countChallenge++ | ||
} else { | ||
encryptor.cast(encrypted.confirmationCode) | ||
} | ||
} | ||
} | ||
encryptor.close() | ||
|
||
if (allOk) { | ||
logger.info { "success" } | ||
} else { | ||
logger.error { "failure" } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.