• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 11280486949

10 Oct 2024 07:12PM UTC coverage: 90.994% (-0.4%) from 91.389%
11280486949

push

github

web-flow
Merge pull request #4364 from randombit/jack/refactor-speed

Initial refactoring of speed cmdlet

89935 of 98836 relevant lines covered (90.99%)

8791476.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.69
/src/lib/utils/poly_dbl/poly_dbl.cpp
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/ct_utils.h>
11
#include <botan/internal/loadstor.h>
12

13
namespace Botan {
14

15
namespace {
16

17
/*
18
* The minimum weight irreducible binary polynomial of size n
19
*
20
* See "Table of Low-Weight Binary Irreducible Polynomials"
21
* by Gadiel Seroussi, HP Labs Tech Report HPL-98-135
22
* http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf
23
*/
24
enum class MinWeightPolynomial : uint32_t {
25
   P64 = 0x1B,
26
   P128 = 0x87,
27
   P192 = 0x87,
28
   P256 = 0x425,
29
   P512 = 0x125,
30
   P1024 = 0x80043,
31
};
32

33
/**
34
* If the top bit of c is set, returns the carry (the polynomial)
35
*
36
* Otherwise returns zero.
37
*/
38
template <MinWeightPolynomial P>
39
inline uint64_t return_carry(uint64_t c) {
434,511✔
40
   return CT::Mask<uint64_t>::expand_top_bit(c).if_set_return(static_cast<uint64_t>(P));
434,511✔
41
}
42

43
template <size_t LIMBS, MinWeightPolynomial P>
44
void poly_double(uint8_t out[], const uint8_t in[]) {
22,501✔
45
   uint64_t W[LIMBS];
46
   load_be(W, in, LIMBS);
22,501✔
47

48
   const uint64_t carry = return_carry<P>(W[0]);
22,501✔
49

50
   if constexpr(LIMBS > 0) {
51
      for(size_t i = 0; i != LIMBS - 1; ++i) {
63,226✔
52
         W[i] = (W[i] << 1) ^ (W[i + 1] >> 63);
40,725✔
53
      }
54
   }
55

56
   W[LIMBS - 1] = (W[LIMBS - 1] << 1) ^ carry;
22,501✔
57

58
   copy_out_be(std::span(out, LIMBS * 8), W);
22,501✔
59
}
22,501✔
60

61
template <size_t LIMBS, MinWeightPolynomial P>
62
void poly_double_le(uint8_t out[], const uint8_t in[]) {
47,588✔
63
   uint64_t W[LIMBS];
64
   load_le(W, in, LIMBS);
47,588✔
65

66
   const uint64_t carry = return_carry<P>(W[LIMBS - 1]);
47,588✔
67

68
   if constexpr(LIMBS > 0) {
69
      for(size_t i = 0; i != LIMBS - 1; ++i) {
65,638✔
70
         W[LIMBS - 1 - i] = (W[LIMBS - 1 - i] << 1) ^ (W[LIMBS - 2 - i] >> 63);
18,050✔
71
      }
72
   }
73

74
   W[0] = (W[0] << 1) ^ carry;
47,588✔
75

76
   copy_out_le(std::span(out, LIMBS * 8), W);
47,588✔
77
}
47,588✔
78

79
}  // namespace
80

81
void poly_double_n(uint8_t out[], const uint8_t in[], size_t n) {
22,501✔
82
   switch(n) {
22,501✔
83
      case 8:
632✔
84
         return poly_double<1, MinWeightPolynomial::P64>(out, in);
632✔
85
      case 16:
14,129✔
86
         return poly_double<2, MinWeightPolynomial::P128>(out, in);
14,129✔
87
      case 24:
3,368✔
88
         return poly_double<3, MinWeightPolynomial::P192>(out, in);
3,368✔
89
      case 32:
2,690✔
90
         return poly_double<4, MinWeightPolynomial::P256>(out, in);
2,690✔
91
      case 64:
1,680✔
92
         return poly_double<8, MinWeightPolynomial::P512>(out, in);
1,680✔
93
      case 128:
2✔
94
         return poly_double<16, MinWeightPolynomial::P1024>(out, in);
2✔
95
      default:
×
96
         throw Invalid_Argument("Unsupported size for poly_double_n");
×
97
   }
98
}
99

100
void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n) {
47,588✔
101
   switch(n) {
47,588✔
102
      case 8:
29,874✔
103
         return poly_double_le<1, MinWeightPolynomial::P64>(out, in);
29,874✔
104
      case 16:
17,610✔
105
         return poly_double_le<2, MinWeightPolynomial::P128>(out, in);
17,610✔
106
      case 24:
×
107
         return poly_double_le<3, MinWeightPolynomial::P192>(out, in);
×
108
      case 32:
72✔
109
         return poly_double_le<4, MinWeightPolynomial::P256>(out, in);
72✔
110
      case 64:
32✔
111
         return poly_double_le<8, MinWeightPolynomial::P512>(out, in);
32✔
112
      case 128:
×
113
         return poly_double_le<16, MinWeightPolynomial::P1024>(out, in);
×
114
      default:
×
115
         throw Invalid_Argument("Unsupported size for poly_double_n_le");
×
116
   }
117
}
118

119
void xts_update_tweak_block(uint8_t tweak[], size_t BS, size_t blocks_in_tweak) {
34,700✔
120
   if(BS == 16) {
34,700✔
121
      constexpr size_t LIMBS = 2;
26,778✔
122

123
      uint64_t W[LIMBS];
26,778✔
124
      load_le(W, &tweak[0], LIMBS);
26,778✔
125

126
      for(size_t i = 1; i < blocks_in_tweak; ++i) {
391,200✔
127
         const uint64_t carry = return_carry<MinWeightPolynomial::P128>(W[1]);
364,422✔
128
         W[1] = (W[1] << 1) ^ (W[0] >> 63);
364,422✔
129
         W[0] = (W[0] << 1) ^ carry;
364,422✔
130
         copy_out_le(std::span(&tweak[i * BS], 2 * 8), W);
364,422✔
131
      }
132
   } else {
133
      for(size_t i = 1; i < blocks_in_tweak; ++i) {
31,728✔
134
         const uint8_t* prev = &tweak[(i - 1) * BS];
23,806✔
135
         uint8_t* cur = &tweak[i * BS];
23,806✔
136
         poly_double_n_le(cur, prev, BS);
23,806✔
137
      }
138
   }
139
}
34,700✔
140

141
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc