Skip to content

Commit 4cafe3a

Browse files
committed
EdDSA cleanup (thx zzz)
- Remove duplicate load3 and load4 methods - Change load3 return type to int - Comment out dead stores - Re-roll the add, subtract, and negate loops; there's no speed benefit or timing reason to unroll them - Check for field already set - Remove shifts by 0
1 parent f9a9213 commit 4cafe3a

File tree

4 files changed

+93
-170
lines changed

4 files changed

+93
-170
lines changed

src/net/i2p/crypto/eddsa/math/Encoding.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
public abstract class Encoding {
1010
protected Field f;
1111

12-
public void setField(Field f) {
12+
public synchronized void setField(Field f) {
13+
if (this.f != null)
14+
throw new IllegalStateException("already set");
1315
this.f = f;
1416
}
1517

src/net/i2p/crypto/eddsa/math/ed25519/Ed25519FieldElement.java

Lines changed: 12 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Ed25519FieldElement extends FieldElement {
1313
/**
1414
* Variable is package private for encoding.
1515
*/
16-
int[] t;
16+
final int[] t;
1717

1818
public Ed25519FieldElement(Field f, int[] t) {
1919
super(f);
@@ -22,11 +22,11 @@ public Ed25519FieldElement(Field f, int[] t) {
2222
this.t = t;
2323
}
2424

25-
private static final byte[] zero = new byte[32];
25+
private static final byte[] ZERO = new byte[32];
2626

2727
public boolean isNonZero() {
2828
byte[] s = toByteArray();
29-
return Utils.equal(s, zero) == 1;
29+
return Utils.equal(s, ZERO) == 1;
3030
}
3131

3232
/**
@@ -42,47 +42,10 @@ public boolean isNonZero() {
4242
*/
4343
public FieldElement add(FieldElement val) {
4444
int[] g = ((Ed25519FieldElement)val).t;
45-
int f0 = t[0];
46-
int f1 = t[1];
47-
int f2 = t[2];
48-
int f3 = t[3];
49-
int f4 = t[4];
50-
int f5 = t[5];
51-
int f6 = t[6];
52-
int f7 = t[7];
53-
int f8 = t[8];
54-
int f9 = t[9];
55-
int g0 = g[0];
56-
int g1 = g[1];
57-
int g2 = g[2];
58-
int g3 = g[3];
59-
int g4 = g[4];
60-
int g5 = g[5];
61-
int g6 = g[6];
62-
int g7 = g[7];
63-
int g8 = g[8];
64-
int g9 = g[9];
65-
int h0 = f0 + g0;
66-
int h1 = f1 + g1;
67-
int h2 = f2 + g2;
68-
int h3 = f3 + g3;
69-
int h4 = f4 + g4;
70-
int h5 = f5 + g5;
71-
int h6 = f6 + g6;
72-
int h7 = f7 + g7;
73-
int h8 = f8 + g8;
74-
int h9 = f9 + g9;
7545
int[] h = new int[10];
76-
h[0] = h0;
77-
h[1] = h1;
78-
h[2] = h2;
79-
h[3] = h3;
80-
h[4] = h4;
81-
h[5] = h5;
82-
h[6] = h6;
83-
h[7] = h7;
84-
h[8] = h8;
85-
h[9] = h9;
46+
for (int i = 0; i < 10; i++) {
47+
h[i] = t[i] + g[i];
48+
}
8649
return new Ed25519FieldElement(f, h);
8750
}
8851

@@ -99,47 +62,10 @@ public FieldElement add(FieldElement val) {
9962
**/
10063
public FieldElement subtract(FieldElement val) {
10164
int[] g = ((Ed25519FieldElement)val).t;
102-
int f0 = t[0];
103-
int f1 = t[1];
104-
int f2 = t[2];
105-
int f3 = t[3];
106-
int f4 = t[4];
107-
int f5 = t[5];
108-
int f6 = t[6];
109-
int f7 = t[7];
110-
int f8 = t[8];
111-
int f9 = t[9];
112-
int g0 = g[0];
113-
int g1 = g[1];
114-
int g2 = g[2];
115-
int g3 = g[3];
116-
int g4 = g[4];
117-
int g5 = g[5];
118-
int g6 = g[6];
119-
int g7 = g[7];
120-
int g8 = g[8];
121-
int g9 = g[9];
122-
int h0 = f0 - g0;
123-
int h1 = f1 - g1;
124-
int h2 = f2 - g2;
125-
int h3 = f3 - g3;
126-
int h4 = f4 - g4;
127-
int h5 = f5 - g5;
128-
int h6 = f6 - g6;
129-
int h7 = f7 - g7;
130-
int h8 = f8 - g8;
131-
int h9 = f9 - g9;
13265
int[] h = new int[10];
133-
h[0] = h0;
134-
h[1] = h1;
135-
h[2] = h2;
136-
h[3] = h3;
137-
h[4] = h4;
138-
h[5] = h5;
139-
h[6] = h6;
140-
h[7] = h7;
141-
h[8] = h8;
142-
h[9] = h9;
66+
for (int i = 0; i < 10; i++) {
67+
h[i] = t[i] - g[i];
68+
}
14369
return new Ed25519FieldElement(f, h);
14470
}
14571

@@ -153,37 +79,10 @@ public FieldElement subtract(FieldElement val) {
15379
* |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
15480
*/
15581
public FieldElement negate() {
156-
int f0 = t[0];
157-
int f1 = t[1];
158-
int f2 = t[2];
159-
int f3 = t[3];
160-
int f4 = t[4];
161-
int f5 = t[5];
162-
int f6 = t[6];
163-
int f7 = t[7];
164-
int f8 = t[8];
165-
int f9 = t[9];
166-
int h0 = -f0;
167-
int h1 = -f1;
168-
int h2 = -f2;
169-
int h3 = -f3;
170-
int h4 = -f4;
171-
int h5 = -f5;
172-
int h6 = -f6;
173-
int h7 = -f7;
174-
int h8 = -f8;
175-
int h9 = -f9;
17682
int[] h = new int[10];
177-
h[0] = h0;
178-
h[1] = h1;
179-
h[2] = h2;
180-
h[3] = h3;
181-
h[4] = h4;
182-
h[5] = h5;
183-
h[6] = h6;
184-
h[7] = h7;
185-
h[8] = h8;
186-
h[9] = h9;
83+
for (int i = 0; i < 10; i++) {
84+
h[i] = - t[i];
85+
}
18786
return new Ed25519FieldElement(f, h);
18887
}
18988

src/net/i2p/crypto/eddsa/math/ed25519/Ed25519LittleEndianEncoding.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public byte[] encode(FieldElement x) {
8888
*/
8989

9090
byte[] s = new byte[32];
91-
s[0] = (byte) (h0 >> 0);
91+
s[0] = (byte) h0;
9292
s[1] = (byte) (h0 >> 8);
9393
s[2] = (byte) (h0 >> 16);
9494
s[3] = (byte) ((h0 >> 24) | (h1 << 2));
@@ -104,7 +104,7 @@ public byte[] encode(FieldElement x) {
104104
s[13] = (byte) (h4 >> 2);
105105
s[14] = (byte) (h4 >> 10);
106106
s[15] = (byte) (h4 >> 18);
107-
s[16] = (byte) (h5 >> 0);
107+
s[16] = (byte) h5;
108108
s[17] = (byte) (h5 >> 8);
109109
s[18] = (byte) (h5 >> 16);
110110
s[19] = (byte) ((h5 >> 24) | (h6 << 1));
@@ -123,14 +123,14 @@ public byte[] encode(FieldElement x) {
123123
return s;
124124
}
125125

126-
private static long load_3(byte[] in, int offset) {
126+
static int load_3(byte[] in, int offset) {
127127
int result = in[offset++] & 0xff;
128128
result |= (in[offset++] & 0xff) << 8;
129129
result |= (in[offset] & 0xff) << 16;
130130
return result;
131131
}
132132

133-
private static long load_4(byte[] in, int offset) {
133+
static long load_4(byte[] in, int offset) {
134134
int result = in[offset++] & 0xff;
135135
result |= (in[offset++] & 0xff) << 8;
136136
result |= (in[offset++] & 0xff) << 16;

0 commit comments

Comments
 (0)