Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Feat/HINT_BUFFER opcode for Rv32 (#1256)" #1277

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

12 changes: 8 additions & 4 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_buffer_u32, hint_input, hint_store_u32};
use openvm_rv32im_guest::{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);
hint_store_u32!(addr, 0);
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);
hint_store_u32!(ptr, 0);
#[cfg(not(target_os = "zkvm"))]
unsafe {
*ptr = crate::host::read_u32();
Expand All @@ -68,7 +68,11 @@ pub(crate) fn read_vec_by_len(len: usize) -> Vec<u8> {
let ptr_start = unsafe { alloc::alloc::alloc(layout) };
let mut ptr = ptr_start;

hint_buffer_u32!(ptr, num_words);
// 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) };
}
unsafe { Vec::from_raw_parts(ptr_start, len, capacity) }
}
#[cfg(not(target_os = "zkvm"))]
Expand Down
27 changes: 7 additions & 20 deletions crates/toolchain/openvm/src/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 @@ -30,15 +28,10 @@ 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) {
#[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;
for w in words.iter_mut() {
hint_store_word(w as *mut u32);
}
self.bytes_remaining = new_remaining;
Ok(())
} else {
Err(crate::serde::Error::DeserializeUnexpectedEnd)
Expand All @@ -50,17 +43,11 @@ impl WordRead for Reader {
return Err(crate::serde::Error::DeserializeUnexpectedEnd);
}
let mut num_padded_bytes = bytes.len();
#[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 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 = bytes.chunks_exact_mut(WORD_SIZE).into_remainder();
let remainder = words.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: 4 additions & 1 deletion extensions/ecc/sw-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ 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)
openvm_rv32im_guest::hint_buffer_u32!(ptr, <#intmod_type as openvm_algebra_guest::IntMod>::NUM_LIMBS / 4);
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);
}
y.assume_init()
}
}
Expand Down
9 changes: 6 additions & 3 deletions extensions/pairing/guest/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 @@ -330,8 +329,12 @@ impl PairingCheck for Bls12_381 {
rs1 = In &p_fat_ptr,
rs2 = In &q_fat_ptr
);
let ptr = hint.as_ptr() as *const u8;
hint_buffer_u32!(ptr, (48 * 12 * 2) / 4);
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);
}
hint.assume_init()
}
}
Expand Down
8 changes: 5 additions & 3 deletions extensions/pairing/guest/src/bn254/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -362,9 +361,12 @@ impl PairingCheck for Bn254 {
rs1 = In &p_fat_ptr,
rs2 = In &q_fat_ptr
);
let ptr = hint.as_ptr() as *const u8;
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)
hint_buffer_u32!(ptr, (32 * 12 * 2) / 4);
for _ in (0..32 * 12 * 2).step_by(4) {
openvm_rv32im_guest::hint_store_u32!(ptr, 0);
ptr = ptr.add(4);
}
hint.assume_init()
}
}
Expand Down
1 change: 0 additions & 1 deletion extensions/rv32im/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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