Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
7.1 KiB

  1. // ec2n.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "ec2n.h"
  5. #include "asn.h"
  6. #include "integer.h"
  7. #include "filters.h"
  8. #include "algebra.cpp"
  9. #include "eprecomp.cpp"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. EC2N::EC2N(BufferedTransformation &bt)
  12. : m_field(BERDecodeGF2NP(bt))
  13. {
  14. BERSequenceDecoder seq(bt);
  15. m_field->BERDecodeElement(seq, m_a);
  16. m_field->BERDecodeElement(seq, m_b);
  17. // skip optional seed
  18. if (!seq.EndReached())
  19. {
  20. SecByteBlock seed;
  21. unsigned int unused;
  22. BERDecodeBitString(seq, seed, unused);
  23. }
  24. seq.MessageEnd();
  25. }
  26. void EC2N::DEREncode(BufferedTransformation &bt) const
  27. {
  28. m_field->DEREncode(bt);
  29. DERSequenceEncoder seq(bt);
  30. m_field->DEREncodeElement(seq, m_a);
  31. m_field->DEREncodeElement(seq, m_b);
  32. seq.MessageEnd();
  33. }
  34. bool EC2N::DecodePoint(EC2N::Point &P, const byte *encodedPoint, size_t encodedPointLen) const
  35. {
  36. StringStore store(encodedPoint, encodedPointLen);
  37. return DecodePoint(P, store, encodedPointLen);
  38. }
  39. bool EC2N::DecodePoint(EC2N::Point &P, BufferedTransformation &bt, size_t encodedPointLen) const
  40. {
  41. byte type;
  42. if (encodedPointLen < 1 || !bt.Get(type))
  43. return false;
  44. switch (type)
  45. {
  46. case 0:
  47. P.identity = true;
  48. return true;
  49. case 2:
  50. case 3:
  51. {
  52. if (encodedPointLen != EncodedPointSize(true))
  53. return false;
  54. P.identity = false;
  55. P.x.Decode(bt, m_field->MaxElementByteLength());
  56. if (P.x.IsZero())
  57. {
  58. P.y = m_field->SquareRoot(m_b);
  59. return true;
  60. }
  61. FieldElement z = m_field->Square(P.x);
  62. assert(P.x == m_field->SquareRoot(z));
  63. P.y = m_field->Divide(m_field->Add(m_field->Multiply(z, m_field->Add(P.x, m_a)), m_b), z);
  64. assert(P.x == m_field->Subtract(m_field->Divide(m_field->Subtract(m_field->Multiply(P.y, z), m_b), z), m_a));
  65. z = m_field->SolveQuadraticEquation(P.y);
  66. assert(m_field->Add(m_field->Square(z), z) == P.y);
  67. z.SetCoefficient(0, type & 1);
  68. P.y = m_field->Multiply(z, P.x);
  69. return true;
  70. }
  71. case 4:
  72. {
  73. if (encodedPointLen != EncodedPointSize(false))
  74. return false;
  75. unsigned int len = m_field->MaxElementByteLength();
  76. P.identity = false;
  77. P.x.Decode(bt, len);
  78. P.y.Decode(bt, len);
  79. return true;
  80. }
  81. default:
  82. return false;
  83. }
  84. }
  85. void EC2N::EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
  86. {
  87. if (P.identity)
  88. NullStore().TransferTo(bt, EncodedPointSize(compressed));
  89. else if (compressed)
  90. {
  91. bt.Put(2 + (!P.x ? 0 : m_field->Divide(P.y, P.x).GetBit(0)));
  92. P.x.Encode(bt, m_field->MaxElementByteLength());
  93. }
  94. else
  95. {
  96. unsigned int len = m_field->MaxElementByteLength();
  97. bt.Put(4); // uncompressed
  98. P.x.Encode(bt, len);
  99. P.y.Encode(bt, len);
  100. }
  101. }
  102. void EC2N::EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
  103. {
  104. ArraySink sink(encodedPoint, EncodedPointSize(compressed));
  105. EncodePoint(sink, P, compressed);
  106. assert(sink.TotalPutLength() == EncodedPointSize(compressed));
  107. }
  108. EC2N::Point EC2N::BERDecodePoint(BufferedTransformation &bt) const
  109. {
  110. SecByteBlock str;
  111. BERDecodeOctetString(bt, str);
  112. Point P;
  113. if (!DecodePoint(P, str, str.size()))
  114. BERDecodeError();
  115. return P;
  116. }
  117. void EC2N::DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
  118. {
  119. SecByteBlock str(EncodedPointSize(compressed));
  120. EncodePoint(str, P, compressed);
  121. DEREncodeOctetString(bt, str);
  122. }
  123. bool EC2N::ValidateParameters(RandomNumberGenerator &rng, unsigned int level) const
  124. {
  125. CRYPTOPP_UNUSED(rng);
  126. bool pass = !!m_b;
  127. pass = pass && m_a.CoefficientCount() <= m_field->MaxElementBitLength();
  128. pass = pass && m_b.CoefficientCount() <= m_field->MaxElementBitLength();
  129. if (level >= 1)
  130. pass = pass && m_field->GetModulus().IsIrreducible();
  131. return pass;
  132. }
  133. bool EC2N::VerifyPoint(const Point &P) const
  134. {
  135. const FieldElement &x = P.x, &y = P.y;
  136. return P.identity ||
  137. (x.CoefficientCount() <= m_field->MaxElementBitLength()
  138. && y.CoefficientCount() <= m_field->MaxElementBitLength()
  139. && !(((x+m_a)*x*x+m_b-(x+y)*y)%m_field->GetModulus()));
  140. }
  141. bool EC2N::Equal(const Point &P, const Point &Q) const
  142. {
  143. if (P.identity && Q.identity)
  144. return true;
  145. if (P.identity && !Q.identity)
  146. return false;
  147. if (!P.identity && Q.identity)
  148. return false;
  149. return (m_field->Equal(P.x,Q.x) && m_field->Equal(P.y,Q.y));
  150. }
  151. const EC2N::Point& EC2N::Identity() const
  152. {
  153. return Singleton<Point>().Ref();
  154. }
  155. const EC2N::Point& EC2N::Inverse(const Point &P) const
  156. {
  157. if (P.identity)
  158. return P;
  159. else
  160. {
  161. m_R.identity = false;
  162. m_R.y = m_field->Add(P.x, P.y);
  163. m_R.x = P.x;
  164. return m_R;
  165. }
  166. }
  167. const EC2N::Point& EC2N::Add(const Point &P, const Point &Q) const
  168. {
  169. if (P.identity) return Q;
  170. if (Q.identity) return P;
  171. if (Equal(P, Q)) return Double(P);
  172. if (m_field->Equal(P.x, Q.x) && m_field->Equal(P.y, m_field->Add(Q.x, Q.y))) return Identity();
  173. FieldElement t = m_field->Add(P.y, Q.y);
  174. t = m_field->Divide(t, m_field->Add(P.x, Q.x));
  175. FieldElement x = m_field->Square(t);
  176. m_field->Accumulate(x, t);
  177. m_field->Accumulate(x, Q.x);
  178. m_field->Accumulate(x, m_a);
  179. m_R.y = m_field->Add(P.y, m_field->Multiply(t, x));
  180. m_field->Accumulate(x, P.x);
  181. m_field->Accumulate(m_R.y, x);
  182. m_R.x.swap(x);
  183. m_R.identity = false;
  184. return m_R;
  185. }
  186. const EC2N::Point& EC2N::Double(const Point &P) const
  187. {
  188. if (P.identity) return P;
  189. if (!m_field->IsUnit(P.x)) return Identity();
  190. FieldElement t = m_field->Divide(P.y, P.x);
  191. m_field->Accumulate(t, P.x);
  192. m_R.y = m_field->Square(P.x);
  193. m_R.x = m_field->Square(t);
  194. m_field->Accumulate(m_R.x, t);
  195. m_field->Accumulate(m_R.x, m_a);
  196. m_field->Accumulate(m_R.y, m_field->Multiply(t, m_R.x));
  197. m_field->Accumulate(m_R.y, m_R.x);
  198. m_R.identity = false;
  199. return m_R;
  200. }
  201. // ********************************************************
  202. /*
  203. EcPrecomputation<EC2N>& EcPrecomputation<EC2N>::operator=(const EcPrecomputation<EC2N> &rhs)
  204. {
  205. m_ec = rhs.m_ec;
  206. m_ep = rhs.m_ep;
  207. m_ep.m_group = m_ec.get();
  208. return *this;
  209. }
  210. void EcPrecomputation<EC2N>::SetCurveAndBase(const EC2N &ec, const EC2N::Point &base)
  211. {
  212. m_ec.reset(new EC2N(ec));
  213. m_ep.SetGroupAndBase(*m_ec, base);
  214. }
  215. void EcPrecomputation<EC2N>::Precompute(unsigned int maxExpBits, unsigned int storage)
  216. {
  217. m_ep.Precompute(maxExpBits, storage);
  218. }
  219. void EcPrecomputation<EC2N>::Load(BufferedTransformation &bt)
  220. {
  221. BERSequenceDecoder seq(bt);
  222. word32 version;
  223. BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1);
  224. m_ep.m_exponentBase.BERDecode(seq);
  225. m_ep.m_windowSize = m_ep.m_exponentBase.BitCount() - 1;
  226. m_ep.m_bases.clear();
  227. while (!seq.EndReached())
  228. m_ep.m_bases.push_back(m_ec->BERDecodePoint(seq));
  229. seq.MessageEnd();
  230. }
  231. void EcPrecomputation<EC2N>::Save(BufferedTransformation &bt) const
  232. {
  233. DERSequenceEncoder seq(bt);
  234. DEREncodeUnsigned<word32>(seq, 1); // version
  235. m_ep.m_exponentBase.DEREncode(seq);
  236. for (unsigned i=0; i<m_ep.m_bases.size(); i++)
  237. m_ec->DEREncodePoint(seq, m_ep.m_bases[i]);
  238. seq.MessageEnd();
  239. }
  240. EC2N::Point EcPrecomputation<EC2N>::Exponentiate(const Integer &exponent) const
  241. {
  242. return m_ep.Exponentiate(exponent);
  243. }
  244. EC2N::Point EcPrecomputation<EC2N>::CascadeExponentiate(const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const
  245. {
  246. return m_ep.CascadeExponentiate(exponent, static_cast<const EcPrecomputation<EC2N> &>(pc2).m_ep, exponent2);
  247. }
  248. */
  249. NAMESPACE_END
  250. #endif