darkfi_money_contract/model/
nullifier.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi_sdk::{crypto::pasta_prelude::PrimeField, error::ContractError, pasta::pallas};
20use darkfi_serial::{SerialDecodable, SerialEncodable};
21
22#[cfg(feature = "client")]
23use darkfi_serial::async_trait;
24
25/// The `Nullifier` is represented as a base field element.
26#[repr(C)]
27#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
28pub struct Nullifier(pallas::Base);
29
30impl Nullifier {
31    /// Reference the raw inner base field element
32    pub fn inner(&self) -> pallas::Base {
33        self.0
34    }
35
36    /// Create a `Nullifier` object from given bytes
37    pub fn from_bytes(x: [u8; 32]) -> Result<Self, ContractError> {
38        match pallas::Base::from_repr(x).into() {
39            Some(v) => Ok(Self(v)),
40            None => Err(ContractError::IoError("Noncanonical bytes for Nullifier".to_string())),
41        }
42    }
43
44    /// Convert the `Nullifier` type into 32 raw bytes
45    pub fn to_bytes(&self) -> [u8; 32] {
46        self.0.to_repr()
47    }
48}
49
50use core::str::FromStr;
51darkfi_sdk::fp_from_bs58!(Nullifier);
52darkfi_sdk::fp_to_bs58!(Nullifier);
53darkfi_sdk::ty_from_fp!(Nullifier);