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
33// elliptic curve types
34pub use dusk_bls12_381::BlsScalar;
35pub use dusk_jubjub::{
36    JubJubAffine, JubJubExtended, JubJubScalar, GENERATOR_EXTENDED,
37    GENERATOR_NUMS_EXTENDED,
38};
39
40/// Signatures used in the Dusk protocol.
41pub mod signatures {
42    /// Types for the bls-signature scheme operating on the `bls12_381` curve.
43    pub mod bls {
44        pub use bls12_381_bls::{
45            Error, MultisigPublicKey, MultisigSignature, PublicKey, SecretKey,
46            Signature,
47        };
48    }
49
50    /// Types for the schnorr-signature scheme operating on the `jubjub` curve.
51    pub mod schnorr {
52        pub use jubjub_schnorr::{
53            PublicKey, SecretKey, Signature, SignatureDouble,
54        };
55    }
56}
57
58/// Types and traits to create plonk circuits and generate and verify plonk
59/// proofs.
60#[cfg(feature = "plonk")]
61pub mod plonk {
62    pub use dusk_plonk::prelude::{
63        Circuit, Compiler, Composer, Constraint, Error, Proof, Prover,
64        PublicParameters, Verifier, Witness, WitnessPoint,
65    };
66}
67
68/// Groth16 circuitry
69#[cfg(feature = "groth16")]
70pub mod groth16 {
71    pub use ark_bn254 as bn254;
72    pub use ark_groth16::{
73        data_structures, generator, prepare_verifying_key, prover, r1cs_to_qap,
74        verifier, Groth16, PreparedVerifyingKey, Proof, ProvingKey,
75        VerifyingKey,
76    };
77    pub use ark_relations as relations;
78    pub use ark_serialize as serialize;
79}
80
81#[inline]
82const fn reserved(b: u8) -> abi::ContractId {
83    let mut bytes = [0u8; abi::CONTRACT_ID_BYTES];
84    bytes[0] = b;
85    abi::ContractId::from_bytes(bytes)
86}
87
88use alloc::string::String;
89use alloc::vec::Vec;
90
91use dusk_bytes::{DeserializableSlice, Error as BytesError};
92
93/// Reads vector from a buffer.
94/// Resets buffer to a position after the bytes read.
95///
96/// # Errors
97/// When length or data could not be read.
98fn read_vec(buf: &mut &[u8]) -> Result<Vec<u8>, BytesError> {
99    let len = usize::try_from(u64::from_reader(buf)?)
100        .map_err(|_| BytesError::InvalidData)?;
101    if buf.len() < len {
102        return Err(BytesError::InvalidData);
103    }
104    let bytes = buf[..len].into();
105    *buf = &buf[len..];
106    Ok(bytes)
107}
108
109/// Reads string from a buffer.
110/// Resets buffer to a position after the bytes read.
111///
112/// # Errors
113/// When length or data could not be read.
114fn read_str(buf: &mut &[u8]) -> Result<String, BytesError> {
115    let len = usize::try_from(u64::from_reader(buf)?)
116        .map_err(|_| BytesError::InvalidData)?;
117    if buf.len() < len {
118        return Err(BytesError::InvalidData);
119    }
120    let str = String::from_utf8(buf[..len].into())
121        .map_err(|_| BytesError::InvalidData)?;
122    *buf = &buf[len..];
123    Ok(str)
124}
125
126/// Reads array from a buffer.
127/// Resets buffer to a position after the bytes read.
128///
129/// # Errors
130/// When length or data could not be read.
131fn read_arr<const N: usize>(buf: &mut &[u8]) -> Result<[u8; N], BytesError> {
132    if buf.len() < N {
133        return Err(BytesError::InvalidData);
134    }
135    let mut a = [0u8; N];
136    a.copy_from_slice(&buf[..N]);
137    *buf = &buf[N..];
138    Ok(a)
139}
140
141#[cfg(test)]
142mod tests {
143    // the `unused_crate_dependencies` lint complains for dev-dependencies that
144    // are only used in integration tests, so adding this work-around here
145    use serde_json as _;
146}