darkfi_sdk/crypto/
mod.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
19/// Blinding factors
20pub mod blind;
21pub use blind::{BaseBlind, Blind, ScalarBlind};
22
23/// Cryptographic constants
24pub mod constants;
25
26/// Diffie-Hellman techniques
27pub mod diffie_hellman;
28
29/// Miscellaneous utilities
30pub mod util;
31pub use util::poseidon_hash;
32
33/// Keypairs, secret keys, and public keys
34pub mod keypair;
35pub use keypair::{Keypair, PublicKey, SecretKey};
36
37/// Contract ID definitions and methods
38pub mod contract_id;
39pub use contract_id::{ContractId, DAO_CONTRACT_ID, DEPLOYOOOR_CONTRACT_ID, MONEY_CONTRACT_ID};
40
41/// Function ID definitions and methods
42pub mod func_ref;
43pub use func_ref::{FuncId, FuncRef};
44
45/// Merkle node definitions
46pub mod merkle_node;
47pub use merkle_node::{MerkleNode, MerkleTree};
48
49/// Note encryption
50pub mod note;
51
52/// Pedersen commitment utilities
53pub mod pedersen;
54pub use pedersen::{pedersen_commitment_base, pedersen_commitment_u64};
55
56/// Schnorr signature traits
57pub mod schnorr;
58
59/// MiMC VDF
60pub mod mimc_vdf;
61
62/// Elliptic curve VRF (Verifiable Random Function)
63pub mod ecvrf;
64
65/// Sparse Merkle Tree implementation
66pub mod smt;
67
68/// Convenience module to import all the pasta traits.
69/// You still have to import the curves.
70pub mod pasta_prelude {
71    pub use pasta_curves::{
72        arithmetic::{CurveAffine, CurveExt},
73        group::{
74            ff::{Field, FromUniformBytes, PrimeField},
75            prime::PrimeCurveAffine,
76            Curve, Group,
77        },
78    };
79}
80
81#[macro_export]
82macro_rules! fp_from_bs58 {
83    ($ty:ident) => {
84        impl FromStr for $ty {
85            type Err = ContractError;
86
87            fn from_str(s: &str) -> Result<Self, Self::Err> {
88                let bytes = match bs58::decode(s).into_vec() {
89                    Ok(v) => v,
90                    Err(e) => return Err(ContractError::IoError(e.to_string())),
91                };
92
93                if bytes.len() != 32 {
94                    return Err(ContractError::IoError(
95                        "Length of decoded bytes is not 32".to_string(),
96                    ))
97                }
98
99                Self::from_bytes(bytes.try_into().unwrap())
100            }
101        }
102    };
103}
104
105#[macro_export]
106macro_rules! fp_to_bs58 {
107    ($ty:ident) => {
108        impl std::fmt::Display for $ty {
109            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
110                write!(f, "{}", bs58::encode(self.to_bytes()).into_string())
111            }
112        }
113    };
114}
115
116#[macro_export]
117macro_rules! ty_from_fp {
118    ($ty:ident) => {
119        impl From<pallas::Base> for $ty {
120            fn from(x: pallas::Base) -> Self {
121                Self(x)
122            }
123        }
124    };
125}