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

Hadar/b const mult #723

Merged
merged 5 commits into from
Jan 6, 2025
Merged

Hadar/b const mult #723

merged 5 commits into from
Jan 6, 2025

Conversation

HadarIngonyama
Copy link
Contributor

This PR optimizes the multiplication by the constant 3b in the EC addition.
It seems to significantly improve the CPU MSM performance and the CUDA MSM performance a tiny bit.

Copy link
Contributor

@mickeyasa mickeyasa left a comment

Choose a reason for hiding this comment

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

some questions and optimizations

@@ -25,6 +25,11 @@ namespace bls12_377 {
static constexpr point_field_t weierstrass_b = {0x00000001, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000};
static constexpr point_field_t weierstrass_3b = {0x00000003, 0x00000000, 0x00000000, 0x00000000,
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really need that extra definition?
using constexpr in the code does not yield the same performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it probably will, i can try

if constexpr (Gen::is_b_u32) {
r = mul_unsigned<Field{Gen::weierstrass_3b}.limbs_storage.limbs[0], Field>(xs);
if constexpr (Gen::is_b_neg)
return neg(r);
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 it will be more efficient to write here another multiplication by neg(b) which can be calculated in constexpr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand.. xs is known only at runtime, this cannot be calculated in constexpr

Copy link
Contributor

Choose a reason for hiding this comment

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

according to your code, the neg(r) is calculated in runtime
you can write instead of "return neg(r)"
return mul_unsigned<neg(Field{Gen::weierstrass_3b}.limbs_storage.limbs[0], Field)>(xs);
that way the neg operation will be calculated at compile time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that will ruin the whole point of the optimization... 3b is stored in absolute value because it's small that way

if constexpr (Gen::is_b_u32_g2_re) {
r = FF::template mul_unsigned<FF{Gen::weierstrass_3b_g2_re}.limbs_storage.limbs[0], FF>(xs);
if constexpr (Gen::is_b_neg_g2_re)
return FF::neg(r);
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 it will be more efficient to write here another multiplication by neg(b) which can be calculated in constexpr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here

Copy link
Collaborator

@yshekel yshekel left a comment

Choose a reason for hiding this comment

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

Agree with miki that we should compute weistrass_3b at compile time instead. Other than that LGTM

@mickeyasa
Copy link
Contributor

.

Copy link
Contributor

@mickeyasa mickeyasa left a comment

Choose a reason for hiding this comment

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

V

@HadarIngonyama HadarIngonyama merged commit 33c708f into main Jan 6, 2025
15 checks passed
@HadarIngonyama HadarIngonyama deleted the hadar/b-const-mult branch January 6, 2025 16:10
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.

3 participants