Botan
3.7.1
Crypto and TLS for C&
src
lib
utils
rotate.h
Go to the documentation of this file.
1
/*
2
* Word Rotation Operations
3
* (C) 1999-2008,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_WORD_ROTATE_H_
9
#define BOTAN_WORD_ROTATE_H_
10
11
#include <botan/types.h>
12
13
namespace
Botan
{
14
15
/**
16
* Bit rotation left by a compile-time constant amount
17
* @param input the input word
18
* @return input rotated left by ROT bits
19
*/
20
template
<
size_t
ROT,
typename
T>
21
inline
constexpr
T
rotl
(T input)
22
requires
(ROT > 0 && ROT < 8 *
sizeof
(T))
23
{
24
return
static_cast<
T
>
((input << ROT) | (input >> (8 *
sizeof
(T) - ROT)));
25
}
26
27
/**
28
* Bit rotation right by a compile-time constant amount
29
* @param input the input word
30
* @return input rotated right by ROT bits
31
*/
32
template
<
size_t
ROT,
typename
T>
33
inline
constexpr
T
rotr
(T input)
34
requires
(ROT > 0 && ROT < 8 *
sizeof
(T))
35
{
36
return
static_cast<
T
>
((input >> ROT) | (input << (8 *
sizeof
(T) - ROT)));
37
}
38
39
/**
40
* SHA-2 Sigma style function
41
*/
42
template
<
size_t
R1,
size_t
R2,
size_t
S,
typename
T>
43
inline
constexpr
T
sigma
(T x) {
44
return
rotr<R1>
(x) ^
rotr<R2>
(x) ^ (x >> S);
45
}
46
47
/**
48
* SHA-2 Sigma style function
49
*/
50
template
<
size_t
R1,
size_t
R2,
size_t
R3,
typename
T>
51
inline
constexpr
T
rho
(T x) {
52
return
rotr<R1>
(x) ^
rotr<R2>
(x) ^
rotr<R3>
(x);
53
}
54
55
/**
56
* Bit rotation left, variable rotation amount
57
* @param input the input word
58
* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
59
* @return input rotated left by rot bits
60
*/
61
template
<
typename
T>
62
inline
constexpr
T
rotl_var
(T input,
size_t
rot) {
63
return
rot ?
static_cast<
T
>
((input << rot) | (input >> (
sizeof
(T) * 8 - rot))) : input;
64
}
65
66
/**
67
* Bit rotation right, variable rotation amount
68
* @param input the input word
69
* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
70
* @return input rotated right by rot bits
71
*/
72
template
<
typename
T>
73
inline
constexpr
T
rotr_var
(T input,
size_t
rot) {
74
return
rot ?
static_cast<
T
>
((input >> rot) | (input << (
sizeof
(T) * 8 - rot))) : input;
75
}
76
77
#if defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
78
79
template
<>
80
inline
uint32_t
rotl_var
(uint32_t input,
size_t
rot) {
81
asm
(
"roll %1,%0"
:
"+r"
(input) :
"c"
(
static_cast<
uint8_t
>
(rot)) :
"cc"
);
82
return
input;
83
}
84
85
template
<>
86
inline
uint32_t
rotr_var
(uint32_t input,
size_t
rot) {
87
asm
(
"rorl %1,%0"
:
"+r"
(input) :
"c"
(
static_cast<
uint8_t
>
(rot)) :
"cc"
);
88
return
input;
89
}
90
91
#endif
92
93
}
// namespace Botan
94
95
#endif
Botan
Definition
alg_id.cpp:13
Botan::rho
constexpr T rho(T x)
Definition
rotate.h:51
Botan::sigma
constexpr T sigma(T x)
Definition
rotate.h:43
Botan::rotl
constexpr T rotl(T input)
Definition
rotate.h:21
Botan::rotr
constexpr T rotr(T input)
Definition
rotate.h:33
Botan::rotl_var
constexpr T rotl_var(T input, size_t rot)
Definition
rotate.h:62
Botan::rotr_var
constexpr T rotr_var(T input, size_t rot)
Definition
rotate.h:73
Generated by
1.13.2