|
| 1 | +package org.bouncycastle.pqc.math.ntru; |
| 2 | + |
| 3 | +import junit.framework.TestCase; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression tests for NTRU's modular reductions. |
| 7 | + * <p> |
| 8 | + * {@link Polynomial#mod3(short)}, {@link Polynomial#mod3(byte)} and |
| 9 | + * {@link Polynomial#modQ(int, int)} used to be written with the {@code %} operator, which |
| 10 | + * compiles to an integer division whose latency depends on its operands - and all three run on |
| 11 | + * secret data (the secret key polynomials f and g, the message polynomials r and m, and the |
| 12 | + * coefficients recovered during decapsulation). They are now division-free, matching the |
| 13 | + * reference implementation, and these tests pin them to the values {@code %} produced across the |
| 14 | + * entire input domain that reaches them. |
| 15 | + * <p> |
| 16 | + * This test has to live in {@code org.bouncycastle.pqc.math.ntru} rather than the |
| 17 | + * {@code ...ntru.test} package alongside {@link org.bouncycastle.pqc.math.ntru.test.PolynomialTest}, |
| 18 | + * because the methods are package-private. |
| 19 | + */ |
| 20 | +public class PolynomialModTest |
| 21 | + extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Every logQ appearing in a parameter set: NTRUHPS2048509/677 use 11, NTRUHPS4096821/1229 |
| 25 | + * use 12, NTRUHRSS701 uses 13 and NTRUHRSS1373 uses 14. NTRUParameterSet.q() is 1 << logQ, |
| 26 | + * which is what makes masking a valid reduction. |
| 27 | + */ |
| 28 | + private static final int[] LOG_QS = new int[]{11, 12, 13, 14}; |
| 29 | + |
| 30 | + public void testMod3ShortOverEveryInput() |
| 31 | + { |
| 32 | + for (int a = Short.MIN_VALUE; a <= Short.MAX_VALUE; ++a) |
| 33 | + { |
| 34 | + short s = (short)a; |
| 35 | + assertEquals("mod3(short " + s + ")", (s & 0xffff) % 3, Polynomial.mod3(s)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void testMod3ByteOverEveryInput() |
| 40 | + { |
| 41 | + for (int a = Byte.MIN_VALUE; a <= Byte.MAX_VALUE; ++a) |
| 42 | + { |
| 43 | + byte b = (byte)a; |
| 44 | + assertEquals("mod3(byte " + b + ")", (byte)((b & 0xff) % 3), Polynomial.mod3(b)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * The callers treat the result as a ternary coefficient, so nothing may escape 0..2 - the |
| 50 | + * masked conditional subtraction has to leave the folded value fully reduced. |
| 51 | + */ |
| 52 | + public void testMod3IsAlwaysTernary() |
| 53 | + { |
| 54 | + for (int a = Short.MIN_VALUE; a <= Short.MAX_VALUE; ++a) |
| 55 | + { |
| 56 | + short r = Polynomial.mod3((short)a); |
| 57 | + assertTrue("mod3(short " + a + ") = " + r, r >= 0 && r <= 2); |
| 58 | + } |
| 59 | + |
| 60 | + for (int a = Byte.MIN_VALUE; a <= Byte.MAX_VALUE; ++a) |
| 61 | + { |
| 62 | + byte r = Polynomial.mod3((byte)a); |
| 63 | + assertTrue("mod3(byte " + a + ") = " + r, r >= 0 && r <= 2); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Every call site passes a dividend already masked to 16 bits, so that is the domain that |
| 69 | + * has to agree with {@code %}; q is a power of two for every parameter set. |
| 70 | + */ |
| 71 | + public void testModQOverEveryInputAndParameterSet() |
| 72 | + { |
| 73 | + for (int i = 0; i < LOG_QS.length; ++i) |
| 74 | + { |
| 75 | + int q = 1 << LOG_QS[i]; |
| 76 | + for (int x = 0; x <= 0xffff; ++x) |
| 77 | + { |
| 78 | + assertEquals("modQ(" + x + ", " + q + ")", x % q, Polynomial.modQ(x, q)); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void testModQIsInRange() |
| 84 | + { |
| 85 | + for (int i = 0; i < LOG_QS.length; ++i) |
| 86 | + { |
| 87 | + int q = 1 << LOG_QS[i]; |
| 88 | + for (int x = 0; x <= 0xffff; ++x) |
| 89 | + { |
| 90 | + int r = Polynomial.modQ(x, q); |
| 91 | + assertTrue("modQ(" + x + ", " + q + ") = " + r, r >= 0 && r < q); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments