Skip to content

Commit a521912

Browse files
committed
implement missing traits for G1/G2/Fr to be used as contracttype
1 parent 7eb5bff commit a521912

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

soroban-sdk/src/bytes.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ macro_rules! impl_bytesn_repr {
143143
}
144144
}
145145

146-
impl IntoVal<Env, Val> for $elem {
147-
fn into_val(&self, e: &Env) -> Val {
148-
self.0.into_val(e)
149-
}
150-
}
151-
152146
impl TryFromVal<Env, Val> for $elem {
153147
type Error = ConversionError;
154148

@@ -158,6 +152,28 @@ macro_rules! impl_bytesn_repr {
158152
}
159153
}
160154

155+
impl TryFromVal<Env, $elem> for Val {
156+
type Error = ConversionError;
157+
158+
fn try_from_val(_env: &Env, elt: &$elem) -> Result<Self, Self::Error> {
159+
Ok(elt.to_val())
160+
}
161+
}
162+
163+
#[cfg(not(target_family = "wasm"))]
164+
impl From<&$elem> for ScVal {
165+
fn from(v: &$elem) -> Self {
166+
Self::from(&v.0)
167+
}
168+
}
169+
170+
#[cfg(not(target_family = "wasm"))]
171+
impl From<$elem> for ScVal {
172+
fn from(v: $elem) -> Self {
173+
(&v).into()
174+
}
175+
}
176+
161177
impl IntoVal<Env, BytesN<$size>> for $elem {
162178
fn into_val(&self, _e: &Env) -> BytesN<$size> {
163179
self.0.clone()

soroban-sdk/src/crypto/bls12_381.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use core::{
1010
ops::{Add, Mul, Neg, Sub},
1111
};
1212

13+
#[cfg(not(target_family = "wasm"))]
14+
use crate::xdr::ScVal;
15+
1316
/// Bls12_381 provides access to curve and field arithmetics on the BLS12-381
1417
/// curve.
1518
pub struct Bls12_381 {
@@ -227,6 +230,14 @@ impl G1Affine {
227230
}
228231
}
229232

233+
// impl TryFromVal<Env, Val> for G1Affine {
234+
// type Error = ConversionError;
235+
236+
// fn try_from_val(env: &Env, v: &Val) -> Result<Self, Self::Error> {
237+
// Ok(G1Affine(BytesN::try_from_val(env, v)?))
238+
// }
239+
// }
240+
230241
impl Add for G1Affine {
231242
type Output = G1Affine;
232243

soroban-sdk/src/testutils/arbitrary.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ mod objects {
320320

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

@@ -680,6 +681,64 @@ mod objects {
680681
Ok(sc_duration.into_val(env))
681682
}
682683
}
684+
685+
// For G1Affine (96 bytes)
686+
#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
687+
pub struct ArbitraryG1Affine {
688+
bytes: [u8; 96],
689+
}
690+
691+
impl SorobanArbitrary for G1Affine {
692+
type Prototype = ArbitraryG1Affine;
693+
}
694+
695+
impl TryFromVal<Env, ArbitraryG1Affine> for G1Affine {
696+
type Error = ConversionError;
697+
698+
fn try_from_val(env: &Env, v: &ArbitraryG1Affine) -> Result<Self, Self::Error> {
699+
let mut bytes = v.bytes;
700+
bytes[0] &= 0b0001_1111; // Clear the top 3 bits
701+
Ok(G1Affine::from_array(env, &bytes))
702+
}
703+
}
704+
705+
// For G2Affine (192 bytes)
706+
#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
707+
pub struct ArbitraryG2Affine {
708+
bytes: [u8; 192],
709+
}
710+
711+
impl SorobanArbitrary for G2Affine {
712+
type Prototype = ArbitraryG2Affine;
713+
}
714+
715+
impl TryFromVal<Env, ArbitraryG2Affine> for G2Affine {
716+
type Error = ConversionError;
717+
718+
fn try_from_val(env: &Env, v: &ArbitraryG2Affine) -> Result<Self, Self::Error> {
719+
let mut bytes = v.bytes;
720+
bytes[0] &= 0b0001_1111; // Clear the top 3 bits
721+
Ok(G2Affine::from_array(env, &bytes))
722+
}
723+
}
724+
725+
#[derive(Arbitrary, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
726+
pub struct ArbitraryFr {
727+
bytes: [u8; 32],
728+
}
729+
730+
impl SorobanArbitrary for Fr {
731+
type Prototype = ArbitraryFr;
732+
}
733+
734+
impl TryFromVal<Env, ArbitraryFr> for Fr {
735+
type Error = ConversionError;
736+
737+
fn try_from_val(env: &Env, v: &ArbitraryFr) -> Result<Self, Self::Error> {
738+
// Convert bytes to Fr via U256
739+
Ok(Fr::from_bytes(BytesN::from_array(env, &v.bytes)))
740+
}
741+
}
683742
}
684743

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

0 commit comments

Comments
 (0)