Skip to content

Commit 253338e

Browse files
committed
Add a bunch of verification tests with data lifted from python
1 parent f943ed9 commit 253338e

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

src/common/standard_coin.rs

+76-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use chia_bls;
77
use clvm_traits::{clvm_curried_args, ToClvm};
88

99
use clvmr::allocator::NodePtr;
10+
use clvm_tools_rs::util::number_from_u8;
1011

1112
use clvm_tools_rs::classic::clvm::__type_compatibility__::{
1213
Bytes, Stream, UnvalidatedBytesFromType,
@@ -75,6 +76,17 @@ fn calculate_synthetic_offset(public_key: &PublicKey, hidden_puzzle_hash: &Puzzl
7576
BigInt::from_bytes_be(Sign::Plus, blob.bytes()) % group_order_int()
7677
}
7778

79+
#[test]
80+
fn test_calculate_synthetic_offset() {
81+
let pk_bytes: [u8; 48] = [0xa3, 0xbb, 0xce, 0xd3, 0x3d, 0x27, 0x32, 0x9d, 0xa1, 0xe3, 0x60, 0xff, 0x4b, 0x0f, 0x00, 0xdb, 0x17, 0x47, 0xee, 0xe8, 0xe6, 0x6c, 0x0c, 0x0a, 0xe4, 0x50, 0xf9, 0x0b, 0x76, 0x0f, 0x42, 0x97, 0x22, 0x16, 0xc2, 0xff, 0x02, 0x76, 0x36, 0xae, 0xeb, 0x52, 0x68, 0xbc, 0x2b, 0xe2, 0xce, 0xdb];
82+
let pk = PublicKey::from_bytes(pk_bytes).expect("should be ok");
83+
let default_hidden_puzzle_hash = PuzzleHash::from_bytes(DEFAULT_HIDDEN_PUZZLE_HASH.clone());
84+
let offset = calculate_synthetic_offset(&pk, &default_hidden_puzzle_hash);
85+
let want_offset_bytes = [0x69, 0x51, 0x33, 0xf4, 0x61, 0x0a, 0x5e, 0x50, 0x7b, 0x2f, 0x24, 0x98, 0x22, 0x21, 0x91, 0xde, 0x54, 0x6e, 0xeb, 0x53, 0x90, 0x46, 0x34, 0x52, 0x74, 0x61, 0x39, 0x71, 0x4f, 0x05, 0x94, 0x65];
86+
let want_offset = number_from_u8(&want_offset_bytes);
87+
assert_eq!(offset, want_offset);
88+
}
89+
7890
pub fn calculate_synthetic_public_key(
7991
public_key: &PublicKey,
8092
hidden_puzzle_hash: &PuzzleHash,
@@ -93,6 +105,17 @@ pub fn calculate_synthetic_public_key(
93105
Ok(public_key.clone() + public_of_synthetic)
94106
}
95107

108+
#[test]
109+
fn test_calculate_synthetic_public_key() {
110+
let pk_bytes: [u8; 48] = [0xa3, 0xbb, 0xce, 0xd3, 0x3d, 0x27, 0x32, 0x9d, 0xa1, 0xe3, 0x60, 0xff, 0x4b, 0x0f, 0x00, 0xdb, 0x17, 0x47, 0xee, 0xe8, 0xe6, 0x6c, 0x0c, 0x0a, 0xe4, 0x50, 0xf9, 0x0b, 0x76, 0x0f, 0x42, 0x97, 0x22, 0x16, 0xc2, 0xff, 0x02, 0x76, 0x36, 0xae, 0xeb, 0x52, 0x68, 0xbc, 0x2b, 0xe2, 0xce, 0xdb];
111+
let pk = PublicKey::from_bytes(pk_bytes).expect("should be ok");
112+
let default_hidden_puzzle_hash = PuzzleHash::from_bytes(DEFAULT_HIDDEN_PUZZLE_HASH.clone());
113+
let spk = calculate_synthetic_public_key(&pk, &default_hidden_puzzle_hash).expect("should be ok");
114+
let want_spk_bytes: [u8; 48] = [0x93, 0xbd, 0x85, 0x12, 0x8d, 0x0e, 0x9f, 0xbc, 0xfc, 0xa5, 0x47, 0xb9, 0x64, 0xbd, 0x31, 0x80, 0x77, 0x7c, 0x6f, 0xe9, 0xfa, 0xd8, 0x08, 0xdd, 0xa4, 0x15, 0xbb, 0x32, 0x88, 0x70, 0x22, 0x86, 0x47, 0x74, 0xb5, 0xff, 0x04, 0x45, 0x2b, 0x88, 0xbc, 0x98, 0x29, 0x40, 0x8f, 0xb7, 0xf8, 0x87];
115+
let want_spk = PublicKey::from_bytes(want_spk_bytes).expect("should be ok");
116+
assert_eq!(spk, want_spk);
117+
}
118+
96119
pub fn puzzle_for_synthetic_public_key(
97120
allocator: &mut AllocEncoder,
98121
standard_coin_puzzle: &Puzzle,
@@ -176,6 +199,29 @@ pub fn puzzle_hash_for_synthetic_public_key(
176199
Ok(curry_and_treehash(&quoted_mod_hash, &[public_key_hash]))
177200
}
178201

202+
#[test]
203+
fn test_puzzle_for_synthetic_public_key() {
204+
let mut allocator = AllocEncoder::new();
205+
let expect_hex = "ff02ffff01ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff01ff02ff17ff2f80ffff01ff088080ff0180ffff01ff04ffff04ff04ffff04ff05ffff04ffff02ff06ffff04ff02ffff04ff17ff80808080ff80808080ffff02ff17ff2f808080ff0180ffff04ffff01ff32ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080ffff04ffff01b0a3bbced33d27329da1e360ff4b0f00db1747eee8e66c0c0ae450f90b760f42972216c2ff027636aeeb5268bc2be2cedbff018080";
206+
let expect_program = hex_to_sexp(&mut allocator, expect_hex.to_string()).expect("should be good hex");
207+
let expect_hash = Node(expect_program).sha256tree(&mut allocator);
208+
209+
let pk_bytes: [u8; 48] = [0xa3, 0xbb, 0xce, 0xd3, 0x3d, 0x27, 0x32, 0x9d, 0xa1, 0xe3, 0x60, 0xff, 0x4b, 0x0f, 0x00, 0xdb, 0x17, 0x47, 0xee, 0xe8, 0xe6, 0x6c, 0x0c, 0x0a, 0xe4, 0x50, 0xf9, 0x0b, 0x76, 0x0f, 0x42, 0x97, 0x22, 0x16, 0xc2, 0xff, 0x02, 0x76, 0x36, 0xae, 0xeb, 0x52, 0x68, 0xbc, 0x2b, 0xe2, 0xce, 0xdb];
210+
let pk = PublicKey::from_bytes(pk_bytes).expect("should be ok");
211+
212+
let standard_coin_puzzle = get_standard_coin_puzzle(&mut allocator).expect("should read");
213+
let puzzle_for_synthetic_public_key = puzzle_for_synthetic_public_key(
214+
&mut allocator,
215+
&standard_coin_puzzle,
216+
&pk
217+
).expect("should work");
218+
assert_eq!(puzzle_for_synthetic_public_key.sha256tree(&mut allocator), expect_hash);
219+
assert_eq!(expect_hash, puzzle_hash_for_synthetic_public_key(
220+
&mut allocator,
221+
&pk
222+
).expect("should make"));
223+
}
224+
179225
pub fn puzzle_for_pk(
180226
allocator: &mut AllocEncoder,
181227
public_key: &PublicKey,
@@ -206,6 +252,26 @@ pub fn puzzle_hash_for_pk(
206252
)?)
207253
}
208254

255+
#[test]
256+
fn test_puzzle_for_pk() {
257+
let mut allocator = AllocEncoder::new();
258+
259+
let pk_bytes: [u8; 48] = [0xa3, 0xbb, 0xce, 0xd3, 0x3d, 0x27, 0x32, 0x9d, 0xa1, 0xe3, 0x60, 0xff, 0x4b, 0x0f, 0x00, 0xdb, 0x17, 0x47, 0xee, 0xe8, 0xe6, 0x6c, 0x0c, 0x0a, 0xe4, 0x50, 0xf9, 0x0b, 0x76, 0x0f, 0x42, 0x97, 0x22, 0x16, 0xc2, 0xff, 0x02, 0x76, 0x36, 0xae, 0xeb, 0x52, 0x68, 0xbc, 0x2b, 0xe2, 0xce, 0xdb];
260+
let pk = PublicKey::from_bytes(pk_bytes).expect("should be ok");
261+
262+
let want_puzzle_for_pk = "ff02ffff01ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff01ff02ff17ff2f80ffff01ff088080ff0180ffff01ff04ffff04ff04ffff04ff05ffff04ffff02ff06ffff04ff02ffff04ff17ff80808080ff80808080ffff02ff17ff2f808080ff0180ffff04ffff01ff32ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080ffff04ffff01b093bd85128d0e9fbcfca547b964bd3180777c6fe9fad808dda415bb32887022864774b5ff04452b88bc9829408fb7f887ff018080";
263+
let want_puzzle = hex_to_sexp(&mut allocator, want_puzzle_for_pk.to_string()).expect("should be ok hex");
264+
let want_puzzle_hash = Node(want_puzzle).sha256tree(&mut allocator);
265+
266+
let got_puzzle = puzzle_for_pk(&mut allocator, &pk).expect("should be ok");
267+
let got_puzzle_hash = got_puzzle.sha256tree(&mut allocator);
268+
269+
let predicted_puzzle_hash = puzzle_hash_for_pk(&mut allocator, &pk).expect("should be ok");
270+
271+
assert_eq!(want_puzzle_hash, got_puzzle_hash);
272+
assert_eq!(got_puzzle_hash, predicted_puzzle_hash);
273+
}
274+
209275
pub fn solution_for_delegated_puzzle(
210276
allocator: &mut AllocEncoder,
211277
delegated_puzzle: Program,
@@ -296,7 +362,11 @@ pub fn sign_agg_sig_me(
296362
agg_sig_me_extra_data: &Hash,
297363
) -> Aggsig {
298364
let message = agg_sig_me_message(raw_message, coin_id, agg_sig_me_extra_data);
299-
secret_key.sign(&message)
365+
eprintln!("sign message {message:?}");
366+
let public_key = private_to_public_key(secret_key);
367+
let signed = secret_key.sign(&message);
368+
assert!(signed.verify(&public_key, &message));
369+
signed
300370
}
301371

302372
pub fn standard_solution(
@@ -405,10 +475,13 @@ impl ChiaIdentity {
405475
private_key: PrivateKey
406476
) -> Result<Self, types::Error> {
407477
let public_key = private_to_public_key(&private_key);
478+
let puzzle = puzzle_for_pk(allocator, &public_key)?;
479+
let puzzle_hash = puzzle_hash_for_pk(allocator, &public_key)?;
480+
assert_eq!(puzzle.sha256tree(allocator), puzzle_hash);
408481
Ok(ChiaIdentity {
409482
private_key,
410-
puzzle: puzzle_for_pk(allocator, &public_key)?,
411-
puzzle_hash: puzzle_hash_for_pk(allocator, &public_key)?,
483+
puzzle,
484+
puzzle_hash,
412485
public_key,
413486
})
414487
}

0 commit comments

Comments
 (0)