zebra_chain/orchard/note/
nullifiers.rs

1//! Orchard nullifier types and conversions.
2
3use std::hash::{Hash, Hasher};
4
5use halo2::pasta::{group::ff::PrimeField, pallas};
6
7use crate::serialization::{serde_helpers, SerializationError};
8
9/// A Nullifier for Orchard transactions
10#[derive(Clone, Copy, Debug, Eq, Serialize, Deserialize)]
11pub struct Nullifier(#[serde(with = "serde_helpers::Base")] pub(crate) pallas::Base);
12
13impl Hash for Nullifier {
14    fn hash<H: Hasher>(&self, state: &mut H) {
15        self.0.to_repr().hash(state);
16    }
17}
18
19impl TryFrom<[u8; 32]> for Nullifier {
20    type Error = SerializationError;
21
22    fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
23        let possible_point = pallas::Base::from_repr(bytes);
24
25        if possible_point.is_some().into() {
26            Ok(Self(possible_point.unwrap()))
27        } else {
28            Err(SerializationError::Parse(
29                "Invalid pallas::Base value for orchard Nullifier",
30            ))
31        }
32    }
33}
34
35impl PartialEq for Nullifier {
36    fn eq(&self, other: &Self) -> bool {
37        self.0 == other.0
38    }
39}
40
41impl From<Nullifier> for [u8; 32] {
42    fn from(n: Nullifier) -> Self {
43        n.0.into()
44    }
45}