1package org.bouncycastle.math.ec.custom.sec;
2
3import java.math.BigInteger;
4
5import org.bouncycastle.math.ec.ECFieldElement;
6import org.bouncycastle.math.raw.Mod;
7import org.bouncycastle.math.raw.Nat;
8import org.bouncycastle.math.raw.Nat224;
9import org.bouncycastle.util.Arrays;
10
11public class SecP224R1FieldElement extends ECFieldElement
12{
13    public static final BigInteger Q = SecP224R1Curve.q;
14
15    protected int[] x;
16
17    public SecP224R1FieldElement(BigInteger x)
18    {
19        if (x == null || x.signum() < 0 || x.compareTo(Q) >= 0)
20        {
21            throw new IllegalArgumentException("x value invalid for SecP224R1FieldElement");
22        }
23
24        this.x = SecP224R1Field.fromBigInteger(x);
25    }
26
27    public SecP224R1FieldElement()
28    {
29        this.x = Nat224.create();
30    }
31
32    protected SecP224R1FieldElement(int[] x)
33    {
34        this.x = x;
35    }
36
37    public boolean isZero()
38    {
39        return Nat224.isZero(x);
40    }
41
42    public boolean isOne()
43    {
44        return Nat224.isOne(x);
45    }
46
47    public boolean testBitZero()
48    {
49        return Nat224.getBit(x, 0) == 1;
50    }
51
52    public BigInteger toBigInteger()
53    {
54        return Nat224.toBigInteger(x);
55    }
56
57    public String getFieldName()
58    {
59        return "SecP224R1Field";
60    }
61
62    public int getFieldSize()
63    {
64        return Q.bitLength();
65    }
66
67    public ECFieldElement add(ECFieldElement b)
68    {
69        int[] z = Nat224.create();
70        SecP224R1Field.add(x, ((SecP224R1FieldElement)b).x, z);
71        return new SecP224R1FieldElement(z);
72    }
73
74    public ECFieldElement addOne()
75    {
76        int[] z = Nat224.create();
77        SecP224R1Field.addOne(x, z);
78        return new SecP224R1FieldElement(z);
79    }
80
81    public ECFieldElement subtract(ECFieldElement b)
82    {
83        int[] z = Nat224.create();
84        SecP224R1Field.subtract(x, ((SecP224R1FieldElement)b).x, z);
85        return new SecP224R1FieldElement(z);
86    }
87
88    public ECFieldElement multiply(ECFieldElement b)
89    {
90        int[] z = Nat224.create();
91        SecP224R1Field.multiply(x, ((SecP224R1FieldElement)b).x, z);
92        return new SecP224R1FieldElement(z);
93    }
94
95    public ECFieldElement divide(ECFieldElement b)
96    {
97//        return multiply(b.invert());
98        int[] z = Nat224.create();
99        Mod.invert(SecP224R1Field.P, ((SecP224R1FieldElement)b).x, z);
100        SecP224R1Field.multiply(z, x, z);
101        return new SecP224R1FieldElement(z);
102    }
103
104    public ECFieldElement negate()
105    {
106        int[] z = Nat224.create();
107        SecP224R1Field.negate(x, z);
108        return new SecP224R1FieldElement(z);
109    }
110
111    public ECFieldElement square()
112    {
113        int[] z = Nat224.create();
114        SecP224R1Field.square(x, z);
115        return new SecP224R1FieldElement(z);
116    }
117
118    public ECFieldElement invert()
119    {
120//        return new SecP224R1FieldElement(toBigInteger().modInverse(Q));
121        int[] z = Nat224.create();
122        Mod.invert(SecP224R1Field.P, x, z);
123        return new SecP224R1FieldElement(z);
124    }
125
126    /**
127     * return a sqrt root - the routine verifies that the calculation returns the right value - if
128     * none exists it returns null.
129     */
130    public ECFieldElement sqrt()
131    {
132        int[] c = this.x;
133        if (Nat224.isZero(c) || Nat224.isOne(c))
134        {
135            return this;
136        }
137
138        int[] nc = Nat224.create();
139        SecP224R1Field.negate(c, nc);
140
141        int[] r = Mod.random(SecP224R1Field.P);
142        int[] t = Nat224.create();
143
144        if (!isSquare(c))
145        {
146            return null;
147        }
148
149        while (!trySqrt(nc, r, t))
150        {
151            SecP224R1Field.addOne(r, r);
152        }
153
154        SecP224R1Field.square(t, r);
155
156        return Nat224.eq(c, r) ? new SecP224R1FieldElement(t) : null;
157    }
158
159    public boolean equals(Object other)
160    {
161        if (other == this)
162        {
163            return true;
164        }
165
166        if (!(other instanceof SecP224R1FieldElement))
167        {
168            return false;
169        }
170
171        SecP224R1FieldElement o = (SecP224R1FieldElement)other;
172        return Nat224.eq(x, o.x);
173    }
174
175    public int hashCode()
176    {
177        return Q.hashCode() ^ Arrays.hashCode(x, 0, 7);
178    }
179
180    private static boolean isSquare(int[] x)
181    {
182        int[] t1 = Nat224.create();
183        int[] t2 = Nat224.create();
184        Nat224.copy(x, t1);
185
186        for (int i = 0; i < 7; ++i)
187        {
188            Nat224.copy(t1, t2);
189            SecP224R1Field.squareN(t1, 1 << i, t1);
190            SecP224R1Field.multiply(t1, t2, t1);
191        }
192
193        SecP224R1Field.squareN(t1, 95, t1);
194        return Nat224.isOne(t1);
195    }
196
197    private static void RM(int[] nc, int[] d0, int[] e0, int[] d1, int[] e1, int[] f1, int[] t)
198    {
199        SecP224R1Field.multiply(e1, e0, t);
200        SecP224R1Field.multiply(t, nc, t);
201        SecP224R1Field.multiply(d1, d0, f1);
202        SecP224R1Field.add(f1, t, f1);
203        SecP224R1Field.multiply(d1, e0, t);
204        Nat224.copy(f1, d1);
205        SecP224R1Field.multiply(e1, d0, e1);
206        SecP224R1Field.add(e1, t, e1);
207        SecP224R1Field.square(e1, f1);
208        SecP224R1Field.multiply(f1, nc, f1);
209    }
210
211    private static void RP(int[] nc, int[] d1, int[] e1, int[] f1, int[] t)
212    {
213        Nat224.copy(nc, f1);
214
215        int[] d0 = Nat224.create();
216        int[] e0 = Nat224.create();
217
218        for (int i = 0; i < 7; ++i)
219        {
220            Nat224.copy(d1, d0);
221            Nat224.copy(e1, e0);
222
223            int j = 1 << i;
224            while (--j >= 0)
225            {
226                RS(d1, e1, f1, t);
227            }
228
229            RM(nc, d0, e0, d1, e1, f1, t);
230        }
231    }
232
233    private static void RS(int[] d, int[] e, int[] f, int[] t)
234    {
235        SecP224R1Field.multiply(e, d, e);
236        SecP224R1Field.twice(e, e);
237        SecP224R1Field.square(d, t);
238        SecP224R1Field.add(f, t, d);
239        SecP224R1Field.multiply(f, t, f);
240        int c = Nat.shiftUpBits(7, f, 2, 0);
241        SecP224R1Field.reduce32(c, f);
242    }
243
244    private static boolean trySqrt(int[] nc, int[] r, int[] t)
245    {
246        int[] d1 = Nat224.create();
247        Nat224.copy(r, d1);
248        int[] e1 = Nat224.create();
249        e1[0] = 1;
250        int[] f1 = Nat224.create();
251        RP(nc, d1, e1, f1, t);
252
253        int[] d0 = Nat224.create();
254        int[] e0 = Nat224.create();
255
256        for (int k = 1; k < 96; ++k)
257        {
258            Nat224.copy(d1, d0);
259            Nat224.copy(e1, e0);
260
261            RS(d1, e1, f1, t);
262
263            if (Nat224.isZero(d1))
264            {
265                Mod.invert(SecP224R1Field.P, e0, t);
266                SecP224R1Field.multiply(t, d0, t);
267                return true;
268            }
269        }
270
271        return false;
272    }
273}
274