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

feat(math): implement binary field initially #416

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

chokobole
Copy link
Contributor

Description

This PR implements BinaryField for binius.

See https://gitlab.com/IrreducibleOSS/binius

@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 34f7b89 to 4f9f771 Compare May 16, 2024 04:56
Copy link
Contributor

@TomTaehoonKim TomTaehoonKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chokobole chokobole marked this pull request as draft May 16, 2024 11:21
@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 4f9f771 to 4c63976 Compare May 23, 2024 08:38
@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 4c63976 to 285cb26 Compare May 28, 2024 06:12
@chokobole chokobole marked this pull request as ready for review May 28, 2024 06:13
@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 285cb26 to 24dd738 Compare May 29, 2024 13:27
Copy link
Contributor

@dongchangYoo dongchangYoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

tachyon/math/base/big_int.h Outdated Show resolved Hide resolved
tachyon/math/base/big_int.h Outdated Show resolved Hide resolved
@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 24dd738 to 6a1026c Compare May 30, 2024 07:44
Copy link
Contributor

@Insun35 Insun35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@ashjeong ashjeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the following commit messages!
e1f8e85
feat(math): implement BinaryField initially up to 8 bits
Also change for a4e9f33
390c5f5
Remove the "the"
feat(base): add (u)int128 cases to base::Range

":binary_field4",
":binary_field64",
":binary_field8",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove clang formatting on this to they can be in order of 1,2,4,8,16,32,64?

EXPECT_EQ(f * f_inv, PrimeField::One());
f.InverseInPlace();
EXPECT_EQ(f, f_inv);
if (!f.IsZero()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed this in #424 check this commit 92e338d
NOTE: it is set to change currently as per reviews I've received, but regardless, will be changed!

@@ -180,7 +201,10 @@ def _do_generate_prime_fields(
use_asm,
use_montgomery,
**kwargs):
is_small_prime_field = int(modulus) < 1 << 32
modulus_int = int(modulus)
is_binary_prime_field = modulus_int == 1 << 1 or modulus_int == 1 << 2 or modulus_int == 1 << 4 or modulus_int == 1 << 8 or \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the moduli beside the first if statement do not denote a prime field! Please change binary field to be not prime field (except maybe F2)

Comment on lines 324 to 328
SubPrimeField z2 = lhs1 * rhs1;
SubPrimeField z0z2 = z0 + z2;
SubPrimeField z1 = (lhs0 + lhs1) * (rhs0 + rhs1) - z0z2;
SubPrimeField z2a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z2);
return F::Compose(z0z2, z1 + z2a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rename these to be in order?

Suggested change
SubPrimeField z2 = lhs1 * rhs1;
SubPrimeField z0z2 = z0 + z2;
SubPrimeField z1 = (lhs0 + lhs1) * (rhs0 + rhs1) - z0z2;
SubPrimeField z2a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z2);
return F::Compose(z0z2, z1 + z2a);
SubPrimeField z1 = lhs1 * rhs1;
SubPrimeField z0z1 = z0 + z1;
SubPrimeField z2 = (lhs0 + lhs1) * (rhs0 + rhs1) - z0z1;
SubPrimeField z1a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z1);
return F::Compose(z0z1, z2 + z1a);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I've taken the naming convention from the original code, though. Okay!

Comment on lines 349 to 351
SubPrimeField z2 = x1.Square();
SubPrimeField z2a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z2);
return F::Compose(z0 + z2, z2a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SubPrimeField z2 = x1.Square();
SubPrimeField z2a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z2);
return F::Compose(z0 + z2, z2a);
SubPrimeField z1 = x1.Square();
SubPrimeField z1a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z1);
return F::Compose(z0 + z1, z1a);

Comment on lines 439 to 447
constexpr BigInt operator&(const BigInt& other) const {
BigInt ret;
if constexpr (N == 1) {
ret[0] = limbs[0] & other[0];
} else if constexpr (N == 2) {
ret[0] = limbs[0] & other.limbs[0];
ret[1] = limbs[1] & other.limbs[1];
} else {
DoAnd(*this, other, ret);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the main desire for these logic statement hopes of returning the simple cases early? I feel as though simply having DoAnd(*this, other, ret); only would suffice otherwise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is!

is_small_prime_field = int(modulus) < 1 << 32
modulus_int = int(modulus)
is_binary_prime_field = modulus_int == 1 << 1 or modulus_int == 1 << 2 or modulus_int == 1 << 4 or modulus_int == 1 << 8 or \
modulus_int == 1 << 16 or modulus_int == 1 << 32 or modulus_int == 1 << 64 or modulus_int == 1 << 128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should be spliced up into the commits of 64/128!

@@ -14,6 +14,15 @@

namespace tachyon::math {

bool IsBinaryField(const mpz_class& m) {
mpz_class binary_modulus(2);
for (int i = 0; i < 8; ++i) {
Copy link
Contributor

@ashjeong ashjeong May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though nitpicky, I think changing this to i < 4, 7, 8 for the commits of up to 8, 64, 128 would help to prevent confusion.

Comment on lines 40 to 41
std::conditional_t<kBits == 16, uint16_t,
std::conditional_t<kBits == 32, uint32_t, uint64_t>>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this to the next up to 64 commit!

Comment on lines 253 to 260
case 16 + 1:
modulus_type = "uint32_t";
value_type = "uint16_t";
break;
case 32 + 1:
modulus_type = "uint64_t";
value_type = "uint32_t";
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be added to the next commit!

@chokobole chokobole force-pushed the feat/implement-binary-field-initially branch from 6a1026c to 332a181 Compare May 31, 2024 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants