From 77b0a9361b3ae066774bed6a3a32a4e3f1065944 Mon Sep 17 00:00:00 2001 From: hadaringonyama Date: Thu, 2 Jan 2025 16:30:45 +0200 Subject: [PATCH 1/5] poc --- icicle/include/icicle/curves/params/bn254.h | 4 +++ icicle/include/icicle/curves/projective.h | 36 +++++++++++++-------- icicle/include/icicle/fields/field.h | 32 ++++++++++++++++++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/icicle/include/icicle/curves/params/bn254.h b/icicle/include/icicle/curves/params/bn254.h index b8095b53f..6841b3429 100644 --- a/icicle/include/icicle/curves/params/bn254.h +++ b/icicle/include/icicle/curves/params/bn254.h @@ -24,6 +24,10 @@ namespace bn254 { 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr point_field_t weierstrass_b = {0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b = {0x00000009, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = false; }; // G1 struct G2 { diff --git a/icicle/include/icicle/curves/projective.h b/icicle/include/icicle/curves/projective.h index 87fe0ab23..ab70ab506 100644 --- a/icicle/include/icicle/curves/projective.h +++ b/icicle/include/icicle/curves/projective.h @@ -64,7 +64,9 @@ class Projective Z3 = Z3 + Z3; // 4. Z3 ← Z3 + Z3 FF t1 = Y * Z; // 5. t1 ← Y · Z FF t2 = FF::sqr(Z); // 6. t2 ← Z · Z - t2 = FF::template mul_unsigned<3>(FF::template mul_const(t2)); // 7. t2 ← b3 · t2 + t2 = FF::template mul_weierstrass_3b(t2); // 7. t2 ← b3 · t2 + // t2 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t2)); // 7. t2 ← b3 · t2 + // t2 = FF::template mul_unsigned<3>(FF::template mul_const(t2)); // 7. t2 ← b3 · t2 FF X3 = t2 * Z3; // 8. X3 ← t2 · Z3 FF Y3 = t0 + t2; // 9. Y3 ← t0 + t2 Z3 = t1 * Z3; // 10. Z3 ← t1 · Z3 @@ -107,12 +109,16 @@ class Projective const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 - const FF t20 = - FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 - const FF t23 = - FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 + const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 + // const FF t20 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t02)); // t20 ← b3 · t02 < 2 + // const FF t20 = + // FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 + // const FF t23 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t17)); // t23 ← b3 · t17 < 2 + // const FF t23 = + // FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 @@ -154,12 +160,16 @@ class Projective const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 - const FF t20 = - FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 - const FF t23 = - FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 + const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 + // const FF t20 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t02)); // t20 ← b3 · t02 < 2 + // const FF t20 = + // FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 + // const FF t23 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t17)); // t23 ← b3 · t17 < 2 + // const FF t23 = + // FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 diff --git a/icicle/include/icicle/fields/field.h b/icicle/include/icicle/fields/field.h index 80c4b4178..47ebf3c07 100644 --- a/icicle/include/icicle/fields/field.h +++ b/icicle/include/icicle/fields/field.h @@ -875,6 +875,38 @@ class Field friend HOST_DEVICE bool operator!=(const Field& xs, const Field& ys) { return !(xs == ys); } +template + static HOST_DEVICE_INLINE Field mul_weierstrass_b(const Field& xs) + { + Field r = {}; + if constexpr (Gen::is_b_u32) { + r = mul_unsigned(xs); + if constexpr (Gen::is_b_neg) + return neg(r); + else { + return r; + } + } else { + return Field{Gen::weierstrass_b} * xs; + } + } + + template + static HOST_DEVICE_INLINE Field mul_weierstrass_3b(const Field& xs) + { + Field r = {}; + if constexpr (Gen::is_b_u32) { + r = mul_unsigned(xs); + if constexpr (Gen::is_b_neg) + return neg(r); + else { + return r; + } + } else { + return Field{Gen::weierstrass_3b} * xs; + } + } + template static HOST_DEVICE_INLINE Field mul_const(const Field& xs) { From 9b3fbd623704f1d7a440090e63f6a5063ad4177c Mon Sep 17 00:00:00 2001 From: hadaringonyama Date: Thu, 2 Jan 2025 17:09:19 +0200 Subject: [PATCH 2/5] changed params --- .../include/icicle/curves/params/bls12_377.h | 16 ++ .../include/icicle/curves/params/bls12_381.h | 16 ++ icicle/include/icicle/curves/params/bn254.h | 11 +- icicle/include/icicle/curves/params/bw6_761.h | 25 ++- .../include/icicle/curves/params/grumpkin.h | 11 +- icicle/include/icicle/curves/projective.h | 194 ++++++++---------- .../include/icicle/fields/complex_extension.h | 46 +++++ icicle/include/icicle/fields/field.h | 16 -- 8 files changed, 209 insertions(+), 126 deletions(-) diff --git a/icicle/include/icicle/curves/params/bls12_377.h b/icicle/include/icicle/curves/params/bls12_377.h index da19ab2bd..71089f8f8 100644 --- a/icicle/include/icicle/curves/params/bls12_377.h +++ b/icicle/include/icicle/curves/params/bls12_377.h @@ -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, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = false; }; // G1 struct G2 { @@ -46,6 +51,17 @@ namespace bls12_377 { static constexpr point_field_t weierstrass_b_g2_im = {0x9999999a, 0x1c9ed999, 0x1ccccccd, 0x0dd39e5c, 0x3c6bf800, 0x129207b6, 0xcd5fd889, 0xdc7b4f91, 0x7460c589, 0x43bd0373, 0xdb0fd6f3, 0x010222f6}; + static constexpr point_field_t weierstrass_3b_g2_re = {0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b_g2_im = {0xcccccccd, 0xd0d3cccc, 0x26666666, 0x126f7dd0, + 0xfb3aa000, 0x18c2b4f2, 0x672a760c, 0x7b4f14c2, + 0xf0810762, 0x04fc0499, 0x796a73ef, 0x01582e9e}; + + static constexpr bool is_b_u32_g2_re = true; + static constexpr bool is_b_neg_g2_re = false; + static constexpr bool is_b_u32_g2_im = false; + static constexpr bool is_b_neg_g2_im = false; static constexpr g2_point_field_t gen_x = {g2_gen_x_re, g2_gen_x_im}; static constexpr g2_point_field_t gen_y = {g2_gen_y_re, g2_gen_y_im}; diff --git a/icicle/include/icicle/curves/params/bls12_381.h b/icicle/include/icicle/curves/params/bls12_381.h index 7457bd49c..116af3c14 100644 --- a/icicle/include/icicle/curves/params/bls12_381.h +++ b/icicle/include/icicle/curves/params/bls12_381.h @@ -25,6 +25,11 @@ namespace bls12_381 { static constexpr point_field_t weierstrass_b = {0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = false; }; struct G2 { @@ -47,6 +52,17 @@ namespace bls12_381 { static constexpr point_field_t weierstrass_b_g2_im = {0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b_g2_re = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b_g2_im = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + + static constexpr bool is_b_u32_g2_re = true; + static constexpr bool is_b_neg_g2_re = false; + static constexpr bool is_b_u32_g2_im = true; + static constexpr bool is_b_neg_g2_im = false; static constexpr g2_point_field_t gen_x = {g2_gen_x_re, g2_gen_x_im}; static constexpr g2_point_field_t gen_y = {g2_gen_y_re, g2_gen_y_im}; diff --git a/icicle/include/icicle/curves/params/bn254.h b/icicle/include/icicle/curves/params/bn254.h index 6841b3429..9c2b8c5fa 100644 --- a/icicle/include/icicle/curves/params/bn254.h +++ b/icicle/include/icicle/curves/params/bn254.h @@ -25,7 +25,7 @@ namespace bn254 { static constexpr point_field_t weierstrass_b = {0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr point_field_t weierstrass_3b = {0x00000009, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = false; }; // G1 @@ -43,6 +43,15 @@ namespace bn254 { 0x1be06ac3, 0x81be1899, 0xceb8aaae, 0x2b149d40}; static constexpr point_field_t weierstrass_b_g2_im = {0x85c315d2, 0xe4a2bd06, 0xe52d1852, 0xa74fa084, 0xeed8fdf4, 0xcd2cafad, 0x3af0fed4, 0x009713b0}; + static constexpr point_field_t weierstrass_3b_g2_re = {0xbce9b021, 0x1ef69c66, 0x3cb039cf, 0xf21b7c8d, + 0x509e8f8f, 0x1499be5e, 0xa9c6bfb8, 0x20753adc}; + static constexpr point_field_t weierstrass_3b_g2_im = {0x91494176, 0xade83713, 0xaf8748f8, 0xf5eee18e, + 0xcc8af9dd, 0x67860f09, 0xb0d2fc7e, 0x01c53b10}; + + static constexpr bool is_b_u32_g2_re = false; + static constexpr bool is_b_neg_g2_re = false; + static constexpr bool is_b_u32_g2_im = false; + static constexpr bool is_b_neg_g2_im = false; static constexpr g2_point_field_t gen_x = {g2_gen_x_re, g2_gen_x_im}; static constexpr g2_point_field_t gen_y = {g2_gen_y_re, g2_gen_y_im}; diff --git a/icicle/include/icicle/curves/params/bw6_761.h b/icicle/include/icicle/curves/params/bw6_761.h index c0a1fcf5e..12d19b50f 100644 --- a/icicle/include/icicle/curves/params/bw6_761.h +++ b/icicle/include/icicle/curves/params/bw6_761.h @@ -25,10 +25,22 @@ namespace bw6_761 { 0xb3053253, 0x9f9df141, 0x6fc2cdd4, 0xbe3fb90b, 0x717a4c55, 0xcc685d31, 0x71b5b806, 0xc5b8fa17, 0xaf7e0dba, 0x265909f1, 0xa2e573a3, 0x1a7348d2, 0x884c9ec6, 0x0f952589, 0x45cc2a42, 0xe6fd637b, 0x0a6fc574, 0x0058b84e}; + // actual value: + // static constexpr point_field_t weierstrass_b = { + // 0x0000008a, 0xf49d0000, 0x70000082, 0xe6913e68, 0xeaf0a437, 0x160cf8ae, 0x5667a8f8, 0x98a116c2, + // 0x73ebff2e, 0x71dcd3dc, 0x12f9fd90, 0x8689c8ed, 0x25b42304, 0x03cebaff, 0xe584e919, 0x707ba638, + // 0x8087be41, 0x528275ef, 0x81d14688, 0xb926186a, 0x04faff3e, 0xd187c940, 0xfb83ce0a, 0x0122e824}; static constexpr point_field_t weierstrass_b = { - 0x0000008a, 0xf49d0000, 0x70000082, 0xe6913e68, 0xeaf0a437, 0x160cf8ae, 0x5667a8f8, 0x98a116c2, - 0x73ebff2e, 0x71dcd3dc, 0x12f9fd90, 0x8689c8ed, 0x25b42304, 0x03cebaff, 0xe584e919, 0x707ba638, - 0x8087be41, 0x528275ef, 0x81d14688, 0xb926186a, 0x04faff3e, 0xd187c940, 0xfb83ce0a, 0x0122e824}; + 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b = { + 0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = true; }; struct G2 { @@ -44,5 +56,12 @@ namespace bw6_761 { 0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr g2_point_field_t weierstrass_3b = { + 0x0000000c, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = false; }; } // namespace bw6_761 diff --git a/icicle/include/icicle/curves/params/grumpkin.h b/icicle/include/icicle/curves/params/grumpkin.h index a468fd911..e968589bc 100644 --- a/icicle/include/icicle/curves/params/grumpkin.h +++ b/icicle/include/icicle/curves/params/grumpkin.h @@ -16,7 +16,14 @@ namespace grumpkin { static constexpr point_field_t gen_y = {0x823f272c, 0x833fc48d, 0xf1181294, 0x2d270d45, 0x6a45d63, 0xcf135e75, 0x00000002, 0x00000000}; - static constexpr point_field_t weierstrass_b = {0xeffffff0, 0x43e1f593, 0x79b97091, 0x2833e848, - 0x8181585d, 0xb85045b6, 0xe131a029, 0x30644e72}; + // actual value: + // static constexpr point_field_t weierstrass_b = {0xeffffff0, 0x43e1f593, 0x79b97091, 0x2833e848, + // 0x8181585d, 0xb85045b6, 0xe131a029, 0x30644e72}; + static constexpr point_field_t weierstrass_b = {0x00000011, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr point_field_t weierstrass_3b = {0x00000033, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; + static constexpr bool is_b_neg = true; }; // G1 } // namespace grumpkin diff --git a/icicle/include/icicle/curves/projective.h b/icicle/include/icicle/curves/projective.h index ab70ab506..0ba8eefc8 100644 --- a/icicle/include/icicle/curves/projective.h +++ b/icicle/include/icicle/curves/projective.h @@ -58,76 +58,68 @@ class Projective const FF Z = point.z; // TODO: Change to efficient dbl once implemented for field.cuh - FF t0 = FF::sqr(Y); // 1. t0 ← Y · Y - FF Z3 = t0 + t0; // 2. Z3 ← t0 + t0 - Z3 = Z3 + Z3; // 3. Z3 ← Z3 + Z3 - Z3 = Z3 + Z3; // 4. Z3 ← Z3 + Z3 - FF t1 = Y * Z; // 5. t1 ← Y · Z - FF t2 = FF::sqr(Z); // 6. t2 ← Z · Z + FF t0 = FF::sqr(Y); // 1. t0 ← Y · Y + FF Z3 = t0 + t0; // 2. Z3 ← t0 + t0 + Z3 = Z3 + Z3; // 3. Z3 ← Z3 + Z3 + Z3 = Z3 + Z3; // 4. Z3 ← Z3 + Z3 + FF t1 = Y * Z; // 5. t1 ← Y · Z + FF t2 = FF::sqr(Z); // 6. t2 ← Z · Z t2 = FF::template mul_weierstrass_3b(t2); // 7. t2 ← b3 · t2 - // t2 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t2)); // 7. t2 ← b3 · t2 - // t2 = FF::template mul_unsigned<3>(FF::template mul_const(t2)); // 7. t2 ← b3 · t2 - FF X3 = t2 * Z3; // 8. X3 ← t2 · Z3 - FF Y3 = t0 + t2; // 9. Y3 ← t0 + t2 - Z3 = t1 * Z3; // 10. Z3 ← t1 · Z3 - t1 = t2 + t2; // 11. t1 ← t2 + t2 - t2 = t1 + t2; // 12. t2 ← t1 + t2 - t0 = t0 - t2; // 13. t0 ← t0 − t2 - Y3 = t0 * Y3; // 14. Y3 ← t0 · Y3 - Y3 = X3 + Y3; // 15. Y3 ← X3 + Y3 - t1 = X * Y; // 16. t1 ← X · Y - X3 = t0 * t1; // 17. X3 ← t0 · t1 - X3 = X3 + X3; // 18. X3 ← X3 + X3 + FF X3 = t2 * Z3; // 8. X3 ← t2 · Z3 + FF Y3 = t0 + t2; // 9. Y3 ← t0 + t2 + Z3 = t1 * Z3; // 10. Z3 ← t1 · Z3 + t1 = t2 + t2; // 11. t1 ← t2 + t2 + t2 = t1 + t2; // 12. t2 ← t1 + t2 + t0 = t0 - t2; // 13. t0 ← t0 − t2 + Y3 = t0 * Y3; // 14. Y3 ← t0 · Y3 + Y3 = X3 + Y3; // 15. Y3 ← X3 + Y3 + t1 = X * Y; // 16. t1 ← X · Y + X3 = t0 * t1; // 17. X3 ← t0 · t1 + X3 = X3 + X3; // 18. X3 ← X3 + X3 return {X3, Y3, Z3}; } friend HOST_DEVICE Projective operator+(Projective p1, const Projective& p2) { - const FF X1 = p1.x; // < 2 - const FF Y1 = p1.y; // < 2 - const FF Z1 = p1.z; // < 2 - const FF X2 = p2.x; // < 2 - const FF Y2 = p2.y; // < 2 - const FF Z2 = p2.z; // < 2 - const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 - const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 - const FF t02 = Z1 * Z2; // t02 ← Z1 · Z2 < 2 - const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 - const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 - const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 - const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 - const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 - const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 - const FF t09 = Y2 + Z2; // t09 ← Y2 + Z2 < 4 - const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 - const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 - const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 - const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 - const FF t14 = X2 + Z2; // t14 ← X2 + Z2 < 4 - const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 - const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 - const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 - const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 - const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 + const FF X1 = p1.x; // < 2 + const FF Y1 = p1.y; // < 2 + const FF Z1 = p1.z; // < 2 + const FF X2 = p2.x; // < 2 + const FF Y2 = p2.y; // < 2 + const FF Z2 = p2.z; // < 2 + const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 + const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 + const FF t02 = Z1 * Z2; // t02 ← Z1 · Z2 < 2 + const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 + const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 + const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 + const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 + const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 + const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 + const FF t09 = Y2 + Z2; // t09 ← Y2 + Z2 < 4 + const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 + const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 + const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 + const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 + const FF t14 = X2 + Z2; // t14 ← X2 + Z2 < 4 + const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 + const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 + const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 + const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 + const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 - // const FF t20 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t02)); // t20 ← b3 · t02 < 2 - // const FF t20 = - // FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 - // const FF t23 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t17)); // t23 ← b3 · t17 < 2 - // const FF t23 = - // FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 - const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 - const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 - const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 - const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 - const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 - const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 - const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 - const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 - const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 + const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 + const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 + const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 + const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 + const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 + const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 + const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 + const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 + const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 return {X3, Y3, Z3}; } @@ -135,50 +127,44 @@ class Projective friend HOST_DEVICE Projective operator+(Projective p1, const Affine& p2) { - const FF X1 = p1.x; // < 2 - const FF Y1 = p1.y; // < 2 - const FF Z1 = p1.z; // < 2 - const FF X2 = p2.x; // < 2 - const FF Y2 = p2.y; // < 2 - const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 - const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 - const FF t02 = Z1; // t02 ← Z1 < 2 - const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 - const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 - const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 - const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 - const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 - const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 - const FF t09 = Y2 + FF::one(); // t09 ← Y2 + 1 < 4 - const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 - const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 - const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 - const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 - const FF t14 = X2 + FF::one(); // t14 ← X2 + 1 < 4 - const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 - const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 - const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 - const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 - const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 + const FF X1 = p1.x; // < 2 + const FF Y1 = p1.y; // < 2 + const FF Z1 = p1.z; // < 2 + const FF X2 = p2.x; // < 2 + const FF Y2 = p2.y; // < 2 + const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 + const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 + const FF t02 = Z1; // t02 ← Z1 < 2 + const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 + const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 + const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 + const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 + const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 + const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 + const FF t09 = Y2 + FF::one(); // t09 ← Y2 + 1 < 4 + const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 + const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 + const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 + const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 + const FF t14 = X2 + FF::one(); // t14 ← X2 + 1 < 4 + const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 + const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 + const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 + const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 + const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 - // const FF t20 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t02)); // t20 ← b3 · t02 < 2 - // const FF t20 = - // FF::template mul_unsigned<3>(FF::template mul_const(t02)); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 - // const FF t23 = FF::template mul_unsigned<3>(FF::template mul_weierstrass_b(t17)); // t23 ← b3 · t17 < 2 - // const FF t23 = - // FF::template mul_unsigned<3>(FF::template mul_const(t17)); // t23 ← b3 · t17 < 2 - const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 - const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 - const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 - const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 - const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 - const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 - const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 - const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 - const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 + const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 + const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 + const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 + const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 + const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 + const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 + const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 + const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 + const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 return {X3, Y3, Z3}; } diff --git a/icicle/include/icicle/fields/complex_extension.h b/icicle/include/icicle/fields/complex_extension.h index 42740859f..5a451759d 100644 --- a/icicle/include/icicle/fields/complex_extension.h +++ b/icicle/include/icicle/fields/complex_extension.h @@ -158,6 +158,52 @@ class ComplexExtensionField return !(xs == ys); } + template + static HOST_DEVICE_INLINE FF mul_weierstrass_3b_real(const FF& xs) + { + FF r = {}; + if constexpr (Gen::is_b_u32_g2_re) { + r = FF::template mul_unsigned(xs); + if constexpr (Gen::is_b_neg_g2_re) + return FF::neg(r); + else { + return r; + } + } else { + return FF{Gen::weierstrass_3b_g2_re} * xs; + } + } + + template + static HOST_DEVICE_INLINE FF mul_weierstrass_3b_imag(const FF& xs) + { + FF r = {}; + if constexpr (Gen::is_b_u32_g2_im) { + r = FF::template mul_unsigned(xs); + if constexpr (Gen::is_b_neg_g2_im) + return FF::neg(r); + else { + return r; + } + } else { + return FF{Gen::weierstrass_3b_g2_im} * xs; + } + } + + template + static HOST_DEVICE_INLINE ComplexExtensionField mul_weierstrass_3b(const ComplexExtensionField& xs) + { + const FF xs_real = xs.real; + const FF xs_imaginary = xs.imaginary; + FF real_prod = mul_weierstrass_3b_real(xs_real); + FF imaginary_prod = mul_weierstrass_3b_imag(xs_imaginary); + FF re_im = mul_weierstrass_3b_real(xs_imaginary); + FF im_re = mul_weierstrass_3b_imag(xs_real); + FF nonresidue_times_im = FF::template mul_unsigned(imaginary_prod); + nonresidue_times_im = CONFIG::nonresidue_is_negative ? FF::neg(nonresidue_times_im) : nonresidue_times_im; + return ComplexExtensionField{real_prod + nonresidue_times_im, re_im + im_re}; + } + template static HOST_DEVICE_INLINE ComplexExtensionField mul_const(const ComplexExtensionField& xs) { diff --git a/icicle/include/icicle/fields/field.h b/icicle/include/icicle/fields/field.h index 47ebf3c07..4fe5891c2 100644 --- a/icicle/include/icicle/fields/field.h +++ b/icicle/include/icicle/fields/field.h @@ -875,22 +875,6 @@ class Field friend HOST_DEVICE bool operator!=(const Field& xs, const Field& ys) { return !(xs == ys); } -template - static HOST_DEVICE_INLINE Field mul_weierstrass_b(const Field& xs) - { - Field r = {}; - if constexpr (Gen::is_b_u32) { - r = mul_unsigned(xs); - if constexpr (Gen::is_b_neg) - return neg(r); - else { - return r; - } - } else { - return Field{Gen::weierstrass_b} * xs; - } - } - template static HOST_DEVICE_INLINE Field mul_weierstrass_3b(const Field& xs) { From cb5fe00a1d40e3a9fd864e8030cd220cd77825f7 Mon Sep 17 00:00:00 2001 From: hadaringonyama Date: Thu, 2 Jan 2025 17:41:22 +0200 Subject: [PATCH 3/5] fix is_on_curve func --- icicle/include/icicle/curves/projective.h | 2 +- .../include/icicle/fields/complex_extension.h | 46 +++++++++++++++++++ icicle/include/icicle/fields/field.h | 16 +++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/icicle/include/icicle/curves/projective.h b/icicle/include/icicle/curves/projective.h index 0ba8eefc8..7974cbd16 100644 --- a/icicle/include/icicle/curves/projective.h +++ b/icicle/include/icicle/curves/projective.h @@ -231,7 +231,7 @@ class Projective { if (is_zero(point)) return true; bool eq_holds = - (FF::template mul_const(FF::sqr(point.z) * point.z) + FF::sqr(point.x) * point.x == + (FF::template mul_weierstrass_b(FF::sqr(point.z) * point.z) + FF::sqr(point.x) * point.x == point.z * FF::sqr(point.y)); return point.z != FF::zero() && eq_holds; } diff --git a/icicle/include/icicle/fields/complex_extension.h b/icicle/include/icicle/fields/complex_extension.h index 5a451759d..aac882c82 100644 --- a/icicle/include/icicle/fields/complex_extension.h +++ b/icicle/include/icicle/fields/complex_extension.h @@ -158,6 +158,52 @@ class ComplexExtensionField return !(xs == ys); } + template + static HOST_DEVICE_INLINE FF mul_weierstrass_b_real(const FF& xs) + { + FF r = {}; + if constexpr (Gen::is_b_u32_g2_re) { + r = FF::template mul_unsigned(xs); + if constexpr (Gen::is_b_neg_g2_re) + return FF::neg(r); + else { + return r; + } + } else { + return FF{Gen::weierstrass_b_g2_re} * xs; + } + } + + template + static HOST_DEVICE_INLINE FF mul_weierstrass_b_imag(const FF& xs) + { + FF r = {}; + if constexpr (Gen::is_b_u32_g2_im) { + r = FF::template mul_unsigned(xs); + if constexpr (Gen::is_b_neg_g2_im) + return FF::neg(r); + else { + return r; + } + } else { + return FF{Gen::weierstrass_b_g2_im} * xs; + } + } + + template + static HOST_DEVICE_INLINE ComplexExtensionField mul_weierstrass_b(const ComplexExtensionField& xs) + { + const FF xs_real = xs.real; + const FF xs_imaginary = xs.imaginary; + FF real_prod = mul_weierstrass_b_real(xs_real); + FF imaginary_prod = mul_weierstrass_b_imag(xs_imaginary); + FF re_im = mul_weierstrass_b_real(xs_imaginary); + FF im_re = mul_weierstrass_b_imag(xs_real); + FF nonresidue_times_im = FF::template mul_unsigned(imaginary_prod); + nonresidue_times_im = CONFIG::nonresidue_is_negative ? FF::neg(nonresidue_times_im) : nonresidue_times_im; + return ComplexExtensionField{real_prod + nonresidue_times_im, re_im + im_re}; + } + template static HOST_DEVICE_INLINE FF mul_weierstrass_3b_real(const FF& xs) { diff --git a/icicle/include/icicle/fields/field.h b/icicle/include/icicle/fields/field.h index 4fe5891c2..8f787ec46 100644 --- a/icicle/include/icicle/fields/field.h +++ b/icicle/include/icicle/fields/field.h @@ -875,6 +875,22 @@ class Field friend HOST_DEVICE bool operator!=(const Field& xs, const Field& ys) { return !(xs == ys); } + template + static HOST_DEVICE_INLINE Field mul_weierstrass_b(const Field& xs) + { + Field r = {}; + if constexpr (Gen::is_b_u32) { + r = mul_unsigned(xs); + if constexpr (Gen::is_b_neg) + return neg(r); + else { + return r; + } + } else { + return Field{Gen::weierstrass_b} * xs; + } + } + template static HOST_DEVICE_INLINE Field mul_weierstrass_3b(const Field& xs) { From 8e234825049dda5d76e6ca51d2b1de723dc231fa Mon Sep 17 00:00:00 2001 From: hadaringonyama Date: Mon, 6 Jan 2025 16:08:53 +0200 Subject: [PATCH 4/5] 3b computation --- .../include/icicle/curves/params/bls12_377.h | 10 +- .../include/icicle/curves/params/bls12_381.h | 10 +- icicle/include/icicle/curves/params/bn254.h | 7 +- icicle/include/icicle/curves/params/bw6_761.h | 8 - .../include/icicle/curves/params/grumpkin.h | 3 +- icicle/include/icicle/curves/projective.h | 190 +++++++++--------- .../include/icicle/fields/complex_extension.h | 60 +++++- icicle/include/icicle/fields/field.h | 42 ++-- 8 files changed, 171 insertions(+), 159 deletions(-) diff --git a/icicle/include/icicle/curves/params/bls12_377.h b/icicle/include/icicle/curves/params/bls12_377.h index 71089f8f8..6ad767e5f 100644 --- a/icicle/include/icicle/curves/params/bls12_377.h +++ b/icicle/include/icicle/curves/params/bls12_377.h @@ -25,9 +25,7 @@ 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, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = false; }; // G1 @@ -51,12 +49,6 @@ namespace bls12_377 { static constexpr point_field_t weierstrass_b_g2_im = {0x9999999a, 0x1c9ed999, 0x1ccccccd, 0x0dd39e5c, 0x3c6bf800, 0x129207b6, 0xcd5fd889, 0xdc7b4f91, 0x7460c589, 0x43bd0373, 0xdb0fd6f3, 0x010222f6}; - static constexpr point_field_t weierstrass_3b_g2_re = {0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b_g2_im = {0xcccccccd, 0xd0d3cccc, 0x26666666, 0x126f7dd0, - 0xfb3aa000, 0x18c2b4f2, 0x672a760c, 0x7b4f14c2, - 0xf0810762, 0x04fc0499, 0x796a73ef, 0x01582e9e}; static constexpr bool is_b_u32_g2_re = true; static constexpr bool is_b_neg_g2_re = false; diff --git a/icicle/include/icicle/curves/params/bls12_381.h b/icicle/include/icicle/curves/params/bls12_381.h index 116af3c14..18fbf9749 100644 --- a/icicle/include/icicle/curves/params/bls12_381.h +++ b/icicle/include/icicle/curves/params/bls12_381.h @@ -25,9 +25,7 @@ namespace bls12_381 { static constexpr point_field_t weierstrass_b = {0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = false; }; @@ -52,12 +50,6 @@ namespace bls12_381 { static constexpr point_field_t weierstrass_b_g2_im = {0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b_g2_re = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b_g2_im = {0x0000000c, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr bool is_b_u32_g2_re = true; static constexpr bool is_b_neg_g2_re = false; diff --git a/icicle/include/icicle/curves/params/bn254.h b/icicle/include/icicle/curves/params/bn254.h index 9c2b8c5fa..0a628afcb 100644 --- a/icicle/include/icicle/curves/params/bn254.h +++ b/icicle/include/icicle/curves/params/bn254.h @@ -24,8 +24,7 @@ namespace bn254 { 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr point_field_t weierstrass_b = {0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b = {0x00000009, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = false; }; // G1 @@ -43,10 +42,6 @@ namespace bn254 { 0x1be06ac3, 0x81be1899, 0xceb8aaae, 0x2b149d40}; static constexpr point_field_t weierstrass_b_g2_im = {0x85c315d2, 0xe4a2bd06, 0xe52d1852, 0xa74fa084, 0xeed8fdf4, 0xcd2cafad, 0x3af0fed4, 0x009713b0}; - static constexpr point_field_t weierstrass_3b_g2_re = {0xbce9b021, 0x1ef69c66, 0x3cb039cf, 0xf21b7c8d, - 0x509e8f8f, 0x1499be5e, 0xa9c6bfb8, 0x20753adc}; - static constexpr point_field_t weierstrass_3b_g2_im = {0x91494176, 0xade83713, 0xaf8748f8, 0xf5eee18e, - 0xcc8af9dd, 0x67860f09, 0xb0d2fc7e, 0x01c53b10}; static constexpr bool is_b_u32_g2_re = false; static constexpr bool is_b_neg_g2_re = false; diff --git a/icicle/include/icicle/curves/params/bw6_761.h b/icicle/include/icicle/curves/params/bw6_761.h index 12d19b50f..7760c2023 100644 --- a/icicle/include/icicle/curves/params/bw6_761.h +++ b/icicle/include/icicle/curves/params/bw6_761.h @@ -34,10 +34,6 @@ namespace bw6_761 { 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b = { - 0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = true; @@ -56,10 +52,6 @@ namespace bw6_761 { 0x00000004, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr g2_point_field_t weierstrass_3b = { - 0x0000000c, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = false; diff --git a/icicle/include/icicle/curves/params/grumpkin.h b/icicle/include/icicle/curves/params/grumpkin.h index e968589bc..d09390bcc 100644 --- a/icicle/include/icicle/curves/params/grumpkin.h +++ b/icicle/include/icicle/curves/params/grumpkin.h @@ -21,8 +21,7 @@ namespace grumpkin { // 0x8181585d, 0xb85045b6, 0xe131a029, 0x30644e72}; static constexpr point_field_t weierstrass_b = {0x00000011, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}; - static constexpr point_field_t weierstrass_3b = {0x00000033, 0x00000000, 0x00000000, 0x00000000, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; + static constexpr bool is_b_u32 = true; static constexpr bool is_b_neg = true; }; // G1 diff --git a/icicle/include/icicle/curves/projective.h b/icicle/include/icicle/curves/projective.h index 7974cbd16..17cf9329c 100644 --- a/icicle/include/icicle/curves/projective.h +++ b/icicle/include/icicle/curves/projective.h @@ -58,68 +58,68 @@ class Projective const FF Z = point.z; // TODO: Change to efficient dbl once implemented for field.cuh - FF t0 = FF::sqr(Y); // 1. t0 ← Y · Y - FF Z3 = t0 + t0; // 2. Z3 ← t0 + t0 - Z3 = Z3 + Z3; // 3. Z3 ← Z3 + Z3 - Z3 = Z3 + Z3; // 4. Z3 ← Z3 + Z3 - FF t1 = Y * Z; // 5. t1 ← Y · Z - FF t2 = FF::sqr(Z); // 6. t2 ← Z · Z - t2 = FF::template mul_weierstrass_3b(t2); // 7. t2 ← b3 · t2 - FF X3 = t2 * Z3; // 8. X3 ← t2 · Z3 - FF Y3 = t0 + t2; // 9. Y3 ← t0 + t2 - Z3 = t1 * Z3; // 10. Z3 ← t1 · Z3 - t1 = t2 + t2; // 11. t1 ← t2 + t2 - t2 = t1 + t2; // 12. t2 ← t1 + t2 - t0 = t0 - t2; // 13. t0 ← t0 − t2 - Y3 = t0 * Y3; // 14. Y3 ← t0 · Y3 - Y3 = X3 + Y3; // 15. Y3 ← X3 + Y3 - t1 = X * Y; // 16. t1 ← X · Y - X3 = t0 * t1; // 17. X3 ← t0 · t1 - X3 = X3 + X3; // 18. X3 ← X3 + X3 + FF t0 = FF::sqr(Y); // 1. t0 ← Y · Y + FF Z3 = t0 + t0; // 2. Z3 ← t0 + t0 + Z3 = Z3 + Z3; // 3. Z3 ← Z3 + Z3 + Z3 = Z3 + Z3; // 4. Z3 ← Z3 + Z3 + FF t1 = Y * Z; // 5. t1 ← Y · Z + FF t2 = FF::sqr(Z); // 6. t2 ← Z · Z + t2 = FF::template mul_weierstrass_b(t2); // 7. t2 ← b3 · t2 + FF X3 = t2 * Z3; // 8. X3 ← t2 · Z3 + FF Y3 = t0 + t2; // 9. Y3 ← t0 + t2 + Z3 = t1 * Z3; // 10. Z3 ← t1 · Z3 + t1 = t2 + t2; // 11. t1 ← t2 + t2 + t2 = t1 + t2; // 12. t2 ← t1 + t2 + t0 = t0 - t2; // 13. t0 ← t0 − t2 + Y3 = t0 * Y3; // 14. Y3 ← t0 · Y3 + Y3 = X3 + Y3; // 15. Y3 ← X3 + Y3 + t1 = X * Y; // 16. t1 ← X · Y + X3 = t0 * t1; // 17. X3 ← t0 · t1 + X3 = X3 + X3; // 18. X3 ← X3 + X3 return {X3, Y3, Z3}; } friend HOST_DEVICE Projective operator+(Projective p1, const Projective& p2) { - const FF X1 = p1.x; // < 2 - const FF Y1 = p1.y; // < 2 - const FF Z1 = p1.z; // < 2 - const FF X2 = p2.x; // < 2 - const FF Y2 = p2.y; // < 2 - const FF Z2 = p2.z; // < 2 - const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 - const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 - const FF t02 = Z1 * Z2; // t02 ← Z1 · Z2 < 2 - const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 - const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 - const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 - const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 - const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 - const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 - const FF t09 = Y2 + Z2; // t09 ← Y2 + Z2 < 4 - const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 - const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 - const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 - const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 - const FF t14 = X2 + Z2; // t14 ← X2 + Z2 < 4 - const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 - const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 - const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 - const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 - const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 - const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 - const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 - const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 - const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 - const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 - const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 - const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 - const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 - const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 - const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 - const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 + const FF X1 = p1.x; // < 2 + const FF Y1 = p1.y; // < 2 + const FF Z1 = p1.z; // < 2 + const FF X2 = p2.x; // < 2 + const FF Y2 = p2.y; // < 2 + const FF Z2 = p2.z; // < 2 + const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 + const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 + const FF t02 = Z1 * Z2; // t02 ← Z1 · Z2 < 2 + const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 + const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 + const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 + const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 + const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 + const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 + const FF t09 = Y2 + Z2; // t09 ← Y2 + Z2 < 4 + const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 + const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 + const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 + const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 + const FF t14 = X2 + Z2; // t14 ← X2 + Z2 < 4 + const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 + const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 + const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 + const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 + const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 + const FF t20 = FF::template mul_weierstrass_b(t02); // t20 ← b3 · t02 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t23 = FF::template mul_weierstrass_b(t17); // t23 ← b3 · t17 < 2 + const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 + const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 + const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 + const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 + const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 + const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 + const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 + const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 + const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 return {X3, Y3, Z3}; } @@ -127,44 +127,44 @@ class Projective friend HOST_DEVICE Projective operator+(Projective p1, const Affine& p2) { - const FF X1 = p1.x; // < 2 - const FF Y1 = p1.y; // < 2 - const FF Z1 = p1.z; // < 2 - const FF X2 = p2.x; // < 2 - const FF Y2 = p2.y; // < 2 - const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 - const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 - const FF t02 = Z1; // t02 ← Z1 < 2 - const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 - const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 - const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 - const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 - const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 - const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 - const FF t09 = Y2 + FF::one(); // t09 ← Y2 + 1 < 4 - const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 - const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 - const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 - const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 - const FF t14 = X2 + FF::one(); // t14 ← X2 + 1 < 4 - const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 - const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 - const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 - const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 - const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 - const FF t20 = FF::template mul_weierstrass_3b(t02); // t20 ← b3 · t02 < 2 - const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 - const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 - const FF t23 = FF::template mul_weierstrass_3b(t17); // t23 ← b3 · t17 < 2 - const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 - const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 - const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 - const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 - const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 - const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 - const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 - const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 - const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 + const FF X1 = p1.x; // < 2 + const FF Y1 = p1.y; // < 2 + const FF Z1 = p1.z; // < 2 + const FF X2 = p2.x; // < 2 + const FF Y2 = p2.y; // < 2 + const FF t00 = X1 * X2; // t00 ← X1 · X2 < 2 + const FF t01 = Y1 * Y2; // t01 ← Y1 · Y2 < 2 + const FF t02 = Z1; // t02 ← Z1 < 2 + const FF t03 = X1 + Y1; // t03 ← X1 + Y1 < 4 + const FF t04 = X2 + Y2; // t04 ← X2 + Y2 < 4 + const FF t05 = t03 * t04; // t03 ← t03 · t04 < 3 + const FF t06 = t00 + t01; // t06 ← t00 + t01 < 4 + const FF t07 = t05 - t06; // t05 ← t05 − t06 < 2 + const FF t08 = Y1 + Z1; // t08 ← Y1 + Z1 < 4 + const FF t09 = Y2 + FF::one(); // t09 ← Y2 + 1 < 4 + const FF t10 = t08 * t09; // t10 ← t08 · t09 < 3 + const FF t11 = t01 + t02; // t11 ← t01 + t02 < 4 + const FF t12 = t10 - t11; // t12 ← t10 − t11 < 2 + const FF t13 = X1 + Z1; // t13 ← X1 + Z1 < 4 + const FF t14 = X2 + FF::one(); // t14 ← X2 + 1 < 4 + const FF t15 = t13 * t14; // t15 ← t13 · t14 < 3 + const FF t16 = t00 + t02; // t16 ← t00 + t02 < 4 + const FF t17 = t15 - t16; // t17 ← t15 − t16 < 2 + const FF t18 = t00 + t00; // t18 ← t00 + t00 < 2 + const FF t19 = t18 + t00; // t19 ← t18 + t00 < 2 + const FF t20 = FF::template mul_weierstrass_b(t02); // t20 ← b3 · t02 < 2 + const FF t21 = t01 + t20; // t21 ← t01 + t20 < 2 + const FF t22 = t01 - t20; // t22 ← t01 − t20 < 2 + const FF t23 = FF::template mul_weierstrass_b(t17); // t23 ← b3 · t17 < 2 + const auto t24 = FF::mul_wide(t12, t23); // t24 ← t12 · t23 < 2 + const auto t25 = FF::mul_wide(t07, t22); // t25 ← t07 · t22 < 2 + const FF X3 = FF::reduce(t25 - t24); // X3 ← t25 − t24 < 2 + const auto t27 = FF::mul_wide(t23, t19); // t27 ← t23 · t19 < 2 + const auto t28 = FF::mul_wide(t22, t21); // t28 ← t22 · t21 < 2 + const FF Y3 = FF::reduce(t28 + t27); // Y3 ← t28 + t27 < 2 + const auto t30 = FF::mul_wide(t19, t07); // t30 ← t19 · t07 < 2 + const auto t31 = FF::mul_wide(t21, t12); // t31 ← t21 · t12 < 2 + const FF Z3 = FF::reduce(t31 + t30); // Z3 ← t31 + t30 < 2 return {X3, Y3, Z3}; } diff --git a/icicle/include/icicle/fields/complex_extension.h b/icicle/include/icicle/fields/complex_extension.h index aac882c82..6879048df 100644 --- a/icicle/include/icicle/fields/complex_extension.h +++ b/icicle/include/icicle/fields/complex_extension.h @@ -158,47 +158,85 @@ class ComplexExtensionField return !(xs == ys); } - template + template static HOST_DEVICE_INLINE FF mul_weierstrass_b_real(const FF& xs) { FF r = {}; + constexpr FF b_mult = []() { + FF b_mult = FF{Gen::weierstrass_b_g2_re}; + if constexpr (!IS_3B) return b_mult; + typename FF::ff_storage temp = {}; + typename FF::ff_storage modulus = FF::get_modulus(); + host_math::template add_sub_limbs( + b_mult.limbs_storage, b_mult.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + host_math::template add_sub_limbs( + b_mult.limbs_storage, FF{Gen::weierstrass_b_g2_re}.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + return b_mult; + }(); if constexpr (Gen::is_b_u32_g2_re) { - r = FF::template mul_unsigned(xs); + r = FF::template mul_unsigned(xs); if constexpr (Gen::is_b_neg_g2_re) return FF::neg(r); else { return r; } } else { - return FF{Gen::weierstrass_b_g2_re} * xs; + return b_mult * xs; } } - template + template static HOST_DEVICE_INLINE FF mul_weierstrass_b_imag(const FF& xs) { FF r = {}; + constexpr FF b_mult = []() { + FF b_mult = FF{Gen::weierstrass_b_g2_im}; + if constexpr (!IS_3B) return b_mult; + typename FF::ff_storage temp = {}; + typename FF::ff_storage modulus = FF::get_modulus(); + host_math::template add_sub_limbs( + b_mult.limbs_storage, b_mult.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + host_math::template add_sub_limbs( + b_mult.limbs_storage, FF{Gen::weierstrass_b_g2_im}.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + return b_mult; + }(); if constexpr (Gen::is_b_u32_g2_im) { - r = FF::template mul_unsigned(xs); + r = FF::template mul_unsigned(xs); if constexpr (Gen::is_b_neg_g2_im) return FF::neg(r); else { return r; } } else { - return FF{Gen::weierstrass_b_g2_im} * xs; + return b_mult * xs; } } - template + template static HOST_DEVICE_INLINE ComplexExtensionField mul_weierstrass_b(const ComplexExtensionField& xs) { const FF xs_real = xs.real; const FF xs_imaginary = xs.imaginary; - FF real_prod = mul_weierstrass_b_real(xs_real); - FF imaginary_prod = mul_weierstrass_b_imag(xs_imaginary); - FF re_im = mul_weierstrass_b_real(xs_imaginary); - FF im_re = mul_weierstrass_b_imag(xs_real); + FF real_prod = mul_weierstrass_b_real(xs_real); + FF imaginary_prod = mul_weierstrass_b_imag(xs_imaginary); + FF re_im = mul_weierstrass_b_real(xs_imaginary); + FF im_re = mul_weierstrass_b_imag(xs_real); FF nonresidue_times_im = FF::template mul_unsigned(imaginary_prod); nonresidue_times_im = CONFIG::nonresidue_is_negative ? FF::neg(nonresidue_times_im) : nonresidue_times_im; return ComplexExtensionField{real_prod + nonresidue_times_im, re_im + im_re}; diff --git a/icicle/include/icicle/fields/field.h b/icicle/include/icicle/fields/field.h index 8f787ec46..3563424c0 100644 --- a/icicle/include/icicle/fields/field.h +++ b/icicle/include/icicle/fields/field.h @@ -875,35 +875,39 @@ class Field friend HOST_DEVICE bool operator!=(const Field& xs, const Field& ys) { return !(xs == ys); } - template + template static HOST_DEVICE_INLINE Field mul_weierstrass_b(const Field& xs) { Field r = {}; - if constexpr (Gen::is_b_u32) { - r = mul_unsigned(xs); - if constexpr (Gen::is_b_neg) - return neg(r); - else { - return r; - } - } else { - return Field{Gen::weierstrass_b} * xs; - } - } + constexpr Field b_mult = []() { + Field b_mult = Field{Gen::weierstrass_b}; + if constexpr (!IS_3B) return b_mult; + ff_storage temp = {}; + ff_storage modulus = get_modulus<>(); + host_math::template add_sub_limbs( + b_mult.limbs_storage, b_mult.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + host_math::template add_sub_limbs( + b_mult.limbs_storage, Field{Gen::weierstrass_b}.limbs_storage, b_mult.limbs_storage); + b_mult.limbs_storage = + host_math::template add_sub_limbs(b_mult.limbs_storage, modulus, temp) + ? b_mult.limbs_storage + : temp; + return b_mult; + }(); - template - static HOST_DEVICE_INLINE Field mul_weierstrass_3b(const Field& xs) - { - Field r = {}; - if constexpr (Gen::is_b_u32) { - r = mul_unsigned(xs); + if constexpr (Gen::is_b_u32) { // assumes that 3b is also u32 + r = mul_unsigned(xs); if constexpr (Gen::is_b_neg) return neg(r); else { return r; } } else { - return Field{Gen::weierstrass_3b} * xs; + return b_mult * xs; } } From 46731439aee271211828db7710834792e3e8b542 Mon Sep 17 00:00:00 2001 From: hadaringonyama Date: Mon, 6 Jan 2025 16:13:36 +0200 Subject: [PATCH 5/5] small fix --- .../include/icicle/fields/complex_extension.h | 46 ------------------- 1 file changed, 46 deletions(-) diff --git a/icicle/include/icicle/fields/complex_extension.h b/icicle/include/icicle/fields/complex_extension.h index 6879048df..c65ad37ec 100644 --- a/icicle/include/icicle/fields/complex_extension.h +++ b/icicle/include/icicle/fields/complex_extension.h @@ -242,52 +242,6 @@ class ComplexExtensionField return ComplexExtensionField{real_prod + nonresidue_times_im, re_im + im_re}; } - template - static HOST_DEVICE_INLINE FF mul_weierstrass_3b_real(const FF& xs) - { - FF r = {}; - if constexpr (Gen::is_b_u32_g2_re) { - r = FF::template mul_unsigned(xs); - if constexpr (Gen::is_b_neg_g2_re) - return FF::neg(r); - else { - return r; - } - } else { - return FF{Gen::weierstrass_3b_g2_re} * xs; - } - } - - template - static HOST_DEVICE_INLINE FF mul_weierstrass_3b_imag(const FF& xs) - { - FF r = {}; - if constexpr (Gen::is_b_u32_g2_im) { - r = FF::template mul_unsigned(xs); - if constexpr (Gen::is_b_neg_g2_im) - return FF::neg(r); - else { - return r; - } - } else { - return FF{Gen::weierstrass_3b_g2_im} * xs; - } - } - - template - static HOST_DEVICE_INLINE ComplexExtensionField mul_weierstrass_3b(const ComplexExtensionField& xs) - { - const FF xs_real = xs.real; - const FF xs_imaginary = xs.imaginary; - FF real_prod = mul_weierstrass_3b_real(xs_real); - FF imaginary_prod = mul_weierstrass_3b_imag(xs_imaginary); - FF re_im = mul_weierstrass_3b_real(xs_imaginary); - FF im_re = mul_weierstrass_3b_imag(xs_real); - FF nonresidue_times_im = FF::template mul_unsigned(imaginary_prod); - nonresidue_times_im = CONFIG::nonresidue_is_negative ? FF::neg(nonresidue_times_im) : nonresidue_times_im; - return ComplexExtensionField{real_prod + nonresidue_times_im, re_im + im_re}; - } - template static HOST_DEVICE_INLINE ComplexExtensionField mul_const(const ComplexExtensionField& xs) {