dusk_core/
abi.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//! The Application Binary Interface (ABI) for the Dusk network.
8
9pub use piecrust_uplink::{
10    ContractError, ContractId, Event, StandardBufSerializer, ARGBUF_LEN,
11    CONTRACT_ID_BYTES,
12};
13
14#[cfg(feature = "abi")]
15pub use self::host_queries::*;
16
17/// Enum storing the metadata identifiers.
18pub enum Metadata {}
19
20impl Metadata {
21    /// The chain-id of the network.
22    pub const CHAIN_ID: &'static str = "chain_id";
23    /// The current block-height.
24    pub const BLOCK_HEIGHT: &'static str = "block_height";
25    /// The sender of the transaction, if the transaction is public.
26    pub const PUBLIC_SENDER: &'static str = "public_sender";
27}
28
29/// Enum storing the available host-queries.
30pub enum Query {}
31
32impl Query {
33    /// Host-function name to compute the hash of some input-data.
34    pub const HASH: &'static str = "hash";
35    /// Host-function name to compute the poseidon-hash of some input-data.
36    pub const POSEIDON_HASH: &'static str = "poseidon_hash";
37    /// Host-function name to verify a plonk-proof.
38    pub const VERIFY_PLONK: &'static str = "verify_plonk";
39    /// Host-function name to verify a groth16-bn254 proof.
40    pub const VERIFY_GROTH16_BN254: &'static str = "verify_groth16_bn254";
41    /// Host-function name to verify a schnorr-signature.
42    pub const VERIFY_SCHNORR: &'static str = "verify_schnorr";
43    /// Host-function name to verify a bls-signature.
44    pub const VERIFY_BLS: &'static str = "verify_bls";
45    /// Host-function name to verify a bls-multisig.
46    pub const VERIFY_BLS_MULTISIG: &'static str = "verify_bls_multisig";
47}
48
49#[cfg(feature = "abi")]
50pub(crate) mod host_queries {
51    #[cfg(feature = "abi-debug")]
52    pub use piecrust_uplink::debug as piecrust_debug;
53    pub use piecrust_uplink::{
54        call, call_raw, call_raw_with_limit, call_with_limit, caller,
55        callstack, emit, emit_raw, feed, limit, self_id, spent, wrap_call,
56        wrap_call_unchecked, /* maybe use for our Transaction in
57                              * spend_and_execute */
58    };
59
60    use alloc::vec::Vec;
61
62    use dusk_bytes::Serializable;
63    use piecrust_uplink::{host_query, meta_data};
64
65    use crate::abi::{ContractId, Metadata, Query};
66    use crate::signatures::bls::{
67        MultisigSignature, PublicKey as BlsPublicKey, Signature as BlsSignature,
68    };
69    use crate::signatures::schnorr::{
70        PublicKey as SchnorrPublicKey, Signature as SchnorrSignature,
71    };
72    use crate::BlsScalar;
73
74    /// Compute the blake2b hash of the given bytes, returning the resulting
75    /// scalar. The output of the hasher is truncated (last nibble) to fit
76    /// onto a scalar.
77    #[must_use]
78    pub fn hash(bytes: Vec<u8>) -> BlsScalar {
79        host_query(Query::HASH, bytes)
80    }
81
82    /// Compute the poseidon hash of the given scalars
83    #[must_use]
84    pub fn poseidon_hash(scalars: Vec<BlsScalar>) -> BlsScalar {
85        host_query(Query::POSEIDON_HASH, scalars)
86    }
87
88    /// Verify that a Groth16 proof in the BN254 pairing is valid for a given
89    /// circuit and inputs.
90    ///
91    /// `proof` and `inputs` should be in compressed form, while `pvk`
92    /// uncompressed.
93    #[must_use]
94    pub fn verify_groth16_bn254(
95        pvk: Vec<u8>,
96        proof: Vec<u8>,
97        inputs: Vec<u8>,
98    ) -> bool {
99        host_query(Query::VERIFY_GROTH16_BN254, (pvk, proof, inputs))
100    }
101
102    /// Verify a Plonk proof is valid for a given circuit type and public inputs
103    #[must_use]
104    pub fn verify_plonk(
105        verifier_data: Vec<u8>,
106        proof: Vec<u8>,
107        public_inputs: Vec<BlsScalar>,
108    ) -> bool {
109        host_query(Query::VERIFY_PLONK, (verifier_data, proof, public_inputs))
110    }
111
112    /// Verify a schnorr signature is valid for the given public key and message
113    #[must_use]
114    pub fn verify_schnorr(
115        msg: BlsScalar,
116        pk: SchnorrPublicKey,
117        sig: SchnorrSignature,
118    ) -> bool {
119        host_query(Query::VERIFY_SCHNORR, (msg, pk, sig))
120    }
121
122    /// Verify a BLS signature is valid for the given public key and message
123    #[must_use]
124    pub fn verify_bls(
125        msg: Vec<u8>,
126        pk: BlsPublicKey,
127        sig: BlsSignature,
128    ) -> bool {
129        host_query(Query::VERIFY_BLS, (msg, pk, sig))
130    }
131
132    /// Verify a BLS signature is valid for the given public key and message
133    #[must_use]
134    pub fn verify_bls_multisig(
135        msg: Vec<u8>,
136        keys: Vec<BlsPublicKey>,
137        sig: MultisigSignature,
138    ) -> bool {
139        host_query(Query::VERIFY_BLS_MULTISIG, (msg, keys, sig))
140    }
141
142    /// Get the chain ID.
143    ///
144    /// # Panics
145    /// Panics if the chain doesn't store a `u8` `CHAIN_ID` in the metadata.
146    #[must_use]
147    pub fn chain_id() -> u8 {
148        meta_data(Metadata::CHAIN_ID).unwrap()
149    }
150
151    /// Get the current block height.
152    ///
153    /// # Panics
154    /// Panics if the chain doesn't store a `u64` `BLOCK_HEIGHT` in the
155    /// metadata.
156    #[must_use]
157    pub fn block_height() -> u64 {
158        meta_data(Metadata::BLOCK_HEIGHT).unwrap()
159    }
160
161    /// Get the public sender of the ongoing tx. Returns `None` if the
162    /// transaction is shielded.
163    ///
164    /// # Panics
165    /// Panics if the chain doesn't store a `Option<BlsPublicKey>`
166    /// `PUBLIC_SENDER` in the metadata.
167    #[must_use]
168    pub fn public_sender() -> Option<BlsPublicKey> {
169        meta_data(Metadata::PUBLIC_SENDER)
170            .expect("moonlight sender metadata to be set")
171    }
172
173    /// Query owner of a given contract.
174    /// Returns none if contract is not found.
175    ///
176    /// # Panics
177    /// Panics if owner is not a valid public key (should never happen).
178    #[must_use]
179    pub fn owner(contract: ContractId) -> Option<BlsPublicKey> {
180        owner_raw(contract).map(|buf| {
181            BlsPublicKey::from_bytes(&buf)
182                .expect("Owner should deserialize correctly")
183        })
184    }
185
186    /// Query self owner of a given contract.
187    ///
188    /// # Panics
189    /// Panics if owner is not a valid public key (should never happen).
190    #[must_use]
191    pub fn self_owner() -> BlsPublicKey {
192        let buf = self_owner_raw();
193        BlsPublicKey::from_bytes(&buf)
194            .expect("Owner should deserialize correctly")
195    }
196
197    /// Query raw `to_bytes` serialization of the owner of a given contract.
198    #[must_use]
199    pub fn owner_raw(contract: ContractId) -> Option<[u8; BlsPublicKey::SIZE]> {
200        piecrust_uplink::owner(contract)
201    }
202
203    /// Query raw `to_bytes` serialization of the self owner.
204    #[must_use]
205    pub fn self_owner_raw() -> [u8; BlsPublicKey::SIZE] {
206        piecrust_uplink::self_owner()
207    }
208}