Skip to content

Commit

Permalink
implement bls12-381 functions (#1345)
Browse files Browse the repository at this point in the history
Corresponding env change:
stellar/rs-soroban-env#1456

---------

Co-authored-by: Siddharth Suresh <[email protected]>
Co-authored-by: Leigh McCulloch <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent abbba12 commit 9b7ac44
Show file tree
Hide file tree
Showing 8 changed files with 896 additions and 282 deletions.
524 changes: 245 additions & 279 deletions Cargo.lock

Large diffs are not rendered by default.

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.10.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)
}

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

0 comments on commit 9b7ac44

Please sign in to comment.