From f73c4d86bbbe0ca5346ad937fbeaec1de4421968 Mon Sep 17 00:00:00 2001 From: Raphael Heitjohann <5891816+rheitjoh@users.noreply.github.com> Date: Thu, 22 Jul 2021 09:01:24 +0200 Subject: [PATCH] Make PairingCounter thread-safe --- .../groups/debug/DebugBilinearMapImpl.java | 54 ++++++++++--------- .../debug/DebugGroupImplG1NoExpMultiExp.java | 2 +- .../groups/debug/DebugGroupImplG1Total.java | 2 +- .../debug/DebugGroupImplG2NoExpMultiExp.java | 2 +- .../groups/debug/DebugGroupImplG2Total.java | 2 +- .../debug/DebugGroupImplGTNoExpMultiExp.java | 2 +- .../groups/debug/DebugGroupImplGTTotal.java | 2 +- .../debug/DebugGroupImplNoExpMultiExp.java | 2 +- .../groups/debug/DebugGroupImplTotal.java | 2 +- 9 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugBilinearMapImpl.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugBilinearMapImpl.java index 6537d539..0484490c 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugBilinearMapImpl.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugBilinearMapImpl.java @@ -11,6 +11,11 @@ import java.math.BigInteger; import java.util.HashMap; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; /** * A {@link BilinearMapImpl} implementing a fast, but insecure pairing over {@link Zn}. @@ -42,10 +47,10 @@ public class DebugBilinearMapImpl implements BilinearMapImpl { protected BilinearGroup.Type pairingType; private static class PairingCounter { - private long count; + private final AtomicLong count; public PairingCounter() { - this.count = 0L; + this.count = new AtomicLong(0); } @Override @@ -53,7 +58,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairingCounter that = (PairingCounter) o; - return count == that.count; + return count.equals(that.count); } @Override @@ -62,25 +67,25 @@ public int hashCode() { } public void incNumPairings() { - ++count; + count.incrementAndGet(); } public long getNumPairings() { - return count; + return count.get(); } } /** * Maps bucket names to the number of pairings stored within that bucket. */ - protected static HashMap numPairingsMap; + private static final ConcurrentHashMap numPairingsMap; - protected static PairingCounter defaultBucket; + private static final PairingCounter defaultBucket; - protected static PairingCounter currentBucket; + private static volatile PairingCounter currentBucket; static { - numPairingsMap = new HashMap<>(); + numPairingsMap = new ConcurrentHashMap<>(); defaultBucket = new PairingCounter(); currentBucket = defaultBucket; } @@ -165,53 +170,50 @@ public int hashCode() { return Objects.hash(size, pairingType); } - protected void setBucket(String name) { + void setBucket(String name) { currentBucket = putBucketIfAbsent(name); } - protected void setDefaultBucket() { + void setDefaultBucket() { currentBucket = defaultBucket; } - protected PairingCounter putBucketIfAbsent(String name) { + PairingCounter putBucketIfAbsent(String name) { return numPairingsMap.computeIfAbsent(name, kName -> new PairingCounter()); } - protected HashMap getBucketMap() { + ConcurrentHashMap getBucketMap() { return numPairingsMap; } /** * Retrieves number of pairings computed in this bilinear group for the bucket with the given name. */ - protected long getNumPairings(String bucketName) { + long getNumPairings(String bucketName) { return putBucketIfAbsent(bucketName).getNumPairings(); } - protected long getNumPairingsDefault() { + long getNumPairingsDefault() { return defaultBucket.getNumPairings(); } - protected long getNumPairingsAllBuckets() { - long sum = 0; - for (PairingCounter pc : numPairingsMap.values()) { - sum += pc.getNumPairings(); - } - return sum; + long getNumPairingsAllBuckets() { + return getBucketMap().reduceValuesToLong(Long.MAX_VALUE, pc -> pc.count.get(), 0L, Long::sum) + + getNumPairingsDefault(); } /** * Resets pairing counter. */ - protected void resetNumPairings(String bucketName) { - numPairingsMap.put(bucketName, new PairingCounter()); + void resetNumPairings(String bucketName) { + putBucketIfAbsent(bucketName).count.set(0); } - protected void resetNumPairingsDefault() { - defaultBucket = new PairingCounter(); + void resetNumPairingsDefault() { + defaultBucket.count.set(0); } - protected void resetNumPairingsAllBuckets() { + void resetNumPairingsAllBuckets() { resetNumPairingsDefault(); numPairingsMap.replaceAll((name, numPairings) -> new PairingCounter()); } diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1NoExpMultiExp.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1NoExpMultiExp.java index 56d53b7c..cac2f381 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1NoExpMultiExp.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1NoExpMultiExp.java @@ -26,7 +26,7 @@ public class DebugGroupImplG1NoExpMultiExp extends DebugGroupImplNoExpMultiExp { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1Total.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1Total.java index 79c17981..df3152a7 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1Total.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG1Total.java @@ -26,7 +26,7 @@ public class DebugGroupImplG1Total extends DebugGroupImplTotal { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2NoExpMultiExp.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2NoExpMultiExp.java index 75d7975d..5ac81dde 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2NoExpMultiExp.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2NoExpMultiExp.java @@ -26,7 +26,7 @@ public class DebugGroupImplG2NoExpMultiExp extends DebugGroupImplNoExpMultiExp { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2Total.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2Total.java index 40f7c9bd..f9a3134b 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2Total.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplG2Total.java @@ -26,7 +26,7 @@ public class DebugGroupImplG2Total extends DebugGroupImplTotal { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTNoExpMultiExp.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTNoExpMultiExp.java index 232d5fca..ce9ba664 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTNoExpMultiExp.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTNoExpMultiExp.java @@ -26,7 +26,7 @@ public class DebugGroupImplGTNoExpMultiExp extends DebugGroupImplNoExpMultiExp { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTTotal.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTTotal.java index 589c9ade..3ece0e4e 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTTotal.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplGTTotal.java @@ -26,7 +26,7 @@ public class DebugGroupImplGTTotal extends DebugGroupImplTotal { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplNoExpMultiExp.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplNoExpMultiExp.java index e801796d..82023030 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplNoExpMultiExp.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplNoExpMultiExp.java @@ -31,7 +31,7 @@ public class DebugGroupImplNoExpMultiExp extends DebugGroupImpl { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static { diff --git a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplTotal.java b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplTotal.java index 5cb1b4c0..3bf3d2c6 100644 --- a/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplTotal.java +++ b/src/main/java/org/cryptimeleon/math/structures/groups/debug/DebugGroupImplTotal.java @@ -26,7 +26,7 @@ public class DebugGroupImplTotal extends DebugGroupImpl { /** * The currently used bucket. */ - private static CountingBucket currentBucket; + private static volatile CountingBucket currentBucket; // Initialization block for variables static {