myl7/fss 2.0.0
Function secret sharing (FSS) primitives including distributed point/comparison function (DPF/DCF)
Loading...
Searching...
No Matches
uint128.cuh
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
7
8#pragma once
9#include <fss/group.cuh>
10#include <cuda_runtime.h>
11#include <cassert>
12#include <cstdint>
13
14namespace fss::group {
15
22 uint64_t low;
23 uint64_t high;
24};
25
42template <Uint128Modulus mod = Uint128Modulus{0, uint64_t{1} << 63}>
43 requires(((static_cast<__uint128_t>(mod.high) << 64) | mod.low) > 0 &&
44 ((static_cast<__uint128_t>(mod.high) << 64) | mod.low) <= static_cast<__uint128_t>(1) << 127)
45class Uint128 {
46public:
47 // Groupable interface.
48
50 __host__ __device__ Uint128 operator+(Uint128 rhs) const {
51 if (val_ >= kMod - rhs.val_) return {val_ + rhs.val_ - kMod};
52 else return {val_ + rhs.val_};
53 }
54
56 __host__ __device__ Uint128 operator-() const {
57 if (val_ == 0) return {0};
58 else return {kMod - val_};
59 }
60
62 __host__ __device__ Uint128() : val_(0) {}
63
65 __host__ __device__ static Uint128 From(int4 buf) {
66 assert((buf.w & 1) == 0);
67
68 __uint128_t value = static_cast<__uint128_t>(static_cast<unsigned int>(buf.x)) |
69 static_cast<__uint128_t>(static_cast<unsigned int>(buf.y)) << 32 |
70 static_cast<__uint128_t>(static_cast<unsigned int>(buf.z)) << 64 |
71 // The LSB of buf.w is the clamped bit
72 static_cast<__uint128_t>(static_cast<unsigned int>(buf.w) >> 1) << 96;
73
74 return {value % kMod};
75 }
76
78 __host__ __device__ int4 Into() const {
79 int4 buf = {0, 0, 0, 0};
80 buf.x = static_cast<int>(val_ & 0xffffffff);
81 buf.y = static_cast<int>((val_ >> 32) & 0xffffffff);
82 buf.z = static_cast<int>((val_ >> 64) & 0xffffffff);
83 // The LSB of buf.w is the clamped bit
84 buf.w = static_cast<int>((val_ >> 96) << 1);
85 return buf;
86 }
87
88 // Additional public API.
90 __host__ __device__ __uint128_t Value() const { return val_; }
91
92private:
93 static constexpr __uint128_t kMod = (static_cast<__uint128_t>(mod.high) << 64) | mod.low;
94
95 __uint128_t val_;
96
97 __host__ __device__ Uint128(__uint128_t v) : val_(v) {}
98};
99static_assert(Groupable<Uint128<>>);
100static_assert(Groupable<Uint128<Uint128Modulus{.low = 0xffffffffffffffff, .high = 0x3fffffff}>>);
101
102} // namespace fss::group
The modulus of a Uint128 group, split into two 64-bit halves: the structural form keeps host stubs fr...
Definition uint128.cuh:21
uint64_t high
High 64 bits of the modulus, i.e., the modulus is .
Definition uint128.cuh:23
uint64_t low
Low 64 bits of the modulus.
Definition uint128.cuh:22