Skip to content

Implement BLS12-381 contracttype support #1449

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

Merged
merged 14 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 22 additions & 6 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ macro_rules! impl_bytesn_repr {
}
}

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;

Expand All @@ -158,6 +152,28 @@ macro_rules! impl_bytesn_repr {
}
}

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

fn try_from_val(_env: &Env, elt: &$elem) -> Result<Self, Self::Error> {
Ok(elt.to_val())
}
}

#[cfg(not(target_family = "wasm"))]
impl From<&$elem> for ScVal {
fn from(v: &$elem) -> Self {
Self::from(&v.0)
}
}

#[cfg(not(target_family = "wasm"))]
impl From<$elem> for ScVal {
fn from(v: $elem) -> Self {
(&v).into()
}
}

impl IntoVal<Env, BytesN<$size>> for $elem {
fn into_val(&self, _e: &Env) -> BytesN<$size> {
self.0.clone()
Expand Down
2 changes: 2 additions & 0 deletions soroban-sdk/src/crypto/bls12_381.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(not(target_family = "wasm"))]
use crate::xdr::ScVal;
use crate::{
env::internal::{self, BytesObject, U256Val, U64Val},
impl_bytesn_repr,
Expand Down
89 changes: 89 additions & 0 deletions soroban-sdk/src/testutils/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ mod objects {

use crate::xdr::{Int256Parts, ScVal, UInt256Parts};
use crate::{
crypto::bls12_381::{Fr, G1Affine, G2Affine},
Address, Bytes, BytesN, Duration, Map, String, Symbol, Timepoint, Val, Vec, I256, U256,
};

Expand Down Expand Up @@ -680,6 +681,94 @@ mod objects {
Ok(sc_duration.into_val(env))
}
}

// For G1Affine (96 bytes)
#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct ArbitraryG1Affine {
bytes: [u8; 96],
}

impl SorobanArbitrary for G1Affine {
type Prototype = ArbitraryG1Affine;
}

impl TryFromVal<Env, ArbitraryG1Affine> for G1Affine {
type Error = ConversionError;

fn try_from_val(env: &Env, v: &ArbitraryG1Affine) -> Result<Self, Self::Error> {
let mut bytes = v.bytes;
// the top 3 bits in a G1 point are reserved for flags:
// compression_flag (bit 0), infinity_flag (bit 1) and sort_flag
// (bit 2). Only infinity_flag is possible to be set, in which case
// the rest of the bytes must be zeros. The host will reject any
// invalid input. Manually taking care of the flag bits here to give
// it better chance of being a valid input.
const INFINITY_FLAG: u8 = 0b0100_0000;
const FLAG_MASK: u8 = 0b1110_0000;
if (bytes[0] & INFINITY_FLAG) != 0 {
// infinity flag set, clear rest of bits
bytes = [0; 96];
bytes[0] = INFINITY_FLAG;
} else {
// not an infinity point, we clear the flag bits
bytes[0] &= !FLAG_MASK
}
Ok(G1Affine::from_array(env, &bytes))
}
}

// For G2Affine (192 bytes)
#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct ArbitraryG2Affine {
bytes: [u8; 192],
}

impl SorobanArbitrary for G2Affine {
type Prototype = ArbitraryG2Affine;
}

impl TryFromVal<Env, ArbitraryG2Affine> for G2Affine {
type Error = ConversionError;

fn try_from_val(env: &Env, v: &ArbitraryG2Affine) -> Result<Self, Self::Error> {
let mut bytes = v.bytes;
// the top 3 bits in a G1 point are reserved for flags:
// compression_flag (bit 0), infinity_flag (bit 1) and sort_flag
// (bit 2). Only infinity_flag is possible to be set, in which case
// the rest of the bytes must be zeros. The host will reject any
// invalid input. Manually taking care of the flag bits here to give
// it better chance of being a valid input.
const INFINITY_FLAG: u8 = 0b0100_0000;
const FLAG_MASK: u8 = 0b1110_0000;
if (bytes[0] & INFINITY_FLAG) != 0 {
// infinity flag set, clear rest of bits
bytes = [0; 192];
bytes[0] = INFINITY_FLAG;
} else {
// not an infinity point, we clear the flag bits
bytes[0] &= !FLAG_MASK
}
Ok(G2Affine::from_array(env, &bytes))
}
}

#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct ArbitraryFr {
bytes: [u8; 32],
}

impl SorobanArbitrary for Fr {
type Prototype = ArbitraryFr;
}

impl TryFromVal<Env, ArbitraryFr> for Fr {
type Error = ConversionError;

fn try_from_val(env: &Env, v: &ArbitraryFr) -> Result<Self, Self::Error> {
// Convert bytes to Fr via U256
Ok(Fr::from_bytes(BytesN::from_array(env, &v.bytes)))
}
}
}

/// Implementations of `soroban_sdk::testutils::arbitrary::api` for tuples of Soroban types.
Expand Down