From 7a7ae77c8390288746c87f6d35815cfdf7264696 Mon Sep 17 00:00:00 2001 From: str4d Date: Wed, 12 Nov 2014 10:20:28 +0000 Subject: [PATCH] Updated EdDSA code Source: https://github.com/str4d/ed25519-java Git commit: 58e4efadf972f4dc4f67c05152f82b49fb22bac6 --- core/java/src/net/i2p/crypto/eddsa/Utils.java | 1 + .../src/net/i2p/crypto/eddsa/math/Curve.java | 4 +- .../net/i2p/crypto/eddsa/math/Encoding.java | 2 +- .../src/net/i2p/crypto/eddsa/math/Field.java | 28 +- .../i2p/crypto/eddsa/math/FieldElement.java | 7 +- .../i2p/crypto/eddsa/math/GroupElement.java | 720 +++++++++++++----- .../net/i2p/crypto/eddsa/math/ScalarOps.java | 3 +- .../BigIntegerLittleEndianEncoding.java | 2 +- .../math/ed25519/Ed25519FieldElement.java | 369 ++++++--- .../ed25519/Ed25519LittleEndianEncoding.java | 95 ++- .../eddsa/math/ed25519/Ed25519ScalarOps.java | 181 +++-- 11 files changed, 975 insertions(+), 437 deletions(-) diff --git a/core/java/src/net/i2p/crypto/eddsa/Utils.java b/core/java/src/net/i2p/crypto/eddsa/Utils.java index 92cafaf23..3ade2ced4 100644 --- a/core/java/src/net/i2p/crypto/eddsa/Utils.java +++ b/core/java/src/net/i2p/crypto/eddsa/Utils.java @@ -31,6 +31,7 @@ public class Utils { for (int i = 0; i < 32; i++) { result |= b[i] ^ c[i]; } + return equal(result, 0); } diff --git a/core/java/src/net/i2p/crypto/eddsa/math/Curve.java b/core/java/src/net/i2p/crypto/eddsa/math/Curve.java index 4cbc74757..2c6ade4ea 100644 --- a/core/java/src/net/i2p/crypto/eddsa/math/Curve.java +++ b/core/java/src/net/i2p/crypto/eddsa/math/Curve.java @@ -27,8 +27,8 @@ public class Curve implements Serializable { this.d2 = this.d.add(this.d); this.I = I; - FieldElement zero = f.zero; - FieldElement one = f.one; + FieldElement zero = f.ZERO; + FieldElement one = f.ONE; zeroP2 = GroupElement.p2(this, zero, one, one); zeroP3 = GroupElement.p3(this, zero, one, one, zero); zeroPrecomp = GroupElement.precomp(this, one, one, zero); diff --git a/core/java/src/net/i2p/crypto/eddsa/math/Encoding.java b/core/java/src/net/i2p/crypto/eddsa/math/Encoding.java index ffc01f030..6dcb0e832 100644 --- a/core/java/src/net/i2p/crypto/eddsa/math/Encoding.java +++ b/core/java/src/net/i2p/crypto/eddsa/math/Encoding.java @@ -32,7 +32,7 @@ public abstract class Encoding { public abstract FieldElement decode(byte[] in); /** - * From the Ed25519 paper: + * From the Ed25519 paper:
* x is negative if the (b-1)-bit encoding of x is lexicographically larger * than the (b-1)-bit encoding of -x. If q is an odd prime and the encoding * is the little-endian representation of {0, 1,..., q-1} then the negative diff --git a/core/java/src/net/i2p/crypto/eddsa/math/Field.java b/core/java/src/net/i2p/crypto/eddsa/math/Field.java index ce89b7851..dd8fceddf 100644 --- a/core/java/src/net/i2p/crypto/eddsa/math/Field.java +++ b/core/java/src/net/i2p/crypto/eddsa/math/Field.java @@ -12,12 +12,12 @@ import java.io.Serializable; public class Field implements Serializable { private static final long serialVersionUID = 8746587465875676L; - public final FieldElement zero; - public final FieldElement one; - public final FieldElement two; - public final FieldElement four; - public final FieldElement five; - public final FieldElement eight; + public final FieldElement ZERO; + public final FieldElement ONE; + public final FieldElement TWO; + public final FieldElement FOUR; + public final FieldElement FIVE; + public final FieldElement EIGHT; private final int b; private final FieldElement q; @@ -39,16 +39,16 @@ public class Field implements Serializable { this.q = fromByteArray(q); // Set up constants - zero = fromByteArray(Constants.ZERO); - one = fromByteArray(Constants.ONE); - two = fromByteArray(Constants.TWO); - four = fromByteArray(Constants.FOUR); - five = fromByteArray(Constants.FIVE); - eight = fromByteArray(Constants.EIGHT); + ZERO = fromByteArray(Constants.ZERO); + ONE = fromByteArray(Constants.ONE); + TWO = fromByteArray(Constants.TWO); + FOUR = fromByteArray(Constants.FOUR); + FIVE = fromByteArray(Constants.FIVE); + EIGHT = fromByteArray(Constants.EIGHT); // Precompute values - qm2 = this.q.subtract(two); - qm5d8 = this.q.subtract(five).divide(eight); + qm2 = this.q.subtract(TWO); + qm5d8 = this.q.subtract(FIVE).divide(EIGHT); } public FieldElement fromByteArray(byte[] x) { diff --git a/core/java/src/net/i2p/crypto/eddsa/math/FieldElement.java b/core/java/src/net/i2p/crypto/eddsa/math/FieldElement.java index 4d9fc6a52..7b29590ef 100644 --- a/core/java/src/net/i2p/crypto/eddsa/math/FieldElement.java +++ b/core/java/src/net/i2p/crypto/eddsa/math/FieldElement.java @@ -9,6 +9,9 @@ public abstract class FieldElement { protected final Field f; public FieldElement(Field f) { + if (null == f) { + throw new IllegalArgumentException("field cannot be null"); + } this.f = f; } @@ -29,13 +32,13 @@ public abstract class FieldElement { public abstract FieldElement add(FieldElement val); public FieldElement addOne() { - return add(f.one); + return add(f.ONE); } public abstract FieldElement subtract(FieldElement val); public FieldElement subtractOne() { - return subtract(f.one); + return subtract(f.ONE); } public abstract FieldElement negate(); diff --git a/core/java/src/net/i2p/crypto/eddsa/math/GroupElement.java b/core/java/src/net/i2p/crypto/eddsa/math/GroupElement.java index 1e416abe0..268005ed7 100644 --- a/core/java/src/net/i2p/crypto/eddsa/math/GroupElement.java +++ b/core/java/src/net/i2p/crypto/eddsa/math/GroupElement.java @@ -1,11 +1,22 @@ package net.i2p.crypto.eddsa.math; -import java.io.Serializable; - import net.i2p.crypto.eddsa.Utils; +import java.io.Serializable; +import java.util.Arrays; + /** * A point (x,y) on an EdDSA curve. + *

+ * Reviewed/commented by Bloody Rookie (nemproject@gmx.de) + *

+ * Literature:
+ * [1] Daniel J. Bernstein, Niels Duif, Tanja Lange, Peter Schwabe and Bo-Yin Yang : High-speed high-security signatures
+ * [2] Huseyin Hisil, Kenneth Koon-Ho Wong, Gary Carter, Ed Dawson: Twisted Edwards Curves Revisited
+ * [3] Daniel J. Bernsteina, Tanja Lange: A complete set of addition laws for incomplete Edwards curves
+ * [4] Daniel J. Bernstein, Peter Birkner, Marc Joye, Tanja Lange and Christiane Peters: Twisted Edwards Curves
+ * [5] Christiane Pascale Peters: Curves, Codes, and Cryptography (PhD thesis)
+ * [6] Daniel J. Bernstein, Peter Birkner, Tanja Lange and Christiane Peters: Optimizing double-base elliptic-curve single-scalar multiplication
* * @since 0.9.15 * @author str4d @@ -14,12 +25,21 @@ import net.i2p.crypto.eddsa.Utils; public class GroupElement implements Serializable { private static final long serialVersionUID = 2395879087349587L; + /** + * Available representations for a group element. + *