Debian Codegraph @

ext/ed25519_jruby/net/i2p/crypto/eddsa/math/Field.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;
13
14 import java.io.Serializable;
15
16 /**
17 * An EdDSA finite field. Includes several pre-computed values.
18 * @author str4d
19 *
20 */
21 public class Field implements Serializable {
22 private static final long serialVersionUID = 8746587465875676L;
23
24 public final FieldElement ZERO;
25 public final FieldElement ONE;
26 public final FieldElement TWO;
27 public final FieldElement FOUR;
28 public final FieldElement FIVE;
29 public final FieldElement EIGHT;
30
31 private final int b;
32 private final FieldElement q;
33 /**
34 * q-2
35 */
36 private final FieldElement qm2;
37 /**
38 * (q-5) / 8
39 */
40 private final FieldElement qm5d8;
41 private final Encoding enc;
42
43 public Field(int b, byte[] q, Encoding enc) {
44 this.b = b;
45 this.enc = enc;
46 this.enc.setField(this);
47
48 this.q = fromByteArray(q);
49
50 // Set up constants
51 ZERO = fromByteArray(Constants.ZERO);
52 ONE = fromByteArray(Constants.ONE);
53 TWO = fromByteArray(Constants.TWO);
54 FOUR = fromByteArray(Constants.FOUR);
55 FIVE = fromByteArray(Constants.FIVE);
56 EIGHT = fromByteArray(Constants.EIGHT);
57
58 // Precompute values
59 qm2 = this.q.subtract(TWO);
60 qm5d8 = this.q.subtract(FIVE).divide(EIGHT);
61 }
62
63 public FieldElement fromByteArray(byte[] x) {
64 return enc.decode(x);
65 }
66
67 public int getb() {
68 return b;
69 }
70
71 public FieldElement getQ() {
72 return q;
73 }
74
75 public FieldElement getQm2() {
76 return qm2;
77 }
78
79 public FieldElement getQm5d8() {
80 return qm5d8;
81 }
82
83 public Encoding getEncoding(){
84 return enc;
85 }
86
87 @Override
88 public int hashCode() {
89 return q.hashCode();
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (!(obj instanceof Field))
95 return false;
96 Field f = (Field) obj;
97 return b == f.b && q.equals(f.q);
98 }
99 }