Root out some unwraps

This commit is contained in:
David Palm 2023-11-10 08:53:08 +01:00
parent ec41b754a6
commit d2b5882ae9
No known key found for this signature in database
GPG Key ID: AC8A213E894DB26D
2 changed files with 43 additions and 21 deletions

View File

@ -117,7 +117,8 @@ impl<'a> Interpreter<'a> {
let mut result = Self {
kernel_mode: true,
jumpdests: find_jumpdests(code),
generation_state: GenerationState::new(GenerationInputs::default(), code).unwrap(),
generation_state: GenerationState::new(GenerationInputs::default(), code)
.expect("Default inputs are known-good"),
prover_inputs_map: prover_inputs,
context: 0,
halt_offsets: vec![DEFAULT_HALT_OFFSET],
@ -404,7 +405,7 @@ impl<'a> Interpreter<'a> {
0x5a => todo!(), // "GAS",
0x5b => self.run_jumpdest(), // "JUMPDEST",
x if (0x5f..0x80).contains(&x) => self.run_push(x - 0x5f), // "PUSH"
x if (0x80..0x90).contains(&x) => self.run_dup(x - 0x7f), // "DUP"
x if (0x80..0x90).contains(&x) => self.run_dup(x - 0x7f)?, // "DUP"
x if (0x90..0xa0).contains(&x) => self.run_swap(x - 0x8f)?, // "SWAP"
0xa0 => todo!(), // "LOG0",
0xa1 => todo!(), // "LOG1",
@ -488,7 +489,10 @@ impl<'a> Interpreter<'a> {
fn run_mulfp254(&mut self) {
let x = self.pop();
let y = self.pop();
self.push(U256::try_from(x.full_mul(y) % BN_BASE).unwrap());
self.push(
U256::try_from(x.full_mul(y) % BN_BASE)
.expect("BN_BASE is 254 bit so the U512 fits in a U256"),
);
}
fn run_subfp254(&mut self) {
@ -574,35 +578,40 @@ impl<'a> Interpreter<'a> {
}
fn run_addmod(&mut self) {
let x = U512::from(self.pop());
let y = U512::from(self.pop());
let z = U512::from(self.pop());
let x = self.pop();
let y = self.pop();
let z = self.pop();
self.push(if z.is_zero() {
U256::zero()
z
} else {
U256::try_from((x + y) % z).unwrap()
let (x, y, z) = (U512::from(x), U512::from(y), U512::from(z));
U256::try_from((x + y) % z)
.expect("Inputs are U256 and their sum mod a U256 fits in a U256.")
});
}
fn run_submod(&mut self) {
let x = U512::from(self.pop());
let y = U512::from(self.pop());
let z = U512::from(self.pop());
let x = self.pop();
let y = self.pop();
let z = self.pop();
self.push(if z.is_zero() {
U256::zero()
z
} else {
U256::try_from((z + x - y) % z).unwrap()
let (x, y, z) = (U512::from(x), U512::from(y), U512::from(z));
U256::try_from((z + x - y) % z)
.expect("Inputs are U256 and their difference mod a U256 fits in a U256.")
});
}
fn run_mulmod(&mut self) {
let x = self.pop();
let y = self.pop();
let z = U512::from(self.pop());
let z = self.pop();
self.push(if z.is_zero() {
U256::zero()
z
} else {
U256::try_from(x.full_mul(y) % z).unwrap()
U256::try_from(x.full_mul(y) % z)
.expect("Inputs are U256 and their product mod a U256 fits in a U256.")
});
}
@ -1008,7 +1017,14 @@ impl<'a> Interpreter<'a> {
}
fn run_pc(&mut self) {
self.push((self.generation_state.registers.program_counter - 1).into());
self.push(
(self
.generation_state
.registers
.program_counter
.saturating_sub(1))
.into(),
);
}
fn run_msize(&mut self) {
@ -1042,18 +1058,23 @@ impl<'a> Interpreter<'a> {
self.push(x);
}
fn run_dup(&mut self, n: u8) {
fn run_dup(&mut self, n: u8) -> anyhow::Result<()> {
if n == 0 {
self.push(self.stack_top());
} else {
self.push(stack_peek(&self.generation_state, n as usize - 1).unwrap());
self.push(
stack_peek(&self.generation_state, n as usize - 1)
.map_err(|_| anyhow!("Stack underflow."))?,
);
}
Ok(())
}
fn run_swap(&mut self, n: u8) -> anyhow::Result<()> {
let len = self.stack_len();
ensure!(len > n as usize);
let to_swap = stack_peek(&self.generation_state, n as usize).unwrap();
let to_swap = stack_peek(&self.generation_state, n as usize)
.map_err(|_| anyhow!("Stack underflow"))?;
self.stack_segment_mut()[len - n as usize - 1] = self.stack_top();
self.generation_state.registers.stack_top = to_swap;
Ok(())

View File

@ -105,7 +105,8 @@ pub(crate) fn all_mpt_prover_inputs(trie_inputs: &TrieInputs) -> Result<Vec<U256
.storage_tries
.iter()
.map(|(hashed_address, storage_trie)| {
let key = Nibbles::from_bytes_be(hashed_address.as_bytes()).unwrap();
let key = Nibbles::from_bytes_be(hashed_address.as_bytes())
.expect("An H256 is 32 bytes long");
(key, storage_trie)
})
.collect();