solang icon indicating copy to clipboard operation
solang copied to clipboard

Soroban: `uint256` function arguments fail for small values but work for very large values

Open ahmadsamehh opened this issue 7 months ago • 2 comments

Summary

On the latest Solang Docker image, a Soroban contract using uint256 compiles, deploys, and can be invoked successfully in some cases.

However, public functions that take uint256 arguments fail for small values like 700 and 800, while the same functions succeed for very large uint256 values.

Functions that use uint256 values internally without external arguments work fine.

This suggests a problem at the Soroban external argument boundary for uint256, rather than with uint256 arithmetic itself.


Environment

  • Solang: ghcr.io/hyperledger-solang/solang:latest
  • Stellar CLI:
    • stellar 25.2.0
    • stellar-xdr 25.0.0 (dc9f40fcb83c3054341f70b65a2222073369b37b)
    • xdr curr (0a621ec7811db000a60efae5b35f78dee3aa2533)

Repro contract

pragma solidity 0;

contract math2 {
  function add(uint256 a, uint256 b) public returns (uint256) {
    return a + b;
  }

  function sub(uint256 a, uint256 b) public returns (uint256) {
    return a - b;
  }

  function mul(uint256 a, uint256 b) public returns (uint256) {
    return a * b;
  }

  function div(uint256 b) public returns (uint256) {
    uint256 a = 100;
    return a / b;
  }

  function mod(uint256 b) public returns (uint256) {
    uint256 a = 100;
    return a % b;
  }

  function test_constants() public returns (uint256) {
    uint256 a = 5;
    uint256 b = 4;
    return a + b;
  }

  function test_max_value() public returns (uint256) {
    uint256 max = 2**256 - 1;
    return max;
  }

  function test_zero_ops() public returns (uint256) {
    uint256 a = 0;
    uint256 b = 0;
    return a + b;
  }

  function test_large_numbers() public returns (uint256) {
    uint256 a = 2**128;
    uint256 b = 2**128;
    return a + b;
  }
}

Compilation

docker pull ghcr.io/hyperledger-solang/solang:latest && \
docker run --rm ghcr.io/hyperledger-solang/solang:latest --version && \
docker run --rm -v "$PWD":/sources ghcr.io/hyperledger-solang/solang:latest \
  compile /sources/test256.sol --target soroban -o /sources/output

Compilation succeeds and produces the wasm.


Contract interface

stellar contract info interface --wasm test.wasm

Output:

#[soroban_sdk::contractargs(name = "Args")]
#[soroban_sdk::contractclient(name = "Client")]
pub trait Contract {
    fn add(
        env: soroban_sdk::Env,
        a: soroban_sdk::U256,
        b: soroban_sdk::U256,
    ) -> soroban_sdk::U256;
    fn __constructor(env: soroban_sdk::Env) -> ();
}

Observed behavior

1. No-arg function using internal uint256 values works

stellar contract invoke \
  --id <CONTRACT_ID> \
  --source-account <ACCOUNT> \
  --network testnet \
  -- \
  test_constants

This succeeds and returns the expected result (9).

2. Public function with small uint256 args fails

stellar contract invoke \
  --id <CONTRACT_ID> \
  --source-account <ACCOUNT> \
  --network testnet \
  -- \
  add \
  --a 700 \
  --b 800

Result:

error: transaction simulation failed: HostError: Error(Value, InvalidInput)
DebugInfo not available

3. Same public function with very large uint256 args succeeds

stellar contract invoke \
  --id <CONTRACT_ID> \
  --source-account <ACCOUNT> \
  --network testnet \
  -- \
  add \
  --a 7000000000000000000000000000000000000000000000000000000000000000 \
  --b 8000000000000000000000000000000000000000000000000000000000000000

This succeeds and returns:

"15000000000000000000000000000000000000000000000000000000000000000"

Expected behavior

Any valid uint256 input should be accepted consistently, regardless of magnitude.

For example:

  • add(700, 800) should return 1500
  • very large uint256 inputs should also work

Notes

This does not look like a general uint256 arithmetic problem, because:

  • compilation succeeds
  • deployment succeeds
  • no-arg functions using internal uint256 values work
  • very large external uint256 inputs work

The issue seems specific to decoding or handling external Soroban uint256 function arguments for smaller values.

ahmadsamehh avatar Apr 13 '26 22:04 ahmadsamehh

Hey @ahmadsamehh My Understanding the bug: When the Stellar CLI sends a small uint256 value like 700 as a function argument, Soroban encodes it as a U256 small (inline ScVal, tag 12) since it fits in 56 bits. However, the contract's decode_i256 in soroban_encoding.rs always calls ObjToU256LoLo/LoHi/HiLo/HiHi — assuming a host object — causing the host to throw Error(Value, InvalidInput). Large values work because they can't fit inline and always arrive as proper host objects.

Proposed fix: Mirror the existing decode_i128 logic (which already handles both inline and host-object cases for U128/I128) in decode_i256:

Check if the incoming ScVal tag is 12 (U256 small) or 13 (I256 small) If yes → extract the value inline (shift right 8 bits) If no → call the host functions as today Optionally, update encode_i256 to also emit the inline small format for values that fit in 56 bits (for consistency with encode_i128).

The existing test suite misses this because no test actually passes a uint256 value as an external argument — only no-arg functions are tested.

Would love to work on this — can I be assigned, or should I open a PR directly?

AgrimTawani avatar Apr 15 '26 20:04 AgrimTawani

@AgrimTawani i think your understanding is correct. can u proceed with that fix.

Islam-Imad avatar Jul 04 '26 15:07 Islam-Imad