Skip to content

Commit 184c1e3

Browse files
authored
Update bn254 typenames and add mapping (#1645)
### What Update typenames and add the contracttype mapping.
1 parent 8a41795 commit 184c1e3

File tree

10 files changed

+299
-227
lines changed

10 files changed

+299
-227
lines changed

soroban-sdk-macros/src/map_type.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ pub const FP2_SERIALIZED_SIZE: u32 = FP_SERIALIZED_SIZE * 2;
1616
pub const G1_SERIALIZED_SIZE: u32 = FP_SERIALIZED_SIZE * 2;
1717
pub const G2_SERIALIZED_SIZE: u32 = FP2_SERIALIZED_SIZE * 2;
1818

19+
// BN254 constants - values must match soroban_sdk::crypto::bn254
20+
pub const BN254_FP_SERIALIZED_SIZE: u32 = 32;
21+
pub const BN254_G1_SERIALIZED_SIZE: u32 = BN254_FP_SERIALIZED_SIZE * 2; // 64
22+
pub const BN254_G2_SERIALIZED_SIZE: u32 = BN254_G1_SERIALIZED_SIZE * 2; // 128
23+
1924
#[allow(clippy::too_many_lines)]
2025
pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTypeDef, Error> {
2126
match t {
@@ -53,13 +58,13 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
5358
"MuxedAddress" => Ok(ScSpecTypeDef::MuxedAddress),
5459
"Timepoint" => Ok(ScSpecTypeDef::Timepoint),
5560
"Duration" => Ok(ScSpecTypeDef::Duration),
56-
// The BLS types defined below are represented in the contract's
61+
// The BLS and BN types defined below are represented in the contract's
5762
// interface by their underlying data types, i.e.
5863
// Fp/Fp2/G1Affine/G2Affine => BytesN<N>, Fr => U256. This approach
5964
// simplifies integration with contract development tooling, as it
60-
// avoids introducing new spec types for these BLS constructs.
65+
// avoids introducing new spec types for these constructs.
6166
//
62-
// While this is functionally sound because the BLS types are
67+
// While this is functionally sound because the types are
6368
// essentially newtypes over their inner representations, it means
6469
// that the specific semantic meaning of `G1Affine`, `G2Affine`, or
6570
// `Fr` is not directly visible in the compiled WASM interface. For
@@ -71,6 +76,9 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
7176
// Idiom. For more details, see the tracking issue for supporting
7277
// type aliases:
7378
// https://github.com/stellar/rs-soroban-sdk/issues/1063
79+
80+
// These BLS12-381 unprefixed type names
81+
// will be removed in a future release.
7482
"Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
7583
n: FP_SERIALIZED_SIZE,
7684
})),
@@ -84,6 +92,29 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
8492
n: G2_SERIALIZED_SIZE,
8593
})),
8694
"Fr" => Ok(ScSpecTypeDef::U256),
95+
// BLS12-381 prefixed type names
96+
"Bls12381Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
97+
n: FP_SERIALIZED_SIZE,
98+
})),
99+
"Bls12381Fp2" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
100+
n: FP2_SERIALIZED_SIZE,
101+
})),
102+
"Bls12381G1Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
103+
n: G1_SERIALIZED_SIZE,
104+
})),
105+
"Bls12381G2Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
106+
n: G2_SERIALIZED_SIZE,
107+
})),
108+
// BN254 prefixed type names
109+
"Bn254Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
110+
n: BN254_FP_SERIALIZED_SIZE,
111+
})),
112+
"Bn254G1Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
113+
n: BN254_G1_SERIALIZED_SIZE,
114+
})),
115+
"Bn254G2Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
116+
n: BN254_G2_SERIALIZED_SIZE,
117+
})),
87118
s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt {
88119
name: s.try_into().map_err(|e| {
89120
Error::new(

soroban-sdk/src/crypto/bls12_381.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Bls12_381 {
2424
env: Env,
2525
}
2626

27-
/// `G1Affine` is a point in the G1 group (subgroup defined over the base field
27+
/// `Bls12381G1Affine` is a point in the G1 group (subgroup defined over the base field
2828
/// `Fq`) of the BLS12-381 elliptic curve
2929
///
3030
/// # Serialization:
@@ -42,19 +42,22 @@ pub struct Bls12_381 {
4242
///
4343
/// # Example Usage:
4444
/// ```rust
45-
/// use soroban_sdk::{Env, bytesn, crypto::bls12_381::{Bls12_381, G1Affine}};
45+
/// use soroban_sdk::{Env, bytesn, crypto::bls12_381::{Bls12_381, Bls12381G1Affine}};
4646
/// let env = Env::default();
4747
/// let bls12_381 = env.crypto().bls12_381();
48-
/// let zero = G1Affine::from_bytes(bytesn!(&env, 0x400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000));
49-
/// let one = G1Affine::from_bytes(bytesn!(&env, 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1));
48+
/// let zero = Bls12381G1Affine::from_bytes(bytesn!(&env, 0x400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000));
49+
/// let one = Bls12381G1Affine::from_bytes(bytesn!(&env, 0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1));
5050
/// let res = bls12_381.g1_add(&zero, &one);
5151
/// assert_eq!(res, one);
5252
/// ```
5353
#[derive(Clone)]
5454
#[repr(transparent)]
55-
pub struct G1Affine(BytesN<G1_SERIALIZED_SIZE>);
55+
pub struct Bls12381G1Affine(BytesN<G1_SERIALIZED_SIZE>);
5656

57-
/// `G2Affine` is a point in the G2 group (subgroup defined over the quadratic
57+
/// Type alias for `Bls12381G1Affine` for convenience
58+
pub type G1Affine = Bls12381G1Affine;
59+
60+
/// `Bls12381G2Affine` is a point in the G2 group (subgroup defined over the quadratic
5861
/// extension field `Fq2`) of the BLS12-381 elliptic curve
5962
///
6063
/// # Serialization:
@@ -72,19 +75,25 @@ pub struct G1Affine(BytesN<G1_SERIALIZED_SIZE>);
7275
/// - sort_flag (bit 2): Must always be unset (0).
7376
#[derive(Clone)]
7477
#[repr(transparent)]
75-
pub struct G2Affine(BytesN<G2_SERIALIZED_SIZE>);
78+
pub struct Bls12381G2Affine(BytesN<G2_SERIALIZED_SIZE>);
79+
80+
/// Type alias for `Bls12381G2Affine` for convenience
81+
pub type G2Affine = Bls12381G2Affine;
7682

77-
/// `Fp` represents an element of the base field `Fq` of the BLS12-381 elliptic
83+
/// `Bls12381Fp` represents an element of the base field `Fq` of the BLS12-381 elliptic
7884
/// curve
7985
///
8086
/// # Serialization:
8187
/// - The 48 bytes represent the **big-endian encoding** of an element in the
8288
/// field `Fp`. The value is serialized as a big-endian integer.
8389
#[derive(Clone)]
8490
#[repr(transparent)]
85-
pub struct Fp(BytesN<FP_SERIALIZED_SIZE>);
91+
pub struct Bls12381Fp(BytesN<FP_SERIALIZED_SIZE>);
8692

87-
/// `Fp2` represents an element of the quadratic extension field `Fq2` of the
93+
/// Type alias for `Bls12381Fp` for convenience
94+
pub type Fp = Bls12381Fp;
95+
96+
/// `Bls12381Fp2` represents an element of the quadratic extension field `Fq2` of the
8897
/// BLS12-381 elliptic curve
8998
///
9099
/// # Serialization:
@@ -94,7 +103,10 @@ pub struct Fp(BytesN<FP_SERIALIZED_SIZE>);
94103
/// and imaginary components).
95104
#[derive(Clone)]
96105
#[repr(transparent)]
97-
pub struct Fp2(BytesN<FP2_SERIALIZED_SIZE>);
106+
pub struct Bls12381Fp2(BytesN<FP2_SERIALIZED_SIZE>);
107+
108+
/// Type alias for `Bls12381Fp2` for convenience
109+
pub type Fp2 = Bls12381Fp2;
98110

99111
/// `Fr` represents an element in the BLS12-381 scalar field, which is a prime
100112
/// field of order `r` (the order of the G1 and G2 groups). The struct is
@@ -104,10 +116,10 @@ pub struct Fp2(BytesN<FP2_SERIALIZED_SIZE>);
104116
#[repr(transparent)]
105117
pub struct Fr(U256);
106118

107-
impl_bytesn_repr!(G1Affine, G1_SERIALIZED_SIZE);
108-
impl_bytesn_repr!(G2Affine, G2_SERIALIZED_SIZE);
109-
impl_bytesn_repr!(Fp, FP_SERIALIZED_SIZE);
110-
impl_bytesn_repr!(Fp2, FP2_SERIALIZED_SIZE);
119+
impl_bytesn_repr!(Bls12381G1Affine, G1_SERIALIZED_SIZE);
120+
impl_bytesn_repr!(Bls12381G2Affine, G2_SERIALIZED_SIZE);
121+
impl_bytesn_repr!(Bls12381Fp, FP_SERIALIZED_SIZE);
122+
impl_bytesn_repr!(Bls12381Fp2, FP2_SERIALIZED_SIZE);
111123

112124
impl Fp {
113125
pub fn env(&self) -> &Env {

0 commit comments

Comments
 (0)