Skip to content

Commit a48ee14

Browse files
zzzstr4d
authored andcommitted
Throw IllegalStateException instead of NPE if field not set
1 parent 881a85d commit a48ee14

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public byte[] encode(FieldElement x) {
4040
* Constant time.
4141
*
4242
* @return array of length b/8
43+
* @throws IllegalStateException if field not set
4344
*/
4445
public byte[] encode(BigInteger x) {
46+
if (f == null)
47+
throw new IllegalStateException("field not set");
4548
byte[] in = x.toByteArray();
4649
byte[] out = new byte[f.getb()/8];
4750
for (int i = 0; i < in.length; i++) {
@@ -53,7 +56,18 @@ public byte[] encode(BigInteger x) {
5356
return out;
5457
}
5558

59+
/**
60+
* Decode a FieldElement from its (b-1)-bit encoding.
61+
* The highest bit is masked out.
62+
*
63+
* @param in the (b-1)-bit encoding of a FieldElement.
64+
* @return the FieldElement represented by 'val'.
65+
* @throws IllegalStateException if field not set
66+
* @throws IllegalArgumentException if encoding is invalid
67+
*/
5668
public FieldElement decode(byte[] in) {
69+
if (f == null)
70+
throw new IllegalStateException("field not set");
5771
if (in.length != f.getb()/8)
5872
throw new IllegalArgumentException("Not a valid encoding");
5973
return new BigIntegerFieldElement(f, toBigInteger(in).and(mask));

0 commit comments

Comments
 (0)