Skip to content

Commit aa1670c

Browse files
committed
FCMP++: point to cycle scalar impl
This is the function to convert a Helios point to Selene scalar, and Selene point to Helios scalar, which is critical when growing the curve trees merkle tree. Each tree elem is a hash of the preceeding layer's scalars. The hash is a point, we convert each point to scalars on the curve's respective cycle.
1 parent a68a28e commit aa1670c

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

src/fcmp_pp/fcmp_pp_rust/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fcmp_pp/fcmp_pp_rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ciphersuite = { version = "0.4.2", features = ["ed25519"] }
1212
dalek-ff-group = "0.4.4"
1313
helioselene = { git = "https://github.com/monero-oxide/monero-oxide", rev = "50dd3dbc0cc9048247eb734d27eb1d516aecacd2" }
1414

15+
ec-divisors = { git = "https://github.com/monero-oxide/monero-oxide", rev = "50dd3dbc0cc9048247eb734d27eb1d516aecacd2", features = ["ed25519"] }
1516
full-chain-membership-proofs = { git = "https://github.com/monero-oxide/monero-oxide", rev = "50dd3dbc0cc9048247eb734d27eb1d516aecacd2" }
1617

1718
monero-generators = { git = "https://github.com/monero-oxide/monero-oxide", rev = "50dd3dbc0cc9048247eb734d27eb1d516aecacd2" }

src/fcmp_pp/fcmp_pp_rust/fcmp++.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct HeliosPoint helios_hash_init_point(void);
9898

9999
struct SelenePoint selene_hash_init_point(void);
100100

101+
int selene_point_to_helios_scalar(struct SelenePoint selene_point, struct HeliosScalar *helios_scalar_out);
102+
103+
int helios_point_to_selene_scalar(struct HeliosPoint helios_point, struct SeleneScalar *selene_scalar_out);
104+
101105
struct HeliosScalar helios_zero_scalar(void);
102106

103107
struct SeleneScalar selene_zero_scalar(void);

src/fcmp_pp/fcmp_pp_rust/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use ciphersuite::{
66
Ciphersuite,
77
};
88

9+
use ec_divisors::DivisorCurve;
10+
911
use full_chain_membership_proofs::tree::hash_grow;
1012
use helioselene::{
1113
Field25519 as SeleneScalar, HeliosPoint, HelioseleneField as HeliosScalar, Selene,
@@ -78,6 +80,28 @@ ec_elem_to_bytes!(selene_point_to_bytes, SelenePoint, to_bytes);
7880

7981
ec_elem_from_bytes!(selene_scalar_from_bytes, SeleneScalar, Selene, read_F);
8082

83+
macro_rules! point_to_cycle_scalar {
84+
($fn_name:ident, $Point:ty, $Scalar:ty) => {
85+
/// # Safety
86+
///
87+
/// This function assumes scalar_out is a non-null pointer to the expected type.
88+
#[no_mangle]
89+
pub unsafe extern "C" fn $fn_name(point: $Point, scalar_out: *mut $Scalar) -> c_int {
90+
if scalar_out.is_null() {
91+
return -1;
92+
}
93+
let Some(xy_coords) = <$Point>::to_xy(point) else {
94+
return -2;
95+
};
96+
*scalar_out = xy_coords.0;
97+
0
98+
}
99+
};
100+
}
101+
102+
point_to_cycle_scalar!(selene_point_to_helios_scalar, SelenePoint, HeliosScalar);
103+
point_to_cycle_scalar!(helios_point_to_selene_scalar, HeliosPoint, SeleneScalar);
104+
81105
// Undefined behavior occurs when the data pointer passed to core::slice::from_raw_parts is null,
82106
// even when len is 0. slice_from_raw_parts_0able() lets you pass p as null, as long as len is 0
83107
const unsafe fn slice_from_raw_parts_0able<'a, T>(p: *const T, len: usize) -> &'a [T] {

src/fcmp_pp/tower_cycle.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ Helios::Point Helios::hash_init_point() const
6161
return ::helios_hash_init_point();
6262
}
6363
//----------------------------------------------------------------------------------------------------------------------
64+
Selene::CycleScalar Selene::point_to_cycle_scalar(const Selene::Point &point) const
65+
{
66+
Selene::CycleScalar helios_scalar;
67+
int r = ::selene_point_to_helios_scalar(point, &helios_scalar);
68+
CHECK_FFI_RES;
69+
return helios_scalar;
70+
}
71+
//----------------------------------------------------------------------------------------------------------------------
72+
Helios::CycleScalar Helios::point_to_cycle_scalar(const Helios::Point &point) const
73+
{
74+
Helios::CycleScalar selene_scalar;
75+
int r = ::helios_point_to_selene_scalar(point, &selene_scalar);
76+
CHECK_FFI_RES;
77+
return selene_scalar;
78+
}
79+
//----------------------------------------------------------------------------------------------------------------------
6480
Selene::Scalar Selene::zero_scalar() const
6581
{
6682
return ::selene_zero_scalar();

src/fcmp_pp/tower_cycle.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Curve
4848
public:
4949
virtual typename C::Point hash_init_point() const = 0;
5050

51+
// Read the x-coordinate from this curve's point to get this curve's cycle scalar
52+
virtual typename C::CycleScalar point_to_cycle_scalar(const typename C::Point &point) const = 0;
53+
5154
virtual typename C::Point hash_grow(
5255
const typename C::Point &existing_hash,
5356
const std::size_t offset,
@@ -77,6 +80,8 @@ class Selene final : public Curve<SeleneT>
7780
public:
7881
Point hash_init_point() const override;
7982

83+
CycleScalar point_to_cycle_scalar(const Point &point) const override;
84+
8085
Point hash_grow(
8186
const Point &existing_hash,
8287
const std::size_t offset,
@@ -106,6 +111,8 @@ class Helios final : public Curve<HeliosT>
106111
public:
107112
Point hash_init_point() const override;
108113

114+
CycleScalar point_to_cycle_scalar(const Point &point) const override;
115+
109116
Point hash_grow(
110117
const Point &existing_hash,
111118
const std::size_t offset,

0 commit comments

Comments
 (0)