Counter Strike : Global Offensive Source Code
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.

577 lines
14 KiB

  1. // polynomi.cpp - written and placed in the public domain by Wei Dai
  2. // Part of the code for polynomial evaluation and interpolation
  3. // originally came from Hal Finney's public domain secsplit.c.
  4. #include "pch.h"
  5. #include "polynomi.h"
  6. #include "secblock.h"
  7. #include <sstream>
  8. #include <iostream>
  9. NAMESPACE_BEGIN(CryptoPP)
  10. template <class T>
  11. void PolynomialOver<T>::Randomize(RandomNumberGenerator &rng, const RandomizationParameter &parameter, const Ring &ring)
  12. {
  13. m_coefficients.resize(parameter.m_coefficientCount);
  14. for (unsigned int i=0; i<m_coefficients.size(); ++i)
  15. m_coefficients[i] = ring.RandomElement(rng, parameter.m_coefficientParameter);
  16. }
  17. template <class T>
  18. void PolynomialOver<T>::FromStr(const char *str, const Ring &ring)
  19. {
  20. std::istringstream in((char *)str);
  21. bool positive = true;
  22. CoefficientType coef;
  23. unsigned int power;
  24. while (in)
  25. {
  26. std::ws(in);
  27. if (in.peek() == 'x')
  28. coef = ring.MultiplicativeIdentity();
  29. else
  30. in >> coef;
  31. std::ws(in);
  32. if (in.peek() == 'x')
  33. {
  34. in.get();
  35. std::ws(in);
  36. if (in.peek() == '^')
  37. {
  38. in.get();
  39. in >> power;
  40. }
  41. else
  42. power = 1;
  43. }
  44. else
  45. power = 0;
  46. if (!positive)
  47. coef = ring.Inverse(coef);
  48. SetCoefficient(power, coef, ring);
  49. std::ws(in);
  50. switch (in.get())
  51. {
  52. case '+':
  53. positive = true;
  54. break;
  55. case '-':
  56. positive = false;
  57. break;
  58. default:
  59. return; // something's wrong with the input string
  60. }
  61. }
  62. }
  63. template <class T>
  64. unsigned int PolynomialOver<T>::CoefficientCount(const Ring &ring) const
  65. {
  66. unsigned count = m_coefficients.size();
  67. while (count && ring.Equal(m_coefficients[count-1], ring.Identity()))
  68. count--;
  69. const_cast<std::vector<CoefficientType> &>(m_coefficients).resize(count);
  70. return count;
  71. }
  72. template <class T>
  73. typename PolynomialOver<T>::CoefficientType PolynomialOver<T>::GetCoefficient(unsigned int i, const Ring &ring) const
  74. {
  75. return (i < m_coefficients.size()) ? m_coefficients[i] : ring.Identity();
  76. }
  77. template <class T>
  78. PolynomialOver<T>& PolynomialOver<T>::operator=(const PolynomialOver<T>& t)
  79. {
  80. if (this != &t)
  81. {
  82. m_coefficients.resize(t.m_coefficients.size());
  83. for (unsigned int i=0; i<m_coefficients.size(); i++)
  84. m_coefficients[i] = t.m_coefficients[i];
  85. }
  86. return *this;
  87. }
  88. template <class T>
  89. PolynomialOver<T>& PolynomialOver<T>::Accumulate(const PolynomialOver<T>& t, const Ring &ring)
  90. {
  91. unsigned int count = t.CoefficientCount(ring);
  92. if (count > CoefficientCount(ring))
  93. m_coefficients.resize(count, ring.Identity());
  94. for (unsigned int i=0; i<count; i++)
  95. ring.Accumulate(m_coefficients[i], t.GetCoefficient(i, ring));
  96. return *this;
  97. }
  98. template <class T>
  99. PolynomialOver<T>& PolynomialOver<T>::Reduce(const PolynomialOver<T>& t, const Ring &ring)
  100. {
  101. unsigned int count = t.CoefficientCount(ring);
  102. if (count > CoefficientCount(ring))
  103. m_coefficients.resize(count, ring.Identity());
  104. for (unsigned int i=0; i<count; i++)
  105. ring.Reduce(m_coefficients[i], t.GetCoefficient(i, ring));
  106. return *this;
  107. }
  108. template <class T>
  109. typename PolynomialOver<T>::CoefficientType PolynomialOver<T>::EvaluateAt(const CoefficientType &x, const Ring &ring) const
  110. {
  111. int degree = Degree(ring);
  112. if (degree < 0)
  113. return ring.Identity();
  114. CoefficientType result = m_coefficients[degree];
  115. for (int j=degree-1; j>=0; j--)
  116. {
  117. result = ring.Multiply(result, x);
  118. ring.Accumulate(result, m_coefficients[j]);
  119. }
  120. return result;
  121. }
  122. template <class T>
  123. PolynomialOver<T>& PolynomialOver<T>::ShiftLeft(unsigned int n, const Ring &ring)
  124. {
  125. unsigned int i = CoefficientCount(ring) + n;
  126. m_coefficients.resize(i, ring.Identity());
  127. while (i > n)
  128. {
  129. i--;
  130. m_coefficients[i] = m_coefficients[i-n];
  131. }
  132. while (i)
  133. {
  134. i--;
  135. m_coefficients[i] = ring.Identity();
  136. }
  137. return *this;
  138. }
  139. template <class T>
  140. PolynomialOver<T>& PolynomialOver<T>::ShiftRight(unsigned int n, const Ring &ring)
  141. {
  142. unsigned int count = CoefficientCount(ring);
  143. if (count > n)
  144. {
  145. for (unsigned int i=0; i<count-n; i++)
  146. m_coefficients[i] = m_coefficients[i+n];
  147. m_coefficients.resize(count-n, ring.Identity());
  148. }
  149. else
  150. m_coefficients.resize(0, ring.Identity());
  151. return *this;
  152. }
  153. template <class T>
  154. void PolynomialOver<T>::SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring)
  155. {
  156. if (i >= m_coefficients.size())
  157. m_coefficients.resize(i+1, ring.Identity());
  158. m_coefficients[i] = value;
  159. }
  160. template <class T>
  161. void PolynomialOver<T>::Negate(const Ring &ring)
  162. {
  163. unsigned int count = CoefficientCount(ring);
  164. for (unsigned int i=0; i<count; i++)
  165. m_coefficients[i] = ring.Inverse(m_coefficients[i]);
  166. }
  167. template <class T>
  168. void PolynomialOver<T>::swap(PolynomialOver<T> &t)
  169. {
  170. m_coefficients.swap(t.m_coefficients);
  171. }
  172. template <class T>
  173. bool PolynomialOver<T>::Equals(const PolynomialOver<T>& t, const Ring &ring) const
  174. {
  175. unsigned int count = CoefficientCount(ring);
  176. if (count != t.CoefficientCount(ring))
  177. return false;
  178. for (unsigned int i=0; i<count; i++)
  179. if (!ring.Equal(m_coefficients[i], t.m_coefficients[i]))
  180. return false;
  181. return true;
  182. }
  183. template <class T>
  184. PolynomialOver<T> PolynomialOver<T>::Plus(const PolynomialOver<T>& t, const Ring &ring) const
  185. {
  186. unsigned int i;
  187. unsigned int count = CoefficientCount(ring);
  188. unsigned int tCount = t.CoefficientCount(ring);
  189. if (count > tCount)
  190. {
  191. PolynomialOver<T> result(ring, count);
  192. for (i=0; i<tCount; i++)
  193. result.m_coefficients[i] = ring.Add(m_coefficients[i], t.m_coefficients[i]);
  194. for (; i<count; i++)
  195. result.m_coefficients[i] = m_coefficients[i];
  196. return result;
  197. }
  198. else
  199. {
  200. PolynomialOver<T> result(ring, tCount);
  201. for (i=0; i<count; i++)
  202. result.m_coefficients[i] = ring.Add(m_coefficients[i], t.m_coefficients[i]);
  203. for (; i<tCount; i++)
  204. result.m_coefficients[i] = t.m_coefficients[i];
  205. return result;
  206. }
  207. }
  208. template <class T>
  209. PolynomialOver<T> PolynomialOver<T>::Minus(const PolynomialOver<T>& t, const Ring &ring) const
  210. {
  211. unsigned int i;
  212. unsigned int count = CoefficientCount(ring);
  213. unsigned int tCount = t.CoefficientCount(ring);
  214. if (count > tCount)
  215. {
  216. PolynomialOver<T> result(ring, count);
  217. for (i=0; i<tCount; i++)
  218. result.m_coefficients[i] = ring.Subtract(m_coefficients[i], t.m_coefficients[i]);
  219. for (; i<count; i++)
  220. result.m_coefficients[i] = m_coefficients[i];
  221. return result;
  222. }
  223. else
  224. {
  225. PolynomialOver<T> result(ring, tCount);
  226. for (i=0; i<count; i++)
  227. result.m_coefficients[i] = ring.Subtract(m_coefficients[i], t.m_coefficients[i]);
  228. for (; i<tCount; i++)
  229. result.m_coefficients[i] = ring.Inverse(t.m_coefficients[i]);
  230. return result;
  231. }
  232. }
  233. template <class T>
  234. PolynomialOver<T> PolynomialOver<T>::Inverse(const Ring &ring) const
  235. {
  236. unsigned int count = CoefficientCount(ring);
  237. PolynomialOver<T> result(ring, count);
  238. for (unsigned int i=0; i<count; i++)
  239. result.m_coefficients[i] = ring.Inverse(m_coefficients[i]);
  240. return result;
  241. }
  242. template <class T>
  243. PolynomialOver<T> PolynomialOver<T>::Times(const PolynomialOver<T>& t, const Ring &ring) const
  244. {
  245. if (IsZero(ring) || t.IsZero(ring))
  246. return PolynomialOver<T>();
  247. unsigned int count1 = CoefficientCount(ring), count2 = t.CoefficientCount(ring);
  248. PolynomialOver<T> result(ring, count1 + count2 - 1);
  249. for (unsigned int i=0; i<count1; i++)
  250. for (unsigned int j=0; j<count2; j++)
  251. ring.Accumulate(result.m_coefficients[i+j], ring.Multiply(m_coefficients[i], t.m_coefficients[j]));
  252. return result;
  253. }
  254. template <class T>
  255. PolynomialOver<T> PolynomialOver<T>::DividedBy(const PolynomialOver<T>& t, const Ring &ring) const
  256. {
  257. PolynomialOver<T> remainder, quotient;
  258. Divide(remainder, quotient, *this, t, ring);
  259. return quotient;
  260. }
  261. template <class T>
  262. PolynomialOver<T> PolynomialOver<T>::Modulo(const PolynomialOver<T>& t, const Ring &ring) const
  263. {
  264. PolynomialOver<T> remainder, quotient;
  265. Divide(remainder, quotient, *this, t, ring);
  266. return remainder;
  267. }
  268. template <class T>
  269. PolynomialOver<T> PolynomialOver<T>::MultiplicativeInverse(const Ring &ring) const
  270. {
  271. return Degree(ring)==0 ? ring.MultiplicativeInverse(m_coefficients[0]) : ring.Identity();
  272. }
  273. template <class T>
  274. bool PolynomialOver<T>::IsUnit(const Ring &ring) const
  275. {
  276. return Degree(ring)==0 && ring.IsUnit(m_coefficients[0]);
  277. }
  278. template <class T>
  279. std::istream& PolynomialOver<T>::Input(std::istream &in, const Ring &ring)
  280. {
  281. char c;
  282. unsigned int length = 0;
  283. SecBlock<char> str(length + 16);
  284. bool paren = false;
  285. std::ws(in);
  286. if (in.peek() == '(')
  287. {
  288. paren = true;
  289. in.get();
  290. }
  291. do
  292. {
  293. in.read(&c, 1);
  294. str[length++] = c;
  295. if (length >= str.size())
  296. str.Grow(length + 16);
  297. }
  298. // if we started with a left paren, then read until we find a right paren,
  299. // otherwise read until the end of the line
  300. while (in && ((paren && c != ')') || (!paren && c != '\n')));
  301. str[length-1] = '\0';
  302. *this = PolynomialOver<T>(str, ring);
  303. return in;
  304. }
  305. template <class T>
  306. std::ostream& PolynomialOver<T>::Output(std::ostream &out, const Ring &ring) const
  307. {
  308. unsigned int i = CoefficientCount(ring);
  309. if (i)
  310. {
  311. bool firstTerm = true;
  312. while (i--)
  313. {
  314. if (m_coefficients[i] != ring.Identity())
  315. {
  316. if (firstTerm)
  317. {
  318. firstTerm = false;
  319. if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity()))
  320. out << m_coefficients[i];
  321. }
  322. else
  323. {
  324. CoefficientType inverse = ring.Inverse(m_coefficients[i]);
  325. std::ostringstream pstr, nstr;
  326. pstr << m_coefficients[i];
  327. nstr << inverse;
  328. if (pstr.str().size() <= nstr.str().size())
  329. {
  330. out << " + ";
  331. if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity()))
  332. out << m_coefficients[i];
  333. }
  334. else
  335. {
  336. out << " - ";
  337. if (!i || !ring.Equal(inverse, ring.MultiplicativeIdentity()))
  338. out << inverse;
  339. }
  340. }
  341. switch (i)
  342. {
  343. case 0:
  344. break;
  345. case 1:
  346. out << "x";
  347. break;
  348. default:
  349. out << "x^" << i;
  350. }
  351. }
  352. }
  353. }
  354. else
  355. {
  356. out << ring.Identity();
  357. }
  358. return out;
  359. }
  360. template <class T>
  361. void PolynomialOver<T>::Divide(PolynomialOver<T> &r, PolynomialOver<T> &q, const PolynomialOver<T> &a, const PolynomialOver<T> &d, const Ring &ring)
  362. {
  363. unsigned int i = a.CoefficientCount(ring);
  364. const int dDegree = d.Degree(ring);
  365. if (dDegree < 0)
  366. throw DivideByZero();
  367. r = a;
  368. q.m_coefficients.resize(STDMAX(0, int(i - dDegree)));
  369. while (i > (unsigned int)dDegree)
  370. {
  371. --i;
  372. q.m_coefficients[i-dDegree] = ring.Divide(r.m_coefficients[i], d.m_coefficients[dDegree]);
  373. for (int j=0; j<=dDegree; j++)
  374. ring.Reduce(r.m_coefficients[i-dDegree+j], ring.Multiply(q.m_coefficients[i-dDegree], d.m_coefficients[j]));
  375. }
  376. r.CoefficientCount(ring); // resize r.m_coefficients
  377. }
  378. // ********************************************************
  379. // helper function for Interpolate() and InterpolateAt()
  380. template <class T>
  381. void RingOfPolynomialsOver<T>::CalculateAlpha(std::vector<CoefficientType> &alpha, const CoefficientType x[], const CoefficientType y[], unsigned int n) const
  382. {
  383. for (unsigned int j=0; j<n; ++j)
  384. alpha[j] = y[j];
  385. for (unsigned int k=1; k<n; ++k)
  386. {
  387. for (unsigned int j=n-1; j>=k; --j)
  388. {
  389. m_ring.Reduce(alpha[j], alpha[j-1]);
  390. CoefficientType d = m_ring.Subtract(x[j], x[j-k]);
  391. if (!m_ring.IsUnit(d))
  392. throw InterpolationFailed();
  393. alpha[j] = m_ring.Divide(alpha[j], d);
  394. }
  395. }
  396. }
  397. template <class T>
  398. typename RingOfPolynomialsOver<T>::Element RingOfPolynomialsOver<T>::Interpolate(const CoefficientType x[], const CoefficientType y[], unsigned int n) const
  399. {
  400. assert(n > 0);
  401. std::vector<CoefficientType> alpha(n);
  402. CalculateAlpha(alpha, x, y, n);
  403. std::vector<CoefficientType> coefficients((size_t)n, m_ring.Identity());
  404. coefficients[0] = alpha[n-1];
  405. for (int j=n-2; j>=0; --j)
  406. {
  407. for (unsigned int i=n-j-1; i>0; i--)
  408. coefficients[i] = m_ring.Subtract(coefficients[i-1], m_ring.Multiply(coefficients[i], x[j]));
  409. coefficients[0] = m_ring.Subtract(alpha[j], m_ring.Multiply(coefficients[0], x[j]));
  410. }
  411. return PolynomialOver<T>(coefficients.begin(), coefficients.end());
  412. }
  413. template <class T>
  414. typename RingOfPolynomialsOver<T>::CoefficientType RingOfPolynomialsOver<T>::InterpolateAt(const CoefficientType &position, const CoefficientType x[], const CoefficientType y[], unsigned int n) const
  415. {
  416. assert(n > 0);
  417. std::vector<CoefficientType> alpha(n);
  418. CalculateAlpha(alpha, x, y, n);
  419. CoefficientType result = alpha[n-1];
  420. for (int j=n-2; j>=0; --j)
  421. {
  422. result = m_ring.Multiply(result, m_ring.Subtract(position, x[j]));
  423. m_ring.Accumulate(result, alpha[j]);
  424. }
  425. return result;
  426. }
  427. template <class Ring, class Element>
  428. void PrepareBulkPolynomialInterpolation(const Ring &ring, Element *w, const Element x[], unsigned int n)
  429. {
  430. for (unsigned int i=0; i<n; i++)
  431. {
  432. Element t = ring.MultiplicativeIdentity();
  433. for (unsigned int j=0; j<n; j++)
  434. if (i != j)
  435. t = ring.Multiply(t, ring.Subtract(x[i], x[j]));
  436. w[i] = ring.MultiplicativeInverse(t);
  437. }
  438. }
  439. template <class Ring, class Element>
  440. void PrepareBulkPolynomialInterpolationAt(const Ring &ring, Element *v, const Element &position, const Element x[], const Element w[], unsigned int n)
  441. {
  442. assert(n > 0);
  443. std::vector<Element> a(2*n-1);
  444. unsigned int i;
  445. for (i=0; i<n; i++)
  446. a[n-1+i] = ring.Subtract(position, x[i]);
  447. for (i=n-1; i>1; i--)
  448. a[i-1] = ring.Multiply(a[2*i], a[2*i-1]);
  449. a[0] = ring.MultiplicativeIdentity();
  450. for (i=0; i<n-1; i++)
  451. {
  452. std::swap(a[2*i+1], a[2*i+2]);
  453. a[2*i+1] = ring.Multiply(a[i], a[2*i+1]);
  454. a[2*i+2] = ring.Multiply(a[i], a[2*i+2]);
  455. }
  456. for (i=0; i<n; i++)
  457. v[i] = ring.Multiply(a[n-1+i], w[i]);
  458. }
  459. template <class Ring, class Element>
  460. Element BulkPolynomialInterpolateAt(const Ring &ring, const Element y[], const Element v[], unsigned int n)
  461. {
  462. Element result = ring.Identity();
  463. for (unsigned int i=0; i<n; i++)
  464. ring.Accumulate(result, ring.Multiply(y[i], v[i]));
  465. return result;
  466. }
  467. // ********************************************************
  468. template <class T, int instance>
  469. const PolynomialOverFixedRing<T, instance> &PolynomialOverFixedRing<T, instance>::Zero()
  470. {
  471. return Singleton<ThisType>().Ref();
  472. }
  473. template <class T, int instance>
  474. const PolynomialOverFixedRing<T, instance> &PolynomialOverFixedRing<T, instance>::One()
  475. {
  476. return Singleton<ThisType, NewOnePolynomial>().Ref();
  477. }
  478. NAMESPACE_END