Skip to content

Ruint

The Ruint guest library is a fork of ruint that allows for patching of U256 operations with logic from openvm-bigint-guest.

Example matrix multiplication using U256

See the full example here.

#![allow(clippy::needless_range_loop)]
 
use core::array;
 
use openvm as _;
use openvm_bigint_guest as _;
use openvm_ruint::aliases::U256;
 
const N: usize = 16;
type Matrix = [[U256; N]; N];
 
pub fn get_matrix(val: u32) -> Matrix {
    array::from_fn(|_| array::from_fn(|_| U256::from(val)))
}
 
pub fn mult(a: &Matrix, b: &Matrix) -> Matrix {
    let mut c = get_matrix(0);
    for i in 0..N {
        for j in 0..N {
            for k in 0..N {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    c
}
 
pub fn get_identity_matrix() -> Matrix {
    let mut res = get_matrix(0);
    for i in 0..N {
        res[i][i] = U256::from(1u32);
    }
    res
}
 
pub fn main() {
    let a: Matrix = get_identity_matrix();
    let b: Matrix = get_matrix(28);
    let c: Matrix = mult(&a, &b);
    if c != b {
        panic!("Matrix multiplication failed");
    }
}

To use U256 outside the monorepo, add the following to your Cargo.toml file:

openvm = { git = "https://github.com/openvm-org/openvm.git", tag = "v2.0.0-beta.2", features = ["std"] }
openvm-ruint = { git = "https://github.com/openvm-org/uint.git", branch = "v1.14.0-openvm", package = "ruint" }

Example matrix multiplication using I256

See the full example here.

#![allow(clippy::needless_range_loop)]
 
use core::array;
 
use alloy_primitives::I256;
use openvm as _;
 
const N: usize = 16;
type Matrix = [[I256; N]; N];
 
pub fn get_matrix(val: i32) -> Matrix {
    array::from_fn(|_| array::from_fn(|_| I256::try_from(val).unwrap()))
}
 
pub fn mult(a: &Matrix, b: &Matrix) -> Matrix {
    let mut c = get_matrix(0);
    for i in 0..N {
        for j in 0..N {
            for k in 0..N {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    c
}
 
pub fn get_identity_matrix() -> Matrix {
    let mut res = get_matrix(0);
    for i in 0..N {
        res[i][i] = I256::try_from(1i32).unwrap();
    }
    res
}
 
pub fn main() {
    let a: Matrix = get_identity_matrix();
    let b: Matrix = get_matrix(-28);
    let c: Matrix = mult(&a, &b);
    assert_eq!(c, b);
}

This example uses alloy_primitives::I256, which depends on ruint. The snippet below is the equivalent setup for using it outside the monorepo; the checked-in example uses a local path patch instead.

alloy-primitives = "1.1.2"
 
[patch.crates-io]
ruint = { git = "https://github.com/openvm-org/uint.git", branch = "v1.14.0-openvm" }

Config parameters

For the guest program to build successfully add the following to your .toml file:

[app_vm_config.bigint]