Botan 3.6.1
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/types.h>
12
13namespace 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*/
20template <size_t ROT, typename T>
21inline 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*/
32template <size_t ROT, typename T>
33inline 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*/
42template <size_t R1, size_t R2, size_t S, typename T>
43inline 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*/
50template <size_t R1, size_t R2, size_t R3, typename T>
51inline 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*/
61template <typename T>
62inline 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*/
72template <typename T>
73inline 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
79template <>
80inline 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
85template <>
86inline 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
FE_25519 T
Definition ge.cpp:34
constexpr T rho(T x)
Definition rotate.h:51
constexpr T sigma(T x)
Definition rotate.h:43
constexpr T rotl(T input)
Definition rotate.h:21
constexpr T rotr(T input)
Definition rotate.h:33
constexpr T rotl_var(T input, size_t rot)
Definition rotate.h:62
constexpr T rotr_var(T input, size_t rot)
Definition rotate.h:73