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

Add more derives and constructors for various types #504

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions crates/chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use pyo3::{pyclass, pymethods, IntoPy, PyAny, PyObject, PyResult, Python};
pyclass(name = "G1Element"),
derive(PyStreamable)
)]
#[derive(Clone, Default)]
#[derive(Clone, Copy, Default)]
pub struct PublicKey(pub(crate) blst_p1);

#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -237,7 +237,7 @@ impl Neg for PublicKey {
impl Neg for &PublicKey {
type Output = PublicKey;
fn neg(self) -> Self::Output {
let mut ret = self.clone();
let mut ret = *self;
ret.negate();
ret
}
Expand All @@ -254,7 +254,7 @@ impl AddAssign<&PublicKey> for PublicKey {
impl SubAssign<&PublicKey> for PublicKey {
fn sub_assign(&mut self, rhs: &PublicKey) {
unsafe {
let mut neg = rhs.clone();
let mut neg = *rhs;
blst_p1_cneg(&mut neg.0, true);
blst_p1_add_or_double(&mut self.0, &self.0, &neg.0);
}
Expand Down Expand Up @@ -629,7 +629,7 @@ mod tests {
rng.fill(&mut data[1..]);

let g1 = PublicKey::from_integer(&data);
let mut g1_neg = g1.clone();
let mut g1_neg = g1;
g1_neg.negate();
assert!(g1_neg != g1);

Expand All @@ -641,7 +641,7 @@ mod tests {
#[test]
fn test_negate_infinity() {
let g1 = PublicKey::default();
let mut g1_neg = g1.clone();
let mut g1_neg = g1;
// negate on infinity is a no-op
g1_neg.negate();
assert!(g1_neg == g1);
Expand All @@ -657,10 +657,10 @@ mod tests {
rng.fill(&mut data[1..]);

let g1 = PublicKey::from_integer(&data);
let mut g1_neg = g1.clone();
let mut g1_neg = g1;
g1_neg.negate();

let mut g1_double = g1.clone();
let mut g1_double = g1;
// adding the negative undoes adding the positive
g1_double += &g1;
assert!(g1_double != g1);
Expand All @@ -679,7 +679,7 @@ mod tests {
rng.fill(&mut data[1..]);

let mut g1 = PublicKey::from_integer(&data);
let mut g1_double = g1.clone();
let mut g1_double = g1;
g1_double += &g1;
assert!(g1_double != g1);
// scalar multiply by 2 is the same as adding oneself
Expand Down
8 changes: 4 additions & 4 deletions crates/chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@ mod tests {
for idx in 0..4 {
let derived = sk.derive_hardened(idx as u32);
let pk = derived.public_key();
data.push((pk.clone(), msg));
data.push((pk, msg));
let sig = sign(&derived, msg);
agg1.aggregate(&sig);
agg2 += &sig;
sigs.push(sig);
pairs.push((pk.clone(), aug_msg_to_g2(&pk, msg)));
pairs.push((pk, aug_msg_to_g2(&pk, msg)));
}
let agg3 = aggregate(&sigs);
let agg4 = &sigs[0] + &sigs[1] + &sigs[2] + &sigs[3];
Expand Down Expand Up @@ -771,10 +771,10 @@ mod tests {
let mut pairs = Vec::<(PublicKey, Signature)>::new();
for _idx in 0..2 {
let pk = sk.public_key();
data.push((pk.clone(), msg));
data.push((pk, msg));
agg.aggregate(&sign(&sk, *msg));

pairs.push((pk.clone(), aug_msg_to_g2(&pk, msg)));
pairs.push((pk, aug_msg_to_g2(&pk, msg)));
}

assert_eq!(agg.to_bytes(), <[u8; 96]>::from_hex("a1cca6540a4a06d096cb5b5fc76af5fd099476e70b623b8c6e4cf02ffde94fc0f75f4e17c67a9e350940893306798a3519368b02dc3464b7270ea4ca233cfa85a38da9e25c9314e81270b54d1e773a2ec5c3e14c62dac7abdebe52f4688310d3").unwrap());
Expand Down
20 changes: 13 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,29 @@ 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;
fn derive_synthetic_hidden(&self, hidden_puzzle_hash: &[u8; 32]) -> Self;
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 +52,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 +89,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 +124,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
69 changes: 66 additions & 3 deletions crates/chia-puzzles/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
use chia_protocol::Bytes32;
use clvm_traits::{FromClvm, ToClvm};

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(untagged, tuple)]
pub enum Proof {
Lineage(LineageProof),
Eve(EveProof),
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
impl Proof {
pub fn lineage(
parent_parent_coin_id: Bytes32,
parent_inner_puzzle_hash: Bytes32,
parent_amount: u64,
) -> Self {
Self::Lineage(LineageProof::new(
parent_parent_coin_id,
parent_inner_puzzle_hash,
parent_amount,
))
}

pub fn eve(parent_coin_info: Bytes32, amount: u64) -> Self {
Self::Eve(EveProof::new(parent_coin_info, amount))
}

pub fn is_lineage(&self) -> bool {
matches!(self, Self::Lineage(_))
}

pub fn is_eve(&self) -> bool {
matches!(self, Self::Eve(_))
}

pub fn as_lineage(&self) -> Option<LineageProof> {
match self {
Self::Lineage(proof) => Some(*proof),
_ => None,
}
}

pub fn as_eve(&self) -> Option<EveProof> {
match self {
Self::Eve(proof) => Some(*proof),
_ => None,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(list)]
pub struct LineageProof {
Expand All @@ -18,10 +58,33 @@ pub struct LineageProof {
pub parent_amount: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
impl LineageProof {
pub fn new(
parent_parent_coin_id: Bytes32,
parent_inner_puzzle_hash: Bytes32,
parent_amount: u64,
) -> Self {
Self {
parent_parent_coin_id,
parent_inner_puzzle_hash,
parent_amount,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(list)]
pub struct EveProof {
pub parent_coin_info: Bytes32,
pub amount: u64,
}

impl EveProof {
pub fn new(parent_coin_info: Bytes32, amount: u64) -> Self {
Self {
parent_coin_info,
amount,
}
}
}
41 changes: 33 additions & 8 deletions crates/chia-puzzles/src/puzzles/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,53 @@ use hex_literal::hex;

use crate::LineageProof;

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(curry)]
pub struct CatArgs<I> {
pub mod_hash: Bytes32,
pub tail_program_hash: Bytes32,
pub inner_puzzle: I,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
impl<I> CatArgs<I> {
pub fn new(asset_id: Bytes32, inner_puzzle: I) -> Self {
Self {
mod_hash: CAT_PUZZLE_HASH.into(),
tail_program_hash: asset_id,
inner_puzzle,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(curry)]
pub struct EverythingWithSignatureTailArgs {
pub public_key: PublicKey,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
impl EverythingWithSignatureTailArgs {
pub fn new(public_key: PublicKey) -> Self {
Self { public_key }
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(curry)]
pub struct GenesisByCoinIdTailArgs {
pub genesis_coin_id: Bytes32,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
impl GenesisByCoinIdTailArgs {
pub fn new(genesis_coin_id: Bytes32) -> Self {
Self { genesis_coin_id }
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(list)]
pub struct CatSolution<I> {
pub inner_puzzle_solution: I,
Expand All @@ -38,7 +64,8 @@ pub struct CatSolution<I> {
pub extra_delta: i64,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(list)]
pub struct CoinProof {
pub parent_coin_info: Bytes32,
Expand Down Expand Up @@ -292,9 +319,7 @@ mod tests {
let mod_ptr = node_from_bytes(&mut a, &EVERYTHING_WITH_SIGNATURE_TAIL_PUZZLE).unwrap();
let curried_ptr = CurriedProgram {
program: mod_ptr,
args: EverythingWithSignatureTailArgs {
public_key: public_key.clone(),
},
args: EverythingWithSignatureTailArgs { public_key },
}
.to_node_ptr(&mut a)
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions crates/chia-puzzles/src/puzzles/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use hex_literal::hex;

use crate::singleton::SingletonStruct;

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[clvm(curry)]
pub struct DidArgs<I, M> {
pub inner_puzzle: I,
Expand All @@ -17,7 +18,8 @@ pub struct DidArgs<I, M> {
pub metadata: M,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum DidSolution<I> {
InnerSpend(I),
}
Expand Down
Loading
Loading