Less cloning and pattern simplifications (#3216)

* Less cloning and pattern simplifications

* Revert inclusive range and remove unecessary Error:From
This commit is contained in:
Quentin Le Sceller
2020-02-05 11:02:07 -05:00
committed by GitHub
parent a41965e024
commit c4e69717ab
34 changed files with 153 additions and 263 deletions
+1 -1
View File
@@ -174,7 +174,7 @@ pub fn header_version(height: u64) -> HeaderVersion {
/// Check whether the block version is valid at a given height, implements
/// 6 months interval scheduled hard forks for the first 2 years.
pub fn valid_header_version(height: u64, version: HeaderVersion) -> bool {
return height < 3 * HARD_FORK_INTERVAL && version == header_version(height);
height < 3 * HARD_FORK_INTERVAL && version == header_version(height)
}
/// Number of blocks used to calculate difficulty adjustments
+1 -1
View File
@@ -67,7 +67,7 @@ impl<H: Hashed> ShortIdentifiable for H {
}
/// Short id for identifying inputs/outputs/kernels
#[derive(Clone, Serialize, Deserialize, Hash)]
#[derive(Clone, Serialize, Deserialize)]
pub struct ShortId([u8; 6]);
impl DefaultHashable for ShortId {}
+1 -1
View File
@@ -347,7 +347,7 @@ where
let mut last_ts = last_n.last().unwrap().timestamp;
for _ in n..needed_block_count {
last_ts = last_ts.saturating_sub(last_ts_delta);
last_n.push(HeaderInfo::from_ts_diff(last_ts, last_diff.clone()));
last_n.push(HeaderInfo::from_ts_diff(last_ts, last_diff));
}
}
last_n.reverse();
+1 -1
View File
@@ -235,7 +235,7 @@ where
// Store the kernel offset (k2) on the tx.
// Commitments will sum correctly when accounting for the offset.
tx.offset = k2.clone();
tx.offset = k2;
// Set the kernel on the tx.
let tx = tx.replace_kernel(kern);
+22 -100
View File
@@ -163,9 +163,8 @@ where
&self.rewind_hash
};
let res = blake2b(32, &commit.0, hash);
SecretKey::from_slice(self.keychain.secp(), res.as_bytes()).map_err(|e| {
ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e).to_string()).into()
})
SecretKey::from_slice(self.keychain.secp(), res.as_bytes())
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
}
}
@@ -279,9 +278,8 @@ where
fn nonce(&self, commit: &Commitment) -> Result<SecretKey, Error> {
let res = blake2b(32, &commit.0, &self.root_hash);
SecretKey::from_slice(self.keychain.secp(), res.as_bytes()).map_err(|e| {
ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e).to_string()).into()
})
SecretKey::from_slice(self.keychain.secp(), res.as_bytes())
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
}
}
@@ -365,9 +363,8 @@ where
impl ProofBuild for ViewKey {
fn rewind_nonce(&self, secp: &Secp256k1, commit: &Commitment) -> Result<SecretKey, Error> {
let res = blake2b(32, &commit.0, &self.rewind_hash);
SecretKey::from_slice(secp, res.as_bytes()).map_err(|e| {
ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e).to_string()).into()
})
SecretKey::from_slice(secp, res.as_bytes())
.map_err(|e| ErrorKind::RangeProof(format!("Unable to create nonce: {:?}", e)).into())
}
fn private_nonce(&self, _secp: &Secp256k1, _commit: &Commitment) -> Result<SecretKey, Error> {
@@ -452,17 +449,8 @@ mod tests {
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 proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
assert!(verify(&keychain.secp(), commit.clone(), proof.clone(), None).is_ok());
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
assert!(verify(&keychain.secp(), commit, proof, None).is_ok());
let rewind = rewind(keychain.secp(), &builder, commit, None, proof).unwrap();
assert!(rewind.is_some());
let (r_amount, r_id, r_switch) = rewind.unwrap();
@@ -482,18 +470,9 @@ mod tests {
let commit_a = {
let switch = SwitchCommitmentType::Regular;
let commit = keychain.commit(amount, &id, switch).unwrap();
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
assert!(verify(&keychain.secp(), commit.clone(), proof.clone(), None).is_ok());
let rewind = rewind(keychain.secp(), &builder, commit.clone(), None, proof).unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
assert!(verify(&keychain.secp(), commit, proof, None).is_ok());
let rewind = rewind(keychain.secp(), &builder, commit, None, proof).unwrap();
assert!(rewind.is_some());
let (r_amount, r_id, r_switch) = rewind.unwrap();
assert_eq!(r_amount, amount);
@@ -505,18 +484,9 @@ mod tests {
let commit_b = {
let switch = SwitchCommitmentType::None;
let commit = keychain.commit(amount, &id, switch).unwrap();
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
assert!(verify(&keychain.secp(), commit.clone(), proof.clone(), None).is_ok());
let rewind = rewind(keychain.secp(), &builder, commit.clone(), None, proof).unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
assert!(verify(&keychain.secp(), commit, proof, None).is_ok());
let rewind = rewind(keychain.secp(), &builder, commit, None, proof).unwrap();
assert!(rewind.is_some());
let (r_amount, r_id, r_switch) = rewind.unwrap();
assert_eq!(r_amount, amount);
@@ -583,18 +553,9 @@ mod tests {
let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder..
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
// ..and rewind with ViewKey
let rewind = rewind(keychain.secp(), &view_key, commit.clone(), None, proof);
let rewind = rewind(keychain.secp(), &view_key, commit, None, proof);
assert!(rewind.is_ok());
let rewind = rewind.unwrap();
@@ -628,18 +589,9 @@ mod tests {
let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder..
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
// ..and rewind with ViewKey
let rewind = rewind(keychain.secp(), &view_key, commit.clone(), None, proof);
let rewind = rewind(keychain.secp(), &view_key, commit, None, proof);
assert!(rewind.is_ok());
let rewind = rewind.unwrap();
@@ -680,24 +632,9 @@ mod tests {
let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder..
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
// ..and rewind with child ViewKey
let rewind = rewind(
keychain.secp(),
&child_view_key,
commit.clone(),
None,
proof,
);
let rewind = rewind(keychain.secp(), &child_view_key, commit, None, proof);
assert!(rewind.is_ok());
let rewind = rewind.unwrap();
@@ -731,24 +668,9 @@ mod tests {
let commit = keychain.commit(amount, &id, switch).unwrap();
// Generate proof with ProofBuilder..
let proof = create(
&keychain,
&builder,
amount,
&id,
switch,
commit.clone(),
None,
)
.unwrap();
let proof = create(&keychain, &builder, amount, &id, switch, commit, None).unwrap();
// ..and rewind with child ViewKey
let rewind = rewind(
keychain.secp(),
&child_view_key,
commit.clone(),
None,
proof,
);
let rewind = rewind(keychain.secp(), &child_view_key, commit, None, proof);
assert!(rewind.is_ok());
let rewind = rewind.unwrap();
+3 -3
View File
@@ -173,10 +173,10 @@ mod test {
#[test]
fn cuckaroo19_vectors() {
let mut ctx = new_impl::<u64>(19, 42);
ctx.params.siphash_keys = V1_19_HASH.clone();
assert!(ctx.verify(&Proof::new(V1_19_SOL.to_vec().clone())).is_ok());
ctx.params.siphash_keys = V1_19_HASH;
assert!(ctx.verify(&Proof::new(V1_19_SOL.to_vec())).is_ok());
ctx.params.siphash_keys = V2_19_HASH.clone();
assert!(ctx.verify(&Proof::new(V2_19_SOL.to_vec().clone())).is_ok());
assert!(ctx.verify(&Proof::new(V2_19_SOL.to_vec())).is_ok());
assert!(ctx.verify(&Proof::zero(42)).is_err());
}
+4 -8
View File
@@ -175,16 +175,12 @@ mod test {
#[test]
fn cuckarood19_29_vectors() {
let mut ctx19 = new_impl::<u64>(19, 42);
ctx19.params.siphash_keys = V1_19_HASH.clone();
assert!(ctx19
.verify(&Proof::new(V1_19_SOL.to_vec().clone()))
.is_ok());
ctx19.params.siphash_keys = V1_19_HASH;
assert!(ctx19.verify(&Proof::new(V1_19_SOL.to_vec())).is_ok());
assert!(ctx19.verify(&Proof::zero(42)).is_err());
let mut ctx29 = new_impl::<u64>(29, 42);
ctx29.params.siphash_keys = V2_29_HASH.clone();
assert!(ctx29
.verify(&Proof::new(V2_29_SOL.to_vec().clone()))
.is_ok());
ctx29.params.siphash_keys = V2_29_HASH;
assert!(ctx29.verify(&Proof::new(V2_29_SOL.to_vec())).is_ok());
assert!(ctx29.verify(&Proof::zero(42)).is_err());
}
+4 -8
View File
@@ -168,16 +168,12 @@ mod test {
#[test]
fn cuckaroom19_29_vectors() {
let mut ctx19 = new_impl::<u64>(19, 42);
ctx19.params.siphash_keys = V1_19_HASH.clone();
assert!(ctx19
.verify(&Proof::new(V1_19_SOL.to_vec().clone()))
.is_ok());
ctx19.params.siphash_keys = V1_19_HASH;
assert!(ctx19.verify(&Proof::new(V1_19_SOL.to_vec())).is_ok());
assert!(ctx19.verify(&Proof::zero(42)).is_err());
let mut ctx29 = new_impl::<u64>(29, 42);
ctx29.params.siphash_keys = V2_29_HASH.clone();
assert!(ctx29
.verify(&Proof::new(V2_29_SOL.to_vec().clone()))
.is_ok());
ctx29.params.siphash_keys = V2_29_HASH;
assert!(ctx29.verify(&Proof::new(V2_29_SOL.to_vec())).is_ok());
assert!(ctx29.verify(&Proof::zero(42)).is_err());
}
+5 -5
View File
@@ -409,7 +409,7 @@ mod test {
{
let mut ctx = CuckatooContext::<u32>::new_impl(29, 42, 10).unwrap();
ctx.set_header_nonce([0u8; 80].to_vec(), Some(20), false)?;
assert!(ctx.verify(&Proof::new(V1_29.to_vec().clone())).is_ok());
assert!(ctx.verify(&Proof::new(V1_29.to_vec())).is_ok());
Ok(())
}
@@ -419,7 +419,7 @@ mod test {
{
let mut ctx = CuckatooContext::<u32>::new_impl(31, 42, 10).unwrap();
ctx.set_header_nonce([0u8; 80].to_vec(), Some(99), false)?;
assert!(ctx.verify(&Proof::new(V1_31.to_vec().clone())).is_ok());
assert!(ctx.verify(&Proof::new(V1_31.to_vec())).is_ok());
Ok(())
}
@@ -431,11 +431,11 @@ mod test {
let mut header = [0u8; 80];
header[0] = 1u8;
ctx.set_header_nonce(header.to_vec(), Some(20), false)?;
assert!(!ctx.verify(&Proof::new(V1_29.to_vec().clone())).is_ok());
assert!(!ctx.verify(&Proof::new(V1_29.to_vec())).is_ok());
header[0] = 0u8;
ctx.set_header_nonce(header.to_vec(), Some(20), false)?;
assert!(ctx.verify(&Proof::new(V1_29.to_vec().clone())).is_ok());
let mut bad_proof = V1_29.clone();
assert!(ctx.verify(&Proof::new(V1_29.to_vec())).is_ok());
let mut bad_proof = V1_29;
bad_proof[0] = 0x48a9e1;
assert!(!ctx.verify(&Proof::new(bad_proof.to_vec())).is_ok());
Ok(())
+1 -1
View File
@@ -61,7 +61,7 @@ pub fn siphash_block(v: &[u64; 4], nonce: u64, rot_e: u8, xor_all: bool) -> u64
for i in xor_from..SIPHASH_BLOCK_SIZE {
xor ^= nonce_hash[i as usize];
}
return xor;
xor
}
/// Implements siphash 2-4 specialized for a 4 u64 array key and a u64 nonce
+3 -3
View File
@@ -96,7 +96,7 @@ impl Difficulty {
}
/// Converts the difficulty into a u64
pub fn to_num(&self) -> u64 {
pub fn to_num(self) -> u64 {
self.num
}
}
@@ -389,7 +389,7 @@ impl Proof {
}
}
fn extract_bits(bits: &Vec<u8>, bit_start: usize, bit_count: usize, read_from: usize) -> u64 {
fn extract_bits(bits: &[u8], bit_start: usize, bit_count: usize, read_from: usize) -> u64 {
let mut buf: [u8; 8] = [0; 8];
buf.copy_from_slice(&bits[read_from..read_from + 8]);
if bit_count == 64 {
@@ -400,7 +400,7 @@ fn extract_bits(bits: &Vec<u8>, bit_start: usize, bit_count: usize, read_from: u
u64::from_le_bytes(buf) >> skip_bits & bit_mask
}
fn read_number(bits: &Vec<u8>, bit_start: usize, bit_count: usize) -> u64 {
fn read_number(bits: &[u8], bit_start: usize, bit_count: usize) -> u64 {
if bit_count == 0 {
return 0;
}
+1 -1
View File
@@ -1255,7 +1255,7 @@ where
}
}
}
const VARIANTS: &'static [&'static str] = &[
const VARIANTS: &'static [&str] = &[
"NotFound",
"PermissionDenied",
"ConnectionRefused",