Skip to content

Commit

Permalink
Add return values to all CLIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnLCaron committed Apr 28, 2024
1 parent a61aa37 commit da1ab57
Show file tree
Hide file tree
Showing 25 changed files with 293 additions and 153 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![License](https://img.shields.io/github/license/JohnLCaron/egk-ec)](https://github.com/JohnLCaron/egk-ec-mixnet/blob/main/LICENSE.txt)
![GitHub branch checks state](https://img.shields.io/github/actions/workflow/status/JohnLCaron/egk-ec-mixnet/unit-tests.yml)
![Coverage](https://img.shields.io/badge/coverage-90.6%25%20LOC%20(1356/1497)-blue)
![Coverage](https://img.shields.io/badge/coverage-89.3%25%20LOC%20(1424/1595)-blue)

# Egk Elliptic Curves Mixnet

_last update 04/21/2024_
_last update 04/28/2024_

Implementation of a mixnet using the [ElectionGuard Kotlin Elliptical Curve library](https://github.com/JohnLCaron/egk-ec),
and the [Verificatum library](https://www.verificatum.org/). The mixnet uses the Terelius / Wikström (TW) mixnet
Expand Down
Binary file modified libs/egk-ec-2.1-SNAPSHOT.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion scripts/eg-tally-decrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ java -classpath $CLASSPATH \
-trustees ${PRIVATE_DIR}/trustees \
-out ${PUBLIC_DIR}

echo " [DONE] Decrypted electionguard tally into ${PUBLIC_DIR}/tally.json"
retval=$?

echo " [DONE] Decrypted electionguard tally into ${PUBLIC_DIR}/tally.json returns $retval"
4 changes: 3 additions & 1 deletion scripts/eg-tally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ java -classpath $CLASSPATH \
-out ${PUBLIC_DIR} \
-count

echo " [DONE] Tallying encrypted ballots."
retval=$?

echo " [DONE] Tallying encrypted ballots returns $retval"
4 changes: 3 additions & 1 deletion scripts/eg-verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ java -classpath $CLASSPATH \
org.cryptobiotic.eg.cli.RunVerifier \
-in ${PUBLIC_DIR}

echo " [DONE] Verifying ElectionGuard record in ${PUBLIC_DIR}"
retval=$?

echo " [DONE] Verifying ElectionGuard record in ${PUBLIC_DIR} returns $retval"
8 changes: 7 additions & 1 deletion scripts/election-initialize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ java -classpath $CLASSPATH org.cryptobiotic.eg.cli.RunCreateElectionConfig \
-quorum 3 \
-out ${PRIVATE_DIR}

echo " run KeyCeremony to generate the election keypair"
retval=$?

echo " run CreateElectionConfig to generate configuration returns $retval"

java -classpath $CLASSPATH org.cryptobiotic.eg.cli.RunTrustedKeyCeremony \
-in ${PRIVATE_DIR} \
-trustees ${PRIVATE_DIR}/trustees \
-out ${PRIVATE_DIR}

retval=$?

echo " run TrustedKeyCeremony to generate the election keypair returns $retval"

echo " copy electionguard files to public workspace ${PUBLIC_DIR}"

mkdir -p ${PUBLIC_DIR}
Expand Down
8 changes: 6 additions & 2 deletions scripts/mixnet-shuffle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ java -classpath $CLASSPATH \
-publicDir ${PUBLIC_DIR} \
--mixName mix1

echo " mixnet-shuffle and proof written to ${PUBLIC_DIR}/mix1"
retval=$?

echo " mixnet-shuffle and proof written to ${PUBLIC_DIR}/mix1 retval=$retval"

java -classpath $CLASSPATH \
org.cryptobiotic.mixnet.cli.RunMixnet \
-publicDir ${PUBLIC_DIR} \
--inputMixDir ${PUBLIC_DIR}/mix1 \
--mixName mix2

echo " [DONE] mixnet-shuffle and proof written to ${PUBLIC_DIR}/mix2"
retval=$?

echo " [DONE] mixnet-shuffle and proof written to ${PUBLIC_DIR}/mix2 retval=$retval"
6 changes: 5 additions & 1 deletion scripts/mixnet-verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ java -classpath $CLASSPATH \
-publicDir ${PUBLIC_DIR} \
--outputMixDir ${PUBLIC_DIR}/mix1

retval1=$?

java -classpath $CLASSPATH \
org.cryptobiotic.mixnet.cli.RunProofOfShuffleVerifier \
-publicDir ${PUBLIC_DIR} \
--inputMixDir ${PUBLIC_DIR}/mix1 \
--outputMixDir ${PUBLIC_DIR}/mix2

echo " [DONE] Verifying mix1 and mix2 "
retval2=$?

echo " [DONE] Verifying mix1 retval=$retval1 and mix2 retval=$retval2"
4 changes: 3 additions & 1 deletion scripts/pballot-decrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ java -classpath $CLASSPATH \
--mixDir ${PUBLIC_DIR}/mix2 \
-out ${PRIVATE_DIR}/decrypted_ballots

echo " [DONE] Decrypted paper ballot into${PRIVATE_DIR}/decrypted_ballots/"
retval=$?

echo " [DONE] Decrypted paper ballot into${PRIVATE_DIR}/decrypted_ballots/ retval=$retval"
File renamed without changes.
4 changes: 3 additions & 1 deletion scripts/table-mixnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ java -classpath $CLASSPATH \
-trustees ${PRIVATE_DIR}/trustees \
--mixDir ${PUBLIC_DIR}/mix2

echo " [DONE] Generating decrypted_sns.json "
retval=$?

echo " [DONE] Generating decrypted_sns.json retval=$retval"
4 changes: 3 additions & 1 deletion scripts/table-pballot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ CLASSPATH="build/libs/egk-ec-mixnet-2.1-SNAPSHOT-uber.jar"
-pballotDir ${PRIVATE_DIR}/input_ballots \
--missingPct 10

echo " [DONE] Generated simulated paper ballot table into ${PUBLIC_DIR}/table-pballot.json"
retval=$?

echo " [DONE] Generated simulated paper ballot table into ${PUBLIC_DIR}/table-pballot.json retval=$retval"
4 changes: 3 additions & 1 deletion scripts/verify-decryptions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ java -classpath $CLASSPATH \
-pballots ${PRIVATE_DIR}/input_ballots \
--show

echo " [DONE] verified decrypted ballots"
retval=$?

echo " [DONE] verified decrypted ballots retval=$retval"
36 changes: 25 additions & 11 deletions src/main/kotlin/org/cryptobiotic/mixnet/cli/RunMixnet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.required
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.cli.default
import org.cryptobiotic.eg.core.*
import org.cryptobiotic.eg.core.encrypt
import org.cryptobiotic.eg.election.Manifest
Expand All @@ -20,6 +21,7 @@ import org.cryptobiotic.mixnet.ProofOfShuffle
import org.cryptobiotic.mixnet.runProof
import org.cryptobiotic.mixnet.shuffle
import org.cryptobiotic.mixnet.writer.*
import kotlin.system.exitProcess

class RunMixnet {

Expand Down Expand Up @@ -56,6 +58,12 @@ class RunMixnet {
shortName = "out",
description = "output directory (default is publicDir)"
)
val noexit by parser.option(
ArgType.Boolean,
shortName = "noexit",
description = "Dont call System.exit"
).default(false)

parser.parse(args)

val info = buildString {
Expand All @@ -79,14 +87,14 @@ class RunMixnet {
val result = readMixnetConfigFromFile(lastFilename)
if (result is Err) {
logger.error {"Error reading MixnetConfig err = $result" }
return
if (!noexit) exitProcess(1) else return
}
val previousConfig = result.unwrap()
width = previousConfig.width
val ballotResult = readShuffledBallotsFromFile( mixnet.group, inputMixDir!!, previousConfig.width)
if (ballotResult is Err) {
logger.error {"Error reading input ballots in $inputMixDir = $ballotResult" }
return
if (!noexit) exitProcess(2) else return
}
inputBallots = ballotResult.unwrap()
ballotStyles = previousConfig.ballotStyles
Expand All @@ -101,17 +109,23 @@ class RunMixnet {
noncesSeed = seed.toUInt256safe().publishJson()
}

logger.info { "runShuffleProof with ${inputBallots.size} ballots" }
val (shuffled, proof) = mixnet.runShuffleProof(inputBallots, mixName)
try {
logger.info { "runShuffleProof with ${inputBallots.size} ballots" }
val (shuffled, proof) = mixnet.runShuffleProof(inputBallots, mixName)

val topdir = outputDir ?: publicDir
val outputDirMix = "$topdir/$mixName"
writeShuffledBallotsToFile(true, outputDirMix, shuffled)
writeProofOfShuffleJsonToFile(proof, "$outputDirMix/$proofFilename")

val topdir = outputDir ?: publicDir
val outputDirMix = "$topdir/$mixName"
writeShuffledBallotsToFile(true, outputDirMix, shuffled)
writeProofOfShuffleJsonToFile(proof, "$outputDirMix/$proofFilename")
val config = MixnetConfig(mixName, mixnet.electionId.publishJson(), ballotStyles, width, noncesSeed)
writeMixnetConfigToFile(config, "$outputDirMix/$configFilename")
logger.info { "ShuffleProof success" }

val config = MixnetConfig(mixName, mixnet.electionId.publishJson(), ballotStyles, width, noncesSeed)
writeMixnetConfigToFile(config, "$outputDirMix/$configFilename")
logger.info { "success" }
} catch (t: Throwable) {
logger.error { "Exception= ${t.message} ${t.stackTraceToString()}" }
if (!noexit) exitProcess(-1)
}
}
}
}
Expand Down
33 changes: 23 additions & 10 deletions src/main/kotlin/org/cryptobiotic/mixnet/cli/RunMixnetTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.michaelbull.result.unwrap
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.cli.RunTrustedTallyDecryption
import org.cryptobiotic.eg.decrypt.CipherDecryptionAndProof
Expand All @@ -14,6 +15,7 @@ import org.cryptobiotic.eg.decrypt.Guardians
import org.cryptobiotic.eg.publish.makeConsumer
import org.cryptobiotic.mixnet.writer.*
import org.cryptobiotic.util.ErrorMessages
import kotlin.system.exitProcess

class RunMixnetTable {

Expand Down Expand Up @@ -43,6 +45,12 @@ class RunMixnetTable {
shortName = "out",
description = "output directory (default is publicDir)"
)
val noexit by parser.option(
ArgType.Boolean,
shortName = "noexit",
description = "Dont call System.exit"
).default(false)

parser.parse(args)

val info = buildString {
Expand All @@ -58,12 +66,18 @@ class RunMixnetTable {
val resultConfig = readMixnetConfigFromFile(configFilename)
if (resultConfig is Err) {
RunMixnet.logger.error { "Error reading MixnetConfig from $configFilename err = $resultConfig" }
return
if (!noexit) exitProcess(1) else return
}
val config = resultConfig.unwrap()

val valid = runGenerateMixnetTable(egkMixnetDir, trusteeDir, mixDir, outputDir, config)
logger.info { "valid = $valid" }
try {
runGenerateMixnetTable(egkMixnetDir, trusteeDir, mixDir, outputDir, config, noexit)
logger.info { "Generate MixnetTable success" }

} catch (t: Throwable) {
logger.error { "Exception= ${t.message} ${t.stackTraceToString()}" }
if (!noexit) exitProcess(-1)
}
}


Expand All @@ -72,21 +86,22 @@ class RunMixnetTable {
trusteeDir: String,
mixDir: String,
outputDir: String?,
config: MixnetConfig
config: MixnetConfig,
noexit: Boolean
) {
val consumerIn = makeConsumer(publicDir)
val initResult = consumerIn.readElectionInitialized()
if (initResult is Err) {
logger.error { "readElectionInitialized error ${initResult.error}" }
return
if (!noexit) exitProcess(2) else return
}
val electionInit = initResult.unwrap()
val group = consumerIn.group

val shuffledResult = readShuffledBallotsFromFile( group, mixDir, config.width)
if (shuffledResult is Err) {
logger.error {"Error reading shuffled ballots in $mixDir = $shuffledResult" }
return
if (!noexit) exitProcess(3) else return
}
val shuffled = shuffledResult.unwrap()
RunProofOfShuffleVerifier.logger.info { " Read ${shuffled.size} shuffled ballots" }
Expand All @@ -102,8 +117,7 @@ class RunMixnetTable {
val decryptStylesAndProof: List<CipherDecryptionAndProof>? = decryptor.decrypt(encryptedStyles, errst)
if (errst.hasErrors()) {
logger.error { "failed = $errst"}
println("failed errors = $errst")
return
if (!noexit) exitProcess(4) else return
}
requireNotNull(decryptStylesAndProof)
val decryptStyles: List<Int> = decryptStylesAndProof.map { (decryption, _) ->
Expand All @@ -114,8 +128,7 @@ class RunMixnetTable {
val decryptionAndProofs: List<CipherDecryptionAndProof>? = decryptor.decrypt(encryptedSns, errs)
if (errs.hasErrors()) {
logger.error { "failed = $errs"}
println("failed errors = $errs")
return
if (!noexit) exitProcess(5) else return
}
requireNotNull(decryptionAndProofs)

Expand Down
Loading

0 comments on commit da1ab57

Please sign in to comment.