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

Stop panicking on invalid orchard nullifiers #2267

Merged
merged 6 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions zebra-chain/src/orchard/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{convert::TryFrom, io};

use halo2::pasta::pallas;

Expand Down Expand Up @@ -61,7 +61,7 @@ impl ZcashDeserialize for Action {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Action {
cv: ValueCommitment::zcash_deserialize(&mut reader)?,
nullifier: Nullifier::from(reader.read_32_bytes()?),
nullifier: Nullifier::try_from(reader.read_32_bytes()?)?,
rk: reader.read_32_bytes()?.into(),
cm_x: pallas::Base::zcash_deserialize(&mut reader)?,
ephemeral_key: keys::EphemeralPublicKey::zcash_deserialize(&mut reader)?,
Expand Down
7 changes: 5 additions & 2 deletions zebra-chain/src/orchard/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::primitives::redpallas::{Signature, SpendAuth, VerificationKeyBytes};

use super::{keys, note, Action, AuthorizedAction, Flags, NoteCommitment, ValueCommitment};

use std::marker::PhantomData;
use std::{convert::TryFrom, marker::PhantomData};

impl Arbitrary for Action {
type Parameters = ();
Expand Down Expand Up @@ -42,7 +42,10 @@ impl Arbitrary for note::Nullifier {
use halo2::arithmetic::FieldExt;

(any::<u64>())
.prop_map(|number| Self::from(pallas::Scalar::from_u64(number).to_bytes()))
.prop_map(|number| {
Self::try_from(pallas::Scalar::from_u64(number).to_bytes())
.expect("a valid generated nullifier")
})
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
.boxed()
}

Expand Down
23 changes: 18 additions & 5 deletions zebra-chain/src/orchard/note/nullifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

use halo2::{arithmetic::FieldExt, pasta::pallas};

use crate::serialization::serde_helpers;
use crate::serialization::{serde_helpers, SerializationError};

use super::super::{
commitment::NoteCommitment, keys::NullifierDerivingKey, note::Note, sinsemilla::*,
};

use std::hash::{Hash, Hasher};
use std::{
convert::TryFrom,
hash::{Hash, Hasher},
};

/// A cryptographic permutation, defined in [poseidonhash].
///
Expand Down Expand Up @@ -46,9 +49,19 @@ impl Hash for Nullifier {
}
}

dconnolly marked this conversation as resolved.
Show resolved Hide resolved
impl From<[u8; 32]> for Nullifier {
fn from(bytes: [u8; 32]) -> Self {
Self(pallas::Base::from_bytes(&bytes).unwrap())
impl TryFrom<[u8; 32]> for Nullifier {
type Error = SerializationError;

fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
let possible_point = pallas::Base::from_bytes(&bytes);

if possible_point.is_some().into() {
Ok(Self(possible_point.unwrap()))
} else {
Err(SerializationError::Parse(
"Invalid pallas::Base value for orchard Nullifier",
))
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions zebra-chain/src/transaction/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,29 @@ fn fake_v5_round_trip_for_network(network: Network) {
);
}
}

#[test]
fn invalid_orchard_nullifier() {
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
zebra_test::init();

use std::convert::TryFrom;

// generated by proptest using something as:
// ```rust
// ...
// array::uniform32(any::<u8>()).prop_map(|x| Self::try_from(x).unwrap()).boxed()
// ...
// ```
let invalid_nullifier_bytes = [
62, 157, 27, 63, 100, 228, 1, 82, 140, 16, 238, 78, 68, 19, 221, 184, 189, 207, 230, 95,
194, 216, 165, 24, 110, 221, 139, 195, 106, 98, 192, 71,
];

assert_eq!(
orchard::Nullifier::try_from(invalid_nullifier_bytes)
.err()
.unwrap()
.to_string(),
SerializationError::Parse("Invalid pallas::Base value for orchard Nullifier").to_string()
);
}