summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/math/ec/custom/sec/SecT571FieldElement.java
blob: 484ad8c3b47b3de5c310bcfe8a32a3701f3e3ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package org.bouncycastle.math.ec.custom.sec;

import java.math.BigInteger;

import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.math.raw.Nat576;
import org.bouncycastle.util.Arrays;

public class SecT571FieldElement extends ECFieldElement.AbstractF2m
{
    protected long[] x;

    public SecT571FieldElement(BigInteger x)
    {
        if (x == null || x.signum() < 0 || x.bitLength() > 571)
        {
            throw new IllegalArgumentException("x value invalid for SecT571FieldElement");
        }

        this.x = SecT571Field.fromBigInteger(x);
    }

    public SecT571FieldElement()
    {
        this.x = Nat576.create64();
    }

    protected SecT571FieldElement(long[] x)
    {
        this.x = x;
    }

//    public int bitLength()
//    {
//        return x.degree();
//    }

    public boolean isOne()
    {
        return Nat576.isOne64(x);
    }

    public boolean isZero()
    {
        return Nat576.isZero64(x);
    }

    public boolean testBitZero()
    {
        return (x[0] & 1L) != 0L;
    }

    public BigInteger toBigInteger()
    {
        return Nat576.toBigInteger64(x);
    }

    public String getFieldName()
    {
        return "SecT571Field";
    }

    public int getFieldSize()
    {
        return 571;
    }

    public ECFieldElement add(ECFieldElement b)
    {
        long[] z = Nat576.create64();
        SecT571Field.add(x, ((SecT571FieldElement)b).x, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement addOne()
    {
        long[] z = Nat576.create64();
        SecT571Field.addOne(x, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement subtract(ECFieldElement b)
    {
        // Addition and subtraction are the same in F2m
        return add(b);
    }

    public ECFieldElement multiply(ECFieldElement b)
    {
        long[] z = Nat576.create64();
        SecT571Field.multiply(x, ((SecT571FieldElement)b).x, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement multiplyMinusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
    {
        return multiplyPlusProduct(b, x, y);
    }

    public ECFieldElement multiplyPlusProduct(ECFieldElement b, ECFieldElement x, ECFieldElement y)
    {
        long[] ax = this.x, bx = ((SecT571FieldElement)b).x;
        long[] xx = ((SecT571FieldElement)x).x, yx = ((SecT571FieldElement)y).x;

        long[] tt = Nat576.createExt64();
        SecT571Field.multiplyAddToExt(ax, bx, tt);
        SecT571Field.multiplyAddToExt(xx, yx, tt);

        long[] z = Nat576.create64();
        SecT571Field.reduce(tt, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement divide(ECFieldElement b)
    {
        return multiply(b.invert());
    }

    public ECFieldElement negate()
    {
        return this;
    }

    public ECFieldElement square()
    {
        long[] z = Nat576.create64();
        SecT571Field.square(x, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement squareMinusProduct(ECFieldElement x, ECFieldElement y)
    {
        return squarePlusProduct(x, y);
    }

    public ECFieldElement squarePlusProduct(ECFieldElement x, ECFieldElement y)
    {
        long[] ax = this.x;
        long[] xx = ((SecT571FieldElement)x).x, yx = ((SecT571FieldElement)y).x;

        long[] tt = Nat576.createExt64();
        SecT571Field.squareAddToExt(ax, tt);
        SecT571Field.multiplyAddToExt(xx, yx, tt);

        long[] z = Nat576.create64();
        SecT571Field.reduce(tt, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement squarePow(int pow)
    {
        if (pow < 1)
        {
            return this;
        }

        long[] z = Nat576.create64();
        SecT571Field.squareN(x, pow, z);
        return new SecT571FieldElement(z);
    }

    public int trace()
    {
        return SecT571Field.trace(x);
    }

    public ECFieldElement invert()
    {
        long[] z = Nat576.create64();
        SecT571Field.invert(x, z);
        return new SecT571FieldElement(z);
    }

    public ECFieldElement sqrt()
    {
        long[] z = Nat576.create64();
        SecT571Field.sqrt(x, z);
        return new SecT571FieldElement(z);
    }

    public int getRepresentation()
    {
        return ECFieldElement.F2m.PPB;
    }

    public int getM()
    {
        return 571;
    }

    public int getK1()
    {
        return 2;
    }

    public int getK2()
    {
        return 5;
    }

    public int getK3()
    {
        return 10;
    }

    public boolean equals(Object other)
    {
        if (other == this)
        {
            return true;
        }

        if (!(other instanceof SecT571FieldElement))
        {
            return false;
        }

        SecT571FieldElement o = (SecT571FieldElement)other;
        return Nat576.eq64(x, o.x);
    }

    public int hashCode()
    {
        return 5711052 ^ Arrays.hashCode(x, 0, 9);
    }
}