dusk_core/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7//! Types used for interacting with Dusk's transfer and stake contracts.
8
9#![no_std]
10#![deny(missing_docs)]
11#![deny(rustdoc::broken_intra_doc_links)]
12#![deny(clippy::pedantic)]
13#![allow(clippy::module_name_repetitions)]
14#![feature(cfg_eval)]
15#![allow(clippy::used_underscore_binding)]
16#![feature(const_fn_floating_point_arithmetic)]
17#![cfg_attr(not(target_family = "wasm"), deny(unused_crate_dependencies))]
18#![deny(unused_extern_crates)]
19
20extern crate alloc;
21
22pub mod abi;
23
24pub mod stake;
25pub mod transfer;
26
27mod error;
28pub use error::{Error, TxPreconditionError};
29
30mod dusk;
31pub use dusk::{dusk, from_dusk, Dusk, LUX};
32
33use blake2b_simd as _; // Required to satisfy unused_crate_dependencies
34
35// elliptic curve types
36pub use dusk_bls12_381::BlsScalar;
37pub use dusk_jubjub::{
38    JubJubAffine, JubJubExtended, JubJubScalar, GENERATOR_EXTENDED,
39    GENERATOR_NUMS_EXTENDED,
40};
41
42/// Signatures used in the Dusk protocol.
43pub mod signatures {
44    /// Types for the bls-signature scheme operating on the `bls12_381` curve.
45    pub mod bls {
46        pub use bls12_381_bls::{
47            Error, MultisigPublicKey, MultisigSignature, PublicKey, SecretKey,
48            Signature,
49        };
50    }
51
52    /// Types for the schnorr-signature scheme operating on the `jubjub` curve.
53    pub mod schnorr {
54        pub use jubjub_schnorr::{
55            PublicKey, SecretKey, Signature, SignatureDouble,
56        };
57    }
58}
59
60/// Types and traits to create plonk circuits and generate and verify plonk
61/// proofs.
62#[cfg(feature = "plonk")]
63pub mod plonk {
64    pub use dusk_plonk::prelude::{
65        Circuit, Compiler, Composer, Constraint, Error, Proof, Prover,
66        PublicParameters, Verifier, Witness, WitnessPoint,
67    };
68}
69
70/// Groth16 circuitry
71#[cfg(feature = "groth16")]
72pub mod groth16 {
73    pub use ark_bn254 as bn254;
74    pub use ark_groth16::{
75        data_structures, generator, prepare_verifying_key, prover, r1cs_to_qap,
76        verifier, Groth16, PreparedVerifyingKey, Proof, ProvingKey,
77        VerifyingKey,
78    };
79    pub use ark_relations as relations;
80    pub use ark_serialize as serialize;
81}
82
83#[inline]
84const fn reserved(b: u8) -> abi::ContractId {
85    let mut bytes = [0u8; abi::CONTRACT_ID_BYTES];
86    bytes[0] = b;
87    abi::ContractId::from_bytes(bytes)
88}
89
90use alloc::string::String;
91use alloc::vec::Vec;
92
93use dusk_bytes::{DeserializableSlice, Error as BytesError};
94
95/// Reads vector from a buffer.
96/// Resets buffer to a position after the bytes read.
97///
98/// # Errors
99/// When length or data could not be read.
100fn read_vec(buf: &mut &[u8]) -> Result<Vec<u8>, BytesError> {
101    let len = usize::try_from(u64::from_reader(buf)?)
102        .map_err(|_| BytesError::InvalidData)?;
103    if buf.len() < len {
104        return Err(BytesError::InvalidData);
105    }
106    let bytes = buf[..len].into();
107    *buf = &buf[len..];
108    Ok(bytes)
109}
110
111/// Reads string from a buffer.
112/// Resets buffer to a position after the bytes read.
113///
114/// # Errors
115/// When length or data could not be read.
116fn read_str(buf: &mut &[u8]) -> Result<String, BytesError> {
117    let len = usize::try_from(u64::from_reader(buf)?)
118        .map_err(|_| BytesError::InvalidData)?;
119    if buf.len() < len {
120        return Err(BytesError::InvalidData);
121    }
122    let str = String::from_utf8(buf[..len].into())
123        .map_err(|_| BytesError::InvalidData)?;
124    *buf = &buf[len..];
125    Ok(str)
126}
127
128/// Reads array from a buffer.
129/// Resets buffer to a position after the bytes read.
130///
131/// # Errors
132/// When length or data could not be read.
133fn read_arr<const N: usize>(buf: &mut &[u8]) -> Result<[u8; N], BytesError> {
134    if buf.len() < N {
135        return Err(BytesError::InvalidData);
136    }
137    let mut a = [0u8; N];
138    a.copy_from_slice(&buf[..N]);
139    *buf = &buf[N..];
140    Ok(a)
141}
142
143#[cfg(test)]
144mod tests {
145    // the `unused_crate_dependencies` lint complains for dev-dependencies that
146    // are only used in integration tests, so adding this work-around here
147    use serde_json as _;
148}