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