Update test vectors to August 4 version (#12)

This commit is contained in:
cygnet 2023-08-07 02:02:15 +02:00 committed by GitHub
parent ad2c5b0e72
commit cd09955b30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 480 additions and 428 deletions

View File

@ -4,10 +4,6 @@ This repo is a rust implementation of BIP352: Silent Payments.
This BIP is still under development, and this repo is by no means ready for real use yet. This BIP is still under development, and this repo is by no means ready for real use yet.
At this point, the repo is no more than a rust rewrite of the `reference.py` python reference implementation. At this point, the repo is no more than a rust rewrite of the `reference.py` python reference implementation.
The `tests/resources` folder contains a copy of the test vectors as of July 23rd 2023. The `tests/resources` folder contains a copy of the test vectors as of August 4th 2023.
However, for ease of reading the data, some slight changes have been made to the formatting:
- Empty labels are given as an empty map `{}` rather than an empty list `[]`
- The label integer `m` is given in 32-byte big-endian hex format
You can test the code using the test vectors by running `cargo test` You can test the code using the test vectors by running `cargo test`

View File

@ -1,11 +1,9 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use secp256k1::{PublicKey, Secp256k1, SecretKey};
use serde::Deserialize; use serde::Deserialize;
use serde_json::from_str; use serde_json::from_str;
use silentpayments::structs::OutputWithSignature; use silentpayments::structs::OutputWithSignature;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::str::FromStr;
use std::{collections::HashMap, fs::File, io::Read}; use std::{collections::HashMap, fs::File, io::Read};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -17,6 +15,7 @@ pub struct TestData {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct ReceivingData { pub struct ReceivingData {
pub supports_labels: bool,
pub given: ReceivingDataGiven, pub given: ReceivingDataGiven,
pub expected: ReceivingDataExpected, pub expected: ReceivingDataExpected,
} }
@ -26,6 +25,8 @@ pub struct ReceivingDataGiven {
pub outpoints: Vec<(String, u32)>, pub outpoints: Vec<(String, u32)>,
pub input_pub_keys: Vec<String>, pub input_pub_keys: Vec<String>,
pub bip32_seed: String, pub bip32_seed: String,
pub scan_priv_key: String,
pub spend_priv_key: String,
pub labels: HashMap<String, String>, pub labels: HashMap<String, String>,
pub outputs: Vec<String>, pub outputs: Vec<String>,
} }
@ -51,7 +52,7 @@ pub struct SendingDataGiven {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SendingDataExpected { pub struct SendingDataExpected {
pub outputs: Vec<HashMap<String, f32>>, pub outputs: Vec<(String, f32)>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -98,37 +99,3 @@ pub fn read_file() -> Vec<TestData> {
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
from_str(&contents).unwrap() from_str(&contents).unwrap()
} }
// Note: this function is only temporary.
// The format for keys from the test vector will be changed soon.
// Until then, this method is used.
pub fn get_testing_silent_payment_key_pair(
bytes: &str,
) -> (SecretKey, SecretKey, PublicKey, PublicKey) {
let secp = Secp256k1::new();
// test vector key input will change soon
let (b_scan_str, b_spend_str) = match bytes {
"0x01" => (
"a6dba5c9af3ee645c2287c6b1d558d3ea968502ef5343398f48715e624ddd183",
"d96b8703387c5ffec5d256f80d4dc9f39152b2150fd05e469b011215251aa259",
),
"0x00" => (
"59984d7f53ff7e0ee345c6e9f5d5e47ae957abf3b55f2272152561db7e700255",
"d41394c1c9dc1745c50028dc550765dfad87e50b3fdfb15a3e4290ec59ce34c6",
),
"0x02" => (
"34c45d7dc16b07aba41463fd5437fad2dd05e3da8afd1805ae13062882d4f7c4",
"944d675e840f52af695d1415564912173b7a4ca740dc946875f9f64b97f8090c",
),
_ => ("", ""),
};
let b_scan = SecretKey::from_str(b_scan_str).unwrap();
let b_spend = SecretKey::from_str(b_spend_str).unwrap();
let B_scan = b_scan.public_key(&secp);
let B_spend = b_spend.public_key(&secp);
(b_scan, b_spend, B_scan, B_spend)
}

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@ mod tests {
use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey}; use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey};
use crate::{ use crate::{
common::input::{self, get_testing_silent_payment_key_pair, ComparableHashMap, TestData}, common::input::{self, TestData},
receiving::{ receiving::{
get_A_sum_public_keys, get_receiving_addresses, scanning, get_A_sum_public_keys, get_receiving_addresses, scanning,
verify_and_calculate_signatures, verify_and_calculate_signatures,
@ -36,9 +36,10 @@ mod tests {
for sendingtest in test_case.sending { for sendingtest in test_case.sending {
let given = sendingtest.given; let given = sendingtest.given;
let expected = sendingtest.expected; let expected = sendingtest.expected.outputs;
let expected_comparable: HashSet<ComparableHashMap> =
expected.outputs.into_iter().map(|x| x.into()).collect(); let expected_output_addresses: HashSet<String> =
expected.iter().map(|(x, _)| x.into()).collect();
let input_priv_keys: Vec<(SecretKey, bool)> = given let input_priv_keys: Vec<(SecretKey, bool)> = given
.input_priv_keys .input_priv_keys
@ -55,10 +56,7 @@ mod tests {
} }
} }
let outputs_comparable: HashSet<ComparableHashMap> = assert_eq!(sending_outputs, expected_output_addresses);
outputs.into_iter().map(|x| x.into()).collect();
assert_eq!(outputs_comparable, expected_comparable);
} }
for receivingtest in &test_case.receiving { for receivingtest in &test_case.receiving {
@ -71,8 +69,11 @@ mod tests {
// to the expected receiving outputs // to the expected receiving outputs
assert!(sending_outputs.is_subset(&receiving_outputs)); assert!(sending_outputs.is_subset(&receiving_outputs));
let (b_scan, b_spend, B_scan, B_spend) = let b_scan = SecretKey::from_str(&given.scan_priv_key).unwrap();
get_testing_silent_payment_key_pair(&given.bip32_seed); let b_spend = SecretKey::from_str(&given.spend_priv_key).unwrap();
let secp = secp256k1::Secp256k1::new();
let B_scan: PublicKey = b_scan.public_key(&secp);
let B_spend: PublicKey = b_spend.public_key(&secp);
let receiving_addresses = let receiving_addresses =
get_receiving_addresses(B_scan, B_spend, &given.labels).unwrap(); get_receiving_addresses(B_scan, B_spend, &given.labels).unwrap();