Correcting some trivial Rust option/iterator warts

This commit is contained in:
François Garillot 2020-01-16 07:36:29 -08:00
parent 18aceea225
commit 865275e2a2
No known key found for this signature in database
GPG Key ID: A6EFBA9E3F33E320
9 changed files with 37 additions and 70 deletions

View File

@ -582,7 +582,7 @@ mod test {
let expected = hex!("0af5695115ced92c8a0341e43869209636e9aa6472e4576f0f2b996cf812b30e");
let mut out = r.into_iter();
for b in expected.into_iter() {
for b in expected.iter() {
for i in 0..8 {
let c = out.next().unwrap().get_value().unwrap();
@ -619,7 +619,7 @@ mod test {
let expected = hex!("2ab8f0683167ba220eef19dccf4f9b1a8193cc09b35e0235842323950530f18a");
let mut out = r.into_iter();
for b in expected.into_iter() {
for b in expected.iter() {
for i in 0..8 {
let c = out.next().unwrap().get_value().unwrap();

View File

@ -402,26 +402,15 @@ fn priv_get_note(
r: *const [c_uchar; 32],
) -> Result<Note<Bls12>, ()> {
let diversifier = Diversifier(unsafe { *diversifier });
let g_d = match diversifier.g_d::<Bls12>(&JUBJUB) {
Some(g_d) => g_d,
None => return Err(()),
};
let g_d = diversifier.g_d::<Bls12>(&JUBJUB).ok_or(())?;
let pk_d = match edwards::Point::<Bls12, Unknown>::read(&(unsafe { &*pk_d })[..], &JUBJUB) {
Ok(p) => p,
Err(_) => return Err(()),
};
let pk_d = edwards::Point::<Bls12, Unknown>::read(&(unsafe { &*pk_d })[..], &JUBJUB)
.map_err(|_| ())?;
let pk_d = match pk_d.as_prime_order(&JUBJUB) {
Some(pk_d) => pk_d,
None => return Err(()),
};
let pk_d = pk_d.as_prime_order(&JUBJUB).ok_or(())?;
// Deserialize randomness
let r = match Fs::from_repr(read_fs(unsafe { &*r })) {
Ok(r) => r,
Err(_) => return Err(()),
};
let r = Fs::from_repr(read_fs(unsafe { &*r })).map_err(|_| ())?;
let note = Note {
value,

View File

@ -134,12 +134,11 @@ pub fn scan_block(
// mutable references to wtxs for too long.
let mut block_witnesses: Vec<_> = wtxs
.iter_mut()
.map(|tx| {
.flat_map(|tx| {
tx.shielded_outputs
.iter_mut()
.map(|output| &mut output.witness)
})
.flatten()
.collect();
for to_scan in tx.outputs.into_iter().enumerate() {

View File

@ -255,18 +255,18 @@ fn tree_validator(p: &Params, state: &Blake2bState, indices: &[u32]) -> Option<N
if indices.len() > 1 {
let end = indices.len();
let mid = end / 2;
match tree_validator(p, state, &indices[0..mid]) {
Some(a) => match tree_validator(p, state, &indices[mid..end]) {
Some(b) => {
if validate_subtrees(p, &a, &b) {
Some(Node::from_children(a, b, p.collision_byte_length()))
} else {
None
}
match (
tree_validator(p, state, &indices[0..mid]),
tree_validator(p, state, &indices[mid..end]),
) {
(Some(a), Some(b)) => {
if validate_subtrees(p, &a, &b) {
Some(Node::from_children(a, b, p.collision_byte_length()))
} else {
None
}
None => None,
},
None => None,
}
_ => None,
}
} else {
Some(Node::new(&p, &state, indices[0]))

View File

@ -89,10 +89,8 @@ impl<E: JubjubEngine> Point<E, Unknown> {
y_repr.as_mut()[3] &= 0x7fffffffffffffff;
match E::Fr::from_repr(y_repr) {
Ok(y) => match Self::get_for_y(y, x_sign, params) {
Some(p) => Ok(p),
None => Err(io::Error::new(io::ErrorKind::InvalidInput, "not on curve")),
},
Ok(y) => Self::get_for_y(y, x_sign, params)
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "not on curve")),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"y is not in field",

View File

@ -164,14 +164,8 @@ impl<Node: Hashable> CommitmentTree<Node> {
// - Empty leaves are used as needed.
let leaf_root = Node::combine(
0,
&match self.left {
Some(node) => node,
None => filler.next(0),
},
&match self.right {
Some(node) => node,
None => filler.next(0),
},
&self.left.unwrap_or_else(|| filler.next(0)),
&self.right.unwrap_or_else(|| filler.next(0)),
);
// 2) Hash in parents up to the currently-filled depth.

View File

@ -14,13 +14,8 @@ fn read_scalar<E: JubjubEngine, R: Read>(reader: R) -> io::Result<E::Fs> {
let mut s_repr = <E::Fs as PrimeField>::Repr::default();
s_repr.read_le(reader)?;
match E::Fs::from_repr(s_repr) {
Ok(s) => Ok(s),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"scalar is not in field",
)),
}
E::Fs::from_repr(s_repr)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "scalar is not in field"))
}
fn write_scalar<E: JubjubEngine, W: Write>(s: &E::Fs, writer: W) -> io::Result<()> {

View File

@ -75,15 +75,13 @@ mod data;
fn zip_0143() {
for tv in self::data::zip_0143::make_test_vectors() {
let tx = Transaction::read(&tv.tx[..]).unwrap();
let transparent_input = if let Some(n) = tv.transparent_input {
Some((
let transparent_input = tv.transparent_input.map(|n| {
(
n as usize,
&tv.script_code,
Amount::from_nonnegative_i64(tv.amount).unwrap(),
))
} else {
None
};
)
});
assert_eq!(
signature_hash(&tx, tv.consensus_branch_id, tv.hash_type, transparent_input),
@ -96,15 +94,13 @@ fn zip_0143() {
fn zip_0243() {
for tv in self::data::zip_0243::make_test_vectors() {
let tx = Transaction::read(&tv.tx[..]).unwrap();
let transparent_input = if let Some(n) = tv.transparent_input {
Some((
let transparent_input = tv.transparent_input.map(|n| {
(
n as usize,
&tv.script_code,
Amount::from_nonnegative_i64(tv.amount).unwrap(),
))
} else {
None
};
)
});
assert_eq!(
signature_hash(&tx, tv.consensus_branch_id, tv.hash_type, transparent_input),

View File

@ -83,10 +83,9 @@ impl SaplingProvingContext {
let viewing_key = proof_generation_key.to_viewing_key(params);
// Construct the payment address with the viewing key / diversifier
let payment_address = match viewing_key.to_payment_address(diversifier, params) {
Some(p) => p,
None => return Err(()),
};
let payment_address = viewing_key
.to_payment_address(diversifier, params)
.ok_or(())?;
// This is the result of the re-randomization, we compute it for the caller
let rk = PublicKey::<Bls12>(proof_generation_key.ak.clone().into()).randomize(
@ -266,10 +265,7 @@ impl SaplingProvingContext {
// against our derived bvk.
{
// Compute value balance
let mut value_balance = match compute_value_balance(value_balance, params) {
Some(a) => a,
None => return Err(()),
};
let mut value_balance = compute_value_balance(value_balance, params).ok_or(())?;
// Subtract value_balance from cv_sum to get final bvk
value_balance = value_balance.negate();