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

Add the bignum addu256 and subu256 host funcs #25

Merged
merged 1 commit into from
Oct 3, 2019
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: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
log = "0.4"
env_logger = "0.7"
primitive-types = "0.6"
69 changes: 69 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate wasmi;
extern crate log;
extern crate env_logger;

use primitive_types::U256;
use rustc_hex::{FromHex, ToHex};
use serde::{Deserialize, Serialize};
use std::env;
Expand All @@ -29,6 +30,8 @@ const DEBUG_PRINT32_FUNC: usize = 6;
const DEBUG_PRINT64_FUNC: usize = 7;
const DEBUG_PRINTMEM_FUNC: usize = 8;
const DEBUG_PRINTMEMHEX_FUNC: usize = 9;
const BIGNUM_ADD256_FUNC: usize = 10;
const BIGNUM_SUB256_FUNC: usize = 11;

struct Runtime<'a> {
ticks_left: u32,
Expand Down Expand Up @@ -166,6 +169,64 @@ impl<'a> Externals for Runtime<'a> {
debug!("print.hex: {}", buf.to_hex());
Ok(None)
}
BIGNUM_ADD256_FUNC => {
let a_ptr: u32 = args.nth(0);
let b_ptr: u32 = args.nth(1);
let c_ptr: u32 = args.nth(2);

let mut a_raw = [0u8; 32];
let mut b_raw = [0u8; 32];
let mut c_raw = [0u8; 32];

let memory = self.memory.as_ref().expect("expects memory object");
memory
.get_into(a_ptr, &mut a_raw)
.expect("expects reading from memory to succeed");
memory
.get_into(b_ptr, &mut b_raw)
.expect("expects reading from memory to succeed");

let a = U256::from_big_endian(&a_raw);
let b = U256::from_big_endian(&b_raw);
let c = a.checked_add(b).expect("expects non-overflowing addition");
c.to_big_endian(&mut c_raw);

memory
.set(c_ptr, &c_raw)
.expect("expects writing to memory to succeed");

Ok(None)
}
BIGNUM_SUB256_FUNC => {
let a_ptr: u32 = args.nth(0);
let b_ptr: u32 = args.nth(1);
let c_ptr: u32 = args.nth(2);

let mut a_raw = [0u8; 32];
let mut b_raw = [0u8; 32];
let mut c_raw = [0u8; 32];

let memory = self.memory.as_ref().expect("expects memory object");
memory
.get_into(a_ptr, &mut a_raw)
.expect("expects reading from memory to succeed");
memory
.get_into(b_ptr, &mut b_raw)
.expect("expects reading from memory to succeed");

let a = U256::from_big_endian(&a_raw);
let b = U256::from_big_endian(&b_raw);
let c = a
.checked_sub(b)
.expect("expects non-overflowing subtraction");
c.to_big_endian(&mut c_raw);

memory
.set(c_ptr, &c_raw)
.expect("expects writing to memory to succeed");

Ok(None)
}
_ => panic!("unknown function index"),
}
}
Expand Down Expand Up @@ -220,6 +281,14 @@ impl<'a> ModuleImportResolver for RuntimeModuleImportResolver {
Signature::new(&[ValueType::I32, ValueType::I32][..], None),
DEBUG_PRINTMEMHEX_FUNC,
),
"bignum_add256" => FuncInstance::alloc_host(
Signature::new(&[ValueType::I32, ValueType::I32, ValueType::I32][..], None),
BIGNUM_ADD256_FUNC,
),
"bignum_sub256" => FuncInstance::alloc_host(
Signature::new(&[ValueType::I32, ValueType::I32, ValueType::I32][..], None),
BIGNUM_SUB256_FUNC,
),
_ => {
return Err(InterpreterError::Function(format!(
"host module doesn't export function with name {}",
Expand Down