Skip to content

Commit

Permalink
Merge pull request #77 from dusk-network/piecrust-0.6
Browse files Browse the repository at this point in the history
Port the crate to the `piecrust`-enabled ABI
  • Loading branch information
Eduardo Leegwater Simões authored Jul 4, 2023
2 parents 6d67f2b + a04d877 commit de60174
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 548 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change transactions to have an optional crossover
- Update STCT to use Schnorr signatures [#34]
- Update dependencies
- Update `phoenix-core` to `0.17`
- Update `dusk-pki` to `0.11`
- Update `dusk-schnorr` to `0.11`
- Update `dusk-jubjub` to `0.12`
- Update `dusk-poseidon` to `0.26`
- Update `dusk-plonk` to `0.12`
- Update `dusk-bls12_381-sign` to `0.4`

### Fixed

Expand Down
26 changes: 11 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusk-wallet-core"
version = "0.15.0-rc.0"
version = "0.19.0-piecrust.0.6"
edition = "2021"
description = "The core functionality of the Dusk wallet"
license = "MPL-2.0"
Expand All @@ -9,24 +9,20 @@ license = "MPL-2.0"
rand_core = "^0.6"
rand_chacha = { version = "^0.3", default-features = false }
sha2 = { version = "^0.10", default-features = false }
phoenix-core = { version = "0.16.0-rc", features = ["canon"] }
dusk-pki = { version = "0.10.0-rc", features = ["canon"] }
phoenix-core = { version = "0.18", default-features = false, features = ["alloc", "rkyv-impl"] }
dusk-pki = { version = "0.11", default-features = false }
dusk-bytes = "^0.1"
dusk-schnorr = { version = "0.10.0-rc", default-features = false }
dusk-jubjub = { version = "0.11", default-features = false }
dusk-poseidon = { version = "0.25.0-rc", features = [
"canon",
], default-features = false }
dusk-plonk = { version = "0.10", default-features = false }
rusk-abi = "0.7"
canonical = "0.7"
dusk-bls12_381-sign = { version = "0.3.0-rc", default-features = false, features = [
"canon",
] }
dusk-schnorr = { version = "0.12", default-features = false }
dusk-jubjub = { version = "0.12", default-features = false }
dusk-poseidon = { version = "0.29.1-rc.0", default-features = false }
dusk-merkle = { version = "0.4.1-rc.0", features = ["poseidon", "rkyv-impl"] }
dusk-plonk = { version = "0.13", default-features = false }
rusk-abi = { version = "0.9.0-piecrust.0.6", default-features = false }
dusk-bls12_381-sign = { version = "0.4", default-features = false }
rkyv = { version = "0.7", default-features = false }

[dev-dependencies]
rand = "^0.8"
canonical_derive = "0.7"

[lib]
crate-type = ["cdylib", "rlib"]
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2022-08-28
nightly-2023-01-05
43 changes: 28 additions & 15 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

//! The foreign function interface for the wallet.
use alloc::string::String;
use alloc::vec::Vec;

use core::mem;
use core::num::NonZeroU32;
use core::ptr;

use canonical::{Canon, Source};
use dusk_bls12_381_sign::PublicKey;
use dusk_bytes::Write;
use dusk_bytes::{DeserializableSlice, Serializable};
use dusk_jubjub::{BlsScalar, JubJubAffine, JubJubScalar};
use dusk_merkle::poseidon::Opening as PoseidonOpening;
use dusk_pki::{PublicSpendKey, ViewKey};
use dusk_plonk::prelude::Proof;
use dusk_poseidon::tree::PoseidonBranch;
use dusk_schnorr::Signature;
use phoenix_core::{Crossover, Fee, Note};
use rand_core::{
Expand Down Expand Up @@ -155,18 +155,27 @@ pub unsafe extern "C" fn public_spend_key(
#[no_mangle]
pub unsafe extern "C" fn execute(
contract_id: *const [u8; 32],
call_name_ptr: *mut u8,
call_name_len: *const u32,
call_data_ptr: *mut u8,
call_data_len: *const u32,
sender_index: *const u64,
refund: *const [u8; PublicSpendKey::SIZE],
gas_limit: *const u64,
gas_price: *const u64,
) -> u8 {
let contract_id = ContractId::from(*contract_id);
let contract_id = ContractId::from_bytes(*contract_id);

// SAFETY: the buffer is expected to have been allocated with the
// SAFETY: these buffers are expected to have been allocated with the
// correct size. If this is not the case problems with the allocator
// *may* happen.
let call_name = Vec::from_raw_parts(
call_name_ptr,
call_name_len as usize,
call_name_len as usize,
);
let call_name = unwrap_or_bail!(String::from_utf8(call_name));

let call_data = Vec::from_raw_parts(
call_data_ptr,
call_data_len as usize,
Expand All @@ -178,6 +187,7 @@ pub unsafe extern "C" fn execute(
unwrap_or_bail!(WALLET.execute(
&mut FfiRng,
contract_id,
call_name,
call_data,
*sender_index,
&refund,
Expand Down Expand Up @@ -332,9 +342,6 @@ impl Store for FfiStore {
}
}

// 512 KB for a buffer.
const OPENING_BUF_SIZE: usize = 0x10000;

const STCT_INPUT_SIZE: usize = Fee::SIZE
+ Crossover::SIZE
+ u64::SIZE
Expand Down Expand Up @@ -459,23 +466,28 @@ impl StateClient for FfiStateClient {
fn fetch_opening(
&self,
note: &Note,
) -> Result<PoseidonBranch<POSEIDON_TREE_DEPTH>, Self::Error> {
let mut opening_buf = [0u8; OPENING_BUF_SIZE];
) -> Result<PoseidonOpening<(), POSEIDON_TREE_DEPTH, 4>, Self::Error> {
const OPENING_BUF_SIZE: usize = 3000;

let mut opening_buf = Vec::with_capacity(OPENING_BUF_SIZE);
let mut opening_len = 0;

let note = note.to_bytes();
unsafe {
let r = fetch_opening(&note, &mut opening_buf[0], &mut opening_len);
let r = fetch_opening(
&note,
opening_buf.as_mut_ptr(),
&mut opening_len,
);
if r != 0 {
return Err(r);
}
}

let mut source = Source::new(&opening_buf[..opening_len as usize]);
let branch = PoseidonBranch::decode(&mut source).map_err(
Error::<FfiStore, FfiStateClient, FfiProverClient>::from,
)?;
let branch = rkyv::from_bytes(&opening_buf[..opening_len as usize])
.map_err(
Error::<FfiStore, FfiStateClient, FfiProverClient>::from,
)?;

Ok(branch)
}
Expand Down Expand Up @@ -666,11 +678,12 @@ impl<S: Store, SC: StateClient, PC: ProverClient> From<Error<S, SC, PC>>
Error::Prover(_) => 251,
Error::NotEnoughBalance => 250,
Error::NoteCombinationProblem => 249,
Error::Canon(_) => 248,
Error::Rkyv => 248,
Error::Phoenix(_) => 247,
Error::AlreadyStaked { .. } => 246,
Error::NotStaked { .. } => 245,
Error::NoReward { .. } => 244,
Error::Utf8(_) => 243,
}
}
}
Loading

0 comments on commit de60174

Please sign in to comment.