-
Notifications
You must be signed in to change notification settings - Fork 216
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
base: main
Are you sure you want to change the base?
Conversation
34f7b89
to
4f9f771
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
4f9f771
to
4c63976
Compare
4c63976
to
285cb26
Compare
tachyon/math/finite_fields/generator/prime_field_generator/binary_prime_field_config.h.tpl
Outdated
Show resolved
Hide resolved
tachyon/math/finite_fields/generator/prime_field_generator/binary_prime_field128_config.h.tpl
Outdated
Show resolved
Hide resolved
285cb26
to
24dd738
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
24dd738
to
6a1026c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
":binary_field4", | ||
":binary_field64", | ||
":binary_field8", |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -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 \ |
There was a problem hiding this comment.
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)
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); |
There was a problem hiding this comment.
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?
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); |
There was a problem hiding this comment.
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!
SubPrimeField z2 = x1.Square(); | ||
SubPrimeField z2a = BinaryTowerOperations<SubPrimeField>::MulByAlpha(z2); | ||
return F::Compose(z0 + z2, z2a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
tachyon/math/base/big_int.h
Outdated
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
std::conditional_t<kBits == 16, uint16_t, | ||
std::conditional_t<kBits == 32, uint32_t, uint64_t>>>; |
There was a problem hiding this comment.
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!
case 16 + 1: | ||
modulus_type = "uint32_t"; | ||
value_type = "uint16_t"; | ||
break; | ||
case 32 + 1: | ||
modulus_type = "uint64_t"; | ||
value_type = "uint32_t"; | ||
break; |
There was a problem hiding this comment.
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!
…educexx` are input together
6a1026c
to
332a181
Compare
Description
This PR implements
BinaryField
for binius.See https://gitlab.com/IrreducibleOSS/binius