/src/botan/src/lib/utils/poly_dbl/poly_dbl.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
9 | | #include <botan/exceptn.h> |
10 | | #include <botan/internal/loadstor.h> |
11 | | |
12 | | namespace Botan { |
13 | | |
14 | | namespace { |
15 | | |
16 | | /* |
17 | | * The minimum weight irreducible binary polynomial of size n |
18 | | * |
19 | | * See "Table of Low-Weight Binary Irreducible Polynomials" |
20 | | * by Gadiel Seroussi, HP Labs Tech Report HPL-98-135 |
21 | | * http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf |
22 | | */ |
23 | | enum class MinWeightPolynomial : uint64_t { |
24 | | P64 = 0x1B, |
25 | | P128 = 0x87, |
26 | | P192 = 0x87, |
27 | | P256 = 0x425, |
28 | | P512 = 0x125, |
29 | | P1024 = 0x80043, |
30 | | }; |
31 | | |
32 | | template <size_t LIMBS, MinWeightPolynomial P> |
33 | 0 | void poly_double(uint8_t out[], const uint8_t in[]) { |
34 | 0 | uint64_t W[LIMBS]; |
35 | 0 | load_be(W, in, LIMBS); |
36 | |
|
37 | 0 | const uint64_t POLY = static_cast<uint64_t>(P); |
38 | |
|
39 | 0 | const uint64_t carry = POLY * (W[0] >> 63); |
40 | |
|
41 | 0 | if constexpr(LIMBS > 0) { |
42 | 0 | for(size_t i = 0; i != LIMBS - 1; ++i) { |
43 | 0 | W[i] = (W[i] << 1) ^ (W[i + 1] >> 63); |
44 | 0 | } |
45 | 0 | } |
46 | |
|
47 | 0 | W[LIMBS - 1] = (W[LIMBS - 1] << 1) ^ carry; |
48 | |
|
49 | 0 | copy_out_be(out, LIMBS * 8, W); |
50 | 0 | } Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<1ul, (Botan::(anonymous namespace)::MinWeightPolynomial)27>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<2ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<3ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<4ul, (Botan::(anonymous namespace)::MinWeightPolynomial)1061>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<8ul, (Botan::(anonymous namespace)::MinWeightPolynomial)293>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<16ul, (Botan::(anonymous namespace)::MinWeightPolynomial)524355>(unsigned char*, unsigned char const*) |
51 | | |
52 | | template <size_t LIMBS, MinWeightPolynomial P> |
53 | 0 | void poly_double_le(uint8_t out[], const uint8_t in[]) { |
54 | 0 | uint64_t W[LIMBS]; |
55 | 0 | load_le(W, in, LIMBS); |
56 | |
|
57 | 0 | const uint64_t POLY = static_cast<uint64_t>(P); |
58 | |
|
59 | 0 | const uint64_t carry = POLY * (W[LIMBS - 1] >> 63); |
60 | |
|
61 | 0 | if constexpr(LIMBS > 0) { |
62 | 0 | for(size_t i = 0; i != LIMBS - 1; ++i) { |
63 | 0 | W[LIMBS - 1 - i] = (W[LIMBS - 1 - i] << 1) ^ (W[LIMBS - 2 - i] >> 63); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | 0 | W[0] = (W[0] << 1) ^ carry; |
68 | |
|
69 | 0 | copy_out_le(out, LIMBS * 8, W); |
70 | 0 | } Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<1ul, (Botan::(anonymous namespace)::MinWeightPolynomial)27>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<2ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<3ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<4ul, (Botan::(anonymous namespace)::MinWeightPolynomial)1061>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<8ul, (Botan::(anonymous namespace)::MinWeightPolynomial)293>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<16ul, (Botan::(anonymous namespace)::MinWeightPolynomial)524355>(unsigned char*, unsigned char const*) |
71 | | |
72 | | } // namespace |
73 | | |
74 | 0 | void poly_double_n(uint8_t out[], const uint8_t in[], size_t n) { |
75 | 0 | switch(n) { |
76 | 0 | case 8: |
77 | 0 | return poly_double<1, MinWeightPolynomial::P64>(out, in); |
78 | 0 | case 16: |
79 | 0 | return poly_double<2, MinWeightPolynomial::P128>(out, in); |
80 | 0 | case 24: |
81 | 0 | return poly_double<3, MinWeightPolynomial::P192>(out, in); |
82 | 0 | case 32: |
83 | 0 | return poly_double<4, MinWeightPolynomial::P256>(out, in); |
84 | 0 | case 64: |
85 | 0 | return poly_double<8, MinWeightPolynomial::P512>(out, in); |
86 | 0 | case 128: |
87 | 0 | return poly_double<16, MinWeightPolynomial::P1024>(out, in); |
88 | 0 | default: |
89 | 0 | throw Invalid_Argument("Unsupported size for poly_double_n"); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | 0 | void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n) { |
94 | 0 | switch(n) { |
95 | 0 | case 8: |
96 | 0 | return poly_double_le<1, MinWeightPolynomial::P64>(out, in); |
97 | 0 | case 16: |
98 | 0 | return poly_double_le<2, MinWeightPolynomial::P128>(out, in); |
99 | 0 | case 24: |
100 | 0 | return poly_double_le<3, MinWeightPolynomial::P192>(out, in); |
101 | 0 | case 32: |
102 | 0 | return poly_double_le<4, MinWeightPolynomial::P256>(out, in); |
103 | 0 | case 64: |
104 | 0 | return poly_double_le<8, MinWeightPolynomial::P512>(out, in); |
105 | 0 | case 128: |
106 | 0 | return poly_double_le<16, MinWeightPolynomial::P1024>(out, in); |
107 | 0 | default: |
108 | 0 | throw Invalid_Argument("Unsupported size for poly_double_n_le"); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | } // namespace Botan |