|
| 1 | +package org.bouncycastle.crypto.hash2curve.test.impl; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.math.BigInteger; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +import junit.framework.TestCase; |
| 8 | + |
| 9 | +import org.bouncycastle.crypto.hash2curve.impl.GenericSqrtRatioCalculator; |
| 10 | +import org.bouncycastle.crypto.hash2curve.impl.SqrtRatio; |
| 11 | +import org.bouncycastle.math.ec.ECCurve; |
| 12 | +import org.bouncycastle.math.ec.custom.sec.SecP256R1Curve; |
| 13 | +import org.bouncycastle.math.ec.custom.sec.SecP384R1Curve; |
| 14 | +import org.bouncycastle.math.ec.custom.sec.SecP521R1Curve; |
| 15 | + |
| 16 | +public class GenericSqrtRatioConstantsTest |
| 17 | + extends TestCase |
| 18 | +{ |
| 19 | + public void testConstantsMatchDirectPowersForSmallFields() |
| 20 | + throws Exception |
| 21 | + { |
| 22 | + // Includes c3 == 0 (3, 5, 17, 257) and several two-adic valuations of q - 1. |
| 23 | + int[] primes = { 3, 5, 7, 13, 17, 29, 97, 257 }; |
| 24 | + for (int i = 0; i < primes.length; ++i) |
| 25 | + { |
| 26 | + int p = primes[i]; |
| 27 | + ECCurve curve = smallCurve(BigInteger.valueOf(p)); |
| 28 | + for (int z = -2 * p; z <= 2 * p; ++z) |
| 29 | + { |
| 30 | + checkConstants(curve, BigInteger.valueOf(z)); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public void testConstantsMatchDirectPowersForLargeFields() |
| 36 | + throws Exception |
| 37 | + { |
| 38 | + ECCurve[] curves = { |
| 39 | + new SecP256R1Curve(), new SecP384R1Curve(), new SecP521R1Curve(), |
| 40 | + smallCurve(BigInteger.ONE.shiftLeft(255).subtract(BigInteger.valueOf(19))), |
| 41 | + smallCurve(BigInteger.ONE.shiftLeft(448).subtract(BigInteger.ONE.shiftLeft(224)).subtract(BigInteger.ONE)) |
| 42 | + }; |
| 43 | + Random random = new Random(9380L); |
| 44 | + for (int i = 0; i < curves.length; ++i) |
| 45 | + { |
| 46 | + BigInteger q = curves[i].getField().getCharacteristic(); |
| 47 | + BigInteger[] edges = { |
| 48 | + BigInteger.ZERO, BigInteger.ONE, BigInteger.ONE.negate(), |
| 49 | + BigInteger.valueOf(-10), q.subtract(BigInteger.ONE), q, |
| 50 | + q.add(BigInteger.ONE), q.shiftLeft(1).add(BigInteger.valueOf(3)) |
| 51 | + }; |
| 52 | + for (int j = 0; j < edges.length; ++j) |
| 53 | + { |
| 54 | + checkConstants(curves[i], edges[j]); |
| 55 | + } |
| 56 | + for (int j = 0; j < 16; ++j) |
| 57 | + { |
| 58 | + BigInteger z = new BigInteger(2 * q.bitLength(), random); |
| 59 | + checkConstants(curves[i], (j & 1) == 0 ? z : z.negate()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void testRepeatedRatiosOverSmallFields() |
| 65 | + { |
| 66 | + int[] primes = { 3, 5, 7, 13, 17, 29 }; |
| 67 | + for (int i = 0; i < primes.length; ++i) |
| 68 | + { |
| 69 | + BigInteger q = BigInteger.valueOf(primes[i]); |
| 70 | + BigInteger half = q.subtract(BigInteger.ONE).shiftRight(1); |
| 71 | + BigInteger z = BigInteger.valueOf(2); |
| 72 | + while (z.modPow(half, q).equals(BigInteger.ONE)) |
| 73 | + { |
| 74 | + z = z.add(BigInteger.ONE); |
| 75 | + } |
| 76 | + GenericSqrtRatioCalculator calculator = new GenericSqrtRatioCalculator(smallCurve(q), z); |
| 77 | + for (int u = 1; u < primes[i]; ++u) |
| 78 | + { |
| 79 | + for (int v = 1; v < primes[i]; ++v) |
| 80 | + { |
| 81 | + BigInteger numerator = BigInteger.valueOf(u); |
| 82 | + BigInteger denominator = BigInteger.valueOf(v); |
| 83 | + boolean square = numerator.multiply(denominator.modInverse(q)).mod(q) |
| 84 | + .modPow(half, q).equals(BigInteger.ONE); |
| 85 | + SqrtRatio result = calculator.sqrtRatio(numerator, denominator); |
| 86 | + assertEquals("quadratic-residue flag", square, result.isQR()); |
| 87 | + BigInteger expected = square ? numerator : numerator.multiply(z).mod(q); |
| 88 | + assertEquals("square-root equation", expected, |
| 89 | + result.getRatio().multiply(result.getRatio()).multiply(denominator).mod(q)); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static ECCurve smallCurve(BigInteger q) |
| 96 | + { |
| 97 | + // Fixed, known primes only. No point arithmetic is needed for these constant tests. |
| 98 | + return new ECCurve.Fp(q, BigInteger.ONE, BigInteger.ONE, null, null, true); |
| 99 | + } |
| 100 | + |
| 101 | + private static void checkConstants(ECCurve curve, BigInteger z) |
| 102 | + throws Exception |
| 103 | + { |
| 104 | + BigInteger q = curve.getField().getCharacteristic(); |
| 105 | + BigInteger oddPart = q.subtract(BigInteger.ONE); |
| 106 | + oddPart = oddPart.shiftRight(oddPart.getLowestSetBit()); |
| 107 | + GenericSqrtRatioCalculator calculator = new GenericSqrtRatioCalculator(curve, z); |
| 108 | + // Compare the actual stored constants, not a separate implementation of the rewrite. |
| 109 | + assertEquals("c6 for q=" + q + ", z=" + z, |
| 110 | + z.modPow(oddPart, q), constant(calculator, "c6")); |
| 111 | + assertEquals("c7 for q=" + q + ", z=" + z, |
| 112 | + z.modPow(oddPart.add(BigInteger.ONE).shiftRight(1), q), constant(calculator, "c7")); |
| 113 | + } |
| 114 | + |
| 115 | + private static BigInteger constant(GenericSqrtRatioCalculator calculator, String name) |
| 116 | + throws Exception |
| 117 | + { |
| 118 | + Field field = GenericSqrtRatioCalculator.class.getDeclaredField(name); |
| 119 | + field.setAccessible(true); |
| 120 | + return (BigInteger)field.get(calculator); |
| 121 | + } |
| 122 | +} |
0 commit comments