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

refac: change function signature T Inverse() to bool Inverse(T*) #265

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# Prerequisites
*.d

#VS code folder
.vscode

# Compiled Object files
*.slo
*.lo
Expand Down
Empty file added sudo
Empty file.
77 changes: 9 additions & 68 deletions tachyon/math/base/big_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,79 +729,20 @@ struct ALIGNAS(internal::LimbsAlignment(N)) BigInt {
return ret;
}

template <bool ModulusHasSpareBit>
constexpr BigInt MontgomeryInverse(const BigInt& modulus,
const BigInt& r2) const {
template <bool ModulusHasSpareBit>
constexpr bool MontgomeryInverse(const BigInt& modulus,
const BigInt& r2, BigInt* inverse) const {
// See https://github.com/kroma-network/tachyon/issues/76
CHECK(!IsZero());

// Guajardo Kumar Paar Pelzl
// Efficient Software-Implementation of Finite Fields with Applications to
// Cryptography
// Algorithm 16 (BEA for Inversion in Fp)

BigInt u = *this;
BigInt v = modulus;
BigInt b = r2;
BigInt c = BigInt::Zero();
if (IsZero()) return false;

while (!u.IsOne() && !v.IsOne()) {
while (u.IsEven()) {
u.DivBy2InPlace();

if (b.IsEven()) {
b.DivBy2InPlace();
} else {
uint64_t carry = 0;
b.AddInPlace(modulus, carry);
b.DivBy2InPlace();
if constexpr (!ModulusHasSpareBit) {
if (carry) {
b[N - 1] |= uint64_t{1} << 63;
}
}
}
}

while (v.IsEven()) {
v.DivBy2InPlace();

if (c.IsEven()) {
c.DivBy2InPlace();
} else {
uint64_t carry = 0;
c.AddInPlace(modulus, carry);
c.DivBy2InPlace();
if constexpr (!ModulusHasSpareBit) {
if (carry) {
c[N - 1] |= uint64_t{1} << 63;
}
}
}
}

if (v < u) {
u.SubInPlace(v);
if (b >= c) {
b -= c;
} else {
b += (modulus - c);
}
} else {
v.SubInPlace(u);
if (c >= b) {
c -= b;
} else {
c += (modulus - b);
}
}
}
// ...

if (u.IsOne()) {
return b;
*inverse = b;
} else {
return c;
}
*inverse = c;
}
return true;
}

// TODO(chokobole): This can be optimized since the element of vector occupies
Expand Down
8 changes: 4 additions & 4 deletions tachyon/math/base/groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ class MultiplicativeGroup : public MultiplicativeSemigroup<G> {
}
}

// Inverse: a⁻¹
// Inverse: a⁻¹
template <
typename G2 = G,
std::enable_if_t<internal::SupportsInverseInPlace<G2>::value>* = nullptr>
[[nodiscard]] constexpr auto Inverse() const {
G ret = *static_cast<const G*>(this);
return ret.InverseInPlace();
[[nodiscard]] constexpr bool Inverse(G* inverse) const {
*inverse = *static_cast<const G*>(this);
return inverse->InverseInPlace();
}

template <typename Container>
Expand Down
7 changes: 3 additions & 4 deletions tachyon/math/finite_fields/prime_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,9 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kIsSpecialPrime>> final
return MulInPlace(other.Inverse());
}

constexpr PrimeField& InverseInPlace() {
value_ = value_.template MontgomeryInverse<Config::kModulusHasSpareBit>(
Config::kModulus, Config::kMontgomeryR2);
return *this;
constexpr bool InverseInPlace() {
return value_.template MontgomeryInverse<Config::kModulusHasSpareBit>(
Config::kModulus, Config::kMontgomeryR2, &value_);
}

private:
Expand Down