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

implement bls12-381 functions #1345

Merged
merged 20 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
782 changes: 424 additions & 358 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ soroban-token-sdk = { version = "22.0.0-rc.1", path = "soroban-token-sdk" }
[workspace.dependencies.soroban-env-common]
version = "=22.0.0"
git = "https://github.com/stellar/rs-soroban-env"
rev = "75b782119942a4c8be8003f2901db38b30b6db2d"
rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3"

[workspace.dependencies.soroban-env-guest]
version = "=22.0.0"
git = "https://github.com/stellar/rs-soroban-env"
rev = "75b782119942a4c8be8003f2901db38b30b6db2d"
rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3"

[workspace.dependencies.soroban-env-host]
version = "=22.0.0"
git = "https://github.com/stellar/rs-soroban-env"
rev = "75b782119942a4c8be8003f2901db38b30b6db2d"
rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3"

[workspace.dependencies.stellar-strkey]
version = "=0.0.8"
version = "=0.0.9"

[workspace.dependencies.stellar-xdr]
version = "=22.0.0"
default-features = false
features = ["curr"]
git = "https://github.com/stellar/rs-stellar-xdr"
rev = "39d7dbb0c12bd422ee43a6e2e3277789da4eaac8"
rev = "b5516843b6379e4e29520bf2ba156484f62edc46"

#[patch."https://github.com/stellar/rs-soroban-env"]
#soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" }
Expand Down
2 changes: 2 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ deny = [
# Certain crates/versions that will be skipped when doing duplicate detection.
skip = [
#{ name = "ansi_term", version = "=0.11.0" },
{ name = "hashbrown", version = "=0.14.5" },
sisuresh marked this conversation as resolved.
Show resolved Hide resolved
{ name = "syn", version = "=2.0.77" },
]
# Similarly to `skip` allows you to skip certain crates during duplicate
# detection. Unlike skip, it also includes the entire tree of transitive
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ stellar-xdr = { workspace = true, features = ["curr", "std"] }
syn = {version="2.0",features=["full"]}
quote = "1.0"
proc-macro2 = "1.0"
itertools = "0.11.0"
itertools = "0.10.5"
darling = "0.20.0"
sha2 = "0.10.7"

Expand Down
96 changes: 96 additions & 0 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,102 @@ macro_rules! bytesn {
};
}

#[macro_export]
macro_rules! impl_bytesn_repr {
($elem: ident, $size: literal) => {
impl $elem {
pub fn from_bytes(bytes: BytesN<$size>) -> Self {
Self(bytes)
}
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved

pub fn to_bytes(&self) -> BytesN<$size> {
self.0.clone()
}

pub fn as_bytes(&self) -> &BytesN<$size> {
&self.0
}

pub fn to_array(&self) -> [u8; $size] {
self.0.to_array()
}

pub fn from_array(&self, env: &Env, array: &[u8; $size]) -> Self {
Self(<BytesN<$size>>::from_array(env, array))
}

pub fn as_val(&self) -> &Val {
self.0.as_val()
}

pub fn to_val(&self) -> Val {
self.0.to_val()
}

pub fn as_object(&self) -> &BytesObject {
self.0.as_object()
}

pub fn to_object(&self) -> BytesObject {
self.0.to_object()
}
}

impl IntoVal<Env, Val> for $elem {
fn into_val(&self, e: &Env) -> Val {
self.0.into_val(e)
}
}

impl TryFromVal<Env, Val> for $elem {
type Error = ConversionError;

fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error> {
let bytes = <BytesN<$size>>::try_from_val(env, val)?;
Ok($elem(bytes))
}
}

impl IntoVal<Env, BytesN<$size>> for $elem {
fn into_val(&self, _e: &Env) -> BytesN<$size> {
self.0.clone()
}
}

impl From<$elem> for Bytes {
fn from(v: $elem) -> Self {
v.0.into()
}
}

impl From<$elem> for BytesN<$size> {
fn from(v: $elem) -> Self {
v.0
}
}

impl Into<[u8; $size]> for $elem {
fn into(self) -> [u8; $size] {
self.0.into()
}
}

impl Eq for $elem {}

impl PartialEq for $elem {
fn eq(&self, other: &Self) -> bool {
self.0.partial_cmp(other.as_bytes()) == Some(Ordering::Equal)
}
}

impl Debug for $elem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}({:?})", stringify!($elem), self.to_array())
}
}
};
}

/// Bytes is a contiguous growable array type containing `u8`s.
///
/// The array is stored in the Host and available to the Guest through the
Expand Down
11 changes: 9 additions & 2 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Crypto contains functions for cryptographic functions.

use crate::{
env::internal, env::internal::BytesObject, unwrap::UnwrapInfallible, Bytes, BytesN,
ConversionError, Env, IntoVal, TryFromVal, Val,
env::internal::{self, BytesObject},
unwrap::UnwrapInfallible,
Bytes, BytesN, ConversionError, Env, IntoVal, TryFromVal, Val,
};

pub mod bls12_381;
/// A BytesN<N> generated by a cryptographic hash function.
///
/// The `Hash<N>` type contains a `BytesN<N>` and can only be constructed in
Expand Down Expand Up @@ -174,6 +176,11 @@ impl Crypto {
let env = self.env();
CryptoHazmat::new(env).secp256r1_verify(public_key, &message_digest.0, signature)
}

/// Get a [Bls12_381] for accessing the bls12-381 functions.
pub fn bls12_381(&self) -> bls12_381::Bls12_381 {
bls12_381::Bls12_381::new(self.env())
}
}

/// # ⚠️ Hazardous Materials
Expand Down
Loading
Loading