Skip to content
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
24 changes: 15 additions & 9 deletions soroban-sdk-macros/src/map_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,27 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
)),
// The BLS and BN types defined below are represented in the contract's
// interface by their underlying data types, i.e.
// Fp/Fp2/G1Affine/G2Affine => BytesN<N>, Fr => U256. This approach
// simplifies integration with contract development tooling, as it
// avoids introducing new spec types for these constructs.
// Bls12381Fp/Bls12381Fp2/Bls12381G1Affine/Bls12381G2Affine => BytesN<N>,
// Bls12381Fr/Bn254Fr => U256. This approach simplifies integration with
// contract development tooling, as it avoids introducing new spec types
// for these constructs.
//
// While this is functionally sound because the types are
// essentially newtypes over their inner representations, it means
// that the specific semantic meaning of `G1Affine`, `G2Affine`, or
// `Fr` is not directly visible in the compiled WASM interface. For
// example, a contract function expecting a `G1Affine` will appear
// in the WASM interface as expecting a `BytesN<96>`.
// that the specific semantic meaning of `Bls12381G1Affine`,
// `Bls12381G2Affine`, `Bls12381Fr`, or `Bn254Fr` is not directly visible
// in the compiled WASM interface. For example, a contract function
// expecting a `Bls12381G1Affine` will appear in the WASM interface as
// expecting a `BytesN<96>`.
//
// Future enhancements might allow the macro to automatically deduce
// and utilize the inner types for types defined using the New Type
// Idiom. For more details, see the tracking issue for supporting
// type aliases:
// https://github.com/stellar/rs-soroban-sdk/issues/1063

// These BLS12-381 unprefixed type names
// will be removed in a future release.
// These BLS12-381 unprefixed type names are deprecated.
// Use the Bls12381-prefixed names instead.
"Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
n: FP_SERIALIZED_SIZE,
})),
Expand All @@ -147,6 +149,8 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
"G2Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
n: G2_SERIALIZED_SIZE,
})),
// Deprecated: `Fr` maps to BLS12-381 Fr for backward compat.
// Use `Bls12381Fr` or `Bn254Fr` instead.
"Fr" => Ok(ScSpecTypeDef::U256),
// BLS12-381 prefixed type names
"Bls12381Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
Expand All @@ -161,6 +165,7 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
"Bls12381G2Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
n: G2_SERIALIZED_SIZE,
})),
"Bls12381Fr" => Ok(ScSpecTypeDef::U256),
// BN254 prefixed type names
"Bn254Fp" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
n: BN254_FP_SERIALIZED_SIZE,
Expand All @@ -171,6 +176,7 @@ pub fn map_type(t: &Type, allow_ref: bool, allow_hash: bool) -> Result<ScSpecTyp
"Bn254G2Affine" => Ok(ScSpecTypeDef::BytesN(ScSpecTypeBytesN {
n: BN254_G2_SERIALIZED_SIZE,
})),
"Bn254Fr" => Ok(ScSpecTypeDef::U256),
s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt {
name: s.try_into().map_err(|e| {
Error::new(
Expand Down
10 changes: 5 additions & 5 deletions soroban-sdk/src/_migrating/v25_bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//!
//! - [`Bn254G1Affine`] - A point in the G1 group (64 bytes, Ethereum-compatible format)
//! - [`Bn254G2Affine`] - A point in the G2 group (128 bytes, Ethereum-compatible format)
//! - [`Fr`] - A scalar field element (32 bytes, internally a `U256`)
//! - [`Bn254Fr`] - A scalar field element (32 bytes, internally a `U256`)
//! - [`Bn254Fp`] - A base field element (32 bytes)
//!
//! ## New Operations
Expand All @@ -20,14 +20,14 @@
//!
//! G1 points also support arithmetic operations via Rust traits:
//! - `Add` - Add two G1 points
//! - `Mul<Fr>` - Multiply a G1 point by a scalar
//! - `Mul<Bn254Fr>` - Multiply a G1 point by a scalar
//! - `Neg` - Negate a G1 point
//!
//! ## Example: Basic G1 Operations
//!
//! ```
//! use soroban_sdk::{Env, BytesN, U256};
//! use soroban_sdk::crypto::bn254::{Bn254G1Affine, Fr};
//! use soroban_sdk::crypto::bn254::{Bn254Fr, Bn254G1Affine};
//!
//! # fn main() {
//! let env = Env::default();
Expand All @@ -51,7 +51,7 @@
//! assert_eq!(g1_doubled, g1_doubled_alt);
//!
//! // Scalar multiplication: 2 * G should equal G + G
//! let scalar: Fr = U256::from_u32(&env, 2).into();
//! let scalar: Bn254Fr = U256::from_u32(&env, 2).into();
//! let g1_times_2 = bn254.g1_mul(&g1, &scalar);
//! assert_eq!(g1_doubled, g1_times_2);
//!
Expand Down Expand Up @@ -134,5 +134,5 @@
//!
//! [`Bn254G1Affine`]: crate::crypto::bn254::Bn254G1Affine
//! [`Bn254G2Affine`]: crate::crypto::bn254::Bn254G2Affine
//! [`Fr`]: crate::crypto::bn254::Fr
//! [`Bn254Fr`]: crate::crypto::bn254::Bn254Fr
//! [`Bn254Fp`]: crate::crypto::bn254::Bn254Fp
5 changes: 4 additions & 1 deletion soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::{
pub mod bls12_381;
pub mod bn254;
pub(crate) mod utils;
pub use bn254::Fr as BnScalar;
/// Deprecated alias for `bn254::Bn254Fr`.
/// Use `bn254::Bn254Fr` directly instead.
#[deprecated(note = "use `bn254::Bn254Fr` instead")]
pub use bn254::Bn254Fr as BnScalar;
Comment thread
mootz12 marked this conversation as resolved.

/// A `BytesN<N>` generated by a cryptographic hash function.
///
Expand Down
Loading
Loading