Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SecP521R1FieldElement.java « sec « custom « ec « math « spongycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0b92b6e9b50c9f3a89eb07a552639480d4fa8be (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
package org.spongycastle.math.ec.custom.sec;

import java.math.BigInteger;

import org.spongycastle.math.ec.ECFieldElement;
import org.spongycastle.math.raw.Mod;
import org.spongycastle.math.raw.Nat;
import org.spongycastle.util.Arrays;

public class SecP521R1FieldElement extends ECFieldElement
{
    public static final BigInteger Q = SecP521R1Curve.q;

    protected int[] x;

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

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

    public SecP521R1FieldElement()
    {
        this.x = Nat.create(17);
    }

    protected SecP521R1FieldElement(int[] x)
    {
        this.x = x;
    }

    public boolean isZero()
    {
        return Nat.isZero(17, x);
    }

    public boolean isOne()
    {
        return Nat.isOne(17, x);
    }

    public boolean testBitZero()
    {
        return Nat.getBit(x, 0) == 1;
    }

    public BigInteger toBigInteger()
    {
        return Nat.toBigInteger(17, x);
    }

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

    public int getFieldSize()
    {
        return Q.bitLength();
    }

    public ECFieldElement add(ECFieldElement b)
    {
        int[] z = Nat.create(17);
        SecP521R1Field.add(x, ((SecP521R1FieldElement)b).x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement addOne()
    {
        int[] z = Nat.create(17);
        SecP521R1Field.addOne(x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement subtract(ECFieldElement b)
    {
        int[] z = Nat.create(17);
        SecP521R1Field.subtract(x, ((SecP521R1FieldElement)b).x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement multiply(ECFieldElement b)
    {
        int[] z = Nat.create(17);
        SecP521R1Field.multiply(x, ((SecP521R1FieldElement)b).x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement divide(ECFieldElement b)
    {
//        return multiply(b.invert());
        int[] z = Nat.create(17);
        Mod.invert(SecP521R1Field.P, ((SecP521R1FieldElement)b).x, z);
        SecP521R1Field.multiply(z, x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement negate()
    {
        int[] z = Nat.create(17);
        SecP521R1Field.negate(x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement square()
    {
        int[] z = Nat.create(17);
        SecP521R1Field.square(x, z);
        return new SecP521R1FieldElement(z);
    }

    public ECFieldElement invert()
    {
//        return new SecP521R1FieldElement(toBigInteger().modInverse(Q));
        int[] z = Nat.create(17);
        Mod.invert(SecP521R1Field.P, x, z);
        return new SecP521R1FieldElement(z);
    }

    // D.1.4 91
    /**
     * return a sqrt root - the routine verifies that the calculation returns the right value - if
     * none exists it returns null.
     */
    public ECFieldElement sqrt()
    {
        // Raise this element to the exponent 2^519

        int[] x1 = this.x;
        if (Nat.isZero(17, x1) || Nat.isOne(17, x1))
        {
            return this;
        }

        int[] t1 = Nat.create(17);
        int[] t2 = Nat.create(17);

        SecP521R1Field.squareN(x1, 519, t1);
        SecP521R1Field.square(t1, t2);

        return Nat.eq(17, x1, t2) ? new SecP521R1FieldElement(t1) : null;
    }

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

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

        SecP521R1FieldElement o = (SecP521R1FieldElement)other;
        return Nat.eq(17, x, o.x);
    }

    public int hashCode()
    {
        return Q.hashCode() ^ Arrays.hashCode(x, 0, 17);
    }
}