Botan 3.9.0
Crypto and TLS for C&
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/compiler.h>
12#include <botan/types.h>
13#include <concepts>
14
15namespace Botan {
16
17/**
18* Bit rotation left by a compile-time constant amount
19* @param input the input word
20* @return input rotated left by ROT bits
21*/
22template <size_t ROT, std::unsigned_integral T>
23BOTAN_FORCE_INLINE constexpr T rotl(T input)
24 requires(ROT > 0 && ROT < 8 * sizeof(T))
25{
26 return static_cast<T>((input << ROT) | (input >> (8 * sizeof(T) - ROT)));
27}
28
29/**
30* Bit rotation right by a compile-time constant amount
31* @param input the input word
32* @return input rotated right by ROT bits
33*/
34template <size_t ROT, std::unsigned_integral T>
35BOTAN_FORCE_INLINE constexpr T rotr(T input)
36 requires(ROT > 0 && ROT < 8 * sizeof(T))
37{
38 return static_cast<T>((input >> ROT) | (input << (8 * sizeof(T) - ROT)));
39}
40
41/**
42* SHA-2 Sigma style function
43*/
44template <size_t R1, size_t R2, size_t S, std::unsigned_integral T>
45BOTAN_FORCE_INLINE constexpr T sigma(T x) {
46 return rotr<R1>(x) ^ rotr<R2>(x) ^ (x >> S);
47}
48
49/**
50* SHA-2 Sigma style function
51*/
52template <size_t R1, size_t R2, size_t R3, std::unsigned_integral T>
53BOTAN_FORCE_INLINE constexpr T rho(T x) {
54 return rotr<R1>(x) ^ rotr<R2>(x) ^ rotr<R3>(x);
55}
56
57/**
58* Bit rotation left, variable rotation amount
59* @param input the input word
60* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
61* @return input rotated left by rot bits
62*/
63template <std::unsigned_integral T>
64BOTAN_FORCE_INLINE constexpr T rotl_var(T input, size_t rot) {
65 return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T) * 8 - rot))) : input;
66}
67
68/**
69* Bit rotation right, variable rotation amount
70* @param input the input word
71* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
72* @return input rotated right by rot bits
73*/
74template <std::unsigned_integral T>
75BOTAN_FORCE_INLINE constexpr T rotr_var(T input, size_t rot) {
76 return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T) * 8 - rot))) : input;
77}
78
79} // namespace Botan
80
81#endif
#define BOTAN_FORCE_INLINE
Definition compiler.h:87
BOTAN_FORCE_INLINE constexpr T rotr(T input)
Definition rotate.h:35
BOTAN_FORCE_INLINE constexpr T rho(T x)
Definition rotate.h:53
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
BOTAN_FORCE_INLINE constexpr T rotr_var(T input, size_t rot)
Definition rotate.h:75
BOTAN_FORCE_INLINE constexpr T sigma(T x)
Definition rotate.h:45
BOTAN_FORCE_INLINE constexpr T rotl_var(T input, size_t rot)
Definition rotate.h:64