aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/gnunet/voting/simulation/CryptoUtil.java')
-rw-r--r--src/main/java/org/gnunet/voting/simulation/CryptoUtil.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java b/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java
new file mode 100644
index 0000000..87ecd9f
--- /dev/null
+++ b/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java
@@ -0,0 +1,86 @@
1package org.gnunet.voting.simulation;
2
3import java.math.BigInteger;
4import java.security.MessageDigest;
5import java.security.NoSuchAlgorithmException;
6import java.security.SecureRandom;
7import java.util.Random;
8
9/**
10 * Miscellaneous helper functions.
11 *
12 * @author Florian Dold
13 */
14public class CryptoUtil {
15 public static final Random random = new Random();
16
17
18 /**
19 * Return a random BigInteger not less than 'min' and not greater than 'max' with uniform distribution.
20 *
21 * @param min the least value that may be generated
22 * @param max the greatest value that may be generated
23 * @return a random BigInteger value in the range [min,max]
24 */
25 public static BigInteger createRandomInRange(BigInteger min,
26 BigInteger max) {
27 int cmp = min.compareTo(max);
28 if (cmp >= 0) {
29 if (cmp > 0) {
30 throw new IllegalArgumentException("'min' may not be greater than 'max'");
31 }
32
33 return min;
34 }
35
36 if (min.bitLength() > max.bitLength() / 2) {
37 return createRandomInRange(BigInteger.ZERO, max.subtract(min)).add(min);
38 }
39
40 for (int i = 0; i < 1000; ++i) {
41 BigInteger x = new BigInteger(max.bitLength(), random);
42 if (x.compareTo(min) >= 0 && x.compareTo(max) <= 0) {
43 return x;
44 }
45 }
46
47 // fall back to a faster (restricted) method
48 // (using only this distribution would lead to a non-uniform distribution, see the BigInteger constructor)
49 return new BigInteger(max.subtract(min).bitLength() - 1, random).add(min);
50 }
51
52
53
54 /**
55 * Evaluate a polynomial over Zp*. Uses Horner's scheme.
56 *
57 * @param coeffs coefficients of the polynomial, where coeffs[i] is the coefficient of x^i
58 * @param x the polynomial is evaluated at this value
59 * @param p what group are we operating in?
60 * @return the result of evaluating the polynomial at x
61 */
62 public static BigInteger evaluatePolynomial(BigInteger[] coeffs, BigInteger x, BigInteger p) {
63 BigInteger z = BigInteger.ZERO;
64 for (int i = 0; i < coeffs.length; ++i) {
65 // z <- zx + c
66 z = z.multiply(x).add(coeffs[coeffs.length - i - 1]);
67 }
68 return z;
69 }
70
71 public static BigInteger hash(BigInteger... x) {
72 MessageDigest md;
73 try {
74 md = MessageDigest.getInstance("SHA-512");
75 } catch (NoSuchAlgorithmException e) {
76 throw new RuntimeException("no SHA-512 available");
77 }
78
79 for (BigInteger v : x) {
80 md.update(v.toByteArray());
81 }
82
83 return new BigInteger(md.digest());
84 }
85
86}