From 4e38dee8cace3b418b3015ef240bbfa1bd7f101c Mon Sep 17 00:00:00 2001 From: JohnLCaron Date: Wed, 10 Apr 2024 10:38:54 -0600 Subject: [PATCH] Add opCounts to EcGroup --- .../org/cryptobiotic/eg/core/GroupContext.kt | 10 ++++++++++ .../eg/core/ecgroup/EcElementModP.kt | 2 ++ .../eg/core/ecgroup/EcGroupContext.kt | 7 ++++++- src/test/data/workflow/allAvailableEc.zip | Bin 374068 -> 374068 bytes .../org/cryptobiotic/eg/core/GroupTest.kt | 10 ---------- .../eg/workflow/RunEncryptBallotTiming.kt | 7 ++----- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/org/cryptobiotic/eg/core/GroupContext.kt b/src/main/kotlin/org/cryptobiotic/eg/core/GroupContext.kt index de85d9b..167bcc8 100644 --- a/src/main/kotlin/org/cryptobiotic/eg/core/GroupContext.kt +++ b/src/main/kotlin/org/cryptobiotic/eg/core/GroupContext.kt @@ -275,5 +275,15 @@ fun GroupContext.addQ(vararg elements: ElementModQ) = elements.asIterable().addQ */ fun GroupContext.multP(vararg elements: ElementModP) = elements.asIterable().multP() +fun GroupContext.showOpCountResults(where: String): String { + val opCounts = this.getAndClearOpCounts() + return buildString { + appendLine("$where:") + opCounts.toSortedMap().forEach { (key, value) -> + appendLine(" $key : $value") + } + } +} + diff --git a/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcElementModP.kt b/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcElementModP.kt index dae5483..635dad2 100644 --- a/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcElementModP.kt +++ b/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcElementModP.kt @@ -1,6 +1,7 @@ package org.cryptobiotic.eg.core.ecgroup import org.cryptobiotic.eg.core.* +import java.util.concurrent.atomic.AtomicInteger class EcElementModP(val group: EcGroupContext, val ec: VecElementP): ElementModP { override val context: GroupContext = group @@ -36,6 +37,7 @@ class EcElementModP(val group: EcGroupContext, val ec: VecElementP): ElementModP override fun powP(exp: ElementModQ): ElementModP { require (exp is EcElementModQ) + group.opCounts.getOrPut("exp") { AtomicInteger(0) }.incrementAndGet() return EcElementModP(group, ec.exp(exp.element)) } diff --git a/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcGroupContext.kt b/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcGroupContext.kt index 85783c0..34c362e 100644 --- a/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcGroupContext.kt +++ b/src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/EcGroupContext.kt @@ -3,6 +3,7 @@ package org.cryptobiotic.eg.core.ecgroup import org.cryptobiotic.eg.core.* import java.math.BigInteger +import java.util.concurrent.atomic.AtomicInteger class EcGroupContext(val name: String, useNative: Boolean = true): GroupContext { val vecGroup: VecGroup = VecGroups.getEcGroup(name, useNative) @@ -52,8 +53,12 @@ class EcGroupContext(val name: String, useNative: Boolean = true): GroupContext return EcElementModP(this, vecGroup.g.exp(exp.element)) } + var opCounts: HashMap = HashMap() override fun getAndClearOpCounts(): Map { - return emptyMap() + val result = HashMap() + opCounts.forEach { (key, value) -> result[key] = value.get() } + opCounts = HashMap() + return result.toSortedMap() } override fun isCompatible(ctx: GroupContext): Boolean { diff --git a/src/test/data/workflow/allAvailableEc.zip b/src/test/data/workflow/allAvailableEc.zip index e3b184c0348e626bc866c9f53195a189f1a113d3..30b3a0902cbbed4540d3c65d169d20cc74736909 100644 GIT binary patch delta 2467 zcmYjRZBUd|6z1K%2Q-yYLl`g>@?*jr-6d>BQDHYR&D4d=kVzD0O2=>%6|>RNw!q5# zLUb>(f_x(&AIoC5G;_=*bxgFz#K>eA43$i+940^O-h1x*zWjQ4&Uv2aoO|EPHg|_M zcZc?O&9;(|fIfZt1Z22<{g-Q&47Y#1HX1nQ6$T~H0E3gHI3J9 zx`mc;ZO>4VElrC&5X4GGYNiPTqhvaU2sgvq9Fu*{W8zOa+7_Cg@L z>am=aB{}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#&4 zeeko-$uVeruNY*_NriwRJ7z9qX`FRKzRmRyO*_P$0!4M(Y zk`QnR@q$>evDOgyV4%T191S;ugq8QUo8_=X2$cUe(vrLcVnc+;vl-H)C@>quk1BO# zL%cwm4O0ZRWP?j!xMMFY7IGpKErxQFDd&gmxlDEZ``$#}{7pC1BprT-3gw zi@TgtIOYI)_HdM*G3Pf-@T2YdJimV)dXn-`^R>co5AsY8X5CbL=0W7O2UV8esxv1v z%o>}IS?luA+p2I%0rFyn{)dn+Q`oF9s!(NyhCTS@Pnvd!@7GX>nU8o+oIj;VY36r( zwq(&uxmvT2;#~hL3(~C2U8}~`Wo)3D??8w?+MR@a>N}9+se^v zuR#2?LS-zpw4RK{jK3=|Bl0kMH!8fyQO--BUCiV%d!nw<4j3~7(B - appendLine(" $key : $value") - } - } } \ No newline at end of file diff --git a/src/test/kotlin/org/cryptobiotic/eg/workflow/RunEncryptBallotTiming.kt b/src/test/kotlin/org/cryptobiotic/eg/workflow/RunEncryptBallotTiming.kt index fbaed18..6c07e98 100644 --- a/src/test/kotlin/org/cryptobiotic/eg/workflow/RunEncryptBallotTiming.kt +++ b/src/test/kotlin/org/cryptobiotic/eg/workflow/RunEncryptBallotTiming.kt @@ -73,17 +73,14 @@ class RunEncryptBallotTiming { val encryptedBallot = encryptor.encrypt(ballot, ByteArray(0), ErrorMessages("testEncryption")) requireNotNull(encryptedBallot) } - val opCounts = group.getAndClearOpCounts() + group.getAndClearOpCounts() val nencryptions = ncontests + ncontests * nselections println("Encryption ${stopwatch.tookPer(nballots, "ballots")}") println(" ${stopwatch.tookPer(nencryptions, "encryptions")}") println() if (showOperations) { - println("operations:") - println(buildString { - opCounts.forEach { key, value -> println(" $key = $value") } - }) + println(group.showOpCountResults("operations")) println("expect: ${6 * nencryptions * nballots + 2 * nballots}") } }