Skip to content

Commit

Permalink
repo-sync-2024-03-29T10:04:16+0800
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-Cui committed Mar 29, 2024
1 parent 1d89d6b commit b7c9a1f
Show file tree
Hide file tree
Showing 27 changed files with 517 additions and 294 deletions.
2 changes: 1 addition & 1 deletion examples/psu/krtw19_psu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ auto Interpolate(const std::vector<uint64_t>& xs,
}
}
for (size_t k{}; k != size; ++k) {
L_coeffs[k] ^= yacl::GfMul64(Li_coeffs[k], yacl::Inv64(prod));
L_coeffs[k] ^= yacl::GfMul64(Li_coeffs[k], yacl::GfInv64(prod));
}
}
return L_coeffs;
Expand Down
43 changes: 31 additions & 12 deletions yacl/base/aligned_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace yacl {
* @tparam ALIGNMENT_IN_BYTES Must be a positive power of 2.
*/
template <typename ElementType, std::size_t ALIGNMENT_IN_BYTES = 64>
class AlignedAllocator {
class UninitAlignedAllocator {
private:
static_assert(
ALIGNMENT_IN_BYTES >= alignof(ElementType),
Expand All @@ -40,23 +40,24 @@ class AlignedAllocator {
static std::align_val_t constexpr ALIGNMENT{ALIGNMENT_IN_BYTES};

/**
* This is only necessary because AlignedAllocator has a second template
* This is only necessary because UninitAlignedAllocator has a second template
* argument for the alignment that will make the default
* std::allocator_traits implementation fail during compilation.
* @see https://stackoverflow.com/a/48062758/2191065
*/
template <class OtherElementType>
struct rebind {
using other = AlignedAllocator<OtherElementType, ALIGNMENT_IN_BYTES>;
using other = UninitAlignedAllocator<OtherElementType, ALIGNMENT_IN_BYTES>;
};

constexpr AlignedAllocator() noexcept = default;
constexpr UninitAlignedAllocator() noexcept = default;

constexpr AlignedAllocator(const AlignedAllocator&) noexcept = default;
constexpr UninitAlignedAllocator(const UninitAlignedAllocator&) noexcept =
default;

template <typename U>
constexpr AlignedAllocator(
AlignedAllocator<U, ALIGNMENT_IN_BYTES> const&) noexcept {}
constexpr UninitAlignedAllocator(
UninitAlignedAllocator<U, ALIGNMENT_IN_BYTES> const&) noexcept {}

[[nodiscard]] ElementType* allocate(std::size_t nElementsToAllocate) {
if (nElementsToAllocate >
Expand All @@ -77,20 +78,38 @@ class AlignedAllocator {
* the one used in new. */
::operator delete[](allocatedPointer, ALIGNMENT);
}

/*
* unintialised_allocator implementation (avoid meaningless initialization)
* ref: https://stackoverflow.com/a/15966795
*/
// elide trivial default construction of objects of type ElementType only
template <typename U>
typename std::enable_if<
std::is_same<ElementType, U>::value &&
std::is_trivially_default_constructible<U>::value>::type
construct(U*) {}

// elide trivial default destruction of objects of type ElementType only
template <typename U>
typename std::enable_if<std::is_same<ElementType, U>::value &&
std::is_trivially_destructible<U>::value>::type
destroy(U*) {}
};

template <typename T, std::size_t ALIGNMENT_IN_BYTES = 16>
using AlignedVector = std::vector<T, AlignedAllocator<T, ALIGNMENT_IN_BYTES> >;
using UninitAlignedVector =
std::vector<T, UninitAlignedAllocator<T, ALIGNMENT_IN_BYTES> >;

template <typename T, std::size_t A>
bool operator==(AlignedAllocator<T, A> const& a0,
AlignedAllocator<T, A> const& a1) {
bool operator==(UninitAlignedAllocator<T, A> const& a0,
UninitAlignedAllocator<T, A> const& a1) {
return a0.ALIGNMENT == a1.ALIGNMENT;
}

template <typename T, size_t A>
bool operator!=(AlignedAllocator<T, A> const& a0,
AlignedAllocator<T, A> const& a1) {
bool operator!=(UninitAlignedAllocator<T, A> const& a0,
UninitAlignedAllocator<T, A> const& a1) {
return !(a0 == a1);
}
} // namespace yacl
1 change: 1 addition & 0 deletions yacl/crypto/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ yacl_cc_library(
name = "common",
hdrs = ["common.h"],
deps = [
"//yacl/crypto/rand",
"//yacl/link",
"//yacl/utils:serialize",
],
Expand Down
1 change: 1 addition & 0 deletions yacl/crypto/tools/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#pragma once
#include "yacl/crypto/rand/rand.h"
#include "yacl/link/link.h"
#include "yacl/utils/serialize.h"

Expand Down
5 changes: 5 additions & 0 deletions yacl/kernels/algorithms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,14 @@ yacl_cc_library(
hdrs = [
"ferret_ote.h",
],
copts = AES_COPT_FLAGS,
deps = [
":ot_store",
"//yacl:secparam",
"//yacl/base:exception",
"//yacl/crypto/hash:hash_utils",
"//yacl/crypto/rand",
"//yacl/crypto/tools:common",
"//yacl/crypto/tools:prg",
"//yacl/crypto/tools:rp",
"//yacl/kernels/algorithms:gywz_ote",
Expand Down Expand Up @@ -272,6 +275,7 @@ yacl_cc_library(
"//yacl/base:exception",
"//yacl/base:int128",
"//yacl/crypto/rand",
"//yacl/crypto/tools:common",
"//yacl/crypto/tools:crhash",
"//yacl/crypto/tools:prg",
"//yacl/crypto/tools:rp",
Expand Down Expand Up @@ -302,6 +306,7 @@ yacl_cc_library(
":sgrr_ote",
"//yacl/base:exception",
"//yacl/base:int128",
"//yacl/crypto/tools:common",
"//yacl/crypto/tools:crhash",
"//yacl/crypto/tools:prg",
"//yacl/crypto/tools:rp",
Expand Down
6 changes: 4 additions & 2 deletions yacl/kernels/algorithms/base_vole.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace yacl::crypto {
// Convert OT to f2k-VOLE (non-interactive)
// the type of ot_store must be COT
// w = u * delta + v, where delta = send_ot.delta
// notice:
// - non-interactive method, which means that Ot2Vole could achieve malicious
// secure if send_ot/recv_ot are malicious secure.
// usage:
// - Vole: u in GF(2^64); w, v, delta in GF(2^64)
// Ot2VoleSend<uint64_t,uint64_t> / Ot2VoleRecv<uint64_t,uint64_t>
Expand All @@ -44,9 +47,8 @@ void inline Ot2VoleSend(OtSendStore& send_ot, absl::Span<K> w) {

YACL_ENFORCE(send_ot.Size() >= size * T_bits);

std::array<K, T_bits> basis;
std::array<K, T_bits> w_buff;

std::array<K, T_bits> basis;
if (std::is_same<K, uint128_t>::value) {
memcpy(basis.data(), gf128_basis.data(), T_bits * sizeof(K));
} else if (std::is_same<K, uint64_t>::value) {
Expand Down
Loading

0 comments on commit b7c9a1f

Please sign in to comment.