mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-27 13:23:29 +01:00
bulletproofs: speed up vector_power_sum
found by sarang
This commit is contained in:
parent
4e72384318
commit
1bc5f9fa4b
@ -202,13 +202,27 @@ static rct::keyV vector_powers(const rct::key &x, size_t n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Given a scalar, return the sum of its powers from 0 to n-1 */
|
/* Given a scalar, return the sum of its powers from 0 to n-1 */
|
||||||
static rct::key vector_power_sum(const rct::key &x, size_t n)
|
static rct::key vector_power_sum(rct::key x, size_t n)
|
||||||
{
|
{
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return rct::zero();
|
return rct::zero();
|
||||||
rct::key res = rct::identity();
|
rct::key res = rct::identity();
|
||||||
if (n == 1)
|
if (n == 1)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
|
const bool is_power_of_2 = (n & (n - 1)) == 0;
|
||||||
|
if (is_power_of_2)
|
||||||
|
{
|
||||||
|
sc_add(res.bytes, res.bytes, x.bytes);
|
||||||
|
while (n > 2)
|
||||||
|
{
|
||||||
|
sc_mul(x.bytes, x.bytes, x.bytes);
|
||||||
|
sc_muladd(res.bytes, x.bytes, res.bytes, res.bytes);
|
||||||
|
n /= 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
rct::key prev = x;
|
rct::key prev = x;
|
||||||
for (size_t i = 1; i < n; ++i)
|
for (size_t i = 1; i < n; ++i)
|
||||||
{
|
{
|
||||||
@ -216,6 +230,8 @@ static rct::key vector_power_sum(const rct::key &x, size_t n)
|
|||||||
sc_mul(prev.bytes, prev.bytes, x.bytes);
|
sc_mul(prev.bytes, prev.bytes, x.bytes);
|
||||||
sc_add(res.bytes, res.bytes, prev.bytes);
|
sc_add(res.bytes, res.bytes, prev.bytes);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user