Botan 3.0.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/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 {
45 return rotr<R1>(x) ^ rotr<R2>(x) ^ (x >> S);
46 }
47
48/**
49* SHA-2 Sigma style function
50*/
51template<size_t R1, size_t R2, size_t R3, typename T>
52inline constexpr T rho(T x)
53 {
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<typename T>
64inline constexpr T rotl_var(T input, size_t rot)
65 {
66 return rot ? static_cast<T>((input << rot) | (input >> (sizeof(T)*8 - rot))) : input;
67 }
68
69/**
70* Bit rotation right, variable rotation amount
71* @param input the input word
72* @param rot the number of bits to rotate, must be between 0 and sizeof(T)*8-1
73* @return input rotated right by rot bits
74*/
75template<typename T>
76inline constexpr T rotr_var(T input, size_t rot)
77 {
78 return rot ? static_cast<T>((input >> rot) | (input << (sizeof(T)*8 - rot))) : input;
79 }
80
81#if defined(BOTAN_USE_GCC_INLINE_ASM)
82
83#if defined(BOTAN_TARGET_ARCH_IS_X86_64) || defined(BOTAN_TARGET_ARCH_IS_X86_32)
84
85template<>
86inline uint32_t rotl_var(uint32_t input, size_t rot)
87 {
88 asm("roll %1,%0"
89 : "+r" (input)
90 : "c" (static_cast<uint8_t>(rot))
91 : "cc");
92 return input;
93 }
94
95template<>
96inline uint32_t rotr_var(uint32_t input, size_t rot)
97 {
98 asm("rorl %1,%0"
99 : "+r" (input)
100 : "c" (static_cast<uint8_t>(rot))
101 : "cc");
102 return input;
103 }
104
105#endif
106
107#endif
108
109}
110
111#endif
FE_25519 T
Definition: ge.cpp:36
Definition: alg_id.cpp:12
constexpr T rho(T x)
Definition: rotate.h:52
constexpr T sigma(T x)
Definition: rotate.h:43
constexpr T rotr(T input)
Definition: rotate.h:33
constexpr T rotl_var(T input, size_t rot)
Definition: rotate.h:64
constexpr T rotl(T input)
Definition: rotate.h:21
constexpr T rotr_var(T input, size_t rot)
Definition: rotate.h:76