Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 53 additions & 20 deletions src/crypto/crypto-ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,33 @@ void fe_invert(fe out, const fe z) {
return;
}

// Montgomery's trick
// https://iacr.org/archive/pkc2004/29470042/29470042.pdf 2.2
int fe_batch_invert(fe *out, const fe *in, const int n) {
if (n == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the function be labeled vartime due to this branch?

Copy link
Contributor

Choose a reason for hiding this comment

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

Time variable to the width, which generally isn't a secret, shouldn't be an issue. You'd have to argue that the amount of inputs/amounts is a secret and should be constant-time to them accordingly (or whatever defines the width here).

return 0;
}

// Step 1: collect initial muls
fe_copy(out[0], in[0]);
for (int i = 1; i < n; ++i) {
fe_mul(out[i], out[i-1], in[i]);
}

// Step 2: get the inverse of all elems multiplied together
fe a;
fe_invert(a, out[n-1]);

// Step 3: get each inverse
for (int i = n; i > 1; --i) {
fe_mul(out[i-1], a, out[i-2]);
fe_mul(a, a, in[i-1]);
}
fe_copy(out[0], a);

return 0;
}

/* From fe_isnegative.c */

/*
Expand Down Expand Up @@ -1328,16 +1355,9 @@ void ge_double_scalarmult_base_vartime_p3(ge_p3 *r3, const unsigned char *a, con
}
}

/* From ge_frombytes.c, modified */

int ge_frombytes_vartime(ge_p3 *h, const unsigned char *s) {
fe u;
fe v;
fe vxx;
fe check;

/* From fe_frombytes.c */
/* From fe_frombytes.c */

int fe_frombytes_vartime(fe y, const unsigned char *s) {
int64_t h0 = load_4(s);
int64_t h1 = load_3(s + 4) << 6;
int64_t h2 = load_3(s + 7) << 5;
Expand Down Expand Up @@ -1378,18 +1398,31 @@ int ge_frombytes_vartime(ge_p3 *h, const unsigned char *s) {
carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26;
carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26;

h->Y[0] = h0;
h->Y[1] = h1;
h->Y[2] = h2;
h->Y[3] = h3;
h->Y[4] = h4;
h->Y[5] = h5;
h->Y[6] = h6;
h->Y[7] = h7;
h->Y[8] = h8;
h->Y[9] = h9;
y[0] = h0;
y[1] = h1;
y[2] = h2;
y[3] = h3;
y[4] = h4;
y[5] = h5;
y[6] = h6;
y[7] = h7;
y[8] = h8;
y[9] = h9;

/* End fe_frombytes.c */
return 0;
}

/* From ge_frombytes.c, modified */

int ge_frombytes_vartime(ge_p3 *h, const unsigned char *s) {
fe u;
fe v;
fe vxx;
fe check;

if (fe_frombytes_vartime(h->Y, s) != 0) {
return -1;
}

fe_1(h->Z);
fe_sq(u, h->Y);
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void ge_double_scalarmult_base_vartime_p3(ge_p3 *, const unsigned char *, const

extern const fe fe_sqrtm1;
extern const fe fe_d;
int fe_frombytes_vartime(fe, const unsigned char *);
int ge_frombytes_vartime(ge_p3 *, const unsigned char *);

/* From ge_p1p1_to_p2.c */
Expand Down Expand Up @@ -163,6 +164,7 @@ void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
void fe_add(fe h, const fe f, const fe g);
void fe_tobytes(unsigned char *, const fe);
void fe_invert(fe out, const fe z);
int fe_batch_invert(fe *out, const fe *in, const int n);
void fe_mul(fe out, const fe, const fe);
void fe_0(fe h);

Expand Down
1 change: 1 addition & 0 deletions tests/performance_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(performance_tests_headers
construct_tx.h
derive_public_key.h
derive_secret_key.h
fe_batch_invert.h
ge_frombytes_vartime.h
generate_key_derivation.h
generate_key_image.h
Expand Down
79 changes: 79 additions & 0 deletions tests/performance_tests/fe_batch_invert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2025, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#pragma once

#include <memory>

#include "crypto/crypto.h"

template<bool batched>
class test_fe_batch_invert
{
public:
static const size_t loop_count = 50;
static const size_t n_elems = 1000;

bool init()
{
m_fes = std::make_unique<fe[]>(n_elems);

for (std::size_t i = 0; i < n_elems; ++i)
{
crypto::secret_key r;
crypto::random32_unbiased((unsigned char*)r.data);

ge_p3 point;
ge_scalarmult_base(&point, (unsigned char*)r.data);

memcpy(m_fes[i], &point.Y, sizeof(fe));
}

return true;
}

bool test()
{
std::unique_ptr<fe[]> inv_fes = std::make_unique<fe[]>(n_elems);

if constexpr (batched)
fe_batch_invert(inv_fes.get(), m_fes.get(), n_elems);
else
{
for (std::size_t i = 0; i < n_elems; ++i)
fe_invert(inv_fes[i], m_fes[i]);
}

return true;
}

private:
std::unique_ptr<fe[]> m_fes;
};
3 changes: 3 additions & 0 deletions tests/performance_tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "derive_public_key.h"
#include "derive_secret_key.h"
#include "derive_view_tag.h"
#include "fe_batch_invert.h"
#include "ge_frombytes_vartime.h"
#include "ge_tobytes.h"
#include "generate_key_derivation.h"
Expand Down Expand Up @@ -198,6 +199,8 @@ int main(int argc, char** argv)
TEST_PERFORMANCE0(filter, p, test_generate_key_image);
TEST_PERFORMANCE0(filter, p, test_derive_public_key);
TEST_PERFORMANCE0(filter, p, test_derive_secret_key);
TEST_PERFORMANCE1(filter, p, test_fe_batch_invert, true); // batched
TEST_PERFORMANCE1(filter, p, test_fe_batch_invert, false); // individual inversions
TEST_PERFORMANCE0(filter, p, test_ge_frombytes_vartime);
TEST_PERFORMANCE0(filter, p, test_ge_tobytes);
TEST_PERFORMANCE0(filter, p, test_generate_keypair);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit_tests/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,30 @@ TEST(Crypto, generator_consistency)
// ringct/rctTypes.h
ASSERT_TRUE(memcmp(H.data, rct::H.bytes, 32) == 0);
}

TEST(Crypto, batch_inversion)
{
const std::size_t MAX_TEST_ELEMS = 1000;

std::unique_ptr<fe[]> init_elems = std::make_unique<fe[]>(MAX_TEST_ELEMS);
std::unique_ptr<fe[]> norm_inverted = std::make_unique<fe[]>(MAX_TEST_ELEMS);

// Init test elems and individual inversions
for (std::size_t i = 0; i < MAX_TEST_ELEMS; ++i)
{
const cryptonote::keypair kp = cryptonote::keypair::generate(hw::get_device("default"));
ASSERT_EQ(fe_frombytes_vartime(init_elems[i], (unsigned char*)kp.pub.data), 0);
fe_invert(norm_inverted[i], init_elems[i]);
}

// Do batch inversions and compare to individual inversions
for (std::size_t n_elems = 1; n_elems <= MAX_TEST_ELEMS; ++n_elems)
{
std::unique_ptr<fe[]> batch_inverted = std::make_unique<fe[]>(n_elems);
ASSERT_EQ(fe_batch_invert(batch_inverted.get(), init_elems.get(), n_elems), 0);
// Warning: it's possible that internal fe representations are inconsistent in some future update, since the lib
// does not guarantee equivalent representations. We may want to "reduce" fe's in this test if this fails.
// See: https://github.com/seraphis-migration/monero/blob/74a254f8c215986042c40e6875a0f97bd6169a1e/src/crypto/crypto-ops.c#L4047-L4069
ASSERT_EQ(memcmp(batch_inverted.get(), norm_inverted.get(), n_elems * sizeof(fe)), 0);
}
}
Loading