@@ -87,43 +87,43 @@ class ModArith
8787 // EdMSM: Multi-Scalar-Multiplication for SNARKs and Faster Montgomery multiplication
8888 // https://eprint.iacr.org/2022/1400.pdf
8989
90- constexpr uint64_t most_significant_mod_word_limit {std::numeric_limits<uint64_t >::max () >> 1 };
90+ constexpr uint64_t most_significant_mod_word_limit{
91+ std::numeric_limits<uint64_t >::max () >> 1 };
9192 constexpr auto S = UintT::num_words; // TODO(C++23): Make it static
9293
9394 intx::uint<UintT::num_bits + 64 > t;
94- if (mod[S - 1 ] < most_significant_mod_word_limit )
95+ for ( size_t i = 0 ; i != S; ++i )
9596 {
96- for (size_t i = 0 ; i != S; ++i)
97+ uint64_t c = 0 ;
98+ for (size_t j = 0 ; j != S; ++j)
99+ std::tie (c, t[j]) = addmul (t[j], x[j], y[i], c);
100+
101+ uint64_t carry = 0 ;
102+ if (mod[S - 1 ] < most_significant_mod_word_limit)
97103 {
98- uint64_t c = 0 ;
99- for (size_t j = 0 ; j != S; ++j)
100- std::tie (c, t[j]) = addmul (t[j], x[j], y[i], c);
101- auto const c_2 = c;
102- const auto m = t[0 ] * m_mod_inv;
103- std::tie (c, std::ignore) = addmul (t[0 ], m, mod[0 ], 0 );
104- for (size_t j = 1 ; j != S; ++j)
105- std::tie (c, t[j - 1 ]) = addmul (t[j], m, mod[j], c);
106- t[S - 1 ] = c_2 + c;
104+ carry = c;
107105 }
108- }
109- else
110- {
111- for (size_t i = 0 ; i != S; ++i)
106+ else
112107 {
113- uint64_t c = 0 ;
114- for (size_t j = 0 ; j != S; ++j)
115- std::tie (c, t[j]) = addmul (t[j], x[j], y[i], c);
116108 auto tmp = intx::addc (t[S], c);
117109 t[S] = tmp.value ;
118- const auto d = tmp.carry ; // TODO: Carry is 0 for sparse modulus.
110+ carry = tmp.carry ; // Carry from addc.
111+ }
112+
113+ const auto m = t[0 ] * m_mod_inv;
114+ std::tie (c, std::ignore) = addmul (t[0 ], m, mod[0 ], 0 );
115+ for (size_t j = 1 ; j != S; ++j)
116+ std::tie (c, t[j - 1 ]) = addmul (t[j], m, mod[j], c);
119117
120- const auto m = t[0 ] * m_mod_inv;
121- std::tie (c, std::ignore) = addmul (t[0 ], m, mod[0 ], 0 );
122- for (size_t j = 1 ; j != S; ++j)
123- std::tie (c, t[j - 1 ]) = addmul (t[j], m, mod[j], c);
124- tmp = intx::addc (t[S], c);
118+ if (mod[S - 1 ] < most_significant_mod_word_limit)
119+ {
120+ t[S - 1 ] = carry + c;
121+ }
122+ else
123+ {
124+ auto tmp = intx::addc (t[S], c);
125125 t[S - 1 ] = tmp.value ;
126- t[S] = d + tmp.carry ; // TODO: Carry is 0 for sparse modulus.
126+ t[S] = carry + tmp.carry ;
127127 }
128128 }
129129
0 commit comments