00001
00002
00003 #include "pch.h"
00004
00005 #ifndef CRYPTOPP_IMPORTS
00006
00007 #include "ecp.h"
00008 #include "asn.h"
00009 #include "integer.h"
00010 #include "nbtheory.h"
00011 #include "modarith.h"
00012 #include "filters.h"
00013 #include "algebra.cpp"
00014
00015 NAMESPACE_BEGIN(CryptoPP)
00016
00017 ANONYMOUS_NAMESPACE_BEGIN
00018 static inline ECP::Point ToMontgomery(const ModularArithmetic &mr, const ECP::Point &P)
00019 {
00020 return P.identity ? P : ECP::Point(mr.ConvertIn(P.x), mr.ConvertIn(P.y));
00021 }
00022
00023 static inline ECP::Point FromMontgomery(const ModularArithmetic &mr, const ECP::Point &P)
00024 {
00025 return P.identity ? P : ECP::Point(mr.ConvertOut(P.x), mr.ConvertOut(P.y));
00026 }
00027 NAMESPACE_END
00028
00029 ECP::ECP(const ECP &ecp, bool convertToMontgomeryRepresentation)
00030 {
00031 if (convertToMontgomeryRepresentation && !ecp.GetField().IsMontgomeryRepresentation())
00032 {
00033 m_fieldPtr.reset(new MontgomeryRepresentation(ecp.GetField().GetModulus()));
00034 m_a = GetField().ConvertIn(ecp.m_a);
00035 m_b = GetField().ConvertIn(ecp.m_b);
00036 }
00037 else
00038 operator=(ecp);
00039 }
00040
00041 ECP::ECP(BufferedTransformation &bt)
00042 : m_fieldPtr(new Field(bt))
00043 {
00044 BERSequenceDecoder seq(bt);
00045 GetField().BERDecodeElement(seq, m_a);
00046 GetField().BERDecodeElement(seq, m_b);
00047
00048 if (!seq.EndReached())
00049 {
00050 SecByteBlock seed;
00051 unsigned int unused;
00052 BERDecodeBitString(seq, seed, unused);
00053 }
00054 seq.MessageEnd();
00055 }
00056
00057 void ECP::DEREncode(BufferedTransformation &bt) const
00058 {
00059 GetField().DEREncode(bt);
00060 DERSequenceEncoder seq(bt);
00061 GetField().DEREncodeElement(seq, m_a);
00062 GetField().DEREncodeElement(seq, m_b);
00063 seq.MessageEnd();
00064 }
00065
00066 bool ECP::DecodePoint(ECP::Point &P, const byte *encodedPoint, size_t encodedPointLen) const
00067 {
00068 StringStore store(encodedPoint, encodedPointLen);
00069 return DecodePoint(P, store, encodedPointLen);
00070 }
00071
00072 bool ECP::DecodePoint(ECP::Point &P, BufferedTransformation &bt, size_t encodedPointLen) const
00073 {
00074 byte type;
00075 if (encodedPointLen < 1 || !bt.Get(type))
00076 return false;
00077
00078 switch (type)
00079 {
00080 case 0:
00081 P.identity = true;
00082 return true;
00083 case 2:
00084 case 3:
00085 {
00086 if (encodedPointLen != EncodedPointSize(true))
00087 return false;
00088
00089 Integer p = FieldSize();
00090
00091 P.identity = false;
00092 P.x.Decode(bt, GetField().MaxElementByteLength());
00093 P.y = ((P.x*P.x+m_a)*P.x+m_b) % p;
00094
00095 if (Jacobi(P.y, p) !=1)
00096 return false;
00097
00098 P.y = ModularSquareRoot(P.y, p);
00099
00100 if ((type & 1) != P.y.GetBit(0))
00101 P.y = p-P.y;
00102
00103 return true;
00104 }
00105 case 4:
00106 {
00107 if (encodedPointLen != EncodedPointSize(false))
00108 return false;
00109
00110 unsigned int len = GetField().MaxElementByteLength();
00111 P.identity = false;
00112 P.x.Decode(bt, len);
00113 P.y.Decode(bt, len);
00114 return true;
00115 }
00116 default:
00117 return false;
00118 }
00119 }
00120
00121 void ECP::EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
00122 {
00123 if (P.identity)
00124 NullStore().TransferTo(bt, EncodedPointSize(compressed));
00125 else if (compressed)
00126 {
00127 bt.Put(2 + P.y.GetBit(0));
00128 P.x.Encode(bt, GetField().MaxElementByteLength());
00129 }
00130 else
00131 {
00132 unsigned int len = GetField().MaxElementByteLength();
00133 bt.Put(4);
00134 P.x.Encode(bt, len);
00135 P.y.Encode(bt, len);
00136 }
00137 }
00138
00139 void ECP::EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
00140 {
00141 ArraySink sink(encodedPoint, EncodedPointSize(compressed));
00142 EncodePoint(sink, P, compressed);
00143 assert(sink.TotalPutLength() == EncodedPointSize(compressed));
00144 }
00145
00146 ECP::Point ECP::BERDecodePoint(BufferedTransformation &bt) const
00147 {
00148 SecByteBlock str;
00149 BERDecodeOctetString(bt, str);
00150 Point P;
00151 if (!DecodePoint(P, str, str.size()))
00152 BERDecodeError();
00153 return P;
00154 }
00155
00156 void ECP::DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
00157 {
00158 SecByteBlock str(EncodedPointSize(compressed));
00159 EncodePoint(str, P, compressed);
00160 DEREncodeOctetString(bt, str);
00161 }
00162
00163 bool ECP::ValidateParameters(RandomNumberGenerator &rng, unsigned int level) const
00164 {
00165 Integer p = FieldSize();
00166
00167 bool pass = p.IsOdd();
00168 pass = pass && !m_a.IsNegative() && m_a<p && !m_b.IsNegative() && m_b<p;
00169
00170 if (level >= 1)
00171 pass = pass && ((4*m_a*m_a*m_a+27*m_b*m_b)%p).IsPositive();
00172
00173 if (level >= 2)
00174 pass = pass && VerifyPrime(rng, p);
00175
00176 return pass;
00177 }
00178
00179 bool ECP::VerifyPoint(const Point &P) const
00180 {
00181 const FieldElement &x = P.x, &y = P.y;
00182 Integer p = FieldSize();
00183 return P.identity ||
00184 (!x.IsNegative() && x<p && !y.IsNegative() && y<p
00185 && !(((x*x+m_a)*x+m_b-y*y)%p));
00186 }
00187
00188 bool ECP::Equal(const Point &P, const Point &Q) const
00189 {
00190 if (P.identity && Q.identity)
00191 return true;
00192
00193 if (P.identity && !Q.identity)
00194 return false;
00195
00196 if (!P.identity && Q.identity)
00197 return false;
00198
00199 return (GetField().Equal(P.x,Q.x) && GetField().Equal(P.y,Q.y));
00200 }
00201
00202 const ECP::Point& ECP::Identity() const
00203 {
00204 return Singleton<Point>().Ref();
00205 }
00206
00207 const ECP::Point& ECP::Inverse(const Point &P) const
00208 {
00209 if (P.identity)
00210 return P;
00211 else
00212 {
00213 m_R.identity = false;
00214 m_R.x = P.x;
00215 m_R.y = GetField().Inverse(P.y);
00216 return m_R;
00217 }
00218 }
00219
00220 const ECP::Point& ECP::Add(const Point &P, const Point &Q) const
00221 {
00222 if (P.identity) return Q;
00223 if (Q.identity) return P;
00224 if (GetField().Equal(P.x, Q.x))
00225 return GetField().Equal(P.y, Q.y) ? Double(P) : Identity();
00226
00227 FieldElement t = GetField().Subtract(Q.y, P.y);
00228 t = GetField().Divide(t, GetField().Subtract(Q.x, P.x));
00229 FieldElement x = GetField().Subtract(GetField().Subtract(GetField().Square(t), P.x), Q.x);
00230 m_R.y = GetField().Subtract(GetField().Multiply(t, GetField().Subtract(P.x, x)), P.y);
00231
00232 m_R.x.swap(x);
00233 m_R.identity = false;
00234 return m_R;
00235 }
00236
00237 const ECP::Point& ECP::Double(const Point &P) const
00238 {
00239 if (P.identity || P.y==GetField().Identity()) return Identity();
00240
00241 FieldElement t = GetField().Square(P.x);
00242 t = GetField().Add(GetField().Add(GetField().Double(t), t), m_a);
00243 t = GetField().Divide(t, GetField().Double(P.y));
00244 FieldElement x = GetField().Subtract(GetField().Subtract(GetField().Square(t), P.x), P.x);
00245 m_R.y = GetField().Subtract(GetField().Multiply(t, GetField().Subtract(P.x, x)), P.y);
00246
00247 m_R.x.swap(x);
00248 m_R.identity = false;
00249 return m_R;
00250 }
00251
00252 template <class T, class Iterator> void ParallelInvert(const AbstractRing<T> &ring, Iterator begin, Iterator end)
00253 {
00254 size_t n = end-begin;
00255 if (n == 1)
00256 *begin = ring.MultiplicativeInverse(*begin);
00257 else if (n > 1)
00258 {
00259 std::vector<T> vec((n+1)/2);
00260 unsigned int i;
00261 Iterator it;
00262
00263 for (i=0, it=begin; i<n/2; i++, it+=2)
00264 vec[i] = ring.Multiply(*it, *(it+1));
00265 if (n%2 == 1)
00266 vec[n/2] = *it;
00267
00268 ParallelInvert(ring, vec.begin(), vec.end());
00269
00270 for (i=0, it=begin; i<n/2; i++, it+=2)
00271 {
00272 if (!vec[i])
00273 {
00274 *it = ring.MultiplicativeInverse(*it);
00275 *(it+1) = ring.MultiplicativeInverse(*(it+1));
00276 }
00277 else
00278 {
00279 std::swap(*it, *(it+1));
00280 *it = ring.Multiply(*it, vec[i]);
00281 *(it+1) = ring.Multiply(*(it+1), vec[i]);
00282 }
00283 }
00284 if (n%2 == 1)
00285 *it = vec[n/2];
00286 }
00287 }
00288
00289 struct ProjectivePoint
00290 {
00291 ProjectivePoint() {}
00292 ProjectivePoint(const Integer &x, const Integer &y, const Integer &z)
00293 : x(x), y(y), z(z) {}
00294
00295 Integer x,y,z;
00296 };
00297
00298 class ProjectiveDoubling
00299 {
00300 public:
00301 ProjectiveDoubling(const ModularArithmetic &mr, const Integer &m_a, const Integer &m_b, const ECPPoint &Q)
00302 : mr(mr), firstDoubling(true), negated(false)
00303 {
00304 CRYPTOPP_UNUSED(m_b);
00305 if (Q.identity)
00306 {
00307 sixteenY4 = P.x = P.y = mr.MultiplicativeIdentity();
00308 aZ4 = P.z = mr.Identity();
00309 }
00310 else
00311 {
00312 P.x = Q.x;
00313 P.y = Q.y;
00314 sixteenY4 = P.z = mr.MultiplicativeIdentity();
00315 aZ4 = m_a;
00316 }
00317 }
00318
00319 void Double()
00320 {
00321 twoY = mr.Double(P.y);
00322 P.z = mr.Multiply(P.z, twoY);
00323 fourY2 = mr.Square(twoY);
00324 S = mr.Multiply(fourY2, P.x);
00325 aZ4 = mr.Multiply(aZ4, sixteenY4);
00326 M = mr.Square(P.x);
00327 M = mr.Add(mr.Add(mr.Double(M), M), aZ4);
00328 P.x = mr.Square(M);
00329 mr.Reduce(P.x, S);
00330 mr.Reduce(P.x, S);
00331 mr.Reduce(S, P.x);
00332 P.y = mr.Multiply(M, S);
00333 sixteenY4 = mr.Square(fourY2);
00334 mr.Reduce(P.y, mr.Half(sixteenY4));
00335 }
00336
00337 const ModularArithmetic &mr;
00338 ProjectivePoint P;
00339 bool firstDoubling, negated;
00340 Integer sixteenY4, aZ4, twoY, fourY2, S, M;
00341 };
00342
00343 struct ZIterator
00344 {
00345 ZIterator() {}
00346 ZIterator(std::vector<ProjectivePoint>::iterator it) : it(it) {}
00347 Integer& operator*() {return it->z;}
00348 int operator-(ZIterator it2) {return int(it-it2.it);}
00349 ZIterator operator+(int i) {return ZIterator(it+i);}
00350 ZIterator& operator+=(int i) {it+=i; return *this;}
00351 std::vector<ProjectivePoint>::iterator it;
00352 };
00353
00354 ECP::Point ECP::ScalarMultiply(const Point &P, const Integer &k) const
00355 {
00356 Element result;
00357 if (k.BitCount() <= 5)
00358 AbstractGroup<ECPPoint>::SimultaneousMultiply(&result, P, &k, 1);
00359 else
00360 ECP::SimultaneousMultiply(&result, P, &k, 1);
00361 return result;
00362 }
00363
00364 void ECP::SimultaneousMultiply(ECP::Point *results, const ECP::Point &P, const Integer *expBegin, unsigned int expCount) const
00365 {
00366 if (!GetField().IsMontgomeryRepresentation())
00367 {
00368 ECP ecpmr(*this, true);
00369 const ModularArithmetic &mr = ecpmr.GetField();
00370 ecpmr.SimultaneousMultiply(results, ToMontgomery(mr, P), expBegin, expCount);
00371 for (unsigned int i=0; i<expCount; i++)
00372 results[i] = FromMontgomery(mr, results[i]);
00373 return;
00374 }
00375
00376 ProjectiveDoubling rd(GetField(), m_a, m_b, P);
00377 std::vector<ProjectivePoint> bases;
00378 std::vector<WindowSlider> exponents;
00379 exponents.reserve(expCount);
00380 std::vector<std::vector<word32> > baseIndices(expCount);
00381 std::vector<std::vector<bool> > negateBase(expCount);
00382 std::vector<std::vector<word32> > exponentWindows(expCount);
00383 unsigned int i;
00384
00385 for (i=0; i<expCount; i++)
00386 {
00387 assert(expBegin->NotNegative());
00388 exponents.push_back(WindowSlider(*expBegin++, InversionIsFast(), 5));
00389 exponents[i].FindNextWindow();
00390 }
00391
00392 unsigned int expBitPosition = 0;
00393 bool notDone = true;
00394
00395 while (notDone)
00396 {
00397 notDone = false;
00398 bool baseAdded = false;
00399 for (i=0; i<expCount; i++)
00400 {
00401 if (!exponents[i].finished && expBitPosition == exponents[i].windowBegin)
00402 {
00403 if (!baseAdded)
00404 {
00405 bases.push_back(rd.P);
00406 baseAdded =true;
00407 }
00408
00409 exponentWindows[i].push_back(exponents[i].expWindow);
00410 baseIndices[i].push_back((word32)bases.size()-1);
00411 negateBase[i].push_back(exponents[i].negateNext);
00412
00413 exponents[i].FindNextWindow();
00414 }
00415 notDone = notDone || !exponents[i].finished;
00416 }
00417
00418 if (notDone)
00419 {
00420 rd.Double();
00421 expBitPosition++;
00422 }
00423 }
00424
00425
00426 ParallelInvert(GetField(), ZIterator(bases.begin()), ZIterator(bases.end()));
00427 for (i=0; i<bases.size(); i++)
00428 {
00429 if (bases[i].z.NotZero())
00430 {
00431 bases[i].y = GetField().Multiply(bases[i].y, bases[i].z);
00432 bases[i].z = GetField().Square(bases[i].z);
00433 bases[i].x = GetField().Multiply(bases[i].x, bases[i].z);
00434 bases[i].y = GetField().Multiply(bases[i].y, bases[i].z);
00435 }
00436 }
00437
00438 std::vector<BaseAndExponent<Point, Integer> > finalCascade;
00439 for (i=0; i<expCount; i++)
00440 {
00441 finalCascade.resize(baseIndices[i].size());
00442 for (unsigned int j=0; j<baseIndices[i].size(); j++)
00443 {
00444 ProjectivePoint &base = bases[baseIndices[i][j]];
00445 if (base.z.IsZero())
00446 finalCascade[j].base.identity = true;
00447 else
00448 {
00449 finalCascade[j].base.identity = false;
00450 finalCascade[j].base.x = base.x;
00451 if (negateBase[i][j])
00452 finalCascade[j].base.y = GetField().Inverse(base.y);
00453 else
00454 finalCascade[j].base.y = base.y;
00455 }
00456 finalCascade[j].exponent = Integer(Integer::POSITIVE, 0, exponentWindows[i][j]);
00457 }
00458 results[i] = GeneralCascadeMultiplication(*this, finalCascade.begin(), finalCascade.end());
00459 }
00460 }
00461
00462 ECP::Point ECP::CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const
00463 {
00464 if (!GetField().IsMontgomeryRepresentation())
00465 {
00466 ECP ecpmr(*this, true);
00467 const ModularArithmetic &mr = ecpmr.GetField();
00468 return FromMontgomery(mr, ecpmr.CascadeScalarMultiply(ToMontgomery(mr, P), k1, ToMontgomery(mr, Q), k2));
00469 }
00470 else
00471 return AbstractGroup<Point>::CascadeScalarMultiply(P, k1, Q, k2);
00472 }
00473
00474 NAMESPACE_END
00475
00476 #endif