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