Skip to content

Commit

Permalink
Fixed jvm compilation in egklib and egk-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
frog711 committed Sep 11, 2024
1 parent ebe3086 commit e4a8c6b
Show file tree
Hide file tree
Showing 22 changed files with 516 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import electionguard.cli.RunTrustedBallotDecryption.Companion.runDecryptBallots
import electionguard.cli.RunTrustedTallyDecryption.Companion.readDecryptingTrustees
import electionguard.core.productionGroup
import electionguard.publish.makeConsumer
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest

import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -19,57 +21,63 @@ class RunDecryptBallotsJsonTest {
val nthreads = 25

@Test
fun testDecryptBallotsAll() {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/allAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsAllJson"
println("\ntestDecryptBallotsAll")
val n = runDecryptBallots(
group,
inputDir,
outputDir,
readDecryptingTrustees(group, inputDir, trusteeDir),
"ALL",
nthreads,
)
assertEquals(11, n)
fun testDecryptBallotsAll(): TestResult {
return runTest {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/allAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsAllJson"
println("\ntestDecryptBallotsAll")
val n = runDecryptBallots(
group,
inputDir,
outputDir,
readDecryptingTrustees(group, inputDir, trusteeDir),
"ALL",
nthreads,
)
assertEquals(11, n)
}
}

@Test
fun testDecryptBallotsSome() {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/someAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsSomeJson"
println("\ntestDecryptBallotsAll")
val n = runDecryptBallots(
group,
inputDir,
outputDir,
readDecryptingTrustees(group, inputDir, trusteeDir),
"ALL",
nthreads,
)
assertEquals(11, n)
fun testDecryptBallotsSome(): TestResult {
return runTest {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/someAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsSomeJson"
println("\ntestDecryptBallotsAll")
val n = runDecryptBallots(
group,
inputDir,
outputDir,
readDecryptingTrustees(group, inputDir, trusteeDir),
"ALL",
nthreads,
)
assertEquals(11, n)
}
}

@Test
fun testDecryptBallotsSomeFromList() {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/someAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsSomeFromListJson"
println("\ntestDecryptBallotsSomeFromList")
val n = runDecryptBallots(
group, inputDir, outputDir, readDecryptingTrustees(group, inputDir, trusteeDir, "5"),
"id-6," +
"id-7," +
"id-10," +
"id-1,",
3,
)
assertEquals(4, n)
fun testDecryptBallotsSomeFromList(): TestResult {
return runTest {
val group = productionGroup()
val inputDir = "src/commonTest/data/workflow/someAvailableJson"
val trusteeDir = "$inputDir/private_data/trustees"
val outputDir = "testOut/decrypt/testDecryptBallotsSomeFromListJson"
println("\ntestDecryptBallotsSomeFromList")
val n = runDecryptBallots(
group, inputDir, outputDir, readDecryptingTrustees(group, inputDir, trusteeDir, "5"),
"id-6," +
"id-7," +
"id-10," +
"id-1,",
3,
)
assertEquals(4, n)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package electionguard.workflow

import electionguard.demonstrate.ManifestBuilder
import electionguard.model.Manifest
import electionguard.model.PlaintextBallot

class BallotInputBuilder internal constructor(val manifest: Manifest, val id: String) {
private val contests = ArrayList<ContestBuilder>()
private var style = ManifestBuilder.styleDefault

fun setStyle(style: String): BallotInputBuilder {
this.style = style
return this
}

fun addContest(contest_id: String, seqOrder : Int? = null): ContestBuilder {
val c = ContestBuilder(contest_id, seqOrder)
contests.add(c)
return c
}

fun addContest(idx : Int): ContestBuilder {
val contest = manifest.contests[idx]
val c = ContestBuilder(contest.contestId, contest.sequenceOrder)
contests.add(c)
return c
}

fun build(): PlaintextBallot {
return PlaintextBallot(
id,
style,
contests.map {it.build() }
)
}

inner class ContestBuilder internal constructor(val contestId: String, seqOrder : Int? = null) {
private var seq = seqOrder?: 1
private val selections = ArrayList<SelectionBuilder>()
private val writeIns = ArrayList<String>()

fun addSelection(id: String, vote: Int, seqOrder : Int? = null): ContestBuilder {
val s = SelectionBuilder(id, vote, seqOrder)
selections.add(s)
return this
}

fun addSelection(idx : Int, vote: Int): ContestBuilder {
val contest = manifest.contests.find { it.contestId == contestId }
?: throw IllegalArgumentException("Cant find contestId = $contestId")
val selection = contest.selections[idx]
val s = SelectionBuilder(selection.selectionId, vote, selection.sequenceOrder)
selections.add(s)
return this
}

fun addWriteIn(writeIn: String): ContestBuilder {
writeIns.add(writeIn)
return this
}

fun done(): BallotInputBuilder {
return this@BallotInputBuilder
}

fun build(): PlaintextBallot.Contest {
return PlaintextBallot.Contest(
contestId,
seq++,
selections.map { it.build() },
writeIns
)
}

inner class SelectionBuilder internal constructor(private val selectionId: String, private val vote: Int, private val seqOrder : Int? = null) {
fun build(): PlaintextBallot.Selection {
return PlaintextBallot.Selection(selectionId, seqOrder?: seq++, vote)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package electionguard.workflow

import com.github.michaelbull.result.unwrap
import electionguard.core.UInt256
import electionguard.decrypt.DecryptingTrusteeDoerre
import electionguard.keyceremony.KeyCeremonyTrustee
import electionguard.model.Guardian
import electionguard.model.PublicKeys

fun makeDoerreTrustee(ktrustee: KeyCeremonyTrustee, electionId : UInt256): DecryptingTrusteeDoerre {
return DecryptingTrusteeDoerre(
ktrustee.id(),
ktrustee.xCoordinate(),
ktrustee.guardianPublicKey(),
ktrustee.computeSecretKeyShare(),
)
}

fun makeGuardian(trustee: KeyCeremonyTrustee): Guardian {
val publicKeys = trustee.publicKeys().unwrap()
return Guardian(
trustee.id(),
trustee.xCoordinate(),
publicKeys.coefficientProofs,
)
}

fun makeGuardian(publicKeys: PublicKeys): Guardian {
return Guardian(
publicKeys.guardianId,
publicKeys.guardianXCoordinate,
publicKeys.coefficientProofs,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import electionguard.model.*
import electionguard.core.*
import electionguard.encrypt.Encryptor
import electionguard.encrypt.cast
import electionguard.input.BallotInputBuilder
import electionguard.publish.makePublisher
import electionguard.publish.readElectionRecord
import electionguard.util.ErrorMessages
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package electionguard.workflow

import com.github.michaelbull.result.Err
import electionguard.ballot.makeGuardian
import electionguard.core.*
import electionguard.decrypt.DecryptingTrusteeDoerre
import electionguard.decrypt.computeLagrangeCoefficient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import electionguard.decrypt.DecryptingTrusteeIF
import electionguard.publish.*
import electionguard.util.Stats
import electionguard.verifier.Verifier
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.test.assertEquals
Expand All @@ -31,7 +33,7 @@ class TestNumGuardians {

@Test
fun runWorkflows() {
println("productionGroup (Default) = $group class = ${group.javaClass.name}")
println("productionGroup (Default) = $group class = ${group::class.toString()}")
//runWorkflow(name1, 1, 1, listOf(1), 1)
runWorkflow(name1, 1, 1, listOf(1), 25)

Expand All @@ -48,61 +50,65 @@ class TestNumGuardians {
checkBallotsAreEqual()
}

fun runWorkflow(name : String, nguardians: Int, quorum: Int, present: List<Int>, nthreads: Int) {
println("===========================================================")
val workingDir = "testOut/workflow/$name"
val privateDir = "$workingDir/private_data"
val trusteeDir = "${privateDir}/trustees"
val invalidDir = "${privateDir}/invalid"

// delete current workingDir
makePublisher(workingDir, true)

RunCreateElectionConfig.main(
arrayOf(
"-manifest", manifestJson,
"-nguardians", nguardians.toString(),
"-quorum", quorum.toString(),
"-out", workingDir,
"-device", "device11",
"-createdBy", name1,
fun runWorkflow(name : String, nguardians: Int, quorum: Int, present: List<Int>, nthreads: Int): TestResult {
return runTest {
println("===========================================================")
val workingDir = "testOut/workflow/$name"
val privateDir = "$workingDir/private_data"
val trusteeDir = "${privateDir}/trustees"
val invalidDir = "${privateDir}/invalid"

// delete current workingDir
makePublisher(workingDir, true)

RunCreateElectionConfig.main(
arrayOf(
"-manifest", manifestJson,
"-nguardians", nguardians.toString(),
"-quorum", quorum.toString(),
"-out", workingDir,
"-device", "device11",
"-createdBy", name1,
)
)
)

// key ceremony
val (_, init) = runFakeKeyCeremony(group, workingDir, workingDir, trusteeDir, nguardians, quorum, false)
println("FakeKeyCeremony created ElectionInitialized, guardians = $present")

// encrypt
batchEncryption(group, inputDir = workingDir, ballotDir = inputBallotDir, device = "device11",
outputDir = workingDir, null, invalidDir = invalidDir, nthreads, name1)

// tally
runAccumulateBallots(group, workingDir, workingDir, null, "RunWorkflow", name1)

val dtrustees : List<DecryptingTrusteeIF> = readDecryptingTrustees(group, trusteeDir, init, present, true)
runDecryptTally(group, workingDir, workingDir, dtrustees, name1)

// decrypt ballots
RunTrustedBallotDecryption.main(
arrayOf(
"-in", workingDir,
"-trustees", trusteeDir,
"-out", workingDir,
"-challenged", "all",
"-nthreads", nthreads.toString()

// key ceremony
val (_, init) = runFakeKeyCeremony(group, workingDir, workingDir, trusteeDir, nguardians, quorum, false)
println("FakeKeyCeremony created ElectionInitialized, guardians = $present")

// encrypt
batchEncryption(
group, inputDir = workingDir, ballotDir = inputBallotDir, device = "device11",
outputDir = workingDir, null, invalidDir = invalidDir, nthreads, name1
)

// tally
runAccumulateBallots(group, workingDir, workingDir, null, "RunWorkflow", name1)

val dtrustees: List<DecryptingTrusteeIF> = readDecryptingTrustees(group, trusteeDir, init, present, true)
runDecryptTally(group, workingDir, workingDir, dtrustees, name1)

// decrypt ballots
RunTrustedBallotDecryption.main(
arrayOf(
"-in", workingDir,
"-trustees", trusteeDir,
"-out", workingDir,
"-challenged", "all",
"-nthreads", nthreads.toString()
)
)
)

// verify
println("\nRun Verifier")
val record = readElectionRecord(group, workingDir)
val verifier = Verifier(record, nthreads)
val stats = Stats()
val ok = verifier.verify(stats)
stats.show()
println("Verify is $ok")
assertTrue(ok)

// verify
println("\nRun Verifier")
val record = readElectionRecord(group, workingDir)
val verifier = Verifier(record, nthreads)
val stats = Stats()
val ok = verifier.verify(stats)
stats.show()
println("Verify is $ok")
assertTrue(ok)
}
}

fun checkTalliesAreEqual() {
Expand Down
Loading

0 comments on commit e4a8c6b

Please sign in to comment.