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_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 be able to import the U256 struct, add the following to your Cargo.toml file:

openvm-ruint = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.4.0", 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);
}

To be able to import the I256 struct, add the following to your Cargo.toml file:

openvm-ruint = { git = "https://github.com/openvm-org/openvm.git", tag = "v1.4.0", package = "ruint" }

Config parameters

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

[app_vm_config.bigint]