|
| 1 | +package org.cryptimeleon.math.structures.groups.debug; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.atomic.AtomicLong; |
| 8 | +import java.util.concurrent.locks.Lock; |
| 9 | +import java.util.concurrent.locks.ReentrantLock; |
| 10 | + |
| 11 | +/** |
| 12 | + * Stores group operation data. |
| 13 | + * <p> |
| 14 | + * Operations are thread-safe, meaning that all increment and getter methods are implemented automically, |
| 15 | + * and the multi-exponentiation term number list is protected via a lock. |
| 16 | + */ |
| 17 | +public class CountingBucket { |
| 18 | + |
| 19 | + /** |
| 20 | + * The counted number of inversions. |
| 21 | + */ |
| 22 | + final AtomicLong numInversions; |
| 23 | + |
| 24 | + /** |
| 25 | + * The counted number of operations. |
| 26 | + * Squarings are not considered in the group operation counter. |
| 27 | + */ |
| 28 | + final AtomicLong numOps; |
| 29 | + |
| 30 | + /** |
| 31 | + * The counted number of squarings. |
| 32 | + */ |
| 33 | + final AtomicLong numSquarings; |
| 34 | + |
| 35 | + /** |
| 36 | + * The counted number of exponentiations. |
| 37 | + */ |
| 38 | + final AtomicLong numExps; |
| 39 | + |
| 40 | + /** |
| 41 | + * Number of retrieved representations for elements of this group. |
| 42 | + */ |
| 43 | + final AtomicLong numRetrievedRepresentations; |
| 44 | + |
| 45 | + /** |
| 46 | + * Contains number of terms for each multi-exponentiation performed. |
| 47 | + */ |
| 48 | + private final List<Integer> multiExpTermNumbers; |
| 49 | + |
| 50 | + private final Lock multiExpTermNumbersLock; |
| 51 | + |
| 52 | + public CountingBucket() { |
| 53 | + this.numInversions = new AtomicLong(); |
| 54 | + this.numOps = new AtomicLong(); |
| 55 | + this.numSquarings = new AtomicLong(); |
| 56 | + this.numExps = new AtomicLong(); |
| 57 | + this.numRetrievedRepresentations = new AtomicLong(); |
| 58 | + this.multiExpTermNumbers = new ArrayList<>(); |
| 59 | + this.multiExpTermNumbersLock = new ReentrantLock(); |
| 60 | + } |
| 61 | + |
| 62 | + public void incrementNumOps() { |
| 63 | + numOps.incrementAndGet(); |
| 64 | + } |
| 65 | + |
| 66 | + public void incrementNumInversions() { |
| 67 | + numInversions.incrementAndGet(); |
| 68 | + } |
| 69 | + |
| 70 | + public void incrementNumSquarings() { |
| 71 | + numSquarings.incrementAndGet(); |
| 72 | + } |
| 73 | + |
| 74 | + public void incrementNumExps() { |
| 75 | + numExps.incrementAndGet(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Tracks the fact that a multi-exponentiation with the given number of terms was done. |
| 80 | + * @param numTerms the number of terms (bases) in the multi-exponentiation |
| 81 | + */ |
| 82 | + public void addMultiExpBaseNumber(int numTerms) { |
| 83 | + multiExpTermNumbersLock.lock(); |
| 84 | + try { |
| 85 | + if (numTerms > 1) { |
| 86 | + multiExpTermNumbers.add(numTerms); |
| 87 | + } |
| 88 | + } finally { |
| 89 | + multiExpTermNumbersLock.unlock(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Adds the given list of multi-exponentiation term numbers to this bucket. |
| 95 | + * @param newTerms the new terms to add to this bucket |
| 96 | + */ |
| 97 | + public void addAllMultiExpBaseNumbers(List<Integer> newTerms) { |
| 98 | + multiExpTermNumbersLock.lock(); |
| 99 | + try { |
| 100 | + multiExpTermNumbers.addAll(newTerms); |
| 101 | + } finally { |
| 102 | + multiExpTermNumbersLock.unlock(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + void incrementNumRetrievedRepresentations() { |
| 107 | + numRetrievedRepresentations.incrementAndGet(); |
| 108 | + } |
| 109 | + |
| 110 | + public long getNumInversions() { |
| 111 | + return numInversions.get(); |
| 112 | + } |
| 113 | + |
| 114 | + public long getNumOps() { |
| 115 | + return numOps.get(); |
| 116 | + } |
| 117 | + |
| 118 | + public long getNumSquarings() { |
| 119 | + return numSquarings.get(); |
| 120 | + } |
| 121 | + |
| 122 | + public long getNumExps() { |
| 123 | + return numExps.get(); |
| 124 | + } |
| 125 | + |
| 126 | + public long getNumRetrievedRepresentations() { |
| 127 | + return numRetrievedRepresentations.get(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns an immutable copy of the list storing the multi-exponentiation term numbers. |
| 132 | + * This list contains the number of exponentiations in each multi-exponentiation that has been calculated. |
| 133 | + */ |
| 134 | + public List<Integer> getMultiExpTermNumbers() { |
| 135 | + return Collections.unmodifiableList(multiExpTermNumbers); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Resets all counters. |
| 140 | + */ |
| 141 | + public void resetCounters() { |
| 142 | + resetOpsCounter(); |
| 143 | + resetInversionsCounter(); |
| 144 | + resetSquaringsCounter(); |
| 145 | + resetExpsCounter(); |
| 146 | + resetMultiExpTermNumbers(); |
| 147 | + resetRetrievedRepresentationsCounter(); |
| 148 | + } |
| 149 | + |
| 150 | + protected void resetOpsCounter() { |
| 151 | + numOps.set(0); |
| 152 | + } |
| 153 | + |
| 154 | + protected void resetInversionsCounter() { |
| 155 | + numInversions.set(0); |
| 156 | + } |
| 157 | + |
| 158 | + protected void resetSquaringsCounter() { |
| 159 | + numSquarings.set(0); |
| 160 | + } |
| 161 | + |
| 162 | + protected void resetExpsCounter() { numExps.set(0); } |
| 163 | + |
| 164 | + protected void resetMultiExpTermNumbers() { |
| 165 | + multiExpTermNumbersLock.lock(); |
| 166 | + try { |
| 167 | + multiExpTermNumbers.clear(); |
| 168 | + } finally { |
| 169 | + multiExpTermNumbersLock.unlock(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + protected void resetRetrievedRepresentationsCounter() { |
| 174 | + numRetrievedRepresentations.set(0); |
| 175 | + } |
| 176 | + |
| 177 | + protected boolean isEmpty() { |
| 178 | + return numOps.get() == 0 && numInversions.get() == 0 && numSquarings.get() == 0 && numExps.get() == 0 |
| 179 | + && getMultiExpTermNumbers().isEmpty() && numRetrievedRepresentations.get() == 0; |
| 180 | + } |
| 181 | +} |
0 commit comments