summary refs log tree commit diff
path: root/crypto/src/math/ec/custom/sec/SecP384R1FieldElement.cs
blob: 33f251b76ad2944a6ea6a7b565f71b406820e33d (plain) (blame)
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
using System;

using Org.BouncyCastle.Math.Raw;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Encoders;

namespace Org.BouncyCastle.Math.EC.Custom.Sec
{
    internal class SecP384R1FieldElement
        : AbstractFpFieldElement
    {
        public static readonly BigInteger Q = new BigInteger(1,
            Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"));

        protected internal readonly uint[] x;

        public SecP384R1FieldElement(BigInteger x)
        {
            if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
                throw new ArgumentException("value invalid for SecP384R1FieldElement", "x");

            this.x = SecP384R1Field.FromBigInteger(x);
        }

        public SecP384R1FieldElement()
        {
            this.x = Nat.Create(12);
        }

        protected internal SecP384R1FieldElement(uint[] x)
        {
            this.x = x;
        }

        public override bool IsZero
        {
            get { return Nat.IsZero(12, x); }
        }

        public override bool IsOne
        {
            get { return Nat.IsOne(12, x); }
        }

        public override bool TestBitZero()
        {
            return Nat.GetBit(x, 0) == 1;
        }

        public override BigInteger ToBigInteger()
        {
            return Nat.ToBigInteger(12, x);
        }

        public override string FieldName
        {
            get { return "SecP384R1Field"; }
        }

        public override int FieldSize
        {
            get { return Q.BitLength; }
        }

        public override ECFieldElement Add(ECFieldElement b)
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Add(x, ((SecP384R1FieldElement)b).x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement AddOne()
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.AddOne(x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Subtract(ECFieldElement b)
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Subtract(x, ((SecP384R1FieldElement)b).x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Multiply(ECFieldElement b)
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Multiply(x, ((SecP384R1FieldElement)b).x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Divide(ECFieldElement b)
        {
            //return Multiply(b.Invert());
            uint[] z = Nat.Create(12);
            SecP384R1Field.Inv(((SecP384R1FieldElement)b).x, z);
            SecP384R1Field.Multiply(z, x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Negate()
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Negate(x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Square()
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Square(x, z);
            return new SecP384R1FieldElement(z);
        }

        public override ECFieldElement Invert()
        {
            uint[] z = Nat.Create(12);
            SecP384R1Field.Inv(x, z);
            return new SecP384R1FieldElement(z);
        }

        /**
         * return a sqrt root - the routine verifies that the calculation returns the right value - if
         * none exists it returns null.
         */
        public override ECFieldElement Sqrt()
        {
            // Raise this element to the exponent 2^382 - 2^126 - 2^94 + 2^30

            uint[] x1 = this.x;
            if (Nat.IsZero(12, x1) || Nat.IsOne(12, x1))
                return this;

            uint[] tt0 = Nat.Create(24);
            uint[] t1 = Nat.Create(12);
            uint[] t2 = Nat.Create(12);
            uint[] t3 = Nat.Create(12);
            uint[] t4 = Nat.Create(12);

            SecP384R1Field.Square(x1, t1, tt0);
            SecP384R1Field.Multiply(t1, x1, t1, tt0);

            SecP384R1Field.SquareN(t1, 2, t2, tt0);
            SecP384R1Field.Multiply(t2, t1, t2, tt0);

            SecP384R1Field.Square(t2, t2, tt0);
            SecP384R1Field.Multiply(t2, x1, t2, tt0);

            SecP384R1Field.SquareN(t2, 5, t3, tt0);
            SecP384R1Field.Multiply(t3, t2, t3, tt0);

            SecP384R1Field.SquareN(t3, 5, t4, tt0);
            SecP384R1Field.Multiply(t4, t2, t4, tt0);

            SecP384R1Field.SquareN(t4, 15, t2, tt0);
            SecP384R1Field.Multiply(t2, t4, t2, tt0);

            SecP384R1Field.SquareN(t2, 2, t3, tt0);
            SecP384R1Field.Multiply(t1, t3, t1, tt0);

            SecP384R1Field.SquareN(t3, 28, t3, tt0);
            SecP384R1Field.Multiply(t2, t3, t2, tt0);

            SecP384R1Field.SquareN(t2, 60, t3, tt0);
            SecP384R1Field.Multiply(t3, t2, t3, tt0);

            uint[] r = t2;

            SecP384R1Field.SquareN(t3, 120, r, tt0);
            SecP384R1Field.Multiply(r, t3, r, tt0);

            SecP384R1Field.SquareN(r, 15, r, tt0);
            SecP384R1Field.Multiply(r, t4, r, tt0);

            SecP384R1Field.SquareN(r, 33, r, tt0);
            SecP384R1Field.Multiply(r, t1, r, tt0);

            SecP384R1Field.SquareN(r, 64, r, tt0);
            SecP384R1Field.Multiply(r, x1, r, tt0);

            SecP384R1Field.SquareN(r, 30, t1, tt0);
            SecP384R1Field.Square(t1, t2, tt0);

            return Nat.Eq(12, x1, t2) ? new SecP384R1FieldElement(t1) : null;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as SecP384R1FieldElement);
        }

        public override bool Equals(ECFieldElement other)
        {
            return Equals(other as SecP384R1FieldElement);
        }

        public virtual bool Equals(SecP384R1FieldElement other)
        {
            if (this == other)
                return true;
            if (null == other)
                return false;
            return Nat.Eq(12, x, other.x);
        }

        public override int GetHashCode()
        {
            return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 12);
        }
    }
}