Skip to content

Commit

Permalink
add function and test of the inverse of f_2^128 (#359)
Browse files Browse the repository at this point in the history
* add function and test of the inverse of f_2^128

* update format

* simplify the inv

* simplify the inv
  • Loading branch information
ShallMate authored Jul 10, 2024
1 parent e230ae7 commit cf2737c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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) {
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);
}
}

0 comments on commit cf2737c

Please sign in to comment.