Assessment reports>Initia>Threat Model>Modules decimal128.move and decimal256.move

Modules decimal128.move and decimal256.move

These modules implement functions that can be used to work with fixed precision (18 decimal digits) unsigned numbers that can be represented in, respectively, 128 and 256 bits.

The modules expose functions to create a Decimal128/Decimal256 number from a raw value (already premultiplied by 1e18) as well as parsing a number from a string. Note that an issue with number parsing is discussed in issue ref.

These functions are implemented in decimal128:

  • new: Creates a Decimal128 from a (premultiplied) u128 value.

  • new_u64: Creates a Decimal128 from a (premultiplied) u64 value.

  • one: Creates a Decimal128 with value 1.

  • zero: Creates a Decimal128 with zero value.

  • from_ratio_u64: Creates a Decimal128 that represents the ratio between two u64 integers.

  • from_ratio: Creates a Decimal128 that represents the ratio between two u128 integers.

  • add: Adds two Decimal128, returning a new Decimal128 as a result.

  • sub: Adds two Decimal128, returning a new Decimal128 as a result.

  • mul_u64: Multiplies a Decimal128 by a u64 integer value, returning an unscaled u64 integer result.

  • mul_u128: Multiplies a Decimal128 by a u128 integer value, returning an unscaled u128 integer result.

  • mul: Multiplies two Decimal128 numbers, returning a Decimal128 result.

  • div_u64: Divides a Decimal128 by a u64 integer value, returning a Decimal128 result.

  • div: Divides a Decimal128 by a u128 integer value, returning a Decimal128 result.

  • val: Retrieves the inner value of the Decimal128 (which is scaled by 1e18).

  • is_same: Compares two Decimal128s.

  • from_string: Parses a number into a Decimal128.

And these functions are implemented in decimal256:

  • new: Creates a Decimal256 from a (premultiplied) u256 value.

  • new_u64: Creates a Decimal256 from a (premultiplied) u64 value.

  • new_u128: Creates a Decimal256 from a (premultiplied) u128 value.

  • one: Creates a Decimal256 with value 1.

  • zero: Creates a Decimal256 with zero value.

  • from_ratio_u64: Creates a Decimal256 that represents the ratio between two u64 integers.

  • from_ratio_u128: Creates a Decimal256 that represents the ratio between two u128 integers.

  • from_ratio: Creates a Decimal256 that represents the ratio between two u256 integers.

  • add: Adds two Decimal256, returning a new Decimal256 as a result.

  • sub: Adds two Decimal256, returning a new Decimal256 as a result.

  • mul_u64: Multiplies a Decimal256 by a u64 integer value, returning an unscaled u64 integer result.

  • mul_u128: Multiplies a Decimal256 by a u128 integer value, returning an unscaled u128 integer result.

  • mul_u256: Multiplies a Decimal256 by a u256 integer value, returning an unscaled u256 integer result.

  • mul: Multiplies two Decimal256 numbers, returning a Decimal256 result.

  • div_u64: Divides a Decimal256 by a u64 integer value, returning a Decimal256 result.

  • div_u128: Divides a Decimal256 by a u128 integer value, returning a Decimal256 result.

  • div: Divides a Decimal256 by a u256 integer value, returning a Decimal256 result.

  • val: Retrieves the inner value of the Decimal256 (which is scaled by 1e18).

  • is_same: Compares two Decimal256s.

  • from_string: Parses a number into a Decimal256.

Zellic © 2024Back to top ↑