From 5348cf492b79e9feebf44281a8552df11c479557 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 25 Jun 2024 20:58:30 -0400 Subject: [PATCH 1/2] P2 fusion puzzle rewritten in Rue --- examples/p2_fusion.rue | 64 ++++++++++++++++++++++++++++++++++++++++++ tests.toml | 7 +++++ 2 files changed, 71 insertions(+) create mode 100644 examples/p2_fusion.rue diff --git a/examples/p2_fusion.rue b/examples/p2_fusion.rue new file mode 100644 index 0000000..17f629a --- /dev/null +++ b/examples/p2_fusion.rue @@ -0,0 +1,64 @@ +// This puzzle has not been audited or tested, and is for example purposes only. + +// Also known as a "singleton struct". +struct SingletonInfo { + mod_hash: Bytes32, + launcher_id: Bytes32, + ...launcher_puzzle_hash: Bytes32, +} + +// Calculate the full puzzle hash for a singleton. +inline fun singleton_puzzle_hash(singleton_info: SingletonInfo, inner_puzzle_hash: Bytes32) -> Bytes32 { + curry_tree_hash(singleton_info.mod_hash, tree_hash(singleton_info), inner_puzzle_hash) +} + +fun main( + fusion_singleton: SingletonInfo, + fusion_inner_puzzle_hash: Bytes32, + fusion_coin_id: Bytes32, + my_launcher_id: Bytes32, + my_inner_puzzle_hash: Bytes32, + my_amount: Int, + p2_puzzle_hash: Bytes32, +) -> Condition[] { + // The NFT singleton has the same mod hash and launcher puzzle hash as the fusion singleton. + let nft_singleton = SingletonInfo { + mod_hash: fusion_singleton.mod_hash, + launcher_id: my_launcher_id, + launcher_puzzle_hash: fusion_singleton.launcher_puzzle_hash, + }; + + // Calculate the full puzzle hashes for the NFT and fusion singletons. + let fusion_puzzle_hash = singleton_puzzle_hash(fusion_singleton, fusion_inner_puzzle_hash); + let nft_puzzle_hash = singleton_puzzle_hash(nft_singleton, my_inner_puzzle_hash); + + // Calculate the announcement message. + let announcement_message = sha256(fusion_coin_id + my_launcher_id + p2_puzzle_hash); + + [ + // Make sure that the amount in the solution is correct. + Condition::AssertMyAmount { amount: my_amount }, + + // Prove supplied NFT coin matches the expected derived puzzle hash. + Condition::AssertMyPuzzleHash { puzzle_hash: nft_puzzle_hash }, + + + // Assert that the fusion singleton announced this NFT spend. + Condition::AssertPuzzleAnnouncement { + announcement_id: sha256(fusion_puzzle_hash + announcement_message), + }, + + // Unlock the NFT to the new p2 puzzle hash, with a hint. + Condition::CreateCoin { + puzzle_hash: p2_puzzle_hash, + amount: my_amount, + memos: [p2_puzzle_hash], + }, + + // Announce that a specific singleton is being spent to + // help prevent ephemeral singleton spends from influencing. + Condition::CreateCoinAnnouncement { + message: announcement_message, + } + ] +} diff --git a/tests.toml b/tests.toml index 9dc9ee0..dcf1a98 100644 --- a/tests.toml +++ b/tests.toml @@ -442,3 +442,10 @@ cost = 1332 input = "()" output = "5" hash = "32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1" + +[p2_fusion] +bytes = 743 +cost = 87452 +input = "((0x4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7 0x32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1 0x770bb2c0b2582924adf403e62374f8424a2ed510eef70b5f450eccab238a4911) 0x8d496dc7cdbc417db2132eda6894b29a91511f176e93a1ea943d210cd27822b2 0x3878fc2ed6b703c7c3d00f9f5d8f170ec252ca439be6e5b5b516e1dce1adc0d7 0x3fe4d62a7db919b25377cb207f16fa0fb6519e123366eaa23014dd5d7c967ca2 0x837525fb91b88dbf02d9ef5a0b322f6943f93424b6e8fe6d26dd1be6d3311871 1 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)" +output = "((73 1) (72 0x6155f55414a5cd9193bef33744095a78d57852cf24241b25edeffad6e544c499) (63 0x9d6824bfdfb4c726334a210b657a5e4126c3fbb378598bb3a5b7a210bb75cdb8) (g1_negate 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5 1 (0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)) (60 0x81fdd3fbc407906bc0875522e7a2e77409ee5ef17d3eaa611b505b344588f6b6))" +hash = "4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7" From 920c4da06e5e94e9cbee150e6d17f0fdef0662a1 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 25 Jun 2024 21:01:26 -0400 Subject: [PATCH 2/2] Add missing comma --- examples/p2_fusion.rue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/p2_fusion.rue b/examples/p2_fusion.rue index 17f629a..dbf19d0 100644 --- a/examples/p2_fusion.rue +++ b/examples/p2_fusion.rue @@ -59,6 +59,6 @@ fun main( // help prevent ephemeral singleton spends from influencing. Condition::CreateCoinAnnouncement { message: announcement_message, - } + }, ] }