From ee25d0dfc91855d8d32a9480d336505034e56c6f Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Tue, 4 Feb 2020 08:52:00 -0500 Subject: [PATCH] Pass SwitchCommitment by value instead of reference (#3217) --- core/src/core/transaction.rs | 8 +++--- core/src/libtx/aggsig.rs | 6 ++--- core/src/libtx/build.rs | 4 +-- core/src/libtx/proof.rs | 52 ++++++++++++++++++------------------ core/src/libtx/reward.rs | 2 +- core/tests/transaction.rs | 2 +- core/tests/verifier_cache.rs | 2 +- keychain/src/keychain.rs | 14 +++++----- keychain/src/types.rs | 12 ++++----- keychain/src/view_key.rs | 4 +-- 10 files changed, 53 insertions(+), 53 deletions(-) diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index 6de124912..352e82b1a 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -1595,7 +1595,7 @@ mod test { let keychain = ExtKeychain::from_random_seed(false).unwrap(); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let commit = keychain - .commit(5, &key_id, &SwitchCommitmentType::Regular) + .commit(5, &key_id, SwitchCommitmentType::Regular) .unwrap(); // just some bytes for testing ser/deser @@ -1644,12 +1644,12 @@ mod test { let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let commit = keychain - .commit(1003, &key_id, &SwitchCommitmentType::Regular) + .commit(1003, &key_id, SwitchCommitmentType::Regular) .unwrap(); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let commit_2 = keychain - .commit(1003, &key_id, &SwitchCommitmentType::Regular) + .commit(1003, &key_id, SwitchCommitmentType::Regular) .unwrap(); assert!(commit == commit_2); @@ -1660,7 +1660,7 @@ mod test { let keychain = ExtKeychain::from_seed(&[0; 32], false).unwrap(); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let commit = keychain - .commit(5, &key_id, &SwitchCommitmentType::Regular) + .commit(5, &key_id, SwitchCommitmentType::Regular) .unwrap(); let input = Input { diff --git a/core/src/libtx/aggsig.rs b/core/src/libtx/aggsig.rs index 42124b7d7..60440ce8d 100644 --- a/core/src/libtx/aggsig.rs +++ b/core/src/libtx/aggsig.rs @@ -233,7 +233,7 @@ pub fn verify_partial_sig( /// let fees = 10_000; /// let value = reward(fees); /// let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); -/// let switch = &SwitchCommitmentType::Regular; +/// let switch = SwitchCommitmentType::Regular; /// let commit = keychain.commit(value, &key_id, switch).unwrap(); /// let builder = proof::ProofBuilder::new(&keychain); /// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap(); @@ -264,7 +264,7 @@ pub fn sign_from_key_id( where K: Keychain, { - let skey = k.derive_key(value, key_id, &SwitchCommitmentType::Regular)?; // TODO: proper support for different switch commitment schemes + let skey = k.derive_key(value, key_id, SwitchCommitmentType::Regular)?; // TODO: proper support for different switch commitment schemes let sig = aggsig::sign_single(secp, &msg, &skey, s_nonce, None, None, blind_sum, None)?; Ok(sig) } @@ -300,7 +300,7 @@ where /// let fees = 10_000; /// let value = reward(fees); /// let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); -/// let switch = &SwitchCommitmentType::Regular; +/// let switch = SwitchCommitmentType::Regular; /// let commit = keychain.commit(value, &key_id, switch).unwrap(); /// let builder = proof::ProofBuilder::new(&keychain); /// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap(); diff --git a/core/src/libtx/build.rs b/core/src/libtx/build.rs index 07f66b181..907649c01 100644 --- a/core/src/libtx/build.rs +++ b/core/src/libtx/build.rs @@ -69,7 +69,7 @@ where let commit = build .keychain - .commit(value, &key_id, &SwitchCommitmentType::Regular)?; + .commit(value, &key_id, SwitchCommitmentType::Regular)?; // TODO: proper support for different switch commitment schemes let input = Input::new(features, commit); Ok(( @@ -119,7 +119,7 @@ where let (tx, sum) = acc?; // TODO: proper support for different switch commitment schemes - let switch = &SwitchCommitmentType::Regular; + let switch = SwitchCommitmentType::Regular; let commit = build.keychain.commit(value, &key_id, switch)?; diff --git a/core/src/libtx/proof.rs b/core/src/libtx/proof.rs index f5a451b53..86862d814 100644 --- a/core/src/libtx/proof.rs +++ b/core/src/libtx/proof.rs @@ -30,7 +30,7 @@ pub fn create( b: &B, amount: u64, key_id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, _commit: Commitment, extra_data: Option>, ) -> Result @@ -109,7 +109,7 @@ pub trait ProofBuild { &self, secp: &Secp256k1, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result; /// Check if the output belongs to this keychain @@ -139,7 +139,7 @@ where /// Creates a new instance of this proof builder pub fn new(keychain: &'a K) -> Self { let private_root_key = keychain - .derive_key(0, &K::root_key_id(), &SwitchCommitmentType::None) + .derive_key(0, &K::root_key_id(), SwitchCommitmentType::None) .unwrap(); let private_hash = blake2b(32, &[], &private_root_key.0).as_bytes().to_vec(); @@ -191,10 +191,10 @@ where &self, _secp: &Secp256k1, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result { let mut msg = [0; 20]; - msg[2] = u8::from(switch); + msg[2] = switch as u8; let id_bytes = id.to_bytes(); for i in 0..17 { msg[i + 3] = id_bytes[i]; @@ -224,7 +224,7 @@ where let depth = u8::min(msg[3], 4); let id = Identifier::from_serialized_path(depth, &msg[4..]); - let commit_exp = self.keychain.commit(amount, &id, &switch)?; + let commit_exp = self.keychain.commit(amount, &id, switch)?; if commit == &commit_exp { Ok(Some((id, switch))) } else { @@ -270,7 +270,7 @@ where Self { keychain, root_hash: keychain - .derive_key(0, &K::root_key_id(), &SwitchCommitmentType::Regular) + .derive_key(0, &K::root_key_id(), SwitchCommitmentType::Regular) .unwrap() .0 .to_vec(), @@ -305,7 +305,7 @@ where &self, _secp: &Secp256k1, id: &Identifier, - _switch: &SwitchCommitmentType, + _switch: SwitchCommitmentType, ) -> Result { let mut msg = [0; 20]; let id_ser = id.serialize_path(); @@ -335,7 +335,7 @@ where let commit_exp = self .keychain - .commit(amount, &id, &SwitchCommitmentType::Regular)?; + .commit(amount, &id, SwitchCommitmentType::Regular)?; if commit == &commit_exp { Ok(Some((id, SwitchCommitmentType::Regular))) } else { @@ -378,7 +378,7 @@ impl ProofBuild for ViewKey { &self, _secp: &Secp256k1, _id: &Identifier, - _switch: &SwitchCommitmentType, + _switch: SwitchCommitmentType, ) -> Result { unimplemented!(); } @@ -427,7 +427,7 @@ impl ProofBuild for ViewKey { } key = key.ckd_pub(&secp, &mut hasher, child_number)?; } - let pub_key = key.commit(secp, amount, &switch)?; + let pub_key = key.commit(secp, amount, switch)?; if commit.to_pubkey(&secp)? == pub_key { Ok(Some((id, switch))) } else { @@ -451,13 +451,13 @@ mod tests { let amount = rng.gen(); let id = ExtKeychain::derive_key_id(3, rng.gen(), rng.gen(), rng.gen(), 0); let switch = SwitchCommitmentType::Regular; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); let proof = create( &keychain, &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -481,13 +481,13 @@ mod tests { // With switch commitment let commit_a = { let switch = SwitchCommitmentType::Regular; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); let proof = create( &keychain, &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -504,13 +504,13 @@ mod tests { // Without switch commitment let commit_b = { let switch = SwitchCommitmentType::None; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); let proof = create( &keychain, &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -543,7 +543,7 @@ mod tests { //let id = ExtKeychain::derive_key_id(3, rng.gen::() as u32, rng.gen::() as u32, rng.gen::() as u32, 0); let id = ExtKeychain::derive_key_id(0, 0, 0, 0, 0); let switch = SwitchCommitmentType::Regular; - println!("commit_0 = {:?}", keychain.commit(amount, &id, &SwitchCommitmentType::None).unwrap().0.to_vec()); + println!("commit_0 = {:?}", keychain.commit(amount, &id, SwitchCommitmentType::None).unwrap().0.to_vec()); let commit = keychain.commit(amount, &id, &switch).unwrap(); // Generate proof with ProofBuilder.. @@ -580,7 +580,7 @@ mod tests { 0, ); let switch = SwitchCommitmentType::None; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); // Generate proof with ProofBuilder.. let proof = create( @@ -588,7 +588,7 @@ mod tests { &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -625,7 +625,7 @@ mod tests { 0, ); let switch = SwitchCommitmentType::None; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); // Generate proof with ProofBuilder.. let proof = create( @@ -633,7 +633,7 @@ mod tests { &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -677,7 +677,7 @@ mod tests { 0, ); let switch = SwitchCommitmentType::None; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); // Generate proof with ProofBuilder.. let proof = create( @@ -685,7 +685,7 @@ mod tests { &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) @@ -728,7 +728,7 @@ mod tests { 0, ); let switch = SwitchCommitmentType::None; - let commit = keychain.commit(amount, &id, &switch).unwrap(); + let commit = keychain.commit(amount, &id, switch).unwrap(); // Generate proof with ProofBuilder.. let proof = create( @@ -736,7 +736,7 @@ mod tests { &builder, amount, &id, - &switch, + switch, commit.clone(), None, ) diff --git a/core/src/libtx/reward.rs b/core/src/libtx/reward.rs index ec60ab1a4..8b7834954 100644 --- a/core/src/libtx/reward.rs +++ b/core/src/libtx/reward.rs @@ -38,7 +38,7 @@ where { let value = reward(fees); // TODO: proper support for different switch commitment schemes - let switch = &SwitchCommitmentType::Regular; + let switch = SwitchCommitmentType::Regular; let commit = keychain.commit(value, key_id, switch)?; trace!("Block reward - Pedersen Commit is: {:?}", commit,); diff --git a/core/tests/transaction.rs b/core/tests/transaction.rs index e691df7de..a6b73d815 100644 --- a/core/tests/transaction.rs +++ b/core/tests/transaction.rs @@ -26,7 +26,7 @@ use keychain::{ExtKeychain, Keychain}; fn test_output_ser_deser() { let keychain = ExtKeychain::from_random_seed(false).unwrap(); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); - let switch = &keychain::SwitchCommitmentType::Regular; + let switch = keychain::SwitchCommitmentType::Regular; let commit = keychain.commit(5, &key_id, switch).unwrap(); let builder = proof::ProofBuilder::new(&keychain); let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap(); diff --git a/core/tests/verifier_cache.rs b/core/tests/verifier_cache.rs index 6ea939f71..fa752e869 100644 --- a/core/tests/verifier_cache.rs +++ b/core/tests/verifier_cache.rs @@ -32,7 +32,7 @@ fn test_verifier_cache_rangeproofs() { let keychain = ExtKeychain::from_random_seed(false).unwrap(); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); - let switch = &SwitchCommitmentType::Regular; + let switch = SwitchCommitmentType::Regular; let commit = keychain.commit(5, &key_id, switch).unwrap(); let builder = proof::ProofBuilder::new(&keychain); let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap(); diff --git a/keychain/src/keychain.rs b/keychain/src/keychain.rs index 1cccb7b31..721f29008 100644 --- a/keychain/src/keychain.rs +++ b/keychain/src/keychain.rs @@ -100,7 +100,7 @@ impl Keychain for ExtKeychain { &self, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result { let mut h = self.hasher.clone(); let p = id.to_path(); @@ -109,7 +109,7 @@ impl Keychain for ExtKeychain { ext_key = ext_key.ckd_priv(&self.secp, &mut h, p.path[i as usize])?; } - match *switch { + match switch { SwitchCommitmentType::Regular => { Ok(self.secp.blind_switch(amount, ext_key.secret_key)?) } @@ -121,7 +121,7 @@ impl Keychain for ExtKeychain { &self, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result { let key = self.derive_key(amount, id, switch)?; let commit = self.secp.commit(amount, key)?; @@ -136,7 +136,7 @@ impl Keychain for ExtKeychain { let res = self.derive_key( k.value, &Identifier::from_path(&k.ext_keychain_path), - &k.switch, + k.switch, ); if let Ok(s) = res { Some(s) @@ -153,7 +153,7 @@ impl Keychain for ExtKeychain { let res = self.derive_key( k.value, &Identifier::from_path(&k.ext_keychain_path), - &k.switch, + k.switch, ); if let Ok(s) = res { Some(s) @@ -186,7 +186,7 @@ impl Keychain for ExtKeychain { msg: &Message, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result { let skey = self.derive_key(amount, id, switch)?; let sig = self.secp.sign(msg, &skey)?; @@ -220,7 +220,7 @@ mod test { fn test_key_derivation() { let keychain = ExtKeychain::from_random_seed(false).unwrap(); let secp = keychain.secp(); - let switch = &SwitchCommitmentType::None; + let switch = SwitchCommitmentType::None; let path = ExtKeychainPath::new(1, 1, 0, 0, 0); let key_id = path.to_identifier(); diff --git a/keychain/src/types.rs b/keychain/src/types.rs index 2cc4dfe8e..8232d6777 100644 --- a/keychain/src/types.rs +++ b/keychain/src/types.rs @@ -484,13 +484,13 @@ pub trait Keychain: Sync + Send + Clone { &self, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result; fn commit( &self, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result; fn blind_sum(&self, blind_sum: &BlindSum) -> Result; fn sign( @@ -498,7 +498,7 @@ pub trait Keychain: Sync + Send + Clone { msg: &Message, amount: u64, id: &Identifier, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result; fn sign_with_blinding(&self, _: &Message, _: &BlindingFactor) -> Result; fn secp(&self) -> &Secp256k1; @@ -522,9 +522,9 @@ impl TryFrom for SwitchCommitmentType { } } -impl From<&SwitchCommitmentType> for u8 { - fn from(switch: &SwitchCommitmentType) -> Self { - match *switch { +impl From for u8 { + fn from(switch: SwitchCommitmentType) -> Self { + match switch { SwitchCommitmentType::None => 0, SwitchCommitmentType::Regular => 1, } diff --git a/keychain/src/view_key.rs b/keychain/src/view_key.rs index 706094cad..1b9cb903b 100644 --- a/keychain/src/view_key.rs +++ b/keychain/src/view_key.rs @@ -151,11 +151,11 @@ impl ViewKey { &self, secp: &Secp256k1, amount: u64, - switch: &SwitchCommitmentType, + switch: SwitchCommitmentType, ) -> Result { let value_key = secp.commit_value(amount)?.to_pubkey(secp)?; let pub_key = PublicKey::from_combination(secp, vec![&self.public_key, &value_key])?; - match *switch { + match switch { SwitchCommitmentType::None => Ok(pub_key), SwitchCommitmentType::Regular => { // TODO: replace this whole block by a libsecp function