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

Curve25519Point.java « djb « custom « ec « math « bouncycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2913af999f24ba4b5284b73f5bf4e72542e086a6 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package org.bouncycastle.math.ec.custom.djb;

import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.math.ec.ECPoint;

public class Curve25519Point extends ECPoint
{
    /**
     * Create a point which encodes with point compression.
     * 
     * @param curve the curve to use
     * @param x affine x co-ordinate
     * @param y affine y co-ordinate
     * 
     * @deprecated Use ECCurve.createPoint to construct points
     */
    public Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
    {
        this(curve, x, y, false);
    }

    /**
     * Create a point that encodes with or without point compresion.
     * 
     * @param curve the curve to use
     * @param x affine x co-ordinate
     * @param y affine y co-ordinate
     * @param withCompression if true encode with point compression
     * 
     * @deprecated per-point compression property will be removed, refer {@link #getEncoded(boolean)}
     */
    public Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y, boolean withCompression)
    {
        super(curve, x, y);

        if ((x == null) != (y == null))
        {
            throw new IllegalArgumentException("Exactly one of the field elements is null");
        }

        this.withCompression = withCompression;
    }

    Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, boolean withCompression)
    {
        super(curve, x, y, zs);

        this.withCompression = withCompression;
    }

    protected ECPoint detach()
    {
        return new Curve25519Point(null, getAffineXCoord(), getAffineYCoord());
    }

    protected boolean getCompressionYTilde()
    {
        return this.getAffineYCoord().testBitZero();
    }

    public ECFieldElement getZCoord(int index)
    {
        if (index == 1)
        {
            return getJacobianModifiedW();
        }

        return super.getZCoord(index);
    }

    // B.3 pg 62
    public ECPoint add(ECPoint b)
    {
        if (this.isInfinity())
        {
            return b;
        }
        if (b.isInfinity())
        {
            return this;
        }
        if (this == b)
        {
            return twice();
        }

        ECCurve curve = this.getCurve();

        ECFieldElement X1 = this.x, Y1 = this.y;
        ECFieldElement X2 = b.getXCoord(), Y2 = b.getYCoord();

        ECFieldElement Z1 = this.zs[0];
        ECFieldElement Z2 = b.getZCoord(0);

        boolean Z1IsOne = Z1.isOne();

        ECFieldElement Z1Squared, U2, S2;
        if (Z1IsOne)
        {
            Z1Squared = Z1; U2 = X2; S2 = Y2;
        }
        else
        {
            Z1Squared = Z1.square();
            U2 = Z1Squared.multiply(X2);
            ECFieldElement Z1Cubed = Z1Squared.multiply(Z1);
            S2 = Z1Cubed.multiply(Y2);
        }

        boolean Z2IsOne = Z2.isOne();
        ECFieldElement Z2Squared, U1, S1;
        if (Z2IsOne)
        {
            Z2Squared = Z2; U1 = X1; S1 = Y1;
        }
        else
        {
            Z2Squared = Z2.square();
            U1 = Z2Squared.multiply(X1); 
            ECFieldElement Z2Cubed = Z2Squared.multiply(Z2);
            S1 = Z2Cubed.multiply(Y1);
        }

        ECFieldElement H = U1.subtract(U2);
        ECFieldElement R = S1.subtract(S2);

        // Check if b == this or b == -this
        if (H.isZero())
        {
            if (R.isZero())
            {
                // this == b, i.e. this must be doubled
                return this.twice();
            }

            // this == -b, i.e. the result is the point at infinity
            return curve.getInfinity();
        }

        ECFieldElement HSquared = H.square();
        ECFieldElement G = HSquared.multiply(H);
        ECFieldElement V = HSquared.multiply(U1);

        ECFieldElement X3 = R.square().add(G).subtract(two(V));
        ECFieldElement Y3 = V.subtract(X3).multiplyMinusProduct(R, G, S1);

        ECFieldElement Z3 = H;
        if (!Z1IsOne)
        {
            Z3 = Z3.multiply(Z1);
        }
        if (!Z2IsOne)
        {
            Z3 = Z3.multiply(Z2);
        }

        ECFieldElement Z3Squared = (Z3 == H) ? HSquared : null;

        // TODO If the result will only be used in a subsequent addition, we don't need W3
        ECFieldElement W3 = calculateJacobianModifiedW(Z3, Z3Squared);

        ECFieldElement[] zs = new ECFieldElement[]{ Z3, W3 };

        return new Curve25519Point(curve, X3, Y3, zs, this.withCompression);
    }

    // B.3 pg 62
    public ECPoint twice()
    {
        if (this.isInfinity())
        {
            return this;
        }

        ECCurve curve = this.getCurve();

        ECFieldElement Y1 = this.y;
        if (Y1.isZero()) 
        {
            return curve.getInfinity();
        }

        return twiceJacobianModified(true);
    }

    public ECPoint twicePlus(ECPoint b)
    {
        if (this == b)
        {
            return threeTimes();
        }
        if (this.isInfinity())
        {
            return b;
        }
        if (b.isInfinity())
        {
            return twice();
        }

        ECFieldElement Y1 = this.y;
        if (Y1.isZero()) 
        {
            return b;
        }

        return twiceJacobianModified(false).add(b);
    }

    public ECPoint threeTimes()
    {
        if (this.isInfinity())
        {
            return this;
        }

        ECFieldElement Y1 = this.y;
        if (Y1.isZero())
        {
            return this;
        }

        return twiceJacobianModified(false).add(this);
    }

    protected ECFieldElement two(ECFieldElement x)
    {
        return x.add(x);
    }

    protected ECFieldElement three(ECFieldElement x)
    {
        return two(x).add(x);
    }

    // D.3.2 pg 102 (see Note:)
    public ECPoint subtract(ECPoint b)
    {
        if (b.isInfinity())
        {
            return this;
        }

        // Add -b
        return add(b.negate());
    }

    public ECPoint negate()
    {
        if (this.isInfinity())
        {
            return this;
        }

        return new Curve25519Point(this.getCurve(), this.x, this.y.negate(), this.zs, this.withCompression);
    }

    protected ECFieldElement calculateJacobianModifiedW(ECFieldElement Z, ECFieldElement ZSquared)
    {
        ECFieldElement a4 = this.getCurve().getA();
        if (Z.isOne())
        {
            return a4;
        }

        if (ZSquared == null)
        {
            ZSquared = Z.square();
        }

        return ZSquared.square().multiply(a4);
    }

    protected ECFieldElement getJacobianModifiedW()
    {
        ECFieldElement W = this.zs[1];
        if (W == null)
        {
            // NOTE: Rarely, twicePlus will result in the need for a lazy W1 calculation here
            this.zs[1] = W = calculateJacobianModifiedW(this.zs[0], null);
        }
        return W;
    }

    protected Curve25519Point twiceJacobianModified(boolean calculateW)
    {
        ECFieldElement X1 = this.x, Y1 = this.y, Z1 = this.zs[0], W1 = getJacobianModifiedW();

        ECFieldElement X1Squared = X1.square();
        ECFieldElement M = three(X1Squared).add(W1);
        ECFieldElement _2Y1 = two(Y1);
        ECFieldElement _2Y1Squared = _2Y1.multiply(Y1);
        ECFieldElement S = two(X1.multiply(_2Y1Squared));
        ECFieldElement X3 = M.square().subtract(two(S));
        ECFieldElement _4T = _2Y1Squared.square();
        ECFieldElement _8T = two(_4T);
        ECFieldElement Y3 = M.multiply(S.subtract(X3)).subtract(_8T);
        ECFieldElement W3 = calculateW ? two(_8T.multiply(W1)) : null;
        ECFieldElement Z3 = Z1.isOne() ? _2Y1 : _2Y1.multiply(Z1);

        return new Curve25519Point(this.getCurve(), X3, Y3, new ECFieldElement[]{ Z3, W3 }, this.withCompression);
    }
}