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 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 {

View file

@ -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<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)
}
@ -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();

View file

@ -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)?;

View file

@ -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>
@ -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
@ -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<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];
@ -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<ProofMessage, Error> {
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<ProofMessage, Error> {
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::<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..
@ -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,
)

View file

@ -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,);

View file

@ -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();

View file

@ -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();

View file

@ -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();
@ -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<Commitment, Error> {
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<Signature, Error> {
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();

View file

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

View file

@ -151,11 +151,11 @@ impl ViewKey {
&self,
secp: &Secp256k1,
amount: u64,
switch: &SwitchCommitmentType,
switch: SwitchCommitmentType,
) -> Result<PublicKey, Error> {
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