Skip to content

Commit

Permalink
fix: fix bug in sign-extension function (#157)
Browse files Browse the repository at this point in the history
* Fix bug in sign extension function

* fix clippy

* fix fmt
  • Loading branch information
weilzkm authored Aug 12, 2024
1 parent 51e07f5 commit 3eae409
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
38 changes: 14 additions & 24 deletions emulator/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,9 @@ impl Memory {
Some(page) => page,
};

match self.rtrace.get(&page_index) {
None => {
self.rtrace.insert(page_index, page.borrow().clone().data);
}
Some(_) => {}
};
self.rtrace
.entry(page_index)
.or_insert_with(|| page.borrow().clone().data);

if level < 2 {
self.set_hash_trace(page_index, level + 1);
Expand All @@ -239,13 +236,12 @@ impl Memory {
// lookup in page
let page_addr = (addr as usize) & PAGE_ADDR_MASK;

match self.rtrace.get(&page_index) {
None => {
self.rtrace.insert(page_index, cached_page.data);
self.set_hash_trace(page_index, 0);
}
Some(_) => {}
if let std::collections::btree_map::Entry::Vacant(e) = self.rtrace.entry(page_index)
{
e.insert(cached_page.data);
self.set_hash_trace(page_index, 0);
};

u32::from_be_bytes(
(&cached_page.data[page_addr..page_addr + 4])
.try_into()
Expand Down Expand Up @@ -291,12 +287,9 @@ impl Memory {
}
};

match self.rtrace.get(&page_index) {
None => {
self.rtrace.insert(page_index, cached_page.borrow().data);
self.set_hash_trace(page_index, 0);
}
Some(_) => {}
if let std::collections::btree_map::Entry::Vacant(e) = self.rtrace.entry(page_index) {
e.insert(cached_page.borrow().data);
self.set_hash_trace(page_index, 0);
};

self.wtrace[0].insert(page_index, cached_page.clone());
Expand Down Expand Up @@ -346,12 +339,9 @@ impl Memory {
Some(page) => page,
};

match self.rtrace.get(&page_index) {
None => {
self.rtrace.insert(page_index, page.borrow().data);
self.set_hash_trace(page_index, 0);
}
Some(_) => {}
if let std::collections::btree_map::Entry::Vacant(e) = self.rtrace.entry(page_index) {
e.insert(page.borrow().data);
self.set_hash_trace(page_index, 0);
};

self.wtrace[0].insert(page_index, page.clone());
Expand Down
2 changes: 1 addition & 1 deletion emulator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ impl InstrumentedState {

/// se extends the number to 32 bit with sign.
fn sign_extension(dat: u32, idx: u32) -> u32 {
let is_signed = (dat >> (idx - 1)) != 0;
let is_signed = ((dat >> (idx - 1)) & 1) != 0;
let signed = ((1u32 << (32 - idx)) - 1) << idx;
let mask = (1u32 << idx) - 1;
if is_signed {
Expand Down
2 changes: 1 addition & 1 deletion prover/src/witness/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ pub(crate) fn generate_signext<F: Field>(
row.general.io_mut().rt_le = bits_le.try_into().unwrap();

let bits = bits as usize;
let is_signed = (in0 >> (bits - 1)) != 0;
let is_signed = ((in0 >> (bits - 1)) & 0x1) != 0;
let signed = ((1 << (32 - bits)) - 1) << bits;
let mask = (1 << bits) - 1;
let result = if is_signed {
Expand Down

0 comments on commit 3eae409

Please sign in to comment.