pub struct BigUint {
    /// The underlying limbs representing the `BigUint` type.
    limbs: Vec<u64>,
}
Expand description

The BigUint type.

Additional Information

The BigUint type is unsigned. The minimum value is zero and the maximum value is infinity.

Fields

limbs: Vec

The underlying limbs representing the BigUint type.

Implementations

pub fn new() -> Self

Create a new instance of a BigUint.

Returns

  • [BigUint] - A newly instantiated instance of a BigUint.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let new_big_uint = BigUint::new();
    assert(new_big_uint.is_zero());
}

pub fn equal_limb_size(self, other: BigUint) -> bool

Returns true if two BigUint numbers have the same number of limbs.

Arguments

  • other: [BigUint] - The other BigUint to compare with.

Returns

  • [bool] - true if both BigUint values have the same limbs, otherwise false.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let new_big_uint = BigUint::new();
    let u64_big_uint = BigUint::from(u64::max());
    let u256_big_uint = BigUint::from(u256::max());

    assert(new_big_uint.equal_limb_size(u64_big_uint));
    assert(!new_big_uint.equal_limb_size(u256_big_uint));
    assert(!u64_big_uint.equal_limb_size(u256_big_uint));
}

pub fn number_of_limbs(self) -> u64

Returns the number of limbs the BigUint has.

Returns

  • [u64] - The number of limbs the BigUint has.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let new_big_uint = BigUint::new();
    let u64_big_uint = BigUint::from(u64::max());
    let u256_big_uint = BigUint::from(u256::max());

    assert(new_big_uint.number_of_limbs() == 1);
    assert(u64_big_uint.number_of_limbs() == 1);
    assert(u256_big_uint.number_of_limbs() == 4);
}

pub fn limbs(self) -> Vec<u64>

Returns a copy of the BigUint’s limbs.

Returns

  • [Vec] - A clone of the BigUint’s limbs.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let new_big_uint = BigUint::new();
    let new_limbs: Vec<u64> = new_big_uint.limbs();
    assert(new_limbs.len() == 1);
    assert(new_limbs.get(0).unwrap() == 0);

    let u64_big_uint = BigUint::from(u64::max());
    let u64_limbs: Vec<u64> = u64_big_uint.limbs();
    assert(u64_limbs.len() == 1);
    assert(u64_limbs.get(0).unwrap() == u64::max());

    let u256_big_uint = BigUint::from(u256::max());
    let u256_limbs: Vec<u64> = u256_big_uint.limbs();
    assert(u256_limbs.len() == 4);
    assert(u256_limbs.get(0).unwrap() == u64::max());
    assert(u256_limbs.get(1).unwrap() == u64::max());
    assert(u256_limbs.get(2).unwrap() == u64::max());
    assert(u256_limbs.get(3).unwrap() == u64::max());
}

pub fn get_limb(self, index: u64) -> Option<u64>

Returns the BigUint’s limb at a specific index.

Arguments

  • index: [u64] - The index at which to fetch the limb.

Returns

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let new_big_uint = BigUint::new();
    assert(new_big_uint.get_limb(0).unwrap() == 0);
    assert(new_big_uint.get_limb(1).is_none());

    let u64_big_uint = BigUint::from(u64::max());
    assert(u64_big_uint.get_limb(0).unwrap() == u64::max());
    assert(u64_big_uint.get_limb(1).is_none());

    let u256_big_uint = BigUint::from(u256::max());
    assert(u256_big_uint.get_limb(0).unwrap() == u64::max());
    assert(u256_big_uint.get_limb(1).unwrap() == u64::max());
    assert(u256_big_uint.get_limb(2).unwrap() == u64::max());
    assert(u256_big_uint.get_limb(3).unwrap() == u64::max());
    assert(u256_big_uint.get_limb(4).is_none());
}

pub fn zero() -> Self

A zeroed instance of a BigUint.

Returns

  • [BigUint] - A newly created zeroed BigUint.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let zero_big_uint = BigUint::zero();
    assert(zero_big_uint.is_zero());
}

pub fn is_zero(self) -> bool

Returns whether the BigUint is zero.

Returns

  • [bool] - true if the BigUint is zero, otherwise false.

Examples

use sway_libs::bigint::BigUint;

fn foo() {
    let zero_big_uint = BigUint::zero();
    assert(zero_big_uint.is_zero());
}

Trait Implementations

pub fn abi_encode(self, buffer: Buffer) -> Buffer

pub fn abi_decode(refmut buffer: BufferReader) -> Self

pub fn clone(self) -> Self

pub fn from(value: u8) -> Self

pub fn try_into(self) -> Option<u8>

pub fn from(value: u16) -> Self

pub fn try_into(self) -> Option<u16>

pub fn from(value: u32) -> Self

pub fn try_into(self) -> Option<u32>

pub fn from(value: u64) -> Self

pub fn try_into(self) -> Option<u64>

pub fn from(value: U128) -> Self

pub fn try_into(self) -> Option<U128>

pub fn from(value: u256) -> Self

pub fn try_into(self) -> Option<u256>

pub fn from(bytes: Bytes) -> Self

pub fn into(self) -> Bytes

pub fn eq(self, other: Self) -> bool

pub fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

pub fn gt(self, other: Self) -> bool

pub fn lt(self, other: Self) -> bool

pub fn ge(self, other: Self) -> bool

Evaluates if one value of the same type is greater or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is greater than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn gt(self, other: Self) -> bool {
        self.val > other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 >= struct2;
    assert(result);
}

pub fn le(self, other: Self) -> bool

Evaluates if one value of the same type is less or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is less than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn lt(self, other: Self) -> bool {
        self.val < other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 <= struct2;
    assert(result);
}

pub fn add(self, other: Self) -> Self

pub fn multiply(self, other: Self) -> Self

pub fn subtract(self, other: Self) -> Self