Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No_std support #782

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Build ergo-lib
uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features --manifest-path ergo-lib/Cargo.toml
command: rustc
args: --no-default-features --manifest-path ergo-lib/Cargo.toml --crate-type rlib

test:
name: Tests on ${{ matrix.os }}
Expand Down
65 changes: 37 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"sigma-test-util",
"sigma-ser",
Expand Down Expand Up @@ -26,21 +27,21 @@ edition = "2021"

[workspace.dependencies]
sigma-util = { version = "^0.18.0", path = "./sigma-util" }
sigma-ser = { version = "^0.19.0", path = "./sigma-ser" }
ergotree-ir = { version = "^0.28.0", path = "./ergotree-ir" }
ergo-chain-types = { version = "^0.15.0", path = "./ergo-chain-types" }
sigma-ser = { version = "^0.19.0", path = "./sigma-ser", default-features = false }
ergotree-ir = { version = "^0.28.0", path = "./ergotree-ir", default-features = false }
ergo-chain-types = { version = "^0.15.0", path = "./ergo-chain-types", default-features = false }
sigma-test-util = { version = "^0.3.0", path = "./sigma-test-util" }
ergoscript-compiler = { version = "^0.24.0", path = "./ergoscript-compiler" }
ergotree-interpreter = { version = "^0.28.0", path = "./ergotree-interpreter" }
ergotree-interpreter = { version = "^0.28.0", path = "./ergotree-interpreter", default-features = false }
ergo-nipopow = { version = "^0.15", path = "./ergo-nipopow" }
ergo-merkle-tree = { version = "^0.15.0", path = "./ergo-merkle-tree" }
ergo-merkle-tree = { version = "^0.15.0", path = "./ergo-merkle-tree", default-features = false }
ergo-rest = { version = "^0.13.0", path = "./ergo-rest" }
ergo-lib = { version = "^0.28.0", path = "./ergo-lib" }
k256 = { version = "0.13.1", features = ["arithmetic", "ecdsa"] }
k256 = { version = "0.13.1", default-features = false, features = [
"arithmetic",
"ecdsa",
] } # TODO: look into precomputed_tables feature for std, could be useful for speeding up signature verification
elliptic-curve = { version = "0.12", features = ["ff"] }
thiserror = "1"
bounded-vec = { version = "^0.7.0" }
bitvec = { version = "1.0.1" }
derive_more = { version = "0.99", features = [
"add",
"add_assign",
Expand All @@ -52,25 +53,30 @@ derive_more = { version = "0.99", features = [
"from_str",
"display",
] }
blake2 = "0.10"
sha2 = "0.10"
num-derive = "0.4.2"
num-traits = "0.2.14"
num-integer = "0.1.44"
num-bigint = "0.4.0"
lazy_static = "1.4"
bs58 = "0.4.0"
base16 = "0.2.1"
base64 = "0.13.0"
indexmap = { version = "1.3.2", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = [
thiserror = { version = "2.0.1", default-features = false }
bounded-vec = { git = "https://github.com/SethDusek/bounded-vec", branch = "no_std" } # TODO: merge no_std support in bounded-vec upstream
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
blake2 = { version = "0.10.6", default-features = false }
sha2 = { version = "0.10", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
num-integer = { version = "0.1.44", default-features = false }
num-bigint = { version = "0.4.0", default-features = false }
lazy_static = { version = "1.4", features = ["spin_no_std"] }
bs58 = { version = "0.4.0", default-features = false, features = ["alloc"] }
base16 = { version = "0.2.1", default-features = false, features = ["alloc"] }
base64 = { version = "0.13.0", default-features = false, features = ["alloc"] }
indexmap = { version = "2.6.0", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = [
"arbitrary_precision",
"preserve_order",
] }
serde_with = { version = "1.9.1", features = ["json"] }
serde_with = { version = "3.11.0", default-features = false, features = [
"json",
"macros",
] }
rand = "0.8.5"
bytes = "1.1"
bytes = { version = "1.1", default-features = false }
byteorder = "1"
futures = "0.3"
tokio = { version = "1.15.0", features = ["full"] }
Expand All @@ -79,16 +85,19 @@ tokio-util = { version = "0.6.9", features = ["codec"] }
bounded-integer = { version = "^0.5", features = ["types"] }
url = "~2.2"
getrandom = { version = "0.2.7" }
itertools = "0.10.3"
itertools = { version = "0.10.3", default-features = false }
miette = { version = "5", features = ["fancy"] }

hashbrown = { version = "0.14.3", features = ["serde"] }
core2 = { version = "0.4.0", default-features = false, features = ["alloc"] }
# dev-dependencies
proptest = { version = "=1.0", default-features = false, features = ["std"] }
proptest = { version = "1.5.0", default-features = false, features = [
"alloc",
"std",
] }
proptest-derive = "0.3"
pretty_assertions = "1.3"
wasm-bindgen-test = "0.3.37"
expect-test = "1.4.1"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "z"
Expand Down
5 changes: 3 additions & 2 deletions bindings/ergo-lib-c-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ futures-util = "0.3"
url = { workspace = true }
bounded-integer = { workspace = true }
serde_with = { workspace = true }
bounded-vec = { workspace = true, features=["serde"] }
bounded-vec = { workspace = true, features = ["serde"] }

[features]
default = ["mnemonic_gen", "ergo-lib/compiler"]
default = ["mnemonic_gen", "ergo-lib/compiler", "json"]
json = []
rest = ["tokio", "ergo-lib/rest"]
mnemonic_gen = ["ergo-lib/mnemonic_gen"]
7 changes: 2 additions & 5 deletions bindings/ergo-lib-c-core/src/rest/api/node/abortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ use crate::rest::api::runtime::RestApiRuntime;
use crate::Error;

/// Wraps a task with future::Abortable, spawns it on the provided runtime and returns task's abort handle
pub(crate) fn spawn_abortable<T: 'static>(
runtime: &RestApiRuntime,
task: T,
) -> Result<AbortHandle, Error>
pub(crate) fn spawn_abortable<T>(runtime: &RestApiRuntime, task: T) -> Result<AbortHandle, Error>
where
T: futures_util::Future<Output = ()> + Send,
T: futures_util::Future<Output = ()> + Send + 'static,
{
let (abort_handle, abort_registration) = AbortHandle::new_pair();
let future = Abortable::new(task, abort_registration);
Expand Down
4 changes: 2 additions & 2 deletions bindings/ergo-lib-c-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub unsafe fn hints_bag_get(
}

/// TransactionHintsBag
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);
pub type TransactionHintsBagPtr = *mut TransactionHintsBag;
pub type ConstTransactionHintsBagPtr = *const TransactionHintsBag;

Expand All @@ -90,7 +90,7 @@ pub unsafe fn transaction_hints_bag_empty(
let transaction_hints_bag_out =
mut_ptr_as_mut(transaction_hints_bag_out, "transaction_hints_bag_out")?;
*transaction_hints_bag_out = Box::into_raw(Box::new(TransactionHintsBag(
ergo_lib::wallet::multi_sig::TransactionHintsBag::empty(),
ergo_lib::wallet::TransactionHintsBag::empty(),
)));
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions bindings/ergo-lib-wasm/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ impl From<ergo_lib::ergotree_interpreter::sigma_protocol::prover::hint::HintsBag

/// TransactionHintsBag
#[wasm_bindgen]
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);

#[wasm_bindgen]
impl TransactionHintsBag {
/// Empty TransactionHintsBag
pub fn empty() -> TransactionHintsBag {
TransactionHintsBag(ergo_lib::wallet::multi_sig::TransactionHintsBag::empty())
TransactionHintsBag(ergo_lib::wallet::TransactionHintsBag::empty())
}

/// Adding hints for input
Expand All @@ -106,8 +106,8 @@ impl TransactionHintsBag {
}
}

impl From<ergo_lib::wallet::multi_sig::TransactionHintsBag> for TransactionHintsBag {
fn from(t: ergo_lib::wallet::multi_sig::TransactionHintsBag) -> Self {
impl From<ergo_lib::wallet::TransactionHintsBag> for TransactionHintsBag {
fn from(t: ergo_lib::wallet::TransactionHintsBag) -> Self {
TransactionHintsBag(t)
}
}
Expand Down
20 changes: 8 additions & 12 deletions ergo-chain-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ authors = ["Denys Zadorozhnyi <[email protected]>"]
repository.workspace = true
edition.workspace = true
description = "Ergo blockchain types"
exclude = [
"proptest-regressions/*"
]

[lib]
crate-type = ["cdylib", "rlib"]
exclude = ["proptest-regressions/*"]

[dependencies]
thiserror = { workspace = true }
Expand All @@ -22,19 +17,20 @@ base16 = { workspace = true }
base64 = { workspace = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
url = { workspace = true }
url = { workspace = true, optional = true }
k256 = { workspace = true }
elliptic-curve = { workspace = true }
num-bigint = { workspace = true }
byteorder = { workspace = true }
serde_with = { workspace = true, optional = true }
proptest = { workspace = true , optional = true }
proptest-derive = {workspace = true, optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }
num-traits = { workspace = true }
core2 = { workspace = true }

[features]
default = ["json"]
arbitrary = ["proptest", "proptest-derive"]
default = ["std", "json"]
arbitrary = ["proptest", "proptest-derive", "std"]
json = ["serde", "serde_json", "serde_with"]
std = ["dep:url", "base16/std", "base64/std", "serde/std"]

[dev-dependencies]
7 changes: 5 additions & 2 deletions ergo-chain-types/src/base16_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Transitioning type for Base16 encoded bytes in JSON serialization

use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::convert::TryInto;
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::convert::TryInto;
extern crate derive_more;
use derive_more::{From, Into};

Expand Down
1 change: 1 addition & 0 deletions ergo-chain-types/src/block_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Main "remote" type for [BlockId]()

use alloc::{string::String, vec::Vec};
use derive_more::Display;

use crate::DigestNError;
Expand Down
47 changes: 38 additions & 9 deletions ergo-chain-types/src/digest32.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//! Digest types for various sizes

use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::convert::TryInto;
use core::fmt::Formatter;
use sigma_ser::vlq_encode::ReadSigmaVlqExt;
use sigma_ser::vlq_encode::WriteSigmaVlqExt;
use sigma_ser::ScorexParsingError;
use sigma_ser::ScorexSerializable;
use sigma_ser::ScorexSerializeResult;
use sigma_util::AsVecI8;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fmt::Formatter;
use thiserror::Error;

/// N-bytes array in a box. `Digest32` is most type synonym.
Expand Down Expand Up @@ -47,14 +50,14 @@ impl<const N: usize> Digest<N> {
}
}

impl<const N: usize> std::fmt::Debug for Digest<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl<const N: usize> core::fmt::Debug for Digest<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
base16::encode_lower(&(self.0)).fmt(f)
}
}

impl<const N: usize> std::fmt::Display for Digest<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
impl<const N: usize> core::fmt::Display for Digest<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
base16::encode_lower(&(self.0)).fmt(f)
}
}
Expand Down Expand Up @@ -149,14 +152,40 @@ impl AsRef<[u8]> for Digest32 {
#[derive(Error, Debug)]
pub enum DigestNError {
/// error decoding from Base16
#[cfg(feature = "std")]
#[error("error decoding from Base16: {0}")]
Base16DecodingError(#[from] base16::DecodeError),
/// error decoding from Base16
#[cfg(not(feature = "std"))]
#[error("error decoding from Base16")]
Base16DecodingError,
/// Invalid byte array size
#[error("Invalid byte array size ({0})")]
InvalidSize(#[from] std::array::TryFromSliceError),
InvalidSize(#[from] core::array::TryFromSliceError),
/// error decoding from Base64
#[cfg(feature = "std")]
#[error("error decoding from Base64: {0}")]
Base64DecodingError(#[from] base64::DecodeError),

/// error decoding from Base64
#[cfg(not(feature = "std"))]
#[error("error decoding from Base64")]
Base64DecodingError,
}

/// both base16 and base64 don't implement core::error::Error for their error types yet, so we can't use them in thiserror in no_std contexts
#[cfg(not(feature = "std"))]
impl From<base16::DecodeError> for DigestNError {
fn from(_: base16::DecodeError) -> Self {
Self::Base16DecodingError
}
}

#[cfg(not(feature = "std"))]
impl From<base64::DecodeError> for DigestNError {
fn from(_: base64::DecodeError) -> Self {
Self::Base64DecodingError
}
}

/// Arbitrary
Expand All @@ -165,9 +194,9 @@ pub enum DigestNError {
pub(crate) mod arbitrary {

use super::Digest;
use core::convert::TryInto;
use proptest::prelude::{Arbitrary, BoxedStrategy};
use proptest::{collection::vec, prelude::*};
use std::convert::TryInto;

impl<const N: usize> Arbitrary for Digest<N> {
type Parameters = ();
Expand Down
14 changes: 8 additions & 6 deletions ergo-chain-types/src/ec_point.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Elliptic curve point.

use alloc::string::String;
use core::convert::TryFrom;
use core::ops::{Add, Mul, Neg};
use derive_more::{From, Into};
use k256::elliptic_curve::group::prime::PrimeCurveAffine;
use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::{ProjectivePoint, PublicKey, Scalar};
use sigma_ser::vlq_encode::{ReadSigmaVlqExt, WriteSigmaVlqExt};
use sigma_ser::{ScorexParsingError, ScorexSerializable, ScorexSerializeResult};
use std::convert::TryFrom;
use std::ops::{Add, Mul, Neg};

/// Elliptic curve point
#[derive(PartialEq, Clone, Default, From, Into)]
Expand All @@ -19,8 +20,8 @@ use std::ops::{Add, Mul, Neg};
pub struct EcPoint(ProjectivePoint);

#[allow(clippy::unwrap_used)]
impl std::fmt::Debug for EcPoint {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl core::fmt::Debug for EcPoint {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("EC:")?;
f.write_str(&base16::encode_lower(
&self.scorex_serialize_bytes().unwrap(),
Expand All @@ -29,8 +30,8 @@ impl std::fmt::Debug for EcPoint {
}

#[allow(clippy::unwrap_used)]
impl std::fmt::Display for EcPoint {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl core::fmt::Display for EcPoint {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str(&base16::encode_lower(
&self.scorex_serialize_bytes().unwrap(),
))
Expand Down Expand Up @@ -166,6 +167,7 @@ mod arbitrary {

#[allow(clippy::unwrap_used)]
#[cfg(test)]
#[cfg(feature = "arbitrary")]
#[allow(clippy::panic)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions ergo-chain-types/src/extensioncandidate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

/// Extension section of Ergo block. Contains key-value storage.
#[derive(Clone, Debug, Default)]
pub struct ExtensionCandidate {
Expand Down
Loading
Loading