Skip to content

Commit 564be61

Browse files
committed
FCMP++: output_to_tuple {output pubkey, commitment} -> {O,I,C}
- Function to convert an {output pubkey, commitment} to an output tuple {O,I,C} in prepartion to insert the output tuple into the curve tree. - O = torsion cleared valid output pubkey checked for identity. - I = key image generator. - C = torsion cleared valid Commitment checked for identity. - None of {O,I,C} should have torsion nor == identity. - Introduces the OutputPair variant, which can either be Legacy or Carrot V1 types. Legacy outputs are not checked for torsion at consensus, and use the legacy biased hash to point fn to derive the key image generator (I). Carrot V1 outputs **are** checked for torsion at consensus, and use the unbiased hash to point to derive the key image generator (I).
1 parent 297e991 commit 564be61

File tree

8 files changed

+447
-1
lines changed

8 files changed

+447
-1
lines changed

src/fcmp_pp/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929
set(fcmp_pp_sources
30-
fcmp_pp_crypto.cpp)
30+
curve_trees.cpp
31+
fcmp_pp_crypto.cpp
32+
fcmp_pp_types.cpp)
3133

3234
monero_find_all_headers(fcmp_pp_headers "${CMAKE_CURRENT_SOURCE_DIR}")
3335

36+
add_subdirectory(fcmp_pp_rust)
37+
3438
monero_add_library(fcmp_pp
3539
${fcmp_pp_sources}
3640
${fcmp_pp_headers})

src/fcmp_pp/curve_trees.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) 2024, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#include "curve_trees.h"
30+
31+
#include "crypto/crypto.h"
32+
#include "fcmp_pp_crypto.h"
33+
#include "fcmp_pp_types.h"
34+
#include "profile_tools.h"
35+
36+
37+
namespace fcmp_pp
38+
{
39+
namespace curve_trees
40+
{
41+
//----------------------------------------------------------------------------------------------------------------------
42+
//----------------------------------------------------------------------------------------------------------------------
43+
OutputTuple output_to_tuple(const OutputPair &output_pair)
44+
{
45+
const crypto::public_key &output_pubkey = output_pubkey_cref(output_pair);
46+
const crypto::ec_point &commitment = commitment_cref(output_pair);
47+
48+
crypto::ec_point O = output_pubkey;
49+
crypto::ec_point C = commitment;
50+
51+
// If the output has already been checked for torsion, then we don't need to clear torsion here
52+
if (!output_checked_for_torsion(output_pair))
53+
{
54+
TIME_MEASURE_NS_START(clear_torsion_ns);
55+
56+
if (!fcmp_pp::get_valid_torsion_cleared_point(output_pubkey, O))
57+
throw std::runtime_error("O is invalid for insertion to tree");
58+
if (!fcmp_pp::get_valid_torsion_cleared_point(commitment, C))
59+
throw std::runtime_error("C is invalid for insertion to tree");
60+
61+
if (O != output_pubkey)
62+
LOG_PRINT_L2("Output pubkey has torsion: " << output_pubkey);
63+
if (C != commitment)
64+
LOG_PRINT_L2("Commitment has torsion: " << commitment);
65+
66+
TIME_MEASURE_NS_FINISH(clear_torsion_ns);
67+
68+
LOG_PRINT_L3("clear_torsion_ns: " << clear_torsion_ns);
69+
}
70+
71+
#if !defined(NDEBUG)
72+
{
73+
// Debug build safety checks
74+
crypto::ec_point O_debug;
75+
crypto::ec_point C_debug;
76+
assert(fcmp_pp::get_valid_torsion_cleared_point(output_pubkey, O_debug));
77+
assert(fcmp_pp::get_valid_torsion_cleared_point(commitment, C_debug));
78+
assert(O == O_debug);
79+
assert(C == C_debug);
80+
}
81+
#endif
82+
83+
// Redundant check for safety
84+
if (O == crypto::EC_I)
85+
throw std::runtime_error("O cannot equal identity");
86+
if (C == crypto::EC_I)
87+
throw std::runtime_error("C cannot equal identity");
88+
89+
TIME_MEASURE_NS_START(derive_key_image_generator_ns);
90+
91+
// Derive key image generator using original output pubkey
92+
crypto::ec_point I;
93+
crypto::derive_key_image_generator(output_pubkey, use_biased_hash_to_point(output_pair), I);
94+
95+
TIME_MEASURE_NS_FINISH(derive_key_image_generator_ns);
96+
97+
LOG_PRINT_L3("derive_key_image_generator_ns: " << derive_key_image_generator_ns);
98+
99+
return output_tuple_from_bytes(O, I, C);
100+
}
101+
//----------------------------------------------------------------------------------------------------------------------
102+
//----------------------------------------------------------------------------------------------------------------------
103+
} //namespace curve_trees
104+
} //namespace fcmp_pp

src/fcmp_pp/curve_trees.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#pragma once
30+
31+
#include "crypto/crypto.h"
32+
#include "fcmp_pp_types.h"
33+
#include "misc_log_ex.h"
34+
35+
36+
namespace fcmp_pp
37+
{
38+
namespace curve_trees
39+
{
40+
//----------------------------------------------------------------------------------------------------------------------
41+
//----------------------------------------------------------------------------------------------------------------------
42+
OutputTuple output_to_tuple(const OutputPair &output_pair);
43+
//----------------------------------------------------------------------------------------------------------------------
44+
//----------------------------------------------------------------------------------------------------------------------
45+
} //namespace curve_trees
46+
} //namespace fcmp_pp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2024, The Monero Project
2+
#
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without modification, are
6+
# permitted provided that the following conditions are met:
7+
#
8+
# 1. Redistributions of source code must retain the above copyright notice, this list of
9+
# conditions and the following disclaimer.
10+
#
11+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
# of conditions and the following disclaimer in the documentation and/or other
13+
# materials provided with the distribution.
14+
#
15+
# 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
# used to endorse or promote products derived from this software without specific
17+
# prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/fcmp_pp/fcmp_pp_rust/fcmp++.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2025, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#pragma once
30+
31+
#include <stdint.h>
32+
33+
34+
struct OutputTuple
35+
{
36+
uint8_t O[32];
37+
uint8_t I[32];
38+
uint8_t C[32];
39+
};

src/fcmp_pp/fcmp_pp_types.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) 2024, The Monero Project
2+
//
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice, this list of
9+
// conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other
13+
// materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16+
// used to endorse or promote products derived from this software without specific
17+
// prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#include "fcmp_pp_types.h"
30+
31+
#include "misc_log_ex.h"
32+
33+
namespace fcmp_pp
34+
{
35+
//----------------------------------------------------------------------------------------------------------------------
36+
//----------------------------------------------------------------------------------------------------------------------
37+
// Helpers
38+
//----------------------------------------------------------------------------------------------------------------------
39+
OutputTuple output_tuple_from_bytes(const crypto::ec_point &O, const crypto::ec_point &I, const crypto::ec_point &C)
40+
{
41+
OutputTuple output_tuple;
42+
43+
static_assert(sizeof(output_tuple.O) == sizeof(O), "unexpected sizeof O");
44+
static_assert(sizeof(output_tuple.I) == sizeof(I), "unexpected sizeof I");
45+
static_assert(sizeof(output_tuple.C) == sizeof(C), "unexpected sizeof C");
46+
47+
memcpy(output_tuple.O, &O, sizeof(O));
48+
memcpy(output_tuple.I, &I, sizeof(I));
49+
memcpy(output_tuple.C, &C, sizeof(C));
50+
51+
return output_tuple;
52+
}
53+
//----------------------------------------------------------------------------------------------------------------------
54+
//----------------------------------------------------------------------------------------------------------------------
55+
const crypto::public_key &output_pubkey_cref(const OutputPair &output_pair)
56+
{
57+
struct output_pair_visitor
58+
{
59+
const crypto::public_key &operator()(const LegacyOutputPair &o) const
60+
{ return o.output_pubkey; }
61+
const crypto::public_key &operator()(const CarrotOutputPairV1 &o) const
62+
{ return o.output_pubkey; }
63+
};
64+
65+
return std::visit(output_pair_visitor{}, output_pair);
66+
}
67+
//----------------------------------------------------------------------------------------------------------------------
68+
const crypto::ec_point &commitment_cref(const OutputPair &output_pair)
69+
{
70+
struct output_pair_visitor
71+
{
72+
const crypto::ec_point &operator()(const LegacyOutputPair &o) const
73+
{ return o.commitment; }
74+
const crypto::ec_point &operator()(const CarrotOutputPairV1 &o) const
75+
{ return o.commitment; }
76+
};
77+
78+
return std::visit(output_pair_visitor{}, output_pair);
79+
}
80+
//----------------------------------------------------------------------------------------------------------------------
81+
bool output_checked_for_torsion(const OutputPair &output_pair)
82+
{
83+
struct output_pair_visitor
84+
{
85+
bool operator()(const CarrotOutputPairV1&) const
86+
{ return true; }
87+
bool operator()(const LegacyOutputPair&) const
88+
{ return false; }
89+
};
90+
91+
return std::visit(output_pair_visitor{}, output_pair);
92+
}
93+
//----------------------------------------------------------------------------------------------------------------------
94+
bool use_biased_hash_to_point(const OutputPair &output_pair)
95+
{
96+
struct output_pair_visitor
97+
{
98+
bool operator()(const CarrotOutputPairV1&) const
99+
{ return false; }
100+
bool operator()(const LegacyOutputPair&) const
101+
{ return true; }
102+
};
103+
104+
return std::visit(output_pair_visitor{}, output_pair);
105+
}
106+
//----------------------------------------------------------------------------------------------------------------------
107+
//----------------------------------------------------------------------------------------------------------------------
108+
}//namespace fcmp_pp

0 commit comments

Comments
 (0)