Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function and test of the inverse of f_2^128 #359

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions yacl/math/f2k/f2k.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ inline uint64_t GfInv64(uint64_t x) {
return t0;
}

// inverse over Galois Field F_{2^128}
inline uint128_t GfInv128(uint128_t x) {
Jamie-Cui marked this conversation as resolved.
Show resolved Hide resolved
uint128_t t0 = GfMul128(x, x);
uint128_t t1 = t0;
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
for (int i = 0; i < 60; i++) {
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
t0 = GfMul128(t0, t0);
t1 = GfMul128(t1, t0);
}
return t1;
}

// Inner product <x,y>
inline std::pair<uint128_t, uint128_t> ClMul128(absl::Span<const uint128_t> x,
absl::Span<const uint128_t> y) {
Expand Down
12 changes: 12 additions & 0 deletions yacl/math/f2k/f2k_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,16 @@ TEST(F2kTest, GfInv64_inner_product) {
auto check = yacl::GfMul64(x[i], inv);
EXPECT_EQ(uint64_t(1), check);
}
}

// test for the inverse of 128-bit field
TEST(F2kTest, GfInv128_inner_product) {
const uint64_t size = 1001;

auto x = yacl::crypto::RandVec<uint128_t>(size);
for (uint128_t i = 0; i < size; ++i) {
auto inv = yacl::GfInv128(x[i]);
auto check = yacl::GfMul128(x[i], inv);
EXPECT_EQ(uint128_t(1), check);
}
}
Loading