Pass SwitchCommitment by value instead of reference (#3217)

This commit is contained in:
Quentin Le Sceller 2020-02-04 08:52:00 -05:00 committed by GitHub
parent 5c959bc733
commit ee25d0dfc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 53 deletions

View file

@ -1595,7 +1595,7 @@ mod test {
let keychain = ExtKeychain::from_random_seed(false).unwrap(); let keychain = ExtKeychain::from_random_seed(false).unwrap();
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
let commit = keychain let commit = keychain
.commit(5, &key_id, &SwitchCommitmentType::Regular) .commit(5, &key_id, SwitchCommitmentType::Regular)
.unwrap(); .unwrap();
// just some bytes for testing ser/deser // 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 key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
let commit = keychain let commit = keychain
.commit(1003, &key_id, &SwitchCommitmentType::Regular) .commit(1003, &key_id, SwitchCommitmentType::Regular)
.unwrap(); .unwrap();
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
let commit_2 = keychain let commit_2 = keychain
.commit(1003, &key_id, &SwitchCommitmentType::Regular) .commit(1003, &key_id, SwitchCommitmentType::Regular)
.unwrap(); .unwrap();
assert!(commit == commit_2); assert!(commit == commit_2);
@ -1660,7 +1660,7 @@ mod test {
let keychain = ExtKeychain::from_seed(&[0; 32], false).unwrap(); let keychain = ExtKeychain::from_seed(&[0; 32], false).unwrap();
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
let commit = keychain let commit = keychain
.commit(5, &key_id, &SwitchCommitmentType::Regular) .commit(5, &key_id, SwitchCommitmentType::Regular)
.unwrap(); .unwrap();
let input = Input { let input = Input {

View file

@ -233,7 +233,7 @@ pub fn verify_partial_sig(
/// let fees = 10_000; /// let fees = 10_000;
/// let value = reward(fees); /// let value = reward(fees);
/// let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); /// 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 commit = keychain.commit(value, &key_id, switch).unwrap();
/// let builder = proof::ProofBuilder::new(&keychain); /// let builder = proof::ProofBuilder::new(&keychain);
/// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap(); /// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap();
@ -264,7 +264,7 @@ pub fn sign_from_key_id<K>(
where where
K: Keychain, 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)?; let sig = aggsig::sign_single(secp, &msg, &skey, s_nonce, None, None, blind_sum, None)?;
Ok(sig) Ok(sig)
} }
@ -300,7 +300,7 @@ where
/// let fees = 10_000; /// let fees = 10_000;
/// let value = reward(fees); /// let value = reward(fees);
/// let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); /// 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 commit = keychain.commit(value, &key_id, switch).unwrap();
/// let builder = proof::ProofBuilder::new(&keychain); /// let builder = proof::ProofBuilder::new(&keychain);
/// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap(); /// let rproof = proof::create(&keychain, &builder, value, &key_id, switch, commit, None).unwrap();

View file

@ -69,7 +69,7 @@ where
let commit = let commit =
build build
.keychain .keychain
.commit(value, &key_id, &SwitchCommitmentType::Regular)?; .commit(value, &key_id, SwitchCommitmentType::Regular)?;
// TODO: proper support for different switch commitment schemes // TODO: proper support for different switch commitment schemes
let input = Input::new(features, commit); let input = Input::new(features, commit);
Ok(( Ok((
@ -119,7 +119,7 @@ where
let (tx, sum) = acc?; let (tx, sum) = acc?;
// TODO: proper support for different switch commitment schemes // 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)?; let commit = build.keychain.commit(value, &key_id, switch)?;

View file

@ -30,7 +30,7 @@ pub fn create<K, B>(
b: &B, b: &B,
amount: u64, amount: u64,
key_id: &Identifier, key_id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
_commit: Commitment, _commit: Commitment,
extra_data: Option<Vec<u8>>, extra_data: Option<Vec<u8>>,
) -> Result<RangeProof, Error> ) -> Result<RangeProof, Error>
@ -109,7 +109,7 @@ pub trait ProofBuild {
&self, &self,
secp: &Secp256k1, secp: &Secp256k1,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error>; ) -> Result<ProofMessage, Error>;
/// Check if the output belongs to this keychain /// Check if the output belongs to this keychain
@ -139,7 +139,7 @@ where
/// Creates a new instance of this proof builder /// Creates a new instance of this proof builder
pub fn new(keychain: &'a K) -> Self { pub fn new(keychain: &'a K) -> Self {
let private_root_key = keychain let private_root_key = keychain
.derive_key(0, &K::root_key_id(), &SwitchCommitmentType::None) .derive_key(0, &K::root_key_id(), SwitchCommitmentType::None)
.unwrap(); .unwrap();
let private_hash = blake2b(32, &[], &private_root_key.0).as_bytes().to_vec(); let private_hash = blake2b(32, &[], &private_root_key.0).as_bytes().to_vec();
@ -191,10 +191,10 @@ where
&self, &self,
_secp: &Secp256k1, _secp: &Secp256k1,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> { ) -> Result<ProofMessage, Error> {
let mut msg = [0; 20]; let mut msg = [0; 20];
msg[2] = u8::from(switch); msg[2] = switch as u8;
let id_bytes = id.to_bytes(); let id_bytes = id.to_bytes();
for i in 0..17 { for i in 0..17 {
msg[i + 3] = id_bytes[i]; msg[i + 3] = id_bytes[i];
@ -224,7 +224,7 @@ where
let depth = u8::min(msg[3], 4); let depth = u8::min(msg[3], 4);
let id = Identifier::from_serialized_path(depth, &msg[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 { if commit == &commit_exp {
Ok(Some((id, switch))) Ok(Some((id, switch)))
} else { } else {
@ -270,7 +270,7 @@ where
Self { Self {
keychain, keychain,
root_hash: keychain root_hash: keychain
.derive_key(0, &K::root_key_id(), &SwitchCommitmentType::Regular) .derive_key(0, &K::root_key_id(), SwitchCommitmentType::Regular)
.unwrap() .unwrap()
.0 .0
.to_vec(), .to_vec(),
@ -305,7 +305,7 @@ where
&self, &self,
_secp: &Secp256k1, _secp: &Secp256k1,
id: &Identifier, id: &Identifier,
_switch: &SwitchCommitmentType, _switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> { ) -> Result<ProofMessage, Error> {
let mut msg = [0; 20]; let mut msg = [0; 20];
let id_ser = id.serialize_path(); let id_ser = id.serialize_path();
@ -335,7 +335,7 @@ where
let commit_exp = self let commit_exp = self
.keychain .keychain
.commit(amount, &id, &SwitchCommitmentType::Regular)?; .commit(amount, &id, SwitchCommitmentType::Regular)?;
if commit == &commit_exp { if commit == &commit_exp {
Ok(Some((id, SwitchCommitmentType::Regular))) Ok(Some((id, SwitchCommitmentType::Regular)))
} else { } else {
@ -378,7 +378,7 @@ impl ProofBuild for ViewKey {
&self, &self,
_secp: &Secp256k1, _secp: &Secp256k1,
_id: &Identifier, _id: &Identifier,
_switch: &SwitchCommitmentType, _switch: SwitchCommitmentType,
) -> Result<ProofMessage, Error> { ) -> Result<ProofMessage, Error> {
unimplemented!(); unimplemented!();
} }
@ -427,7 +427,7 @@ impl ProofBuild for ViewKey {
} }
key = key.ckd_pub(&secp, &mut hasher, child_number)?; 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 { if commit.to_pubkey(&secp)? == pub_key {
Ok(Some((id, switch))) Ok(Some((id, switch)))
} else { } else {
@ -451,13 +451,13 @@ mod tests {
let amount = rng.gen(); let amount = rng.gen();
let id = ExtKeychain::derive_key_id(3, rng.gen(), rng.gen(), rng.gen(), 0); let id = ExtKeychain::derive_key_id(3, rng.gen(), rng.gen(), rng.gen(), 0);
let switch = SwitchCommitmentType::Regular; let switch = SwitchCommitmentType::Regular;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
let proof = create( let proof = create(
&keychain, &keychain,
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -481,13 +481,13 @@ mod tests {
// With switch commitment // With switch commitment
let commit_a = { let commit_a = {
let switch = SwitchCommitmentType::Regular; let switch = SwitchCommitmentType::Regular;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
let proof = create( let proof = create(
&keychain, &keychain,
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -504,13 +504,13 @@ mod tests {
// Without switch commitment // Without switch commitment
let commit_b = { let commit_b = {
let switch = SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
let proof = create( let proof = create(
&keychain, &keychain,
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -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(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 id = ExtKeychain::derive_key_id(0, 0, 0, 0, 0);
let switch = SwitchCommitmentType::Regular; 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(); let commit = keychain.commit(amount, &id, &switch).unwrap();
// Generate proof with ProofBuilder.. // Generate proof with ProofBuilder..
@ -580,7 +580,7 @@ mod tests {
0, 0,
); );
let switch = SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder.. // Generate proof with ProofBuilder..
let proof = create( let proof = create(
@ -588,7 +588,7 @@ mod tests {
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -625,7 +625,7 @@ mod tests {
0, 0,
); );
let switch = SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder.. // Generate proof with ProofBuilder..
let proof = create( let proof = create(
@ -633,7 +633,7 @@ mod tests {
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -677,7 +677,7 @@ mod tests {
0, 0,
); );
let switch = SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder.. // Generate proof with ProofBuilder..
let proof = create( let proof = create(
@ -685,7 +685,7 @@ mod tests {
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )
@ -728,7 +728,7 @@ mod tests {
0, 0,
); );
let switch = SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, &switch).unwrap(); let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder.. // Generate proof with ProofBuilder..
let proof = create( let proof = create(
@ -736,7 +736,7 @@ mod tests {
&builder, &builder,
amount, amount,
&id, &id,
&switch, switch,
commit.clone(), commit.clone(),
None, None,
) )

View file

@ -38,7 +38,7 @@ where
{ {
let value = reward(fees); let value = reward(fees);
// TODO: proper support for different switch commitment schemes // TODO: proper support for different switch commitment schemes
let switch = &SwitchCommitmentType::Regular; let switch = SwitchCommitmentType::Regular;
let commit = keychain.commit(value, key_id, switch)?; let commit = keychain.commit(value, key_id, switch)?;
trace!("Block reward - Pedersen Commit is: {:?}", commit,); trace!("Block reward - Pedersen Commit is: {:?}", commit,);

View file

@ -26,7 +26,7 @@ use keychain::{ExtKeychain, Keychain};
fn test_output_ser_deser() { fn test_output_ser_deser() {
let keychain = ExtKeychain::from_random_seed(false).unwrap(); let keychain = ExtKeychain::from_random_seed(false).unwrap();
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); 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 commit = keychain.commit(5, &key_id, switch).unwrap();
let builder = proof::ProofBuilder::new(&keychain); let builder = proof::ProofBuilder::new(&keychain);
let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap(); let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap();

View file

@ -32,7 +32,7 @@ fn test_verifier_cache_rangeproofs() {
let keychain = ExtKeychain::from_random_seed(false).unwrap(); let keychain = ExtKeychain::from_random_seed(false).unwrap();
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0); 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 commit = keychain.commit(5, &key_id, switch).unwrap();
let builder = proof::ProofBuilder::new(&keychain); let builder = proof::ProofBuilder::new(&keychain);
let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap(); let proof = proof::create(&keychain, &builder, 5, &key_id, switch, commit, None).unwrap();

View file

@ -100,7 +100,7 @@ impl Keychain for ExtKeychain {
&self, &self,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<SecretKey, Error> { ) -> Result<SecretKey, Error> {
let mut h = self.hasher.clone(); let mut h = self.hasher.clone();
let p = id.to_path(); 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])?; ext_key = ext_key.ckd_priv(&self.secp, &mut h, p.path[i as usize])?;
} }
match *switch { match switch {
SwitchCommitmentType::Regular => { SwitchCommitmentType::Regular => {
Ok(self.secp.blind_switch(amount, ext_key.secret_key)?) Ok(self.secp.blind_switch(amount, ext_key.secret_key)?)
} }
@ -121,7 +121,7 @@ impl Keychain for ExtKeychain {
&self, &self,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<Commitment, Error> { ) -> Result<Commitment, Error> {
let key = self.derive_key(amount, id, switch)?; let key = self.derive_key(amount, id, switch)?;
let commit = self.secp.commit(amount, key)?; let commit = self.secp.commit(amount, key)?;
@ -136,7 +136,7 @@ impl Keychain for ExtKeychain {
let res = self.derive_key( let res = self.derive_key(
k.value, k.value,
&Identifier::from_path(&k.ext_keychain_path), &Identifier::from_path(&k.ext_keychain_path),
&k.switch, k.switch,
); );
if let Ok(s) = res { if let Ok(s) = res {
Some(s) Some(s)
@ -153,7 +153,7 @@ impl Keychain for ExtKeychain {
let res = self.derive_key( let res = self.derive_key(
k.value, k.value,
&Identifier::from_path(&k.ext_keychain_path), &Identifier::from_path(&k.ext_keychain_path),
&k.switch, k.switch,
); );
if let Ok(s) = res { if let Ok(s) = res {
Some(s) Some(s)
@ -186,7 +186,7 @@ impl Keychain for ExtKeychain {
msg: &Message, msg: &Message,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<Signature, Error> { ) -> Result<Signature, Error> {
let skey = self.derive_key(amount, id, switch)?; let skey = self.derive_key(amount, id, switch)?;
let sig = self.secp.sign(msg, &skey)?; let sig = self.secp.sign(msg, &skey)?;
@ -220,7 +220,7 @@ mod test {
fn test_key_derivation() { fn test_key_derivation() {
let keychain = ExtKeychain::from_random_seed(false).unwrap(); let keychain = ExtKeychain::from_random_seed(false).unwrap();
let secp = keychain.secp(); let secp = keychain.secp();
let switch = &SwitchCommitmentType::None; let switch = SwitchCommitmentType::None;
let path = ExtKeychainPath::new(1, 1, 0, 0, 0); let path = ExtKeychainPath::new(1, 1, 0, 0, 0);
let key_id = path.to_identifier(); let key_id = path.to_identifier();

View file

@ -484,13 +484,13 @@ pub trait Keychain: Sync + Send + Clone {
&self, &self,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<SecretKey, Error>; ) -> Result<SecretKey, Error>;
fn commit( fn commit(
&self, &self,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<Commitment, Error>; ) -> Result<Commitment, Error>;
fn blind_sum(&self, blind_sum: &BlindSum) -> Result<BlindingFactor, Error>; fn blind_sum(&self, blind_sum: &BlindSum) -> Result<BlindingFactor, Error>;
fn sign( fn sign(
@ -498,7 +498,7 @@ pub trait Keychain: Sync + Send + Clone {
msg: &Message, msg: &Message,
amount: u64, amount: u64,
id: &Identifier, id: &Identifier,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<Signature, Error>; ) -> Result<Signature, Error>;
fn sign_with_blinding(&self, _: &Message, _: &BlindingFactor) -> Result<Signature, Error>; fn sign_with_blinding(&self, _: &Message, _: &BlindingFactor) -> Result<Signature, Error>;
fn secp(&self) -> &Secp256k1; fn secp(&self) -> &Secp256k1;
@ -522,9 +522,9 @@ impl TryFrom<u8> for SwitchCommitmentType {
} }
} }
impl From<&SwitchCommitmentType> for u8 { impl From<SwitchCommitmentType> for u8 {
fn from(switch: &SwitchCommitmentType) -> Self { fn from(switch: SwitchCommitmentType) -> Self {
match *switch { match switch {
SwitchCommitmentType::None => 0, SwitchCommitmentType::None => 0,
SwitchCommitmentType::Regular => 1, SwitchCommitmentType::Regular => 1,
} }

View file

@ -151,11 +151,11 @@ impl ViewKey {
&self, &self,
secp: &Secp256k1, secp: &Secp256k1,
amount: u64, amount: u64,
switch: &SwitchCommitmentType, switch: SwitchCommitmentType,
) -> Result<PublicKey, Error> { ) -> Result<PublicKey, Error> {
let value_key = secp.commit_value(amount)?.to_pubkey(secp)?; let value_key = secp.commit_value(amount)?.to_pubkey(secp)?;
let pub_key = PublicKey::from_combination(secp, vec![&self.public_key, &value_key])?; let pub_key = PublicKey::from_combination(secp, vec![&self.public_key, &value_key])?;
match *switch { match switch {
SwitchCommitmentType::None => Ok(pub_key), SwitchCommitmentType::None => Ok(pub_key),
SwitchCommitmentType::Regular => { SwitchCommitmentType::Regular => {
// TODO: replace this whole block by a libsecp function // TODO: replace this whole block by a libsecp function