Skip to content

Commit

Permalink
Feat/HINT_BUFFER opcode for Rv32 (#1256)
Browse files Browse the repository at this point in the history
* Cols and AIR constraints

* assert_bool

* execute

* Trace generation

* Adapt old tests

* Fix compile errors

* Constraints passing

* Old test passing

* Buffer test + fixes

* Combine into single test

* Replace old chip with new chip

* Transpilation of hint_buffer + integration in read_vec_by_len

* Integrate into read.rs

* Integrate into sw-setup

* Integrate into bls12_381

* Integrate into bn254

* Lint

* Finish renaming and removal to replace old chip

* Remove immediate

* Remove immediate field from macros

* Change transpilation

* Divide by 4 instead of multiply in ecc

* Remove use of testutils

* Rustfmt

* Update extensions/rv32im/circuit/src/hintstore/mod.rs

Co-authored-by: Zach Langley <[email protected]>

* Use generate_aux_cols

* Use filtered builder

* Move bitwise lookup chip checks to

---------

Co-authored-by: Zach Langley <[email protected]>
  • Loading branch information
TlatoaniHJ and zlangley authored Jan 23, 2025
1 parent ac02ee9 commit 208e00d
Show file tree
Hide file tree
Showing 17 changed files with 653 additions and 626 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions crates/toolchain/openvm/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::alloc::Layout;
use core::fmt::Write;

#[cfg(target_os = "zkvm")]
use openvm_rv32im_guest::{hint_input, hint_store_u32};
use openvm_rv32im_guest::{hint_buffer_u32, hint_input, hint_store_u32};
use serde::de::DeserializeOwned;

#[cfg(not(target_os = "zkvm"))]
Expand Down Expand Up @@ -37,7 +37,7 @@ pub fn read<T: DeserializeOwned>() -> T {
pub fn read_u32() -> u32 {
let ptr = unsafe { alloc::alloc::alloc(Layout::from_size_align(4, 4).unwrap()) };
let addr = ptr as u32;
hint_store_u32!(addr, 0);
hint_store_u32!(addr);
let result: u32;
unsafe {
core::arch::asm!("lw {rd}, ({rs1})", rd = out(reg) result, rs1 = in(reg) addr);
Expand All @@ -47,7 +47,7 @@ pub fn read_u32() -> u32 {

fn hint_store_word(ptr: *mut u32) {
#[cfg(target_os = "zkvm")]
hint_store_u32!(ptr, 0);
hint_store_u32!(ptr);
#[cfg(not(target_os = "zkvm"))]
unsafe {
*ptr = crate::host::read_u32();
Expand All @@ -68,11 +68,7 @@ pub(crate) fn read_vec_by_len(len: usize) -> Vec<u8> {
let ptr_start = unsafe { alloc::alloc::alloc(layout) };
let mut ptr = ptr_start;

// Note: if len % 4 != 0, this will discard some last bytes
for _ in 0..num_words {
hint_store_u32!(ptr, 0);
ptr = unsafe { ptr.add(4) };
}
hint_buffer_u32!(ptr, num_words);
unsafe { Vec::from_raw_parts(ptr_start, len, capacity) }
}
#[cfg(not(target_os = "zkvm"))]
Expand Down
27 changes: 20 additions & 7 deletions crates/toolchain/openvm/src/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::mem::MaybeUninit;

use openvm_platform::WORD_SIZE;
#[cfg(target_os = "zkvm")]
use openvm_rv32im_guest::hint_buffer_u32;

use super::hint_store_word;
use crate::serde::WordRead;
Expand Down Expand Up @@ -28,10 +30,15 @@ impl WordRead for Reader {
fn read_words(&mut self, words: &mut [u32]) -> crate::serde::Result<()> {
let num_words = words.len();
if let Some(new_remaining) = self.bytes_remaining.checked_sub(num_words * WORD_SIZE) {
for w in words.iter_mut() {
hint_store_word(w as *mut u32);
#[cfg(target_os = "zkvm")]
hint_buffer_u32!(words.as_mut_ptr(), words.len());
#[cfg(not(target_os = "zkvm"))]
{
for w in words.iter_mut() {
hint_store_word(w as *mut u32);
}
self.bytes_remaining = new_remaining;
}
self.bytes_remaining = new_remaining;
Ok(())
} else {
Err(crate::serde::Error::DeserializeUnexpectedEnd)
Expand All @@ -43,11 +50,17 @@ impl WordRead for Reader {
return Err(crate::serde::Error::DeserializeUnexpectedEnd);
}
let mut num_padded_bytes = bytes.len();
let mut words = bytes.chunks_exact_mut(WORD_SIZE);
for word in &mut words {
hint_store_word(word as *mut [u8] as *mut u32);
#[cfg(target_os = "zkvm")]
hint_buffer_u32!(bytes as *mut [u8] as *mut u32, num_padded_bytes / WORD_SIZE);
#[cfg(not(target_os = "zkvm"))]
{
let mut words = bytes.chunks_exact_mut(WORD_SIZE);
for word in &mut words {
hint_store_word(word as *mut [u8] as *mut u32);
}
}
let remainder = words.into_remainder();

let remainder = bytes.chunks_exact_mut(WORD_SIZE).into_remainder();
if !remainder.is_empty() {
num_padded_bytes += WORD_SIZE - remainder.len();
let mut padded = MaybeUninit::<[u8; WORD_SIZE]>::uninit();
Expand Down
5 changes: 1 addition & 4 deletions extensions/ecc/sw-setup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ pub fn sw_declare(input: TokenStream) -> TokenStream {
#hint_decompress_extern_func(x as *const _ as usize, rec_id as *const u8 as usize);
let mut ptr = y.as_ptr() as *const u8;
// NOTE[jpw]: this loop could be unrolled using seq_macro and hint_store_u32(ptr, $imm)
for _ in (0..<#intmod_type as openvm_algebra_guest::IntMod>::NUM_LIMBS).step_by(4) {
openvm_rv32im_guest::hint_store_u32!(ptr, 0);
ptr = ptr.add(4);
}
openvm_rv32im_guest::hint_buffer_u32!(ptr, <#intmod_type as openvm_algebra_guest::IntMod>::NUM_LIMBS / 4);
y.assume_init()
}
}
Expand Down
9 changes: 3 additions & 6 deletions extensions/pairing/guest/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use {
core::mem::MaybeUninit,
openvm_platform::custom_insn_r,
openvm_rv32im_guest,
openvm_rv32im_guest::hint_buffer_u32,
};

use super::{Bls12_381, Fp, Fp12, Fp2};
Expand Down Expand Up @@ -329,12 +330,8 @@ impl PairingCheck for Bls12_381 {
rs1 = In &p_fat_ptr,
rs2 = In &q_fat_ptr
);
let mut ptr = hint.as_ptr() as *const u8;
// NOTE[jpw]: this loop could be unrolled using seq_macro and hint_store_u32(ptr, $imm)
for _ in (0..48 * 12 * 2).step_by(4) {
openvm_rv32im_guest::hint_store_u32!(ptr, 0);
ptr = ptr.add(4);
}
let ptr = hint.as_ptr() as *const u8;
hint_buffer_u32!(ptr, (48 * 12 * 2) / 4);
hint.assume_init()
}
}
Expand Down
8 changes: 3 additions & 5 deletions extensions/pairing/guest/src/bn254/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
crate::{PairingBaseFunct7, OPCODE, PAIRING_FUNCT3},
core::mem::MaybeUninit,
openvm_platform::custom_insn_r,
openvm_rv32im_guest::hint_buffer_u32,
};

use super::{Bn254, Fp, Fp12, Fp2};
Expand Down Expand Up @@ -361,12 +362,9 @@ impl PairingCheck for Bn254 {
rs1 = In &p_fat_ptr,
rs2 = In &q_fat_ptr
);
let mut ptr = hint.as_ptr() as *const u8;
let ptr = hint.as_ptr() as *const u8;
// NOTE[jpw]: this loop could be unrolled using seq_macro and hint_store_u32(ptr, $imm)
for _ in (0..32 * 12 * 2).step_by(4) {
openvm_rv32im_guest::hint_store_u32!(ptr, 0);
ptr = ptr.add(4);
}
hint_buffer_u32!(ptr, (32 * 12 * 2) / 4);
hint.assume_init()
}
}
Expand Down
1 change: 1 addition & 0 deletions extensions/rv32im/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ openvm-circuit-derive = { workspace = true }
openvm-instructions = { workspace = true }
openvm-rv32im-transpiler = { workspace = true }

bitcode.workspace = true
strum.workspace = true
itertools.workspace = true
tracing.workspace = true
Expand Down
Loading

0 comments on commit 208e00d

Please sign in to comment.