gf_util.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Copyright 2010 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Defines GfUtil template class which implements
  15. // 1. some useful operations in GF(2^n),
  16. // 2. CRC helper function (e.g. concatenation of CRCs) which are
  17. // not affected by specific implemenation of CRC computation per se.
  18. //
  19. // Please read crc.pdf to understand how it all works.
  20. #ifndef CRCUTIL_GF_UTIL_H_
  21. #define CRCUTIL_GF_UTIL_H_
  22. #include "base_types.h" // uint8, uint64
  23. #include "crc_casts.h" // TO_BYTE()
  24. #include "platform.h" // GCC_ALIGN_ATTRIBUTE(16), SHIFT_*_SAFE
  25. namespace crcutil {
  26. #pragma pack(push, 16)
  27. // "Crc" is the type used internally and to return values of N-bit CRC.
  28. template<typename Crc> class GfUtil {
  29. public:
  30. // Initializes the tables given generating polynomial of degree (degree).
  31. // If "canonical" is true, starting CRC value and computed CRC value will be
  32. // XOR-ed with 111...111.
  33. GfUtil() {}
  34. GfUtil(const Crc &generating_polynomial, size_t degree, bool canonical) {
  35. Init(generating_polynomial, degree, canonical);
  36. }
  37. void Init(const Crc &generating_polynomial, size_t degree, bool canonical) {
  38. Crc one = 1;
  39. one <<= degree - 1;
  40. this->generating_polynomial_ = generating_polynomial;
  41. this->crc_bytes_ = (degree + 7) >> 3;
  42. this->degree_ = degree;
  43. this->one_ = one;
  44. if (canonical) {
  45. this->canonize_ = one | (one - 1);
  46. } else {
  47. this->canonize_ = 0;
  48. }
  49. this->normalize_[0] = 0;
  50. this->normalize_[1] = generating_polynomial;
  51. Crc k = one >> 1;
  52. for (size_t i = 0; i < sizeof(uint64) * 8; ++i) {
  53. this->x_pow_2n_[i] = k;
  54. k = Multiply(k, k);
  55. }
  56. this->crc_of_crc_ = Multiply(this->canonize_,
  57. this->one_ ^ Xpow8N((degree + 7) >> 3));
  58. FindLCD(Xpow8N(this->crc_bytes_), &this->x_pow_minus_W_);
  59. }
  60. // Returns generating polynomial.
  61. Crc GeneratingPolynomial() const {
  62. return this->generating_polynomial_;
  63. }
  64. // Returns number of bits in CRC (degree of generating polynomial).
  65. size_t Degree() const {
  66. return this->degree_;
  67. }
  68. // Returns start/finish adjustment constant.
  69. Crc Canonize() const {
  70. return this->canonize_;
  71. }
  72. // Returns normalized value of 1.
  73. Crc One() const {
  74. return this->one_;
  75. }
  76. // Returns value of CRC(A, |A|, start_new) given known
  77. // crc=CRC(A, |A|, start_old) -- without touching the data.
  78. Crc ChangeStartValue(const Crc &crc, uint64 bytes,
  79. const Crc &start_old,
  80. const Crc &start_new) const {
  81. return (crc ^ Multiply(start_new ^ start_old, Xpow8N(bytes)));
  82. }
  83. // Returns CRC of concatenation of blocks A and B when CRCs
  84. // of blocks A and B are known -- without touching the data.
  85. //
  86. // To be precise, given CRC(A, |A|, startA) and CRC(B, |B|, 0),
  87. // returns CRC(AB, |AB|, startA).
  88. Crc Concatenate(const Crc &crc_A, const Crc &crc_B, uint64 bytes_B) const {
  89. return ChangeStartValue(crc_B, bytes_B, 0 /* start_B */, crc_A);
  90. }
  91. // Returns CRC of sequence of zeroes -- without touching the data.
  92. Crc CrcOfZeroes(uint64 bytes, const Crc &start) const {
  93. Crc tmp = Multiply(start ^ this->canonize_, Xpow8N(bytes));
  94. return (tmp ^ this->canonize_);
  95. }
  96. // Given CRC of a message, stores extra (degree + 7)/8 bytes after
  97. // the message so that CRC(message+extra, start) = result.
  98. // Does not change CRC start value (use ChangeStartValue for that).
  99. // Returns number of stored bytes.
  100. size_t StoreComplementaryCrc(void *dst,
  101. const Crc &message_crc,
  102. const Crc &result) const {
  103. Crc crc0 = Multiply(result ^ this->canonize_, this->x_pow_minus_W_);
  104. crc0 ^= message_crc ^ this->canonize_;
  105. uint8 *d = reinterpret_cast<uint8 *>(dst);
  106. for (size_t i = 0; i < this->crc_bytes_; ++i) {
  107. d[i] = TO_BYTE(crc0);
  108. crc0 >>= 8;
  109. }
  110. return this->crc_bytes_;
  111. }
  112. // Stores given CRC of a message as (degree + 7)/8 bytes filled
  113. // with 0s to the right. Returns number of stored bytes.
  114. // CRC of the message and stored CRC is a constant value returned
  115. // by CrcOfCrc() -- it does not depend on contents of the message.
  116. size_t StoreCrc(void *dst, const Crc &crc) const {
  117. uint8 *d = reinterpret_cast<uint8 *>(dst);
  118. Crc crc0 = crc;
  119. for (size_t i = 0; i < this->crc_bytes_; ++i) {
  120. d[i] = TO_BYTE(crc0);
  121. crc0 >>= 8;
  122. }
  123. return this->crc_bytes_;
  124. }
  125. // Returns expected CRC value of CRC(Message,CRC(Message))
  126. // when CRC is stored after the message. This value is fixed
  127. // and does not depend on the message or CRC start value.
  128. Crc CrcOfCrc() const {
  129. return this->crc_of_crc_;
  130. }
  131. // Returns ((a * b) mod P) where "a" and "b" are of degree <= (D-1).
  132. Crc Multiply(const Crc &aa, const Crc &bb) const {
  133. Crc a = aa;
  134. Crc b = bb;
  135. if ((a ^ (a - 1)) < (b ^ (b - 1))) {
  136. Crc temp = a;
  137. a = b;
  138. b = temp;
  139. }
  140. if (a == 0) {
  141. return a;
  142. }
  143. Crc product = 0;
  144. Crc one = this->one_;
  145. for (; a != 0; a <<= 1) {
  146. if ((a & one) != 0) {
  147. product ^= b;
  148. a ^= one;
  149. }
  150. b = (b >> 1) ^ this->normalize_[Downcast<Crc, size_t>(b & 1)];
  151. }
  152. return product;
  153. }
  154. // Returns ((unnorm * m) mod P) where degree of m is <= (D-1)
  155. // and degree of value "unnorm" is provided explicitly.
  156. Crc MultiplyUnnormalized(const Crc &unnorm, size_t degree,
  157. const Crc &m) const {
  158. Crc v = unnorm;
  159. Crc result = 0;
  160. while (degree > this->degree_) {
  161. degree -= this->degree_;
  162. Crc value = v & (this->one_ | (this->one_ - 1));
  163. result ^= Multiply(value, Multiply(m, XpowN(degree)));
  164. v >>= this->degree_;
  165. }
  166. result ^= Multiply(v << (this->degree_ - degree), m);
  167. return result;
  168. }
  169. // returns ((x ** n) mod P).
  170. Crc XpowN(uint64 n) const {
  171. Crc one = this->one_;
  172. Crc result = one;
  173. for (size_t i = 0; n != 0; ++i, n >>= 1) {
  174. if (n & 1) {
  175. result = Multiply(result, this->x_pow_2n_[i]);
  176. }
  177. }
  178. return result;
  179. }
  180. // Returns (x ** (8 * n) mod P).
  181. Crc Xpow8N(uint64 n) const {
  182. return XpowN(n << 3);
  183. }
  184. // Returns remainder (A mod B) and sets *q = (A/B) of division
  185. // of two polynomials:
  186. // A = dividend + dividend_x_pow_D_coef * x**degree,
  187. // B = divisor.
  188. Crc Divide(const Crc &dividend0, int dividend_x_pow_D_coef,
  189. const Crc &divisor0, Crc *q) const {
  190. Crc divisor = divisor0;
  191. Crc dividend = dividend0;
  192. Crc quotient = 0;
  193. Crc coef = this->one_;
  194. while ((divisor & 1) == 0) {
  195. divisor >>= 1;
  196. coef >>= 1;
  197. }
  198. if (dividend_x_pow_D_coef) {
  199. quotient = coef >> 1;
  200. dividend ^= divisor >> 1;
  201. }
  202. Crc x_pow_degree_b = 1;
  203. for (;;) {
  204. if ((dividend & x_pow_degree_b) != 0) {
  205. dividend ^= divisor;
  206. quotient ^= coef;
  207. }
  208. if (coef == this->one_) {
  209. break;
  210. }
  211. coef <<= 1;
  212. x_pow_degree_b <<= 1;
  213. divisor <<= 1;
  214. }
  215. *q = quotient;
  216. return dividend;
  217. }
  218. // Extended Euclid's algorith -- for given A finds LCD(A, P) and
  219. // value B such that (A * B) mod P = LCD(A, P).
  220. Crc FindLCD(const Crc &A, Crc *B) const {
  221. if (A == 0 || A == this->one_) {
  222. *B = A;
  223. return A;
  224. }
  225. // Actually, generating polynomial is
  226. // (generating_polynomial_ + x**degree).
  227. int r0_x_pow_D_coef = 1;
  228. Crc r0 = this->generating_polynomial_;
  229. Crc b0 = 0;
  230. Crc r1 = A;
  231. Crc b1 = this->one_;
  232. for (;;) {
  233. Crc q;
  234. Crc r = Divide(r0, r0_x_pow_D_coef, r1, &q);
  235. if (r == 0) {
  236. break;
  237. }
  238. r0_x_pow_D_coef = 0;
  239. r0 = r1;
  240. r1 = r;
  241. Crc b = b0 ^ Multiply(q, b1);
  242. b0 = b1;
  243. b1 = b;
  244. }
  245. *B = b1;
  246. return r1;
  247. }
  248. protected:
  249. Crc canonize_;
  250. Crc x_pow_2n_[sizeof(uint64) * 8];
  251. Crc generating_polynomial_;
  252. Crc one_;
  253. Crc x_pow_minus_W_;
  254. Crc crc_of_crc_;
  255. Crc normalize_[2];
  256. size_t crc_bytes_;
  257. size_t degree_;
  258. } GCC_ALIGN_ATTRIBUTE(16);
  259. #pragma pack(pop)
  260. } // namespace crcutil
  261. #endif // CRCUTIL_GF_UTIL_H_