native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
sha.h
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2#pragma once
3
4#include "native/config.h"
5#include <hint.h>
6#include "native/isa.h"
7#if NATIVE_HOST_X86
8#include <immintrin.h>
9
10namespace native::detail::x86_sha {
11 template<isa<x86> Arch, unsigned Selector> requires(Arch.has(x86_feature::sha) && Selector < 4)
12 [[nodiscard]] hint_inline hint_const hint_target("sha")
13 __m128i sha1rnds4(__m128i a, __m128i b) noexcept {
14 return _mm_sha1rnds4_epu32(a, b, Selector);
15 }
16
17 template<isa<x86> Arch, unsigned Selector, class... Args>
18 void sha1rnds4(Args...) = delete;
19
20 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
21 [[nodiscard]] hint_inline hint_const hint_target("sha")
22 __m128i sha1nexte(__m128i a, __m128i b) noexcept {
23 return _mm_sha1nexte_epu32(a, b);
24 }
25
26 template<isa<x86> Arch, class... Args>
27 void sha1nexte(Args...) = delete;
28
29 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
30 [[nodiscard]] hint_inline hint_const hint_target("sha")
31 __m128i sha1msg1(__m128i a, __m128i b) noexcept {
32 return _mm_sha1msg1_epu32(a, b);
33 }
34
35 template<isa<x86> Arch, class... Args>
36 void sha1msg1(Args...) = delete;
37
38 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
39 [[nodiscard]] hint_inline hint_const hint_target("sha")
40 __m128i sha1msg2(__m128i a, __m128i b) noexcept {
41 return _mm_sha1msg2_epu32(a, b);
42 }
43
44 template<isa<x86> Arch, class... Args>
45 void sha1msg2(Args...) = delete;
46
47 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
48 [[nodiscard]] hint_inline hint_const hint_target("sha")
49 __m128i sha256rnds2(__m128i a, __m128i b, __m128i c) noexcept {
50 return _mm_sha256rnds2_epu32(a, b, c);
51 }
52
53 template<isa<x86> Arch, class... Args>
54 void sha256rnds2(Args...) = delete;
55
56 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
57 [[nodiscard]] hint_inline hint_const hint_target("sha")
58 __m128i sha256msg1(__m128i a, __m128i b) noexcept {
59 return _mm_sha256msg1_epu32(a, b);
60 }
61
62 template<isa<x86> Arch, class... Args>
63 void sha256msg1(Args...) = delete;
64
65 template<isa<x86> Arch> requires(Arch.has(x86_feature::sha))
66 [[nodiscard]] hint_inline hint_const hint_target("sha")
67 __m128i sha256msg2(__m128i a, __m128i b) noexcept {
68 return _mm_sha256msg2_epu32(a, b);
69 }
70
71 template<isa<x86> Arch, class... Args>
72 void sha256msg2(Args...) = delete;
73
74}
75#endif
76
77#include "native/x86/detail/constant_lanes.h"
78#include <array>
79#include <bit>
80#include <cstdint>
81
82namespace native::detail::x86_sha_constant {
83 using x86_constant::lanes;
84
85 constexpr std::uint32_t choose(std::uint32_t x, std::uint32_t y, std::uint32_t z) noexcept {
86 return (x & y) ^ (~x & z);
87 }
88
89 constexpr std::uint32_t majority(std::uint32_t x, std::uint32_t y, std::uint32_t z) noexcept {
90 return (x & y) ^ (x & z) ^ (y & z);
91 }
92
93 constexpr std::uint32_t small_sigma0(std::uint32_t x) noexcept {
94 return std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3);
95 }
96
97 constexpr std::uint32_t small_sigma1(std::uint32_t x) noexcept {
98 return std::rotr(x, 17) ^ std::rotr(x, 19) ^ (x >> 10);
99 }
100
101 // SHA-1 packs A,B,C,D from the most significant dword down. The first
102 // message dword already contains E; later rounds obtain E from the old D.
103 template<unsigned Selector, class V>
104 constexpr V rounds1(V state, V message) noexcept {
105 auto x = lanes(state);
106 auto words = lanes(message);
107 auto a = x[3];
108 auto b = x[2];
109 auto c = x[1];
110 auto d = x[0];
111 std::uint32_t e = 0;
112 constexpr std::uint32_t constants[]{0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};
113 for (unsigned round = 0; round < 4; ++round) {
114 std::uint32_t function;
115 if constexpr (Selector == 0) {
116 function = choose(b, c, d);
117 } else if constexpr (Selector == 2) {
118 function = majority(b, c, d);
119 } else {
120 function = b ^ c ^ d;
121 }
122 auto next = std::rotl(a, 5) + function + e + words[3 - round] + constants[Selector];
123 e = d;
124 d = c;
125 c = std::rotl(b, 30);
126 b = a;
127 a = next;
128 }
129 std::array<std::uint32_t, 4> result{d, c, b, a};
130 return V::load(result.data());
131 }
132
133 template<class V>
134 constexpr V next_e(V state, V message) noexcept {
135 auto x = lanes(state);
136 auto result = lanes(message);
137 result[3] += std::rotl(x[3], 30);
138 return V::load(result.data());
139 }
140
141 template<class V>
142 constexpr V message1_first(V a, V b) noexcept {
143 auto x = lanes(a);
144 auto y = lanes(b);
145 std::array<std::uint32_t, 4> result{x[0] ^ y[2], x[1] ^ y[3], x[2] ^ x[0], x[3] ^ x[1]};
146 return V::load(result.data());
147 }
148
149 template<class V>
150 constexpr V message1_last(V a, V b) noexcept {
151 auto x = lanes(a);
152 auto y = lanes(b);
153 std::array<std::uint32_t, 4> result{};
154 result[3] = std::rotl(x[3] ^ y[2], 1);
155 result[2] = std::rotl(x[2] ^ y[1], 1);
156 result[1] = std::rotl(x[1] ^ y[0], 1);
157 result[0] = std::rotl(x[0] ^ result[3], 1);
158 return V::load(result.data());
159 }
160
161 // CDGH and ABEF are packed from the most significant dword down. Only
162 // message dwords 0/1 participate; both already contain the round constants.
163 template<class V>
164 constexpr V rounds256(V cdgh, V abef, V message) noexcept {
165 auto x = lanes(cdgh);
166 auto y = lanes(abef);
167 auto words = lanes(message);
168 auto a = y[3];
169 auto b = y[2];
170 auto c = x[3];
171 auto d = x[2];
172 auto e = y[1];
173 auto f = y[0];
174 auto g = x[1];
175 auto h = x[0];
176 for (unsigned round = 0; round < 2; ++round) {
177 auto sigma1 = std::rotr(e, 6) ^ std::rotr(e, 11) ^ std::rotr(e, 25);
178 auto sigma0 = std::rotr(a, 2) ^ std::rotr(a, 13) ^ std::rotr(a, 22);
179 auto first = h + sigma1 + choose(e, f, g) + words[round];
180 auto second = sigma0 + majority(a, b, c);
181 h = g;
182 g = f;
183 f = e;
184 e = d + first;
185 d = c;
186 c = b;
187 b = a;
188 a = first + second;
189 }
190 std::array<std::uint32_t, 4> result{f, e, b, a};
191 return V::load(result.data());
192 }
193
194 template<class V>
195 constexpr V message256_first(V a, V b) noexcept {
196 auto x = lanes(a);
197 auto y = lanes(b);
198 std::array<std::uint32_t, 4> result{x[0] + small_sigma0(x[1]), x[1] + small_sigma0(x[2]),
199 x[2] + small_sigma0(x[3]), x[3] + small_sigma0(y[0])};
200 return V::load(result.data());
201 }
202
203 template<class V>
204 constexpr V message256_last(V a, V b) noexcept {
205 auto x = lanes(a);
206 auto y = lanes(b);
207 std::array<std::uint32_t, 4> result{};
208 result[0] = x[0] + small_sigma1(y[2]);
209 result[1] = x[1] + small_sigma1(y[3]);
210 result[2] = x[2] + small_sigma1(result[0]);
211 result[3] = x[3] + small_sigma1(result[1]);
212 return V::load(result.data());
213 }
214}