Skip to content
Merged
313 changes: 72 additions & 241 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,27 @@ soroban-token-spec = { version = "25.3.1", path = "soroban-token-spec" }
stellar-asset-spec = { version = "25.3.1", path = "stellar-asset-spec" }

[workspace.dependencies.soroban-env-common]
version = "=25.0.1"
version = "=26.0.1"
# git = "https://github.com/stellar/rs-soroban-env"
# rev = "cf58d535ab05d02802a5e804a95524650f8c62c7"
# rev = "f4d7b3299df8fc19e92769d0a26be2d032b78ccb"

[workspace.dependencies.soroban-env-guest]
version = "=25.0.1"
version = "=26.0.1"
# git = "https://github.com/stellar/rs-soroban-env"
# rev = "cf58d535ab05d02802a5e804a95524650f8c62c7"
# rev = "f4d7b3299df8fc19e92769d0a26be2d032b78ccb"

[workspace.dependencies.soroban-env-host]
version = "=25.0.1"
version = "=26.0.1"
# git = "https://github.com/stellar/rs-soroban-env"
# rev = "cf58d535ab05d02802a5e804a95524650f8c62c7"
# rev = "f4d7b3299df8fc19e92769d0a26be2d032b78ccb"

[workspace.dependencies.stellar-strkey]
version = "=0.0.16"

[workspace.dependencies.stellar-xdr]
version = "=25.0.0"
version = "=26.0.0"
# git = "https://github.com/stellar/rs-stellar-xdr"
# rev = "dd7a165a193126fd37a751d867bee1cb8f3b55a6"
default-features = false
features = ["curr"]

Expand Down
3 changes: 1 addition & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ deny = [

# Certain crates/versions that will be skipped when doing duplicate detection.
skip = [
{ name = "hashbrown", version = "=0.13.2" },
{ name = "hashbrown", version = "=0.14.5" },
{ name = "stellar-strkey", version = "=0.0.13" },
{ name = "syn", version = "=1.0.109" },
]
# Similarly to `skip` allows you to skip certain crates during duplicate
# detection. Unlike skip, it also includes the entire tree of transitive
Expand Down
2 changes: 1 addition & 1 deletion soroban-ledger-snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl LedgerSnapshot {
impl Default for LedgerSnapshot {
fn default() -> Self {
Self {
protocol_version: 25,
protocol_version: 26,
sequence_number: Default::default(),
timestamp: Default::default(),
network_id: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ stellar-xdr = { workspace = true, features = ["curr", "std"] }
syn = {version="2.0.77",features=["full"]}
quote = "1.0"
proc-macro2 = "1.0"
itertools = "0.10.5"
itertools = "0.13.0"
darling = "0.20.10"
macro-string = "0.1.4"
sha2 = "0.10.7"
Expand Down
16 changes: 16 additions & 0 deletions soroban-sdk/src/crypto/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ impl Bls12_381 {
res.into()
}

/// Checks if a G1 point is on the BLS12-381 curve (no subgroup check).
pub fn g1_is_on_curve(&self, point: &G1Affine) -> bool {
let env = self.env();
internal::Env::bls12_381_g1_is_on_curve(env, point.to_object())
.unwrap_infallible()
.into()
}

/// Adds two points `p0` and `p1` in G1.
pub fn g1_add(&self, p0: &G1Affine, p1: &G1Affine) -> G1Affine {
let env = self.env();
Expand Down Expand Up @@ -671,6 +679,14 @@ impl Bls12_381 {
res.into()
}

/// Checks if a G2 point is on the BLS12-381 curve (no subgroup check).
pub fn g2_is_on_curve(&self, point: &G2Affine) -> bool {
let env = self.env();
internal::Env::bls12_381_g2_is_on_curve(env, point.to_object())
.unwrap_infallible()
.into()
}

/// Adds two points `p0` and `p1` in G2.
pub fn g2_add(&self, p0: &G2Affine, p1: &G2Affine) -> G2Affine {
let env = self.env();
Expand Down
89 changes: 87 additions & 2 deletions soroban-sdk/src/crypto/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
use crate::xdr::ScVal;
use crate::{
crypto::utils::BigInt,
env::internal::{self, BytesObject, U256Val},
env::internal::{self, BytesObject, U256Val, U64Val},
impl_bytesn_repr_without_from_bytes,
unwrap::{UnwrapInfallible, UnwrapOptimized},
Bytes, BytesN, ConversionError, Env, IntoVal, TryFromVal, Val, Vec, U256,
};
use core::{
cmp::Ordering,
fmt::Debug,
ops::{Add, Mul, Neg},
ops::{Add, Mul, Neg, Sub},
};

pub const BN254_FP_SERIALIZED_SIZE: usize = 32; // Size in bytes of a serialized Bn254Fp element in BN254. The field modulus is 254 bits, requiring 32 bytes (256 bits).
Expand Down Expand Up @@ -254,6 +254,38 @@ impl Fr {
pub fn to_val(&self) -> Val {
self.0.to_val()
}

pub fn pow(&self, rhs: u64) -> Self {
self.env().crypto().bn254().fr_pow(self, rhs)
}

pub fn inv(&self) -> Self {
self.env().crypto().bn254().fr_inv(self)
}
}

impl Add for Fr {
type Output = Fr;

fn add(self, rhs: Self) -> Self::Output {
self.env().crypto().bn254().fr_add(&self, &rhs)
}
}

impl Sub for Fr {
type Output = Fr;

fn sub(self, rhs: Self) -> Self::Output {
self.env().crypto().bn254().fr_sub(&self, &rhs)
}
}

impl Mul for Fr {
type Output = Fr;

fn mul(self, rhs: Self) -> Self::Output {
self.env().crypto().bn254().fr_mul(&self, &rhs)
}
}

// BN254 scalar field modulus r in big-endian bytes.
Expand Down Expand Up @@ -386,6 +418,59 @@ impl Bn254 {
.unwrap_infallible()
.into()
}

/// Performs a multi-scalar multiplication (MSM) operation in G1.
pub fn g1_msm(&self, vp: Vec<Bn254G1Affine>, vs: Vec<Fr>) -> Bn254G1Affine {
let env = self.env();
let bin = internal::Env::bn254_g1_msm(env, vp.into(), vs.into()).unwrap_infallible();
unsafe { Bn254G1Affine::from_bytes(BytesN::unchecked_new(env.clone(), bin)) }
}

/// Checks if a G1 point is on the BN254 curve.
pub fn g1_is_on_curve(&self, point: &Bn254G1Affine) -> bool {
let env = self.env();
internal::Env::bn254_g1_is_on_curve(env, point.to_object())
.unwrap_infallible()
.into()
}

// scalar arithmetic

/// Adds two scalars in the BN254 scalar field `Fr`.
pub fn fr_add(&self, lhs: &Fr, rhs: &Fr) -> Fr {
let env = self.env();
let v = internal::Env::bn254_fr_add(env, lhs.into(), rhs.into()).unwrap_infallible();
U256::try_from_val(env, &v).unwrap_infallible().into()
}

/// Subtracts one scalar from another in the BN254 scalar field `Fr`.
pub fn fr_sub(&self, lhs: &Fr, rhs: &Fr) -> Fr {
let env = self.env();
let v = internal::Env::bn254_fr_sub(env, lhs.into(), rhs.into()).unwrap_infallible();
U256::try_from_val(env, &v).unwrap_infallible().into()
}

/// Multiplies two scalars in the BN254 scalar field `Fr`.
pub fn fr_mul(&self, lhs: &Fr, rhs: &Fr) -> Fr {
let env = self.env();
let v = internal::Env::bn254_fr_mul(env, lhs.into(), rhs.into()).unwrap_infallible();
U256::try_from_val(env, &v).unwrap_infallible().into()
}

/// Raises a scalar to the power of a given exponent in the BN254 scalar field `Fr`.
pub fn fr_pow(&self, lhs: &Fr, rhs: u64) -> Fr {
let env = self.env();
let rhs = U64Val::try_from_val(env, &rhs).unwrap_optimized();
let v = internal::Env::bn254_fr_pow(env, lhs.into(), rhs).unwrap_infallible();
U256::try_from_val(env, &v).unwrap_infallible().into()
}

/// Computes the multiplicative inverse of a scalar in the BN254 scalar field `Fr`.
pub fn fr_inv(&self, lhs: &Fr) -> Fr {
let env = self.env();
let v = internal::Env::bn254_fr_inv(env, lhs.into()).unwrap_infallible();
U256::try_from_val(env, &v).unwrap_infallible().into()
}
}

#[cfg(test)]
Expand Down
76 changes: 74 additions & 2 deletions soroban-sdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@
//! ```

use crate::{
env::internal::Env as _, unwrap::UnwrapInfallible, Address, Bytes, BytesN, ConstructorArgs,
Env, IntoVal,
env::internal::{ContractTtlExtension, Env as _},
unwrap::UnwrapInfallible,
Address, Bytes, BytesN, ConstructorArgs, Env, IntoVal,
};

/// Deployer provides access to deploying contracts.
Expand Down Expand Up @@ -272,6 +273,77 @@ impl Deployer {
)
.unwrap_infallible();
}

/// Extend the TTL of the contract instance and code with limits on the extension.
///
/// Extends the TTL of the instance and code to be up to `extend_to` ledgers.
/// The extension only happens if it exceeds `min_extension` ledgers, otherwise
/// this is a no-op. The amount of extension will not exceed `max_extension` ledgers.
///
/// Note that the extension is applied to both the contract code and contract instance,
/// so it's possible that one is extended but not the other depending on their current TTLs.
///
/// The TTL is the number of ledgers between the current ledger and the final ledger
/// the data can still be accessed.
pub fn extend_ttl_with_limits(
&self,
contract_address: Address,
extend_to: u32,
min_extension: u32,
max_extension: u32,
) {
self.env
.extend_contract_instance_and_code_ttl_v2(
contract_address.to_object(),
ContractTtlExtension::InstanceAndCode,
extend_to.into(),
min_extension.into(),
max_extension.into(),
)
.unwrap_infallible();
}

/// Extend the TTL of the contract instance with limits on the extension.
///
/// Same as [`extend_ttl_with_limits`](Self::extend_ttl_with_limits) but only for contract instance.
pub fn extend_ttl_for_contract_instance_with_limits(
&self,
contract_address: Address,
extend_to: u32,
min_extension: u32,
max_extension: u32,
) {
self.env
.extend_contract_instance_and_code_ttl_v2(
contract_address.to_object(),
ContractTtlExtension::Instance,
extend_to.into(),
min_extension.into(),
max_extension.into(),
)
.unwrap_infallible();
}

/// Extend the TTL of the contract code with limits on the extension.
///
/// Same as [`extend_ttl_with_limits`](Self::extend_ttl_with_limits) but only for contract code.
pub fn extend_ttl_for_code_with_limits(
&self,
contract_address: Address,
extend_to: u32,
min_extension: u32,
max_extension: u32,
) {
self.env
.extend_contract_instance_and_code_ttl_v2(
contract_address.to_object(),
ContractTtlExtension::Code,
extend_to.into(),
min_extension.into(),
max_extension.into(),
)
.unwrap_infallible();
}
}

/// A deployer that deploys a contract that has its ID derived from the provided
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub mod internal {
}

pub use internal::xdr;
pub use internal::ContractTtlExtension;
pub use internal::ConversionError;
pub use internal::EnvBase;
pub use internal::Error;
Expand Down
Loading
Loading