Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class Bn254FqParams {

static constexpr uint64_t r_inv = 0x87d20782e4866389UL;

static constexpr uint64_t r_inv_0 = 0x327d7c1b18f7bd41UL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm sure you wanted to maintain the absolutely pristine absence of comments in this file but would you mind adding some anyway :). E.g. a note about the significance of r. Also, it seems like many of these constants were computed using the python script included in this PR? If so can you make a note of that in the comments?

static constexpr uint64_t r_inv_1 = 0xdb8ed52f824ed32fUL;
static constexpr uint64_t r_inv_2 = 0x29b67b05eb29a6a1UL;
static constexpr uint64_t r_inv_3 = 0x19ac99126b459ddaUL;

static constexpr uint64_t r_inv_wasm_0 = 0x17789a9f;
static constexpr uint64_t r_inv_wasm_1 = 0x5ffc3dc;
static constexpr uint64_t r_inv_wasm_2 = 0xd6bde42;
static constexpr uint64_t r_inv_wasm_3 = 0x1cf152e3;
static constexpr uint64_t r_inv_wasm_4 = 0x18eb055f;
static constexpr uint64_t r_inv_wasm_5 = 0xed815e2;
static constexpr uint64_t r_inv_wasm_6 = 0x16626d2;
static constexpr uint64_t r_inv_wasm_7 = 0xb8bab0f;
static constexpr uint64_t r_inv_wasm_8 = 0x6d7c4;

static constexpr uint64_t coset_generators_0[8]{
0x7a17caa950ad28d7ULL, 0x4d750e37163c3674ULL, 0x20d251c4dbcb4411ULL, 0xf42f9552a15a51aeULL,
0x4f4bc0b2b5ef64bdULL, 0x22a904407b7e725aULL, 0xf60647ce410d7ff7ULL, 0xc9638b5c069c8d94ULL,
Expand Down
14 changes: 14 additions & 0 deletions barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ class Bn254FrParams {

static constexpr uint64_t r_inv = 0xc2e1f593efffffffUL;

static constexpr uint64_t r_inv_0 = 0x2d3e8053e396ee4dUL;
static constexpr uint64_t r_inv_1 = 0xca478dbeab3c92cdUL;
static constexpr uint64_t r_inv_2 = 0xb2d8f06f77f52a93UL;
static constexpr uint64_t r_inv_3 = 0x24d6ba07f7aa8f04UL;
static constexpr uint64_t r_inv_wasm_0 = 0x18f05361;
static constexpr uint64_t r_inv_wasm_1 = 0x12bb1fe;
static constexpr uint64_t r_inv_wasm_2 = 0xf5d8135;
static constexpr uint64_t r_inv_wasm_3 = 0x1e6275f6;
static constexpr uint64_t r_inv_wasm_4 = 0x7e7a880;
static constexpr uint64_t r_inv_wasm_5 = 0x10c6bf1f;
static constexpr uint64_t r_inv_wasm_6 = 0x11f74a6c;
static constexpr uint64_t r_inv_wasm_7 = 0x6fdaecb;
static constexpr uint64_t r_inv_wasm_8 = 0x183227;

static constexpr uint64_t coset_generators_0[8]{
0x5eef048d8fffffe7ULL, 0xb8538a9dfffffe2ULL, 0x3057819e4fffffdbULL, 0xdcedb5ba9fffffd6ULL,
0x8983e9d6efffffd1ULL, 0x361a1df33fffffccULL, 0xe2b0520f8fffffc7ULL, 0x8f46862bdfffffc2ULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ struct FqParams {

static constexpr uint64_t r_inv = 15580212934572586289ULL;

static constexpr uint64_t r_inv_0 = 0xffffffff27c7f3a9UL;
static constexpr uint64_t r_inv_1 = 0xffffffffffffffffUL;
static constexpr uint64_t r_inv_2 = 0xffffffffffffffffUL;
static constexpr uint64_t r_inv_3 = 0xd838091dd2253530UL;
static constexpr uint64_t r_inv_wasm_0 = 0xed6544e;
static constexpr uint64_t r_inv_wasm_1 = 0x1ffffffb;
static constexpr uint64_t r_inv_wasm_2 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_3 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_4 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_5 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_6 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_7 = 0x10ffffff;
static constexpr uint64_t r_inv_wasm_8 = 0x9129a9;

static constexpr uint64_t cube_root_0 = 0x58a4361c8e81894eULL;
static constexpr uint64_t cube_root_1 = 0x03fde1631c4b80afULL;
static constexpr uint64_t cube_root_2 = 0xf8e98978d02e3905ULL;
Expand Down Expand Up @@ -101,6 +115,21 @@ struct FrParams {

static constexpr uint64_t r_inv = 5408259542528602431ULL;

static constexpr uint64_t r_inv_0 = 0x9d4ad302583de6dcUL;
static constexpr uint64_t r_inv_1 = 0xa09f710af0155525UL;
static constexpr uint64_t r_inv_2 = 0xffffffffffffffffUL;
static constexpr uint64_t r_inv_3 = 0x4b0dff665588b13eUL;

static constexpr uint64_t r_inv_wasm_0 = 0x3d864e;
static constexpr uint64_t r_inv_wasm_1 = 0x8b9f61c;
static constexpr uint64_t r_inv_wasm_2 = 0x3df60c0;
static constexpr uint64_t r_inv_wasm_3 = 0xa3c71eb;
static constexpr uint64_t r_inv_wasm_4 = 0x1ffff251;
static constexpr uint64_t r_inv_wasm_5 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_6 = 0x1fffffff;
static constexpr uint64_t r_inv_wasm_7 = 0x1effffff;
static constexpr uint64_t r_inv_wasm_8 = 0xac4589;

static constexpr uint64_t coset_generators_0[8]{
0x40e4273feef0b9bbULL, 0x8111c8b31eba787aULL, 0xc13f6a264e843739ULL, 0x16d0b997e4df5f8ULL,
0x419aad0cae17b4b7ULL, 0x81c84e7fdde17376ULL, 0xc1f5eff30dab3235ULL, 0x22391663d74f0f4ULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ struct FqParams {

static constexpr uint64_t r_inv = 1;

static constexpr uint64_t r_inv_0 = 0x100000000UL;
static constexpr uint64_t r_inv_1 = 0x0UL;
static constexpr uint64_t r_inv_2 = 0xffffffff00000001UL;
static constexpr uint64_t r_inv_3 = 0x0UL;

static constexpr uint64_t r_inv_wasm_0 = 0x0;
static constexpr uint64_t r_inv_wasm_1 = 0x0;
static constexpr uint64_t r_inv_wasm_2 = 0x200;
static constexpr uint64_t r_inv_wasm_3 = 0x0;
static constexpr uint64_t r_inv_wasm_4 = 0x0;
static constexpr uint64_t r_inv_wasm_5 = 0x40000;
static constexpr uint64_t r_inv_wasm_6 = 0x1fe00000;
static constexpr uint64_t r_inv_wasm_7 = 0xffffff;
static constexpr uint64_t r_inv_wasm_8 = 0x0;

static constexpr uint64_t coset_generators_0[8]{
0x3ULL, 0x4ULL, 0x5ULL, 0x6ULL, 0x7ULL, 0x8ULL, 0x9ULL, 0xaULL,
};
Expand Down Expand Up @@ -100,6 +115,20 @@ struct FrParams {

static constexpr uint64_t r_inv = 14758798090332847183ULL;

static constexpr uint64_t r_inv_0 = 0x230102a06d6251dcUL;
static constexpr uint64_t r_inv_1 = 0xca5113bcafc4ea28UL;
static constexpr uint64_t r_inv_2 = 0xded10c5bee00bc4eUL;
static constexpr uint64_t r_inv_3 = 0xccd1c8aa212ef3a4UL;
static constexpr uint64_t r_inv_wasm_0 = 0x8517c79;
static constexpr uint64_t r_inv_wasm_1 = 0x1edc694;
static constexpr uint64_t r_inv_wasm_2 = 0x459ee5c;
static constexpr uint64_t r_inv_wasm_3 = 0x705a6a8;
static constexpr uint64_t r_inv_wasm_4 = 0x1ffffe2a;
static constexpr uint64_t r_inv_wasm_5 = 0x113bffff;
static constexpr uint64_t r_inv_wasm_6 = 0x1621c017;
static constexpr uint64_t r_inv_wasm_7 = 0xef1ff43;
static constexpr uint64_t r_inv_wasm_8 = 0x7005e2;

static constexpr uint64_t coset_generators_0[8]{
0x55eb74ab1949fac9ULL, 0x6231a9e81ce6d578ULL, 0x6e77df252083b027ULL, 0x7abe146224208ad6ULL,
0x8704499f27bd6585ULL, 0x934a7edc2b5a4034ULL, 0x9f90b4192ef71ae3ULL, 0xabd6e9563293f592ULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ template <class Params_> struct alignas(32) field {
Params::modulus_wasm_4, Params::modulus_wasm_5,
Params::modulus_wasm_6, Params::modulus_wasm_7,
Params::modulus_wasm_8 };
static constexpr std::array<uint64_t, 9> wasm_r_inv = {
Params::r_inv_wasm_0, Params::r_inv_wasm_1, Params::r_inv_wasm_2, Params::r_inv_wasm_3, Params::r_inv_wasm_4,
Params::r_inv_wasm_5, Params::r_inv_wasm_6, Params::r_inv_wasm_7, Params::r_inv_wasm_8
};

#endif
static constexpr field cube_root_of_unity()
Expand Down Expand Up @@ -617,6 +621,16 @@ template <class Params_> struct alignas(32) field {
uint64_t& result_6,
uint64_t& result_7,
uint64_t& result_8);
BB_INLINE static constexpr void wasm_reduce_yuval(uint64_t& result_0,
uint64_t& result_1,
uint64_t& result_2,
uint64_t& result_3,
uint64_t& result_4,
uint64_t& result_5,
uint64_t& result_6,
uint64_t& result_7,
uint64_t& result_8,
uint64_t& result_9);
BB_INLINE static constexpr std::array<uint64_t, WASM_NUM_LIMBS> wasm_convert(const uint64_t* data);
#endif
BB_INLINE static constexpr std::pair<uint64_t, uint64_t> mul_wide(uint64_t a, uint64_t b) noexcept;
Expand Down
82 changes: 66 additions & 16 deletions barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,36 @@ constexpr void field<T>::wasm_reduce(uint64_t& result_0,
result_7 += k * wasm_modulus[7];
result_8 += k * wasm_modulus[8];
}

/**
* @brief Perform 29-bit montgomery reduction on 1 limb using Yuval's method *
* @details https://hackmd.io/@Ingonyama/Barret-Montgomery

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

quite a nice document!

*
*/
template <class T>
constexpr void field<T>::wasm_reduce_yuval(uint64_t& result_0,
uint64_t& result_1,
uint64_t& result_2,
uint64_t& result_3,
uint64_t& result_4,
uint64_t& result_5,
uint64_t& result_6,
uint64_t& result_7,
uint64_t& result_8,
uint64_t& result_9)
{
constexpr uint64_t mask = 0x1fffffff;
const uint64_t result_0_masked = result_0 & mask;
result_1 += result_0_masked * wasm_r_inv[0] + (result_0 >> WASM_LIMB_BITS);
result_2 += result_0_masked * wasm_r_inv[1];
result_3 += result_0_masked * wasm_r_inv[2];
result_4 += result_0_masked * wasm_r_inv[3];
result_5 += result_0_masked * wasm_r_inv[4];
result_6 += result_0_masked * wasm_r_inv[5];
result_7 += result_0_masked * wasm_r_inv[6];
result_8 += result_0_masked * wasm_r_inv[7];
result_9 += result_0_masked * wasm_r_inv[8];
}
/**
* @brief Convert 4 64-bit limbs into 9 29-bit limbs
*
Expand Down Expand Up @@ -617,14 +647,24 @@ template <class T> constexpr field<T> field<T>::montgomery_mul(const field& othe
wasm_madd(left[6], right, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
wasm_madd(left[7], right, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);
wasm_madd(left[8], right, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15, temp_16);
wasm_reduce(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8);
wasm_reduce(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
wasm_reduce(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
wasm_reduce(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
wasm_reduce(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
wasm_reduce(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
wasm_reduce(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
wasm_reduce(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);

wasm_reduce_yuval(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
wasm_reduce_yuval(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
wasm_reduce_yuval(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
wasm_reduce_yuval(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
wasm_reduce_yuval(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
wasm_reduce_yuval(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
wasm_reduce_yuval(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);
wasm_reduce_yuval(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15, temp_16);

// wasm_reduce(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Left these in in case there is some unforseen issue that is not picked up by CI. Will allow us to quickly switch back

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add some comments that clarify the presence of the commented out code and what exactly would be needed to turn off the new thing and reenable the old thing if that were to be needed? maybe wrapping some of the logic in methods would be helpful. I'm assuming leaving the final calls to wasm_reduce uncommented was intentional but some explicit indication of why they're needed in comments would be helpful.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

// wasm_reduce(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
// wasm_reduce(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
// wasm_reduce(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
// wasm_reduce(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
// wasm_reduce(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
// wasm_reduce(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
// wasm_reduce(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);
wasm_reduce(temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15, temp_16);

// Convert result to unrelaxed form (all limbs are 29 bits)
Expand Down Expand Up @@ -804,14 +844,24 @@ template <class T> constexpr field<T> field<T>::montgomery_square() const noexce
temp_16 += left[8] * left[8];

// Perform reductions
wasm_reduce(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8);
wasm_reduce(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
wasm_reduce(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
wasm_reduce(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
wasm_reduce(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
wasm_reduce(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
wasm_reduce(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
wasm_reduce(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);

wasm_reduce_yuval(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
wasm_reduce_yuval(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
wasm_reduce_yuval(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
wasm_reduce_yuval(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
wasm_reduce_yuval(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
wasm_reduce_yuval(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
wasm_reduce_yuval(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);
wasm_reduce_yuval(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15, temp_16);

// wasm_reduce(temp_0, temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8);
// wasm_reduce(temp_1, temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9);
// wasm_reduce(temp_2, temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10);
// wasm_reduce(temp_3, temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11);
// wasm_reduce(temp_4, temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12);
// wasm_reduce(temp_5, temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13);
// wasm_reduce(temp_6, temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14);
// wasm_reduce(temp_7, temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15);
wasm_reduce(temp_8, temp_9, temp_10, temp_11, temp_12, temp_13, temp_14, temp_15, temp_16);

// Convert to unrelaxed 29-bit form
Expand Down
Loading