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

Optimization: Pass SwitchCommitment by value instead of reference #3217

Merged
merged 1 commit into from
Feb 4, 2020
Merged
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
8 changes: 4 additions & 4 deletions core/src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions core/src/libtx/aggsig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -264,7 +264,7 @@ pub fn sign_from_key_id<K>(
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)
}
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions core/src/libtx/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down Expand Up @@ -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)?;

Expand Down
52 changes: 26 additions & 26 deletions core/src/libtx/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn create<K, B>(
b: &B,
amount: u64,
key_id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
_commit: Commitment,
extra_data: Option<Vec<u8>>,
) -> Result<RangeProof, Error>
Expand Down Expand Up @@ -109,7 +109,7 @@ pub trait ProofBuild {
&self,
secp: &Secp256k1,
id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error>;

/// Check if the output belongs to this keychain
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -191,10 +191,10 @@ where
&self,
_secp: &Secp256k1,
id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> {
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];
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -305,7 +305,7 @@ where
&self,
_secp: &Secp256k1,
id: &Identifier,
_switch: &SwitchCommitmentType,
_switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> {
let mut msg = [0; 20];
let id_ser = id.serialize_path();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -378,7 +378,7 @@ impl ProofBuild for ViewKey {
&self,
_secp: &Secp256k1,
_id: &Identifier,
_switch: &SwitchCommitmentType,
_switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> {
unimplemented!();
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down Expand Up @@ -543,7 +543,7 @@ mod tests {
//let id = ExtKeychain::derive_key_id(3, rng.gen::<u16>() as u32, rng.gen::<u16>() as u32, rng.gen::<u16>() 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..
Expand Down Expand Up @@ -580,15 +580,15 @@ 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(
&keychain,
&builder,
amount,
&id,
&switch,
switch,
commit.clone(),
None,
)
Expand Down Expand Up @@ -625,15 +625,15 @@ 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(
&keychain,
&builder,
amount,
&id,
&switch,
switch,
commit.clone(),
None,
)
Expand Down Expand Up @@ -677,15 +677,15 @@ 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(
&keychain,
&builder,
amount,
&id,
&switch,
switch,
commit.clone(),
None,
)
Expand Down Expand Up @@ -728,15 +728,15 @@ 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(
&keychain,
&builder,
amount,
&id,
&switch,
switch,
commit.clone(),
None,
)
Expand Down
2 changes: 1 addition & 1 deletion core/src/libtx/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,);
Expand Down
2 changes: 1 addition & 1 deletion core/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/tests/verifier_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions keychain/src/keychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Keychain for ExtKeychain {
&self,
amount: u64,
id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<SecretKey, Error> {
let mut h = self.hasher.clone();
let p = id.to_path();
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

match switch {
SwitchCommitmentType::Regular => {
Ok(self.secp.blind_switch(amount, ext_key.secret_key)?)
}
Expand All @@ -121,7 +121,7 @@ impl Keychain for ExtKeychain {
&self,
amount: u64,
id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<Commitment, Error> {
let key = self.derive_key(amount, id, switch)?;
let commit = self.secp.commit(amount, key)?;
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Keychain for ExtKeychain {
msg: &Message,
amount: u64,
id: &Identifier,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<Signature, Error> {
let skey = self.derive_key(amount, id, switch)?;
let sig = self.secp.sign(msg, &skey)?;
Expand Down Expand Up @@ -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();
Expand Down
Loading