|
@@ -16,12 +16,11 @@
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-use core::{fmt, str::FromStr};
|
|
|
|
|
-use std::io;
|
|
|
|
|
-
|
|
|
|
|
use darkfi_serial::{SerialDecodable, SerialEncodable};
|
|
use darkfi_serial::{SerialDecodable, SerialEncodable};
|
|
|
use pasta_curves::{group::ff::PrimeField, pallas};
|
|
use pasta_curves::{group::ff::PrimeField, pallas};
|
|
|
|
|
|
|
|
|
|
+use crate::error::ContractError;
|
|
|
|
|
+
|
|
|
/// The `Nullifier` is represented as a base field element.
|
|
/// The `Nullifier` is represented as a base field element.
|
|
|
#[repr(C)]
|
|
#[repr(C)]
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
|
|
@@ -33,13 +32,11 @@ impl Nullifier {
|
|
|
self.0
|
|
self.0
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Try to create a `Nullifier` type from the given 32 bytes.
|
|
|
|
|
- /// Returns `Some` if the bytes fit in the base field, and `None` if not.
|
|
|
|
|
- pub fn from_bytes(bytes: [u8; 32]) -> Option<Self> {
|
|
|
|
|
- let n = pallas::Base::from_repr(bytes);
|
|
|
|
|
- match bool::from(n.is_some()) {
|
|
|
|
|
- true => Some(Self(n.unwrap())),
|
|
|
|
|
- false => None,
|
|
|
|
|
|
|
+ /// Create a `Nullifier` object from given bytes
|
|
|
|
|
+ pub fn from_bytes(x: [u8; 32]) -> Result<Self, ContractError> {
|
|
|
|
|
+ match pallas::Base::from_repr(x).into() {
|
|
|
|
|
+ Some(v) => Ok(Self(v)),
|
|
|
|
|
+ None => Err(ContractError::IoError("Noncanonical bytes for Nullifier".to_string())),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -49,37 +46,7 @@ impl Nullifier {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-impl From<pallas::Base> for Nullifier {
|
|
|
|
|
- fn from(x: pallas::Base) -> Self {
|
|
|
|
|
- Self(x)
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-impl fmt::Display for Nullifier {
|
|
|
|
|
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
- write!(f, "{}", bs58::encode(self.to_bytes()).into_string())
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-impl FromStr for Nullifier {
|
|
|
|
|
- type Err = io::Error;
|
|
|
|
|
-
|
|
|
|
|
- /// Tries to decode a base58 string into a `Nullifier` type.
|
|
|
|
|
- /// This string is the same string received by calling `Nullifier::to_string()`.
|
|
|
|
|
- fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
- let bytes = match bs58::decode(s).into_vec() {
|
|
|
|
|
- Ok(v) => v,
|
|
|
|
|
- Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- if bytes.len() != 32 {
|
|
|
|
|
- return Err(io::Error::new(io::ErrorKind::Other, "Length of decoded bytes is not 32"))
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if let Some(nullifier) = Self::from_bytes(bytes.try_into().unwrap()) {
|
|
|
|
|
- return Ok(nullifier)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Err(io::Error::new(io::ErrorKind::Other, "Invalid bytes for Nullifier"))
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+use core::str::FromStr;
|
|
|
|
|
+crate::fp_from_bs58!(Nullifier);
|
|
|
|
|
+crate::fp_to_bs58!(Nullifier);
|
|
|
|
|
+crate::ty_from_fp!(Nullifier);
|