From ef86ee0bae22da7948c8f03899b53cbb55e80a06 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 3 Mar 2020 18:32:06 +0100 Subject: [PATCH] sphinx: Migrate sphinx compression to new interface It also removes the duplicate compression code and serialization code. --- common/sphinx.c | 26 +------------------------- common/sphinx.h | 9 --------- devtools/onion.c | 18 ++++++++++++------ 3 files changed, 13 insertions(+), 40 deletions(-) diff --git a/common/sphinx.c b/common/sphinx.c index 4adea1e0c..7b51625e1 100644 --- a/common/sphinx.c +++ b/common/sphinx.c @@ -149,30 +149,6 @@ u8 *serialize_onionpacket( return dst; } -u8 *serialize_compressed_onion(const tal_t *ctx, - const struct sphinx_path *sp, - const struct onionpacket *packet) -{ - u8 *dst; - u8 der[PUBKEY_CMPR_LEN]; - size_t payloads_size = sphinx_path_payloads_size(sp); - size_t max_prefill = ROUTING_INFO_SIZE - payloads_size; - size_t rv_onion_size = TOTAL_PACKET_SIZE - max_prefill; - int p = 0; - - assert(sp->rendezvous_id != NULL); - - dst = tal_arr(ctx, u8, rv_onion_size); - - pubkey_to_der(der, &packet->ephemeralkey); - write_buffer(dst, &packet->version, 1, &p); - write_buffer(dst, der, sizeof(der), &p); - write_buffer(dst, packet->routinginfo, ROUTING_INFO_SIZE - max_prefill, &p); - write_buffer(dst, packet->mac, sizeof(packet->mac), &p); - return dst; -} - - enum onion_type parse_onionpacket(const u8 *src, const size_t srclen, struct onionpacket *dest) @@ -810,7 +786,7 @@ sphinx_compress(const tal_t *ctx, const struct onionpacket *packet, size_t payloads_size = sphinx_path_payloads_size(path); /* We can't compress an onion that doesn't have a rendez-vous node. */ - if (path->rendezvous_id) + if (path->rendezvous_id == NULL) return NULL; res = tal(ctx, struct sphinx_compressed_onion); diff --git a/common/sphinx.h b/common/sphinx.h index dc9caeb2f..ccb57cd1b 100644 --- a/common/sphinx.h +++ b/common/sphinx.h @@ -234,15 +234,6 @@ void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey, */ size_t sphinx_path_payloads_size(const struct sphinx_path *path); -/** - * Compress a rendez-vous onion by removing the unused blinded middle - * part. This middle part can be regenerated by the node processing this - * onion. - */ -u8 *serialize_compressed_onion(const tal_t *ctx, - const struct sphinx_path *sp, - const struct onionpacket *packet); - /** * Set the rendez-vous node_id and make the onion generated from the * sphinx_path compressible. To unset pass in a NULL rendezvous_id. diff --git a/devtools/onion.c b/devtools/onion.c index 49008e6ae..e9e13ef01 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -32,7 +32,9 @@ static void do_generate(int argc, char **argv, struct secret session_key; struct secret *shared_secrets; struct sphinx_path *sp; - + struct sphinx_compressed_onion *comp; + u8 *serialized; + struct onionpacket *packet; const u8* tmp_assocdata =tal_dup_arr(ctx, u8, assocdata, ASSOC_DATA_SIZE, 0); memset(&session_key, 'A', sizeof(struct secret)); @@ -93,13 +95,17 @@ static void do_generate(int argc, char **argv, } } - struct onionpacket *res = create_onionpacket(ctx, sp, &shared_secrets); + packet = create_onionpacket(ctx, sp, &shared_secrets); - if (rvnode_id != NULL) - printf("Rendezvous onion: %s\n", - tal_hex(ctx, serialize_compressed_onion(ctx, sp, res))); + if (rvnode_id != NULL) { + comp = sphinx_compress(ctx, packet, sp); + serialized = sphinx_compressed_onion_serialize(ctx, comp); + printf("Rendezvous onion: %s\n", tal_hex(ctx, serialized)); + } else { + assert(sphinx_compress(ctx, packet, sp) == NULL); + } - u8 *serialized = serialize_onionpacket(ctx, res); + serialized = serialize_onionpacket(ctx, packet); if (!serialized) errx(1, "Error serializing message."); printf("%s\n", tal_hex(ctx, serialized));