Only pass a single prior child

This commit is contained in:
Luke Parker 2024-05-24 02:56:57 -04:00 committed by j-berman
parent c792b21535
commit effa9eea0e
6 changed files with 39 additions and 43 deletions

View File

@ -44,22 +44,20 @@ template class CurveTrees<Helios, Selene>;
template<typename C>
typename C::Point get_new_parent(const C &curve, const typename C::Chunk &new_children)
{
// New parent means no prior children, fill priors with 0
std::vector<typename C::Scalar> prior_children;
tower_cycle::extend_zeroes(curve, new_children.len, prior_children);
return curve.hash_grow(
curve.m_hash_init_point,
0,/*offset*/
typename C::Chunk{prior_children.data(), prior_children.size()},
curve.zero_scalar(),
new_children
);
};
template Helios::Point get_new_parent<Helios>(const Helios &curve, const typename Helios::Chunk &new_children);
template Selene::Point get_new_parent<Selene>(const Selene &curve, const typename Selene::Chunk &new_children);
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
// Static functions
//----------------------------------------------------------------------------------------------------------------------
// Hash the first chunk of children being added to a layer
// Hash the first chunk of the children now being added to a layer
template<typename C>
static typename C::Point get_first_parent(const C &curve,
const typename C::Chunk &new_children,
@ -72,21 +70,17 @@ static typename C::Point get_first_parent(const C &curve,
if (last_chunk_ptr == nullptr)
return get_new_parent<C>(curve, new_children);
std::vector<typename C::Scalar> prior_children;
typename C::Scalar first_child_after_offset = curve.zero_scalar();
if (child_layer_last_hash_updated)
{
// If the last chunk has updated children in it, then we need to get the delta to the old children
prior_children.emplace_back(last_chunk_ptr->last_child);
// Extend prior children by zeroes for any additional new children, since they must be new
if (new_children.len > 1)
tower_cycle::extend_zeroes(curve, new_children.len - 1, prior_children);
first_child_after_offset = last_chunk_ptr->last_child;
}
else if (offset > 0)
{
// If we're updating the parent hash and no children were updated, then we're just adding new children
// to the existing last chunk and can fill priors with 0
tower_cycle::extend_zeroes(curve, new_children.len, prior_children);
// to the existing last chunk and can leave first_child_after_offset as zero
}
else
{
@ -97,7 +91,7 @@ static typename C::Point get_first_parent(const C &curve,
return curve.hash_grow(
last_chunk_ptr->last_parent,
offset,
typename C::Chunk{prior_children.data(), prior_children.size()},
first_child_after_offset,
new_children
);
};

View File

@ -71,7 +71,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciphersuite"
version = "0.4.1"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"blake2",
"dalek-ff-group",
@ -161,7 +161,7 @@ dependencies = [
[[package]]
name = "dalek-ff-group"
version = "0.4.1"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"crypto-bigint",
"curve25519-dalek",
@ -198,7 +198,7 @@ dependencies = [
[[package]]
name = "ec-divisors"
version = "0.1.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"dalek-ff-group",
"group",
@ -272,7 +272,7 @@ checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d"
[[package]]
name = "flexible-transcript"
version = "0.3.2"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"blake2",
"digest",
@ -285,7 +285,7 @@ dependencies = [
[[package]]
name = "full-chain-membership-proofs"
version = "0.1.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"ciphersuite",
"ec-divisors",
@ -305,7 +305,7 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "generalized-bulletproofs"
version = "0.1.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"ciphersuite",
"flexible-transcript",
@ -368,7 +368,7 @@ dependencies = [
[[package]]
name = "helioselene"
version = "0.1.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"crypto-bigint",
"dalek-ff-group",
@ -429,7 +429,7 @@ dependencies = [
[[package]]
name = "minimal-ed448"
version = "0.4.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"crypto-bigint",
"ff",
@ -444,7 +444,7 @@ dependencies = [
[[package]]
name = "multiexp"
version = "0.4.0"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"ff",
"group",
@ -613,7 +613,7 @@ dependencies = [
[[package]]
name = "std-shims"
version = "0.1.1"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a17500708f5c6d79ec9cc33d53c771149db152c3"
source = "git+https://github.com/kayabaNerve/fcmp-plus-plus#a11db094aac0384b9e62930f6f9f0d062d436897"
dependencies = [
"hashbrown",
"spin",
@ -627,9 +627,9 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "2.0.65"
version = "2.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
dependencies = [
"proc-macro2",
"quote",

View File

@ -113,12 +113,12 @@ SeleneScalar selene_zero_scalar();
CResult<HeliosPoint> hash_grow_helios(HeliosPoint existing_hash,
uintptr_t offset,
HeliosScalarSlice prior_children,
HeliosScalar first_child_after_offset,
HeliosScalarSlice new_children);
CResult<SelenePoint> hash_grow_selene(SelenePoint existing_hash,
uintptr_t offset,
SeleneScalarSlice prior_children,
SeleneScalar first_child_after_offset,
SeleneScalarSlice new_children);
} // extern "C"

View File

@ -79,12 +79,11 @@ pub extern "C" fn selene_point_to_bytes(selene_point: SelenePoint) -> *const u8
}
// Get the x coordinate of the ed25519 point
// TODO: Move this to C++
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
pub extern "C" fn ed25519_point_to_selene_scalar(ed25519_point: *const u8) -> SeleneScalar {
let mut ed25519_point = unsafe { core::slice::from_raw_parts(ed25519_point, 32) };
// TODO: If not moved to C++, at least return an error here (instead of unwrapping)
// TODO: Return an error here (instead of unwrapping)
let ed25519_point = <Ed25519>::read_G(&mut ed25519_point).unwrap();
let xy_coords = <Ed25519 as Ciphersuite>::G::to_xy(ed25519_point);
@ -153,14 +152,14 @@ impl<T, E> CResult<T, E> {
pub extern "C" fn hash_grow_helios(
existing_hash: HeliosPoint,
offset: usize,
prior_children: HeliosScalarSlice,
first_child_after_offset: HeliosScalar,
new_children: HeliosScalarSlice,
) -> CResult<HeliosPoint, io::Error> {
let hash = hash_grow(
helios_generators(),
existing_hash,
offset,
prior_children.into(),
first_child_after_offset,
new_children.into(),
);
@ -178,14 +177,14 @@ pub extern "C" fn hash_grow_helios(
pub extern "C" fn hash_grow_selene(
existing_hash: SelenePoint,
offset: usize,
prior_children: SeleneScalarSlice,
first_child_after_offset: SeleneScalar,
new_children: SeleneScalarSlice,
) -> CResult<SelenePoint, io::Error> {
let hash = hash_grow(
selene_generators(),
existing_hash,
offset,
prior_children.into(),
first_child_after_offset,
new_children.into(),
);

View File

@ -48,13 +48,13 @@ Selene::CycleScalar Selene::point_to_cycle_scalar(const Selene::Point &point) co
Helios::Point Helios::hash_grow(
const Helios::Point &existing_hash,
const std::size_t offset,
const Helios::Chunk &prior_children,
const Helios::Scalar &first_child_after_offset,
const Helios::Chunk &new_children) const
{
auto res = fcmp_rust::hash_grow_helios(
existing_hash,
offset,
prior_children,
first_child_after_offset,
new_children);
if (res.err != 0) {
throw std::runtime_error("failed to hash grow");
@ -65,13 +65,13 @@ Helios::Point Helios::hash_grow(
Selene::Point Selene::hash_grow(
const Selene::Point &existing_hash,
const std::size_t offset,
const Selene::Chunk &prior_children,
const Selene::Scalar &first_child_after_offset,
const Selene::Chunk &new_children) const
{
auto res = fcmp_rust::hash_grow_selene(
existing_hash,
offset,
prior_children,
first_child_after_offset,
new_children);
if (res.err != 0) {
throw std::runtime_error("failed to hash grow");
@ -150,6 +150,10 @@ std::string Selene::to_string(const typename Selene::Point &point) const
//----------------------------------------------------------------------------------------------------------------------
SeleneScalar ed_25519_point_to_scalar(const crypto::ec_point &point)
{
// If this function receives the ec_point, this is fine
// If this function can receive a decompressed point, it'd be notably faster
// to extract the Wei25519 x coordinate from the C side of things and then
// pass that
return fcmp_rust::ed25519_point_to_selene_scalar((uint8_t*) &point.data);
}
//----------------------------------------------------------------------------------------------------------------------
@ -182,7 +186,6 @@ void extend_scalars_from_cycle_points(const C_POINTS &curve,
for (const auto &point : points)
{
// TODO: implement reading just the x coordinate of points on curves in curve cycle in C/C++
typename C_SCALARS::Scalar scalar = curve.point_to_cycle_scalar(point);
scalars_out.push_back(std::move(scalar));
}

View File

@ -82,7 +82,7 @@ public:
virtual typename C::Point hash_grow(
const typename C::Point &existing_hash,
const std::size_t offset,
const typename C::Chunk &prior_children,
const typename C::Scalar &first_child_after_offset,
const typename C::Chunk &new_children) const = 0;
virtual typename C::Scalar zero_scalar() const = 0;
@ -120,7 +120,7 @@ public:
Point hash_grow(
const Point &existing_hash,
const std::size_t offset,
const Chunk &prior_children,
const Scalar &first_child_after_offset,
const Chunk &new_children) const override;
Scalar zero_scalar() const override;
@ -154,7 +154,7 @@ public:
Point hash_grow(
const Point &existing_hash,
const std::size_t offset,
const Chunk &prior_children,
const Scalar &first_child_after_offset,
const Chunk &new_children) const override;
Scalar zero_scalar() const override;