Skip to content

Commit

Permalink
tidy up tests and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwicky committed Apr 18, 2024
1 parent 3821ba2 commit 41f5ce0
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 213 deletions.
3 changes: 2 additions & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use sphinx_packet::constants::{
DESTINATION_ADDRESS_LENGTH, IDENTIFIER_LENGTH, NODE_ADDRESS_LENGTH,
};
use sphinx_packet::crypto::keygen;

use sphinx_packet::header::delays;
use sphinx_packet::route::{Destination, DestinationAddressBytes, Node, NodeAddressBytes};
use sphinx_packet::test_utils::fixtures::keygen;
use sphinx_packet::SphinxPacket;
use std::time::Duration;

Expand Down
154 changes: 0 additions & 154 deletions src/crypto/keys.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/header/delays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod delay_summing {
let delay2 = Delay(123);

let expected1 = Delay(165);
assert_eq!(expected1, &delay1 + &delay2);
assert_eq!(expected1, delay1 + delay2);

let expected2 = Delay(265);
let delay3 = Delay(100);
Expand All @@ -185,7 +185,7 @@ mod delay_summing {

#[test]
fn works_with_iterator() {
let delays = vec![Delay(42), Delay(123), Delay(100)];
let delays = [Delay(42), Delay(123), Delay(100)];
let expected = Delay(265);

assert_eq!(expected, delays.iter().sum());
Expand Down
36 changes: 20 additions & 16 deletions src/header/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,11 @@ mod deriving_key_material {
#[test]
fn it_returns_no_routing_keys() {
let empty_route: Vec<Node> = vec![];
let initial_secret = EphemeralSecret::new();
let hacky_secret_copy = EphemeralSecret::from(initial_secret.to_bytes());
let initial_secret = StaticSecret::random();
let key_material = KeyMaterial::derive(&empty_route, &initial_secret);
assert_eq!(0, key_material.routing_keys.len());
assert_eq!(
SharedSecret::from(&hacky_secret_copy).as_bytes(),
PublicKey::from(&initial_secret).as_bytes(),
key_material.initial_shared_secret.as_bytes()
)
}
Expand All @@ -163,12 +162,11 @@ mod deriving_key_material {
use super::*;
use crate::test_utils::random_node;

fn setup() -> (Vec<Node>, EphemeralSecret, KeyMaterial) {
fn setup() -> (Vec<Node>, StaticSecret, KeyMaterial) {
let route: Vec<Node> = vec![random_node(), random_node(), random_node()];
let initial_secret = EphemeralSecret::new();
let hacky_secret_copy = EphemeralSecret::from(initial_secret.to_bytes());
let initial_secret = StaticSecret::random();
let key_material = KeyMaterial::derive(&route, &initial_secret);
(route, hacky_secret_copy, key_material)
(route, initial_secret, key_material)
}

#[test]
Expand All @@ -181,7 +179,7 @@ mod deriving_key_material {
fn it_returns_correctly_inited_shared_secret() {
let (_, initial_secret, key_material) = setup();
assert_eq!(
SharedSecret::from(&initial_secret).as_bytes(),
PublicKey::from(&initial_secret).as_bytes(),
key_material.initial_shared_secret.as_bytes()
);
}
Expand All @@ -194,13 +192,19 @@ mod deriving_key_material {
// incorrectly blinded shared key through the mixnet in the (unencrypted)
// Sphinx packet header. So this test ensures that the accumulator gets incremented
// properly on each run through the loop.
let mut expected_accumulator = initial_secret;
let mut expected_accumulator = vec![initial_secret]; //SW TODO adapt this test to new way of computation
for (i, node) in route.iter().enumerate() {
let expected_shared_key = expected_accumulator.diffie_hellman(&node.pub_key);
let expected_shared_key =
expected_accumulator
.iter()
.fold(node.pub_key, |acc, blinding_factor| {
PublicKey::from(blinding_factor.diffie_hellman(&acc).to_bytes())
});

let expected_routing_keys = RoutingKeys::derive(expected_shared_key);

expected_accumulator = &expected_accumulator
* &Scalar::from_bytes_mod_order(expected_routing_keys.blinding_factor);
expected_accumulator
.push(StaticSecret::from(expected_routing_keys.blinding_factor));
let expected_routing_keys = RoutingKeys::derive(expected_shared_key);
assert_eq!(expected_routing_keys, key_material.routing_keys[i])
}
Expand All @@ -214,8 +218,8 @@ mod key_derivation_function {

#[test]
fn it_expands_the_seed_key_to_expected_length() {
let initial_secret = EphemeralSecret::new();
let shared_key = SharedSecret::from(&initial_secret);
let initial_secret = StaticSecret::random();
let shared_key = PublicKey::from(&initial_secret);
let routing_keys = RoutingKeys::derive(shared_key);
assert_eq!(
crypto::STREAM_CIPHER_KEY_SIZE,
Expand All @@ -225,8 +229,8 @@ mod key_derivation_function {

#[test]
fn it_returns_the_same_output_for_two_equal_inputs() {
let initial_secret = EphemeralSecret::new();
let shared_key = SharedSecret::from(&initial_secret);
let initial_secret = StaticSecret::random();
let shared_key = PublicKey::from(&initial_secret);
let routing_keys1 = RoutingKeys::derive(shared_key);
let routing_keys2 = RoutingKeys::derive(shared_key);
assert_eq!(routing_keys1, routing_keys2);
Expand Down
27 changes: 15 additions & 12 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,29 +245,32 @@ impl SphinxHeader {
#[cfg(test)]
mod create_and_process_sphinx_packet_header {
use super::*;
use crate::{constants::NODE_ADDRESS_LENGTH, test_utils::fixtures::destination_fixture};
use crate::{
constants::NODE_ADDRESS_LENGTH,
test_utils::fixtures::{destination_fixture, keygen},
};
use std::time::Duration;

#[test]
fn it_returns_correct_routing_information_at_each_hop_for_route_of_3_mixnodes() {
let (node1_sk, node1_pk) = crypto::keygen();
let (node1_sk, node1_pk) = keygen();
let node1 = Node {
address: NodeAddressBytes::from_bytes([5u8; NODE_ADDRESS_LENGTH]),
pub_key: node1_pk,
};
let (node2_sk, node2_pk) = crypto::keygen();
let (node2_sk, node2_pk) = keygen();
let node2 = Node {
address: NodeAddressBytes::from_bytes([4u8; NODE_ADDRESS_LENGTH]),
pub_key: node2_pk,
};
let (node3_sk, node3_pk) = crypto::keygen();
let (node3_sk, node3_pk) = keygen();
let node3 = Node {
address: NodeAddressBytes::from_bytes([2u8; NODE_ADDRESS_LENGTH]),
pub_key: node3_pk,
};
let route = [node1, node2, node3];
let destination = destination_fixture();
let initial_secret = EphemeralSecret::new();
let initial_secret = StaticSecret::random();
let average_delay = 1;
let delays =
delays::generate_from_average_duration(route.len(), Duration::from_secs(average_delay));
Expand Down Expand Up @@ -387,24 +390,24 @@ mod unwrap_routing_information {
mod unwrapping_using_previously_derived_keys {
use super::*;
use crate::constants::NODE_ADDRESS_LENGTH;
use crate::test_utils::fixtures::destination_fixture;
use crate::test_utils::fixtures::{destination_fixture, keygen};
use std::time::Duration;

#[test]
fn produces_same_result_for_forward_hop() {
let (node1_sk, node1_pk) = crypto::keygen();
let (node1_sk, node1_pk) = keygen();
let node1 = Node {
address: NodeAddressBytes::from_bytes([5u8; NODE_ADDRESS_LENGTH]),
pub_key: node1_pk,
};
let (_, node2_pk) = crypto::keygen();
let (_, node2_pk) = keygen();
let node2 = Node {
address: NodeAddressBytes::from_bytes([4u8; NODE_ADDRESS_LENGTH]),
pub_key: node2_pk,
};
let route = [node1, node2];
let destination = destination_fixture();
let initial_secret = EphemeralSecret::new();
let initial_secret = StaticSecret::random();
let average_delay = 1;
let delays =
delays::generate_from_average_duration(route.len(), Duration::from_secs(average_delay));
Expand Down Expand Up @@ -439,14 +442,14 @@ mod unwrapping_using_previously_derived_keys {

#[test]
fn produces_same_result_for_final_hop() {
let (node1_sk, node1_pk) = crypto::keygen();
let (node1_sk, node1_pk) = keygen();
let node1 = Node {
address: NodeAddressBytes::from_bytes([5u8; NODE_ADDRESS_LENGTH]),
pub_key: node1_pk,
};
let route = [node1];
let destination = destination_fixture();
let initial_secret = EphemeralSecret::new();
let initial_secret = StaticSecret::random();
let average_delay = 1;
let delays =
delays::generate_from_average_duration(route.len(), Duration::from_secs(average_delay));
Expand Down Expand Up @@ -483,7 +486,7 @@ mod converting_header_to_bytes {
fn it_is_possible_to_convert_back_and_forth() {
let encapsulated_routing_info = encapsulated_routing_information_fixture();
let header = SphinxHeader {
shared_secret: SharedSecret::from(&EphemeralSecret::new()),
shared_secret: PublicKey::from(&StaticSecret::random()),
routing_info: encapsulated_routing_info,
};

Expand Down
2 changes: 1 addition & 1 deletion src/header/routing/destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl FinalRoutingInformation {
// return D || I || PAD
PaddedFinalRoutingInformation {
value: std::iter::once(self.flag)
.chain(self.version.to_bytes().into_iter())
.chain(self.version.to_bytes())
.chain(self.destination.as_bytes().iter().cloned())
.chain(self.identifier.iter().cloned())
.chain(padding.iter().cloned())
Expand Down
2 changes: 1 addition & 1 deletion src/header/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ mod encapsulating_forward_routing_information {
let delay0 = Delay::new_from_nanos(10);
let delay1 = Delay::new_from_nanos(20);
let delay2 = Delay::new_from_nanos(30);
let delays = [delay0.clone(), delay1.clone(), delay2].to_vec();
let delays = [delay0, delay1, delay2].to_vec();
let routing_keys = [
routing_keys_fixture(),
routing_keys_fixture(),
Expand Down
2 changes: 1 addition & 1 deletion src/header/routing/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl RoutingInformation {
.chain(self.version.to_bytes().iter().cloned())
.chain(self.node_address.as_bytes_ref().iter().cloned())
.chain(self.delay.to_bytes().iter().cloned())
.chain(self.header_integrity_mac.into_inner().into_iter())
.chain(self.header_integrity_mac.into_inner())
.chain(self.next_routing_information.iter().cloned())
.collect()
}
Expand Down
Loading

0 comments on commit 41f5ce0

Please sign in to comment.