exShake256.cpp

Hash a message batch with SHAKE-256. Run three SHAKE-256 digests on the GPU and compare every output byte with the independent CPU implementation.

ExSDK source-ownedC++Crypto

Source

Example

1#include <oa/oa.h>
2
3OA_MAIN("ExampleCryptoShake256") {
4 constexpr oa::Array<oa::U8, 15> messagesBytes{
5 97U, 108U, 112U, 104U, 97U, 98U, 114U, 97U, 118U, 111U, 99U, 114U, 121U, 112U, 116U
6 };
7 auto messages = oa::FnMatrix::fromBytes(
8 oa::Span<const oa::U8>(messagesBytes.data(), messagesBytes.size()),
9 {3, 5},
10 oa::ScalarType::UInt8
11 );
12
13 auto digests = oa::FnHash::shake256(messages, 32U);
14
15 oa::Array<oa::U8, 96> gpu{};
16 oa::Array<oa::U8, 96> cpu{};
17 if (not oa::FnMatrix::copyToHost(digests, gpu.data(), gpu.size()).isOk()) return 1;
18 for (oa::Usize row = 0; row < 3U; ++row) {
19 oa::shake256(
20 messagesBytes.data() + row * 5U,
21 5U,
22 cpu.data() + row * 32U,
23 32U
24 );
25 }
26 if (oa::memcmp(gpu.data(), cpu.data(), gpu.size()) != 0) {
27 return 1;
28 }
29
30 oa::print("3 GPU SHAKE-256 digests match the CPU oracle");
31 return 0;
32}
33

Output

Verified Python run. Runtime evidence from the source-owned Python example on an Intel Iris Xe Vulkan device. One SHAKE-256 shader pipeline handles the complete three-message batch.

Exshake256.py · output

$ .venv/bin/python sdk/py/examples/crypto/shake256.py
11:43:46.471 [INFO ] [RT ] ComputeDevice (0): Intel(R) Iris(R) Xe Graphics (TGL GT2), vulkan 1.4.354, 11 GB
11:43:46.481 [INFO ] [RT ] bindless heap: buffers=262143 imageSlots=16383 samplerSlots=2047, UPDATE_AFTER_BIND
11:43:46.499 [INFO ] [COMP] pipeline cache: loaded 10860399 bytes from $OA_ROOT/var/vk/pipeline.vcache
11:43:46.510 [INFO ] [COMP] Loading 1 shader pipeline on demand (1 thread, warm cache)
11:43:46.511 [INFO ] [COMP] Loaded 1/1 shader pipeline on demand (failed=0, threads=1, 0.81 ms)
3 GPU SHAKE-256 digests match the CPU oracle