Botan  2.6.0
Crypto and TLS for C++11
poly_dbl.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2017,2018 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/internal/poly_dbl.h>
8 #include <botan/loadstor.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 /*
16 * The minimum weight irreducible binary polynomial of size n
17 *
18 * See http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf
19 */
20 enum class MinWeightPolynomial : uint64_t {
21  P64 = 0x1B,
22  P128 = 0x87,
23  P192 = 0x87,
24  P256 = 0x425,
25  P512 = 0x125,
26  P1024 = 0x80043,
27 };
28 
29 template<size_t LIMBS, MinWeightPolynomial P>
30 void poly_double(uint8_t out[], const uint8_t in[])
31  {
32  uint64_t W[LIMBS];
33  load_be(W, in, LIMBS);
34 
35  const uint64_t POLY = static_cast<uint64_t>(P);
36 
37  const uint64_t carry = POLY * (W[0] >> 63);
38  for(size_t i = 0; i != LIMBS - 1; ++i)
39  W[i] = (W[i] << 1) ^ (W[i+1] >> 63);
40  W[LIMBS-1] = (W[LIMBS-1] << 1) ^ carry;
41 
42  copy_out_be(out, LIMBS*8, W);
43  }
44 
45 template<size_t LIMBS, MinWeightPolynomial P>
46 void poly_double_le(uint8_t out[], const uint8_t in[])
47  {
48  uint64_t W[LIMBS];
49  load_le(W, in, LIMBS);
50 
51  const uint64_t POLY = static_cast<uint64_t>(P);
52 
53  const uint64_t carry = POLY * (W[LIMBS-1] >> 63);
54  for(size_t i = 0; i != LIMBS - 1; ++i)
55  W[LIMBS-1-i] = (W[LIMBS-1-i] << 1) ^ (W[LIMBS-2-i] >> 63);
56  W[0] = (W[0] << 1) ^ carry;
57 
58  copy_out_le(out, LIMBS*8, W);
59  }
60 
61 }
62 
63 void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
64  {
65  switch(n)
66  {
67  case 8:
68  return poly_double<1, MinWeightPolynomial::P64>(out, in);
69  case 16:
70  return poly_double<2, MinWeightPolynomial::P128>(out, in);
71  case 24:
72  return poly_double<3, MinWeightPolynomial::P192>(out, in);
73  case 32:
74  return poly_double<4, MinWeightPolynomial::P256>(out, in);
75  case 64:
76  return poly_double<8, MinWeightPolynomial::P512>(out, in);
77  case 128:
78  return poly_double<8, MinWeightPolynomial::P1024>(out, in);
79  default:
80  throw Invalid_Argument("Unsupported size for poly_double_n");
81  }
82  }
83 
84 void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n)
85  {
86  switch(n)
87  {
88  case 8:
89  return poly_double_le<1, MinWeightPolynomial::P64>(out, in);
90  case 16:
91  return poly_double_le<2, MinWeightPolynomial::P128>(out, in);
92  case 24:
93  return poly_double_le<3, MinWeightPolynomial::P192>(out, in);
94  case 32:
95  return poly_double_le<4, MinWeightPolynomial::P256>(out, in);
96  case 64:
97  return poly_double_le<8, MinWeightPolynomial::P512>(out, in);
98  case 128:
99  return poly_double_le<8, MinWeightPolynomial::P1024>(out, in);
100  default:
101  throw Invalid_Argument("Unsupported size for poly_double_n_le");
102  }
103  }
104 
105 }
void carry(int64_t &h0, int64_t &h1)
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:675
T load_be(const uint8_t in[], size_t off)
Definition: loadstor.h:105
void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n)
Definition: poly_dbl.cpp:84
T load_le(const uint8_t in[], size_t off)
Definition: loadstor.h:121
Definition: alg_id.cpp:13
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n)
Definition: poly_dbl.cpp:63
void copy_out_be(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:654