2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * An implementation of MutableECPoint for Curve25519.
31 * This is a partial implementation that only supports multiplication
32 * by a scalar, not general addition.
35 internal class MutableECPointCurve25519 : MutableECPoint {
38 ModInt x1, x2, z2, x3, z3;
39 ModInt a, aa, b, bb, c, d, e;
43 * Create a new instance. It is initialized to the point at
44 * infinity (represented with a 0).
46 internal MutableECPointCurve25519()
48 ECCurve25519 ccc = (ECCurve25519)EC.Curve25519;
65 internal override ECCurve Curve {
71 internal override uint IsInfinityCT {
77 internal override void Normalize()
81 internal override byte[] Encode(bool compressed)
83 byte[] r = new byte[32];
88 internal override uint Encode(byte[] dst, bool compressed)
90 if (dst.Length != 32) {
91 throw new CryptoException("invalid output length");
94 for (int i = 0; i < 32; i ++) {
100 internal override uint DecodeCT(byte[] enc)
102 if (enc.Length != 32) {
105 for (int i = 0; i < 32; i ++) {
113 internal override byte[] X {
119 internal override byte[] Y {
121 throw new CryptoException(
122 "Not implemented for Curve25519");
126 internal override MutableECPoint Dup()
128 MutableECPointCurve25519 Q = new MutableECPointCurve25519();
133 internal void Set(byte[] X, byte[] Y, bool check)
135 throw new CryptoException("Not implemented for Curve25519");
138 internal void Set(ModInt X, ModInt Y, bool check)
140 throw new CryptoException("Not implemented for Curve25519");
143 internal override void SetZero()
145 throw new CryptoException("Not implemented for Curve25519");
148 internal override void Set(MutableECPoint Q)
150 MutableECPointCurve25519 R = SameCurve(Q);
154 internal override void Set(MutableECPoint Q, uint ctl)
156 MutableECPointCurve25519 R = SameCurve(Q);
157 x.CondCopy(R.x, ctl);
160 internal override void SetMux(uint ctl,
161 MutableECPoint P1, MutableECPoint P2)
163 SetMuxInner(ctl, SameCurve(P1), SameCurve(P2));
166 void SetMuxInner(uint ctl,
167 MutableECPointCurve25519 P1, MutableECPointCurve25519 P2)
169 x.CopyMux(ctl, P1.x, P2.x);
172 internal override void DoubleCT()
174 throw new CryptoException("Not implemented for Curve25519");
177 internal override uint AddCT(MutableECPoint Q)
179 throw new CryptoException("Not implemented for Curve25519");
182 internal override void NegCT()
184 throw new CryptoException("Not implemented for Curve25519");
187 internal override uint MulSpecCT(byte[] n)
190 * Copy scalar into a temporary array (u[]) for
191 * normalisation to 32 bytes and clamping.
196 Array.Copy(n, 0, u, 32 - n.Length, n.Length);
197 for (int i = 0; i < 32 - n.Length; i ++) {
206 x2.SetMonty(0xFFFFFFFF);
211 ModInt ma24 = ((ECCurve25519)EC.Curve25519).ma24;
213 for (int t = 254; t >= 0; t --) {
214 uint kt = (uint)-((u[31 - (t >> 3)] >> (t & 7)) & 1);
216 x2.CondSwap(x3, swap);
217 z2.CondSwap(z3, swap);
250 x2.CondSwap(x3, swap);
251 z2.CondSwap(z3, swap);
254 * We need to restore z2 to normal representation before
255 * inversion. Then the final Montgomery multiplication
256 * will cancel out with x2, which is still in Montgomery
264 * x2 now contains the result.
270 internal override uint EqCT(MutableECPoint Q)
272 MutableECPointCurve25519 R = SameCurve(Q);
276 MutableECPointCurve25519 SameCurve(MutableECPoint Q)
278 MutableECPointCurve25519 R = Q as MutableECPointCurve25519;
280 throw new CryptoException("Mixed curves");