Home

Awesome

WadRay library for Cairo

tests

This library implements two types of fixed-point decimal numbers, "wad" (18 decimals of precision) and "ray" (27 decimals of decimal numbers), available in signed (SignedWad and SignedRay) and unsigned (Wad and Ray) versions, written in Cairo for Starknet.

Wad and Ray are implemented as structs with a single u128 member for the value, while SignedWad and SignedRay are implemented as structs with a u128 member for the value and bool member for the sign (i.e. if the sign is true, then the value is negative).

Overview

This library includes arithmetic, comparison and conversion functions.

Arithmetic

Addition and Subtraction

Addition and subtraction can be performed via the Add, AddEq, Sub and SubEq traits as follows:

where both a and b are of the same type.

Multiplication and Division

Multiplication and division can be performed via the the Mul, MulEq, Div and DivEq traits as follows:

There is also a set of functions for operations involving a Wad and a Ray:

For multiplication, the prefixes w and r denote whether the product is divided (i.e. scaled down) by a Wad or Ray respectively. For division, the prefixes w and r denote whether the first operand is multiplied (i.e. scaled up) by a Wad or Ray before the division respectively.

As these are fixed point operations, do take note that there will be a loss of precision.

Zero and one values

The following values and functions for both Wad and Ray, and SignedWad and SignedRay, are available via the Zeroable and Oneable traits.

Unsigned
Signed

Bound values

The bound values for both Wad and Ray can be obtained via the BoundedInt trait.

Comparisons

Comparison for both Wad and Ray, and SignedWad and SignedRay, can be performed via the PartialEq and PartialOrd traits as follows:

where both a and b are of the same type.

Conversion

Type Casting

Any type that can be converted to a u128 via the Into trait can similarly be converted to a Wad or Ray via the Into trait.

Additionally, the following conversions from integer types are supported for SignedWad and SignedRay via the Into trait`:

The following conversions from this library's types can also be performed via the Into trait:

The following conversions can be performed via the TryInto trait:

Scaling

The following functions can be used to scale between Wad and Ray:

Additional notes on conversion between Wad and Ray

Overview

Starting from v0.5.0 of this library, we have made significant changes to how Wad values are converted to Ray values and vice versa. This aims to improve type safety and align with the semantics of Rust's Into trait.

Key Changes
  1. Previously, Into<Wad, Ray> scaled the value by 10<sup>9</sup> while Into<Ray, Wad> scaled the value by 10<sup>-9</sup>. Both now perform direct type cast without value modification
  2. Introduced wad_to_ray() and ray_to_wad() functions for value-preserving conversions
Recommended Usage
  1. Prefer wad_to_ray() and ray_to_wad() for most conversions between Wad and Ray.
  2. Use the Into trait only when a simple type cast is required (expected to be rare).

Signed

The following functions are available for SignedWad and SignedRay via the Signed trait:

Usage

To use this library, add the repository as a dependency in your Scarb.toml:

[dependencies]
wadray = "0.5.0"

then add the following line in your .cairo file

use wadray::Wad;

fn add_wad(a: Wad, b: Wad) -> Wad {
    a + b
}

You can also refer to the test file src/test_wadray.cairo for another example.

Development

Prerequisites

Run tests

To run the tests:

scarb test

Formal Verification

This library has been formally verified using our tool Aegis. Specifications and their correctness proof can be found in this repository, the verification currently refers to revision 30f7664 of this repo.

Contribute

We welcome contributions of any kind! Please feel free to submit an issue or open a PR if you have a solution to an existing bug.

License

This library is released under the MIT License.