myl7/fss 1.1.0
Function secret sharing (FSS) primitives including distributed point/comparison function (DPF/DCF)
Loading...
Searching...
No Matches
util.cuh
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
8#pragma once
9#include <cuda_runtime.h>
10#include <cuda/std/array>
11#include <cuda/std/span>
12#include <omp.h>
13
14namespace fss::util {
15
16__host__ __device__ inline int4 Xor(int4 lhs, int4 rhs) {
17 return {lhs.x ^ rhs.x, lhs.y ^ rhs.y, lhs.z ^ rhs.z, lhs.w ^ rhs.w};
18}
19
20__host__ __device__ inline cuda::std::array<int4, 2> Xor(
21 cuda::std::span<const int4, 2> lhs, cuda::std::span<const int4, 2> rhs) {
22 return {Xor(lhs[0], rhs[0]), Xor(lhs[1], rhs[1])};
23}
24
25__host__ __device__ inline cuda::std::array<int4, 4> Xor(
26 cuda::std::span<const int4, 4> lhs, cuda::std::span<const int4, 4> rhs) {
27 return {Xor(lhs[0], rhs[0]), Xor(lhs[1], rhs[1]), Xor(lhs[2], rhs[2]), Xor(lhs[3], rhs[3])};
28}
29
30__host__ __device__ inline int4 SetLsb(int4 val, bool bit) {
31 if (bit) val.w |= 1;
32 else val.w &= ~1;
33 return val;
34}
35
36__host__ __device__ inline bool GetLsb(int4 val) {
37 return (val.w & 1) != 0;
38}
39
40inline int ResolveParDepth(int par_depth) {
41 if (par_depth >= 0) return par_depth;
42 int d = 0, threads = omp_get_max_threads();
43 while ((1 << d) < threads) ++d;
44 return d;
45}
46
47template <typename In>
48__host__ __device__ inline int4 Pack(In val) {
49 int4 buf = {0, 0, 0, 0};
50 if constexpr (sizeof(In) <= 4) {
51 buf.x = static_cast<int>(val);
52 } else if constexpr (sizeof(In) <= 8) {
53 auto v = static_cast<uint64_t>(val);
54 buf.x = static_cast<int>(v & 0xFFFFFFFF);
55 buf.y = static_cast<int>((v >> 32) & 0xFFFFFFFF);
56 } else {
57 auto v = static_cast<__uint128_t>(val);
58 buf.x = static_cast<int>(v & 0xFFFFFFFF);
59 buf.y = static_cast<int>((v >> 32) & 0xFFFFFFFF);
60 buf.z = static_cast<int>((v >> 64) & 0xFFFFFFFF);
61 buf.w = static_cast<int>((v >> 96) & 0xFFFFFFFF);
62 }
63 return buf;
64}
65
66} // namespace fss::util