Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split DeriveSynthetic into a custom and default method #508

Merged
merged 1 commit into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions crates/chia-puzzles/src/derive_synthetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ use hex_literal::hex;
use num_bigint::BigInt;
use sha2::{digest::FixedOutput, Digest, Sha256};

use crate::standard::DEFAULT_HIDDEN_PUZZLE_HASH;

const GROUP_ORDER_BYTES: [u8; 32] =
hex!("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");

pub trait DeriveSynthetic {
fn derive_synthetic(&self, hidden_puzzle_hash: &[u8; 32]) -> Self;
/// Derive a synthetic key that includes a custom hidden puzzle hash.
fn derive_synthetic_hidden(&self, hidden_puzzle_hash: &[u8; 32]) -> Self;

/// Derive a synthetic key that includes [`DEFAULT_HIDDEN_PUZZLE_HASH`].
/// The default hidden puzzle is `(=)`.
fn derive_synthetic(&self) -> Self
where
Self: Sized,
{
self.derive_synthetic_hidden(&DEFAULT_HIDDEN_PUZZLE_HASH)
}
}

impl DeriveSynthetic for PublicKey {
fn derive_synthetic(&self, hidden_puzzle_hash: &[u8; 32]) -> Self {
fn derive_synthetic_hidden(&self, hidden_puzzle_hash: &[u8; 32]) -> Self {
self + &synthetic_offset(self, hidden_puzzle_hash).public_key()
}
}

impl DeriveSynthetic for SecretKey {
fn derive_synthetic(&self, hidden_puzzle_hash: &[u8; 32]) -> Self {
fn derive_synthetic_hidden(&self, hidden_puzzle_hash: &[u8; 32]) -> Self {
self + &synthetic_offset(&self.public_key(), hidden_puzzle_hash)
}
}
Expand All @@ -44,8 +56,6 @@ fn synthetic_offset(public_key: &PublicKey, hidden_puzzle_hash: &[u8; 32]) -> Se

#[cfg(test)]
mod tests {
use crate::standard::DEFAULT_HIDDEN_PUZZLE_HASH;

use super::*;

use chia_bls::{derive_keys::master_to_wallet_unhardened_intermediate, DerivableKey};
Expand Down Expand Up @@ -83,7 +93,7 @@ mod tests {
for (index, hex) in hex_keys.iter().enumerate() {
let key = intermediate
.derive_unhardened(index as u32)
.derive_synthetic(&DEFAULT_HIDDEN_PUZZLE_HASH);
.derive_synthetic();
assert_eq!(key.to_bytes().encode_hex::<String>(), *hex);
}
}
Expand Down Expand Up @@ -118,7 +128,7 @@ mod tests {
for (index, hex) in hex_keys.iter().enumerate() {
let key = intermediate
.derive_unhardened(index as u32)
.derive_synthetic(&DEFAULT_HIDDEN_PUZZLE_HASH);
.derive_synthetic();
assert_eq!(key.to_bytes().encode_hex::<String>(), *hex);
}
}
Expand Down
Loading