Skip to content

Commit de42efe

Browse files
committed
Fixed BigIntegerScalarOps usage of BigIntegerLittleEndianEncoding
1 parent 13a78e2 commit de42efe

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

src/net/i2p/crypto/eddsa/math/bigint/BigIntegerLittleEndianEncoding.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ public void setField(Field f) {
2020
mask = BigInteger.ONE.shiftLeft(f.getb()-1).subtract(BigInteger.ONE);
2121
}
2222

23+
public byte[] encode(FieldElement x) {
24+
return encode(((BigIntegerFieldElement)x).bi.and(mask));
25+
}
26+
2327
/**
2428
* Convert x to little endian.
2529
* Constant time.
2630
*
27-
* @param len must be big enough
28-
* @return array of length len
29-
* @throws ArrayIndexOutOfBoundsException if len not big enough
31+
* @return array of length b/8
3032
*/
31-
public byte[] encode(FieldElement x) {
32-
byte[] in = ((BigIntegerFieldElement)x).bi.and(mask).toByteArray();
33+
public byte[] encode(BigInteger x) {
34+
byte[] in = x.toByteArray();
3335
byte[] out = new byte[f.getb()/8];
3436
for (int i = 0; i < in.length; i++) {
3537
out[i] = in[in.length-1-i];
@@ -40,24 +42,21 @@ public byte[] encode(FieldElement x) {
4042
return out;
4143
}
4244

43-
/**
44-
* Convert in to big endian
45-
*/
4645
public FieldElement decode(byte[] in) {
47-
return decode(in, true);
46+
if (in.length != f.getb()/8)
47+
throw new IllegalArgumentException("Not a valid encoding");
48+
return new BigIntegerFieldElement(f, toBigInteger(in).and(mask));
4849
}
4950

5051
/**
5152
* Convert in to big endian
5253
*/
53-
public FieldElement decode(byte[] in, boolean checkLength) {
54-
if (checkLength && in.length != f.getb()/8)
55-
throw new IllegalArgumentException("Not a valid encoding");
54+
public BigInteger toBigInteger(byte[] in) {
5655
byte[] out = new byte[in.length];
5756
for (int i = 0; i < in.length; i++) {
5857
out[i] = in[in.length-1-i];
5958
}
60-
return new BigIntegerFieldElement(f, new BigInteger(1, out).and(mask));
59+
return new BigInteger(1, out);
6160
}
6261

6362
/**
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package net.i2p.crypto.eddsa.math.bigint;
22

3+
import java.math.BigInteger;
4+
35
import net.i2p.crypto.eddsa.math.Field;
46
import net.i2p.crypto.eddsa.math.ScalarOps;
57

68
public class BigIntegerScalarOps implements ScalarOps {
7-
private final BigIntegerFieldElement l;
9+
private final BigInteger l;
810
private final BigIntegerLittleEndianEncoding enc;
911

10-
public BigIntegerScalarOps(Field f, BigIntegerFieldElement l) {
12+
public BigIntegerScalarOps(Field f, BigInteger l) {
1113
this.l = l;
1214
enc = new BigIntegerLittleEndianEncoding();
1315
enc.setField(f);
1416
}
1517

1618
public byte[] reduce(byte[] s) {
17-
return enc.encode(enc.decode(s, false).mod(l));
19+
return enc.encode(enc.toBigInteger(s).mod(l));
1820
}
1921

2022
public byte[] multiplyAndAdd(byte[] a, byte[] b, byte[] c) {
21-
return enc.encode(enc.decode(a, false).multiply(enc.decode(b, false)).add(enc.decode(c, false)).mod(l));
23+
return enc.encode(enc.toBigInteger(a).multiply(enc.toBigInteger(b)).add(enc.toBigInteger(c)).mod(l));
2224
}
2325

2426
}

src/net/i2p/crypto/eddsa/spec/EdDSANamedCurveTable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.i2p.crypto.eddsa.Utils;
77
import net.i2p.crypto.eddsa.math.Curve;
88
import net.i2p.crypto.eddsa.math.Field;
9-
import net.i2p.crypto.eddsa.math.bigint.BigIntegerFieldElement;
109
import net.i2p.crypto.eddsa.math.bigint.BigIntegerLittleEndianEncoding;
1110
import net.i2p.crypto.eddsa.math.bigint.BigIntegerScalarOps;
1211

@@ -31,8 +30,7 @@ public class EdDSANamedCurveTable {
3130
ed25519curve,
3231
"SHA-512", // H
3332
new BigIntegerScalarOps(ed25519field,
34-
new BigIntegerFieldElement(ed25519field,
35-
new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989"))), // l
33+
new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989")), // l
3634
ed25519curve.createPoint( // B
3735
Utils.hexToBytes("5866666666666666666666666666666666666666666666666666666666666666"),
3836
true)); // Precompute tables for B

test/net/i2p/crypto/eddsa/math/bigint/BigIntegerScalarOpsTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ public class BigIntegerScalarOpsTest {
3131
@Test
3232
public void testReduce() {
3333
ScalarOps sc = new BigIntegerScalarOps(ed25519Field,
34-
new BigIntegerFieldElement(ed25519Field,
35-
new BigInteger("5")));
34+
new BigInteger("5"));
3635
assertThat(sc.reduce(new byte[] {7}),
3736
is(equalTo(Utils.hexToBytes("0200000000000000000000000000000000000000000000000000000000000000"))));
3837

3938
ScalarOps sc2 = new BigIntegerScalarOps(ed25519Field,
40-
new BigIntegerFieldElement(ed25519Field,
41-
new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989")));
39+
new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989"));
4240
// Example from test case 1
4341
byte[] r = Utils.hexToBytes("b6b19cd8e0426f5983fa112d89a143aa97dab8bc5deb8d5b6253c928b65272f4044098c2a990039cde5b6a4818df0bfb6e40dc5dee54248032962323e701352d");
4442
assertThat(sc2.reduce(r), is(equalTo(Utils.hexToBytes("f38907308c893deaf244787db4af53682249107418afc2edc58f75ac58a07404"))));

0 commit comments

Comments
 (0)