Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/math/bigint/BigIntegerLittleEndianEncoding.java

raw symbols SCIP
1 /**
2 * EdDSA-Java by str4d
3 *
4 * To the extent possible under law, the person who associated CC0 with
5 * EdDSA-Java has waived all copyright and related or neighboring rights
6 * to EdDSA-Java.
7 *
8 * You should have received a copy of the CC0 legalcode along with this
9 * work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
10 *
11 */
12 package net.i2p.crypto.eddsa.math.bigint;
13
14 import java.io.Serializable;
15 import java.math.BigInteger;
16
17 import net.i2p.crypto.eddsa.math.Encoding;
18 import net.i2p.crypto.eddsa.math.Field;
19 import net.i2p.crypto.eddsa.math.FieldElement;
20
21 public class BigIntegerLittleEndianEncoding extends Encoding implements Serializable {
22 private static final long serialVersionUID = 3984579843759837L;
23 /**
24 * Mask where only the first b-1 bits are set.
25 */
26 private BigInteger mask;
27
28 @Override
29 public synchronized void setField(Field f) {
30 super.setField(f);
31 mask = BigInteger.ONE.shiftLeft(f.getb()-1).subtract(BigInteger.ONE);
32 }
33
34 public byte[] encode(FieldElement x) {
35 return encode(((BigIntegerFieldElement)x).bi.and(mask));
36 }
37
38 /**
39 * Convert $x$ to little endian.
40 * Constant time.
41 *
42 * @param x the BigInteger value to encode
43 * @return array of length $b/8$
44 * @throws IllegalStateException if field not set
45 */
46 public byte[] encode(BigInteger x) {
47 if (f == null)
48 throw new IllegalStateException("field not set");
49 byte[] in = x.toByteArray();
50 byte[] out = new byte[f.getb()/8];
51 for (int i = 0; i < in.length; i++) {
52 out[i] = in[in.length-1-i];
53 }
54 for (int i = in.length; i < out.length; i++) {
55 out[i] = 0;
56 }
57 return out;
58 }
59
60 /**
61 * Decode a FieldElement from its $(b-1)$-bit encoding.
62 * The highest bit is masked out.
63 *
64 * @param in the $(b-1)$-bit encoding of a FieldElement.
65 * @return the FieldElement represented by 'val'.
66 * @throws IllegalStateException if field not set
67 * @throws IllegalArgumentException if encoding is invalid
68 */
69 public FieldElement decode(byte[] in) {
70 if (f == null)
71 throw new IllegalStateException("field not set");
72 if (in.length != f.getb()/8)
73 throw new IllegalArgumentException("Not a valid encoding");
74 return new BigIntegerFieldElement(f, toBigInteger(in).and(mask));
75 }
76
77 /**
78 * Convert in to big endian
79 *
80 * @param in the $(b-1)$-bit encoding of a FieldElement.
81 * @return the decoded value as a BigInteger
82 */
83 public BigInteger toBigInteger(byte[] in) {
84 byte[] out = new byte[in.length];
85 for (int i = 0; i < in.length; i++) {
86 out[i] = in[in.length-1-i];
87 }
88 return new BigInteger(1, out);
89 }
90
91 /**
92 * From the Ed25519 paper:<br>
93 * $x$ is negative if the $(b-1)$-bit encoding of $x$ is lexicographically larger
94 * than the $(b-1)$-bit encoding of $-x$. If $q$ is an odd prime and the encoding
95 * is the little-endian representation of $\{0, 1,\dots, q-1\}$ then the negative
96 * elements of $F_q$ are $\{1, 3, 5,\dots, q-2\}$.
97 * @return true if negative
98 */
99 public boolean isNegative(FieldElement x) {
100 return ((BigIntegerFieldElement)x).bi.testBit(0);
101 }
102 }