Changeset 2800791


Ignore:
Timestamp:
Jul 20, 2018 3:25:15 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
292b7e6d
Parents:
c35d158
Message:

Test: Backport EdDSA junit changes from github; fixes NPE in 2 tests

Location:
core/java
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • core/java/build.xml

    rc35d158 r2800791  
    330330                    <exclude name="**/ElGamalAESEngineTest.java" />
    331331                    <exclude name="**/StructureTest.java" />
     332                    <exclude name="**/AbstractFieldElementTest.java" />
    332333                    <!-- DataHelperTest is *very* slow with cobertura -->
    333334                    <!--
  • core/java/test/junit/net/i2p/crypto/eddsa/EdDSAEngineTest.java

    rc35d158 r2800791  
    11package net.i2p.crypto.eddsa;
    22
    3 import static org.hamcrest.Matchers.*;
    4 import static org.junit.Assert.*;
     3import static org.hamcrest.Matchers.equalTo;
     4import static org.hamcrest.Matchers.is;
     5import static org.junit.Assert.assertThat;
    56
    67import java.nio.charset.Charset;
     
    3536    @Test
    3637    public void testSign() throws Exception {
     38        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    3739        //Signature sgr = Signature.getInstance("EdDSA", "I2P");
    38         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    39         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     40        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    4041
    4142        for (Ed25519TestVectors.TestTuple testCase : Ed25519TestVectors.testCases) {
     
    5354    @Test
    5455    public void testVerify() throws Exception {
     56        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    5557        //Signature sgr = Signature.getInstance("EdDSA", "I2P");
    56         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    57         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    58 
     58        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    5959        for (Ed25519TestVectors.TestTuple testCase : Ed25519TestVectors.testCases) {
    6060            EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(testCase.pk, spec);
     
    7474    @Test
    7575    public void testVerifyWrongSigLength() throws Exception {
     76        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    7677        //Signature sgr = Signature.getInstance("EdDSA", "I2P");
    77         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    78 
    79         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    80                 EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     78        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
     79        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
    8180        PublicKey vKey = new EdDSAPublicKey(pubKey);
    8281        sgr.initVerify(vKey);
     
    9190    @Test
    9291    public void testSignResetsForReuse() throws Exception {
    93         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    94         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    95 
     92        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     93        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    9694        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
    9795        PrivateKey sKey = new EdDSAPrivateKey(privKey);
     
    109107    @Test
    110108    public void testVerifyResetsForReuse() throws Exception {
    111         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    112 
    113         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    114                 EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     109        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     110        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
     111        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
    115112        PublicKey vKey = new EdDSAPublicKey(pubKey);
    116113        sgr.initVerify(vKey);
     
    127124    @Test
    128125    public void testSignOneShotMode() throws Exception {
    129         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    130         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    131 
     126        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     127        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    132128        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
    133129        PrivateKey sKey = new EdDSAPrivateKey(privKey);
     
    142138    @Test
    143139    public void testVerifyOneShotMode() throws Exception {
    144         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    145 
    146         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    147                 EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     140        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     141        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
     142        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
    148143        PublicKey vKey = new EdDSAPublicKey(pubKey);
    149144        sgr.initVerify(vKey);
     
    157152    @Test
    158153    public void testSignOneShotModeMultipleUpdates() throws Exception {
    159         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    160         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    161 
     154        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     155        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    162156        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
    163157        PrivateKey sKey = new EdDSAPrivateKey(privKey);
     
    174168    @Test
    175169    public void testVerifyOneShotModeMultipleUpdates() throws Exception {
    176         Signature sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    177 
    178         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    179                 EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     170        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     171        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
     172        Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    180173        PublicKey vKey = new EdDSAPublicKey(pubKey);
    181174        sgr.initVerify(vKey);
     
    191184    @Test
    192185    public void testSignOneShot() throws Exception {
    193         EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    194         EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("ed25519-sha-512");
    195 
    196         EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
     186        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     187        EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
     188        EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    197189        PrivateKey sKey = new EdDSAPrivateKey(privKey);
    198190        sgr.initSign(sKey);
     
    203195    @Test
    204196    public void testVerifyOneShot() throws Exception {
    205         EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance("SHA-512"));
    206 
    207         EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK,
    208                 EdDSANamedCurveTable.getByName("ed25519-sha-512"));
     197        EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     198        EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
     199        EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
    209200        PublicKey vKey = new EdDSAPublicKey(pubKey);
    210201        sgr.initVerify(vKey);
  • core/java/test/junit/net/i2p/crypto/eddsa/math/ConstantsTest.java

    rc35d158 r2800791  
    11package net.i2p.crypto.eddsa.math;
    22
    3 import static org.hamcrest.Matchers.*;
    4 import static org.junit.Assert.*;
     3import static org.hamcrest.Matchers.equalTo;
     4import static org.hamcrest.Matchers.greaterThanOrEqualTo;
     5import static org.hamcrest.Matchers.is;
     6import static org.junit.Assert.assertThat;
     7import static org.junit.Assert.fail;
    58
    69import java.security.MessageDigest;
     
    1821 */
    1922public class ConstantsTest {
    20     static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     23    static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    2124    static final Curve curve = ed25519.getCurve();
    2225
  • core/java/test/junit/net/i2p/crypto/eddsa/math/GroupElementTest.java

    rc35d158 r2800791  
    2424    static final byte[] BYTES_ONETEN = Utils.hexToBytes("0a00000000000000000000000000000000000000000000000000000000000080");
    2525
    26     static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     26    static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    2727    static final Curve curve = ed25519.getCurve();
    2828
     
    7272
    7373    /**
     74     * Test method for {@link GroupElement#p3(Curve, FieldElement, FieldElement, FieldElement, FieldElement, boolean)}.
     75     */
     76    @Test
     77    public void testP3WithExplicitFlag() {
     78        final GroupElement t = GroupElement.p3(curve, ZERO, ONE, ONE, ZERO, false);
     79        assertThat(t.curve, is(equalTo(curve)));
     80        assertThat(t.repr, is(GroupElement.Representation.P3));
     81        assertThat(t.X, is(ZERO));
     82        assertThat(t.Y, is(ONE));
     83        assertThat(t.Z, is(ONE));
     84        assertThat(t.T, is(ZERO));
     85    }
     86
     87    /**
    7488     * Test method for {@link GroupElement#p1p1(Curve, FieldElement, FieldElement, FieldElement, FieldElement)}.
    7589     */
     
    128142
    129143    /**
     144     * Test method for {@link GroupElement#GroupElement(Curve, GroupElement.Representation, FieldElement, FieldElement, FieldElement, FieldElement, boolean)}.
     145     */
     146    @Test
     147    public void testGroupElementCurveRepresentationFieldElementFieldElementFieldElementFieldElementWithExplicitFlag() {
     148        final GroupElement t = new GroupElement(curve, GroupElement.Representation.P3, ZERO, ONE, ONE, ZERO, false);
     149        assertThat(t.curve, is(equalTo(curve)));
     150        assertThat(t.repr, is(GroupElement.Representation.P3));
     151        assertThat(t.X, is(ZERO));
     152        assertThat(t.Y, is(ONE));
     153        assertThat(t.Z, is(ONE));
     154        assertThat(t.T, is(ZERO));
     155    }
     156
     157    /**
    130158     * Tests {@link GroupElement#GroupElement(Curve, byte[])} and
    131159     * {@link GroupElement#toByteArray()} against valid public keys.
     
    381409    }
    382410
     411    @Test
     412    public void toP3PrecomputeDoubleReturnsExpectedResultIfGroupElementHasP1P1Representation() {
     413        for (int i=0; i<10; i++) {
     414            // Arrange:
     415            final GroupElement g = MathUtils.toRepresentation(MathUtils.getRandomGroupElement(), GroupElement.Representation.P1P1);
     416
     417            // Act:
     418            final GroupElement h1 = g.toP3PrecomputeDouble();
     419            final GroupElement h2 = MathUtils.toRepresentation(g, GroupElement.Representation.P3PrecomputedDouble);
     420
     421            // Assert:
     422            Assert.assertThat(h1, IsEqual.equalTo(h2));
     423            Assert.assertThat(h1.getRepresentation(), IsEqual.equalTo(GroupElement.Representation.P3));
     424            Assert.assertThat(h1.getX(), IsEqual.equalTo(g.getX().multiply(g.getT())));
     425            Assert.assertThat(h1.getY(), IsEqual.equalTo(g.getY().multiply(g.getZ())));
     426            Assert.assertThat(h1.getZ(), IsEqual.equalTo(g.getZ().multiply(g.getT())));
     427            Assert.assertThat(h1.getT(), IsEqual.equalTo(g.getX().multiply(g.getY())));
     428            Assert.assertThat(h1.precmp, IsNull.nullValue());
     429            Assert.assertThat(h1.dblPrecmp, IsNull.notNullValue());
     430            Assert.assertThat(h1.dblPrecmp, IsEqual.equalTo(h2.dblPrecmp));
     431        }
     432    }
     433
    383434    @Test (expected = IllegalArgumentException.class)
    384435    public void toCachedThrowsIfGroupElementHasP2Representation() {
     
    452503
    453504    /**
    454      * Test method for {@link GroupElement#precompute(boolean)}.
     505     * Test method for precomputation.
    455506     */
    456507    @Test
     
    737788    @Test
    738789    public void scalarMultiplyBasePointReturnsExpectedResult() {
    739         for (int i=0; i<100; i++) {
     790        for (int i=0; i<10; i++) {
    740791            // Arrange:
    741792            final GroupElement basePoint = ed25519.getB();
     
    760811        GroupElement A = new GroupElement(curve, Utils.hexToBytes("d4cf8595571830644bd14af416954d09ab7159751ad9e0f7a6cbd92379e71a66"));
    761812        GroupElement B = ed25519.getB();
    762         GroupElement geZero = curve.getZero(GroupElement.Representation.P3);
    763         //geZero.precompute(false);
     813        GroupElement geZero = curve.getZero(GroupElement.Representation.P3PrecomputedDouble);
    764814
    765815        // 0 * GE(0) + 0 * GE(0) = GE(0)
     
    799849    @Test
    800850    public void doubleScalarMultiplyVariableTimeReturnsExpectedResult() {
    801         for (int i=0; i<50; i++) {
     851        for (int i=0; i<10; i++) {
    802852            // Arrange:
    803853            final GroupElement basePoint = ed25519.getB();
    804             final GroupElement g = MathUtils.getRandomGroupElement();
    805             //g.precompute(false);
     854            final GroupElement g = MathUtils.getRandomGroupElement(true);
    806855            final FieldElement f1 = MathUtils.getRandomFieldElement();
    807856            final FieldElement f2 = MathUtils.getRandomFieldElement();
  • core/java/test/junit/net/i2p/crypto/eddsa/math/MathUtils.java

    rc35d158 r2800791  
    1616    private static final int[] exponents = {0, 26, 26 + 25, 2*26 + 25, 2*26 + 2*25, 3*26 + 2*25, 3*26 + 3*25, 4*26 + 3*25, 4*26 + 4*25, 5*26 + 4*25};
    1717    private static final SecureRandom random = new SecureRandom();
    18     private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     18    private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    1919    private static final Curve curve = ed25519.getCurve();
    2020    private static final BigInteger d = new BigInteger("-121665").multiply(new BigInteger("121666").modInverse(getQ()));
     
    179179     * @return The group element.
    180180     */
    181     public static GroupElement getRandomGroupElement() {
     181    public static GroupElement getRandomGroupElement() { return getRandomGroupElement(false); }
     182
     183    /**
     184     * Gets a random group element in P3 representation, with precmp and dblPrecmp populated.
     185     *
     186     * @return The group element.
     187     */
     188    public static GroupElement getRandomGroupElement(boolean precompute) {
    182189        final byte[] bytes = new byte[32];
    183190        while (true) {
    184191            try {
    185192                random.nextBytes(bytes);
    186                 return new GroupElement(curve, bytes);
     193                return new GroupElement(curve, bytes, precompute);
    187194            } catch (IllegalArgumentException e) {
    188195                // Will fail in about 87.5%, so try again.
     
    243250            case P2:
    244251            case P3:
     252            case P3PrecomputedDouble:
    245253                x = gX.multiply(gZ.modInverse(getQ())).mod(getQ());
    246254                y = gY.multiply(gZ.modInverse(getQ())).mod(getQ());
     
    276284                        toFieldElement(y),
    277285                        getField().ONE,
    278                         toFieldElement(x.multiply(y).mod(getQ())));
     286                        toFieldElement(x.multiply(y).mod(getQ())), false);
     287            case P3PrecomputedDouble:
     288                return GroupElement.p3(
     289                        curve,
     290                        toFieldElement(x),
     291                        toFieldElement(y),
     292                        getField().ONE,
     293                        toFieldElement(x.multiply(y).mod(getQ())), true);
    279294            case P1P1:
    280295                return GroupElement.p1p1(
  • core/java/test/junit/net/i2p/crypto/eddsa/math/PrecomputationTestVectors.java

    rc35d158 r2800791  
    1717
    1818    public static GroupElement[][] getPrecomputation(String fileName) {
    19         EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     19        EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    2020        Curve curve = ed25519.getCurve();
    2121        Field field = curve.getField();
     
    6060
    6161    public static GroupElement[] getDoublePrecomputation(String fileName) {
    62         EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     62        EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    6363        Curve curve = ed25519.getCurve();
    6464        Field field = curve.getField();
  • core/java/test/junit/net/i2p/crypto/eddsa/math/bigint/BigIntegerFieldElementTest.java

    rc35d158 r2800791  
    55
    66import java.math.BigInteger;
     7import java.util.Random;
    78
    89import net.i2p.crypto.eddsa.Utils;
    910import net.i2p.crypto.eddsa.math.Field;
    1011import net.i2p.crypto.eddsa.math.FieldElement;
     12import net.i2p.crypto.eddsa.math.MathUtils;
     13import net.i2p.crypto.eddsa.math.AbstractFieldElementTest;
    1114import org.junit.Test;
    1215
     
    1518 *
    1619 */
    17 public class BigIntegerFieldElementTest {
     20public class BigIntegerFieldElementTest extends AbstractFieldElementTest {
    1821    static final byte[] BYTES_ZERO = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
    1922    static final byte[] BYTES_ONE = Utils.hexToBytes("0100000000000000000000000000000000000000000000000000000000000000");
     
    2831    static final FieldElement ONE = new BigIntegerFieldElement(ed25519Field, BigInteger.ONE);
    2932    static final FieldElement TWO = new BigIntegerFieldElement(ed25519Field, BigInteger.valueOf(2));
     33
     34    protected FieldElement getRandomFieldElement() {
     35        BigInteger r;
     36        Random rnd = new Random();
     37        do {
     38            r = new BigInteger(255, rnd);
     39        } while (r.compareTo(getQ()) >= 0);
     40        return new BigIntegerFieldElement(ed25519Field, r);
     41    }
     42
     43    protected BigInteger toBigInteger(FieldElement f) {
     44        return ((BigIntegerFieldElement)f).bi;
     45    }
     46
     47    protected BigInteger getQ() {
     48        return MathUtils.getQ();
     49    }
     50
     51    protected Field getField() {
     52        return ed25519Field;
     53    }
    3054
    3155    /**
     
    5983    // region isNonZero
    6084
    61     @Test
    62     public void isNonZeroReturnsFalseIfFieldElementIsZero() {
    63         // Assert:
    64         assertThat(ZERO.isNonZero(), is(equalTo(false)));
     85    protected FieldElement getZeroFieldElement() {
     86        return ZERO;
    6587    }
    6688
    67     @Test
    68     public void isNonZeroReturnsTrueIfFieldElementIsNonZero() {
    69         // Assert:
    70         assertThat(TWO.isNonZero(), is(equalTo(true)));
     89    protected FieldElement getNonZeroFieldElement() {
     90        return TWO;
    7191    }
    7292
    7393    // endregion
    74 
    75     /**
    76      * Test method for {@link FieldElement#isNegative()}.
    77      */
    78     @Test
    79     public void testIsNegative() {
    80         //fail("Not yet implemented");
    81     }
    82 
    83     /**
    84      * Test method for {@link FieldElement#add(FieldElement)}.
    85      */
    86     @Test
    87     public void testAdd() {
    88         //fail("Not yet implemented");
    89     }
    90 
    91     /**
    92      * Test method for {@link FieldElement#subtract(FieldElement)}.
    93      */
    94     @Test
    95     public void testSubtract() {
    96         //fail("Not yet implemented");
    97     }
    98 
    99     /**
    100      * Test method for {@link FieldElement#negate()}.
    101      */
    102     @Test
    103     public void testNegate() {
    104         //fail("Not yet implemented");
    105     }
    106 
    107     /**
    108      * Test method for {@link FieldElement#multiply(FieldElement)}.
    109      */
    110     @Test
    111     public void testMultiply() {
    112         //fail("Not yet implemented");
    113     }
    114 
    115     /**
    116      * Test method for {@link FieldElement#square()}.
    117      */
    118     @Test
    119     public void testSquare() {
    120         //fail("Not yet implemented");
    121     }
    122 
    123     /**
    124      * Test method for {@link FieldElement#squareAndDouble()}.
    125      */
    126     @Test
    127     public void testSquareAndDouble() {
    128         //fail("Not yet implemented");
    129     }
    130 
    131     /**
    132      * Test method for {@link FieldElement#invert()}.
    133      */
    134     @Test
    135     public void testInvert() {
    136         //fail("Not yet implemented");
    137     }
    138 
    139     /**
    140      * Test method for {@link FieldElement#pow22523()}.
    141      */
    142     @Test
    143     public void testPow22523() {
    144         //fail("Not yet implemented");
    145     }
    14694
    14795    /**
  • core/java/test/junit/net/i2p/crypto/eddsa/math/bigint/BigIntegerScalarOpsTest.java

    rc35d158 r2800791  
    2323public class BigIntegerScalarOpsTest {
    2424
    25     static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     25    static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
    2626    static final Field ed25519Field = ed25519.getCurve().getField();
    2727
  • core/java/test/junit/net/i2p/crypto/eddsa/math/ed25519/Ed25519FieldElementTest.java

    rc35d158 r2800791  
    1010 * Tests rely on the BigInteger class.
    1111 */
    12 public class Ed25519FieldElementTest {
     12public class Ed25519FieldElementTest extends AbstractFieldElementTest {
     13
     14    protected FieldElement getRandomFieldElement() {
     15        return MathUtils.getRandomFieldElement();
     16    }
     17
     18    protected BigInteger toBigInteger(FieldElement f) {
     19        return MathUtils.toBigInteger(f);
     20    }
     21
     22    protected BigInteger getQ() {
     23        return MathUtils.getQ();
     24    }
     25
     26    protected Field getField() {
     27        return MathUtils.getField();
     28    }
    1329
    1430    // region constructor
     
    3652    // region isNonZero
    3753
    38     @Test
    39     public void isNonZeroReturnsFalseIfFieldElementIsZero() {
    40         // Act:
    41         final FieldElement f = new Ed25519FieldElement(MathUtils.getField(), new int[10]);
    42 
    43         // Assert:
    44         Assert.assertThat(f.isNonZero(), IsEqual.equalTo(false));
     54    protected FieldElement getZeroFieldElement() {
     55        return new Ed25519FieldElement(MathUtils.getField(), new int[10]);
    4556    }
    4657
    47     @Test
    48     public void isNonZeroReturnsTrueIfFieldElementIsNonZero() {
    49         // Act:
     58    protected FieldElement getNonZeroFieldElement() {
    5059        final int[] t = new int[10];
    5160        t[0] = 5;
    52         final FieldElement f = new Ed25519FieldElement(MathUtils.getField(), t);
    53 
    54         // Assert:
    55         Assert.assertThat(f.isNonZero(), IsEqual.equalTo(true));
     61        return new Ed25519FieldElement(MathUtils.getField(), t);
    5662    }
    5763
    5864    // endregion
    5965
    60     // region mod q arithmetic
    61 
    62     @Test
    63     public void addReturnsCorrectResult() {
    64         for (int i=0; i<1000; i++) {
    65             // Arrange:
    66             final FieldElement f1 = MathUtils.getRandomFieldElement();
    67             final FieldElement f2 = MathUtils.getRandomFieldElement();
    68             final BigInteger b1 = MathUtils.toBigInteger(f1);
    69             final BigInteger b2 = MathUtils.toBigInteger(f2);
    70 
    71             // Act:
    72             final FieldElement f3 = f1.add(f2);
    73             final BigInteger b3 = MathUtils.toBigInteger(f3).mod(MathUtils.getQ());
    74 
    75             // Assert:
    76             Assert.assertThat(b3, IsEqual.equalTo(b1.add(b2).mod(MathUtils.getQ())));
    77         }
    78     }
    79 
    80     @Test
    81     public void subtractReturnsCorrectResult() {
    82         for (int i=0; i<1000; i++) {
    83             // Arrange:
    84             final FieldElement f1 = MathUtils.getRandomFieldElement();
    85             final FieldElement f2 = MathUtils.getRandomFieldElement();
    86             final BigInteger b1 = MathUtils.toBigInteger(f1);
    87             final BigInteger b2 = MathUtils.toBigInteger(f2);
    88 
    89             // Act:
    90             final FieldElement f3 = f1.subtract(f2);
    91             final BigInteger b3 = MathUtils.toBigInteger(f3).mod(MathUtils.getQ());
    92 
    93             // Assert:
    94             Assert.assertThat(b3, IsEqual.equalTo(b1.subtract(b2).mod(MathUtils.getQ())));
    95         }
    96     }
    97 
    98     @Test
    99     public void negateReturnsCorrectResult() {
    100         for (int i=0; i<1000; i++) {
    101             // Arrange:
    102             final FieldElement f1 = MathUtils.getRandomFieldElement();
    103             final BigInteger b1 = MathUtils.toBigInteger(f1);
    104 
    105             // Act:
    106             final FieldElement f2 = f1.negate();
    107             final BigInteger b2 = MathUtils.toBigInteger(f2).mod(MathUtils.getQ());
    108 
    109             // Assert:
    110             Assert.assertThat(b2, IsEqual.equalTo(b1.negate().mod(MathUtils.getQ())));
    111         }
    112     }
    113 
    114     @Test
    115     public void multiplyReturnsCorrectResult() {
    116         for (int i=0; i<1000; i++) {
    117             // Arrange:
    118             final FieldElement f1 = MathUtils.getRandomFieldElement();
    119             final FieldElement f2 = MathUtils.getRandomFieldElement();
    120             final BigInteger b1 = MathUtils.toBigInteger(f1);
    121             final BigInteger b2 = MathUtils.toBigInteger(f2);
    122 
    123             // Act:
    124             final FieldElement f3 = f1.multiply(f2);
    125             final BigInteger b3 = MathUtils.toBigInteger(f3).mod(MathUtils.getQ());
    126 
    127             // Assert:
    128             Assert.assertThat(b3, IsEqual.equalTo(b1.multiply(b2).mod(MathUtils.getQ())));
    129         }
    130     }
    131 
    132     @Test
    133     public void squareReturnsCorrectResult() {
    134         for (int i=0; i<1000; i++) {
    135             // Arrange:
    136             final FieldElement f1 = MathUtils.getRandomFieldElement();
    137             final BigInteger b1 = MathUtils.toBigInteger(f1);
    138 
    139             // Act:
    140             final FieldElement f2 = f1.square();
    141             final BigInteger b2 = MathUtils.toBigInteger(f2).mod(MathUtils.getQ());
    142 
    143             // Assert:
    144             Assert.assertThat(b2, IsEqual.equalTo(b1.multiply(b1).mod(MathUtils.getQ())));
    145         }
    146     }
    147 
    148     @Test
    149     public void squareAndDoubleReturnsCorrectResult() {
    150         for (int i=0; i<1000; i++) {
    151             // Arrange:
    152             final FieldElement f1 = MathUtils.getRandomFieldElement();
    153             final BigInteger b1 = MathUtils.toBigInteger(f1);
    154 
    155             // Act:
    156             final FieldElement f2 = f1.squareAndDouble();
    157             final BigInteger b2 = MathUtils.toBigInteger(f2).mod(MathUtils.getQ());
    158 
    159             // Assert:
    160             Assert.assertThat(b2, IsEqual.equalTo(b1.multiply(b1).multiply(new BigInteger("2")).mod(MathUtils.getQ())));
    161         }
    162     }
    163 
    164     @Test
    165     public void invertReturnsCorrectResult() {
    166         for (int i=0; i<1000; i++) {
    167             // Arrange:
    168             final FieldElement f1 = MathUtils.getRandomFieldElement();
    169             final BigInteger b1 = MathUtils.toBigInteger(f1);
    170 
    171             // Act:
    172             final FieldElement f2 = f1.invert();
    173             final BigInteger b2 = MathUtils.toBigInteger(f2).mod(MathUtils.getQ());
    174 
    175             // Assert:
    176             Assert.assertThat(b2, IsEqual.equalTo(b1.modInverse(MathUtils.getQ())));
    177         }
    178     }
    179 
    180     @Test
    181     public void pow22523ReturnsCorrectResult() {
    182         for (int i=0; i<1000; i++) {
    183             // Arrange:
    184             final FieldElement f1 = MathUtils.getRandomFieldElement();
    185             final BigInteger b1 = MathUtils.toBigInteger(f1);
    186 
    187             // Act:
    188             final FieldElement f2 = f1.pow22523();
    189             final BigInteger b2 = MathUtils.toBigInteger(f2).mod(MathUtils.getQ());
    190 
    191             // Assert:
    192             Assert.assertThat(b2, IsEqual.equalTo(b1.modPow(BigInteger.ONE.shiftLeft(252).subtract(new BigInteger("3")), MathUtils.getQ())));
    193         }
    194     }
    195 
    196     // endregion
    197 
    198     // region hashCode / equals
    199 
    200     @Test
    201     public void equalsOnlyReturnsTrueForEquivalentObjects() {
    202         // Arrange:
    203         final FieldElement f1 = MathUtils.getRandomFieldElement();
    204         final FieldElement f2 = MathUtils.getField().getEncoding().decode(f1.toByteArray());
    205         final FieldElement f3 = MathUtils.getRandomFieldElement();
    206         final FieldElement f4 = MathUtils.getRandomFieldElement();
    207 
    208         // Assert:
    209         Assert.assertThat(f1, IsEqual.equalTo(f2));
    210         Assert.assertThat(f1, IsNot.not(IsEqual.equalTo(f3)));
    211         Assert.assertThat(f1, IsNot.not(IsEqual.equalTo(f4)));
    212         Assert.assertThat(f3, IsNot.not(IsEqual.equalTo(f4)));
    213     }
    214 
    215     @Test
    216     public void hashCodesAreEqualForEquivalentObjects() {
    217         // Arrange:
    218         final FieldElement f1 = MathUtils.getRandomFieldElement();
    219         final FieldElement f2 = MathUtils.getField().getEncoding().decode(f1.toByteArray());
    220         final FieldElement f3 = MathUtils.getRandomFieldElement();
    221         final FieldElement f4 = MathUtils.getRandomFieldElement();
    222 
    223         // Assert:
    224         Assert.assertThat(f1.hashCode(), IsEqual.equalTo(f2.hashCode()));
    225         Assert.assertThat(f1.hashCode(), IsNot.not(IsEqual.equalTo(f3.hashCode())));
    226         Assert.assertThat(f1.hashCode(), IsNot.not(IsEqual.equalTo(f4.hashCode())));
    227         Assert.assertThat(f3.hashCode(), IsNot.not(IsEqual.equalTo(f4.hashCode())));
    228     }
    229 
    230     // endregion
    231 
    232     //region toString
     66    // region toString
    23367
    23468    @Test
  • core/java/test/junit/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpecTest.java

    rc35d158 r2800791  
    44import static org.junit.Assert.*;
    55import net.i2p.crypto.eddsa.Utils;
     6import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
    67
     8import org.junit.Rule;
    79import org.junit.Test;
     10import org.junit.rules.ExpectedException;
    811
    912/**
     
    1316public class EdDSAPrivateKeySpecTest {
    1417    static final byte[] ZERO_SEED = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
     18    static final byte[] ZERO_H = Utils.hexToBytes("5046adc1dba838867b2bbbfdd0c3423e58b57970b5267a90f57960924a87f1960a6a85eaa642dac835424b5d7c8d637c00408c7a73da672b7f498521420b6dd3");
    1519    static final byte[] ZERO_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");
    1620
    17     static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("ed25519-sha-512");
     21    static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
     22
     23    @Rule
     24    public ExpectedException exception = ExpectedException.none();
    1825
    1926    /**
     
    2431        EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(ZERO_SEED, ed25519);
    2532        assertThat(key.getSeed(), is(equalTo(ZERO_SEED)));
     33        assertThat(key.getH(), is(equalTo(ZERO_H)));
    2634        assertThat(key.getA().toByteArray(), is(equalTo(ZERO_PK)));
    2735    }
    2836
     37    @Test
     38    public void incorrectSeedLengthThrows() {
     39        exception.expect(IllegalArgumentException.class);
     40        exception.expectMessage("seed length is wrong");
     41        EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(new byte[2], ed25519);
     42    }
     43
     44    /**
     45     * Test method for {@link net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec#EdDSAPrivateKeySpec(net.i2p.crypto.eddsa.spec.EdDSAParameterSpec, byte[])}.
     46     */
     47    @Test
     48    public void testEdDSAPrivateKeySpecFromH() {
     49        EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(ed25519, ZERO_H);
     50        assertThat(key.getSeed(), is(nullValue()));
     51        assertThat(key.getH(), is(equalTo(ZERO_H)));
     52        assertThat(key.getA().toByteArray(), is(equalTo(ZERO_PK)));
     53    }
     54
     55    @Test
     56    public void incorrectHashLengthThrows() {
     57        exception.expect(IllegalArgumentException.class);
     58        exception.expectMessage("hash length is wrong");
     59        EdDSAPrivateKeySpec key = new EdDSAPrivateKeySpec(ed25519, new byte[2]);
     60    }
    2961}
Note: See TracChangeset for help on using the changeset viewer.