Skip to content

Commit

Permalink
Merge pull request #45 from str4d/43-named-curve-contract
Browse files Browse the repository at this point in the history
Re-introduce a constant specifying the name of Ed25519
str4d authored Dec 2, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents c7b1d62 + 877ccef commit 581c6a9
Showing 11 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/net/i2p/crypto/eddsa/EdDSAPrivateKey.java
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ public EdDSAPrivateKey(EdDSAPrivateKeySpec spec) {

public EdDSAPrivateKey(PKCS8EncodedKeySpec spec) throws InvalidKeySpecException {
this(new EdDSAPrivateKeySpec(decode(spec.getEncoded()),
EdDSANamedCurveTable.getByName("Ed25519")));
EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)));
}

@Override
@@ -136,7 +136,7 @@ public String getFormat() {
*/
@Override
public byte[] getEncoded() {
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName("Ed25519")))
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)))
return null;
if (seed == null)
return null;
4 changes: 2 additions & 2 deletions src/net/i2p/crypto/eddsa/EdDSAPublicKey.java
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ public EdDSAPublicKey(EdDSAPublicKeySpec spec) {

public EdDSAPublicKey(X509EncodedKeySpec spec) throws InvalidKeySpecException {
this(new EdDSAPublicKeySpec(decode(spec.getEncoded()),
EdDSANamedCurveTable.getByName("Ed25519")));
EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)));
}

@Override
@@ -113,7 +113,7 @@ public String getFormat() {
*/
@Override
public byte[] getEncoded() {
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName("Ed25519")))
if (!edDsaSpec.equals(EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519)))
return null;
int totlen = 12 + Abyte.length;
byte[] rv = new byte[totlen];
2 changes: 1 addition & 1 deletion src/net/i2p/crypto/eddsa/KeyPairGenerator.java
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public final class KeyPairGenerator extends KeyPairGeneratorSpi {
static {
edParameters = new Hashtable<Integer, AlgorithmParameterSpec>();

edParameters.put(Integer.valueOf(256), new EdDSAGenParameterSpec("Ed25519"));
edParameters.put(Integer.valueOf(256), new EdDSAGenParameterSpec(EdDSANamedCurveTable.ED_25519));
}

public void initialize(int keysize, SecureRandom random) {
4 changes: 3 additions & 1 deletion src/net/i2p/crypto/eddsa/spec/EdDSANamedCurveTable.java
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
*
*/
public class EdDSANamedCurveTable {
public static final String ED_25519 = "Ed25519";

private static final Field ed25519field = new Field(
256, // b
Utils.hexToBytes("edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), // q
@@ -36,7 +38,7 @@ public class EdDSANamedCurveTable {
ed25519field.fromByteArray(Utils.hexToBytes("b0a00e4a271beec478e42fad0618432fa7d7fb3d99004d2b0bdfc14f8024832b"))); // I

private static final EdDSANamedCurveSpec ed25519 = new EdDSANamedCurveSpec(
"Ed25519",
ED_25519,
ed25519curve,
"SHA-512", // H
new Ed25519ScalarOps(), // l
22 changes: 11 additions & 11 deletions test/net/i2p/crypto/eddsa/EdDSAEngineTest.java
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ public class EdDSAEngineTest {

@Test
public void testSign() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));

@@ -67,7 +67,7 @@ public void testSign() throws Exception {

@Test
public void testVerify() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
for (Ed25519TestVectors.TestTuple testCase : Ed25519TestVectors.testCases) {
@@ -87,7 +87,7 @@ public void testVerify() throws Exception {
*/
@Test
public void testVerifyWrongSigLength() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
//Signature sgr = Signature.getInstance("EdDSA", "I2P");
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
@@ -103,7 +103,7 @@ public void testVerifyWrongSigLength() throws Exception {

@Test
public void testSignResetsForReuse() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
PrivateKey sKey = new EdDSAPrivateKey(privKey);
@@ -120,7 +120,7 @@ public void testSignResetsForReuse() throws Exception {

@Test
public void testVerifyResetsForReuse() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
PublicKey vKey = new EdDSAPublicKey(pubKey);
@@ -137,7 +137,7 @@ public void testVerifyResetsForReuse() throws Exception {

@Test
public void testSignOneShotMode() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
PrivateKey sKey = new EdDSAPrivateKey(privKey);
@@ -151,7 +151,7 @@ public void testSignOneShotMode() throws Exception {

@Test
public void testVerifyOneShotMode() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
PublicKey vKey = new EdDSAPublicKey(pubKey);
@@ -165,7 +165,7 @@ public void testVerifyOneShotMode() throws Exception {

@Test
public void testSignOneShotModeMultipleUpdates() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
PrivateKey sKey = new EdDSAPrivateKey(privKey);
@@ -181,7 +181,7 @@ public void testSignOneShotModeMultipleUpdates() throws Exception {

@Test
public void testVerifyOneShotModeMultipleUpdates() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
Signature sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
PublicKey vKey = new EdDSAPublicKey(pubKey);
@@ -197,7 +197,7 @@ public void testVerifyOneShotModeMultipleUpdates() throws Exception {

@Test
public void testSignOneShot() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPrivateKeySpec privKey = new EdDSAPrivateKeySpec(TEST_SEED, spec);
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
PrivateKey sKey = new EdDSAPrivateKey(privKey);
@@ -208,7 +208,7 @@ public void testSignOneShot() throws Exception {

@Test
public void testVerifyOneShot() throws Exception {
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName("Ed25519");
EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
EdDSAPublicKeySpec pubKey = new EdDSAPublicKeySpec(TEST_PK, spec);
EdDSAEngine sgr = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
PublicKey vKey = new EdDSAPublicKey(pubKey);
2 changes: 1 addition & 1 deletion test/net/i2p/crypto/eddsa/math/ConstantsTest.java
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
*
*/
public class ConstantsTest {
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
static final Curve curve = ed25519.getCurve();

static final FieldElement ZERO = curve.getField().ZERO;
2 changes: 1 addition & 1 deletion test/net/i2p/crypto/eddsa/math/GroupElementTest.java
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public class GroupElementTest {
static final byte[] BYTES_TENZERO = Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
static final byte[] BYTES_ONETEN = Utils.hexToBytes("0a00000000000000000000000000000000000000000000000000000000000080");

static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
static final Curve curve = ed25519.getCurve();

static final FieldElement ZERO = curve.getField().ZERO;
2 changes: 1 addition & 1 deletion test/net/i2p/crypto/eddsa/math/MathUtils.java
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
public class MathUtils {
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};
private static final SecureRandom random = new SecureRandom();
private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
private static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
private static final Curve curve = ed25519.getCurve();
private static final BigInteger d = new BigInteger("-121665").multiply(new BigInteger("121666").modInverse(getQ()));
private static final BigInteger groupOrder = BigInteger.ONE.shiftLeft(252).add(new BigInteger("27742317777372353535851937790883648493"));
4 changes: 2 additions & 2 deletions test/net/i2p/crypto/eddsa/math/PrecomputationTestVectors.java
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public class PrecomputationTestVectors {
static GroupElement[] testDblPrecmp = getDoublePrecomputation("baseDblPrecmp");

public static GroupElement[][] getPrecomputation(String fileName) {
EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Curve curve = ed25519.getCurve();
Field field = curve.getField();
GroupElement[][] precmp = new GroupElement[32][8];
@@ -70,7 +70,7 @@ else if (line.equals("},")) {
}

public static GroupElement[] getDoublePrecomputation(String fileName) {
EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
Curve curve = ed25519.getCurve();
Field field = curve.getField();
GroupElement[] dblPrecmp = new GroupElement[8];
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@
*/
public class BigIntegerScalarOpsTest {

static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);
static final Field ed25519Field = ed25519.getCurve().getField();

/**
3 changes: 2 additions & 1 deletion test/net/i2p/crypto/eddsa/spec/EdDSAPrivateKeySpecTest.java
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import net.i2p.crypto.eddsa.Utils;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;

import org.junit.Rule;
import org.junit.Test;
@@ -28,7 +29,7 @@ public class EdDSAPrivateKeySpecTest {
static final byte[] ZERO_H = Utils.hexToBytes("5046adc1dba838867b2bbbfdd0c3423e58b57970b5267a90f57960924a87f1960a6a85eaa642dac835424b5d7c8d637c00408c7a73da672b7f498521420b6dd3");
static final byte[] ZERO_PK = Utils.hexToBytes("3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29");

static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName("Ed25519");
static final EdDSANamedCurveSpec ed25519 = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);

@Rule
public ExpectedException exception = ExpectedException.none();

0 comments on commit 581c6a9

Please sign in to comment.