Skip to content

Commit

Permalink
Fix U/I128 and U/I256 number representation for consistent sorting (#78)
Browse files Browse the repository at this point in the history
* Fix 128 and 256 bits number representation

* fixup! Fix 128 and 256 bits number representation
  • Loading branch information
jayz22 authored Apr 11, 2023
1 parent eab1622 commit e71dd43
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions Stellar-contract.x
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,36 @@ case SST_HOST_AUTH_ERROR:
SCHostAuthErrorCode authCode;
};

struct UInt128Parts {
uint64 hi;
uint64 lo;
};

// A signed int128 has a high sign bit and 127 value bits. We break it into a
// signed high int64 (that carries the sign bit and the high 63 value bits) and
// a low unsigned uint64 that carries the low 64 bits. This will sort in
// generated code in the same order the underlying int128 sorts.
struct Int128Parts {
// Both signed and unsigned 128-bit ints
// are transported in a pair of uint64s
// to reduce the risk of sign-extension.
int64 hi;
uint64 lo;
uint64 hi;
};

struct UInt256Parts {
uint64 hi_hi;
uint64 hi_lo;
uint64 lo_hi;
uint64 lo_lo;
};

// A signed int256 has a high sign bit and 255 value bits. We break it into a
// signed high int64 (that carries the sign bit and the high 63 value bits) and
// three low unsigned `uint64`s that carry the lower bits. This will sort in
// generated code in the same order the underlying int256 sorts.
struct Int256Parts {
int64 hi_hi;
uint64 hi_lo;
uint64 lo_hi;
uint64 lo_lo;
};

enum SCContractExecutableType
Expand Down Expand Up @@ -279,14 +303,14 @@ case SCV_DURATION:
Duration duration;

case SCV_U128:
Int128Parts u128;
UInt128Parts u128;
case SCV_I128:
Int128Parts i128;

case SCV_U256:
uint256 u256;
UInt256Parts u256;
case SCV_I256:
uint256 i256;
Int256Parts i256;

case SCV_BYTES:
SCBytes bytes;
Expand Down

0 comments on commit e71dd43

Please sign in to comment.