Only the NIF remains!
This commit is contained in:
parent
6229badcae
commit
30874296da
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "ecrecover"
|
||||
name = "nifecrecover"
|
||||
version = "0.1.0"
|
||||
authors = ["John Newby <john@newby.org>"]
|
||||
edition = "2018"
|
||||
|
49
src/lib.rs
49
src/lib.rs
@ -7,18 +7,10 @@ extern crate parity_bytes;
|
||||
#[macro_use]
|
||||
extern crate rustler;
|
||||
|
||||
use std::ffi::*;
|
||||
use ethcore_builtin::EcRecover;
|
||||
use crate::ethcore_builtin::Implementation;
|
||||
use c_vec::{CVec};
|
||||
use parity_bytes::BytesRef;
|
||||
use rustler::*;
|
||||
use rustler::types::atom::ok;
|
||||
use std::io::Write;
|
||||
use std::ptr::copy_nonoverlapping;
|
||||
|
||||
const INPUT_LENGTH: usize = 512;
|
||||
const OUTPUT_LENGTH: usize = 32;
|
||||
|
||||
mod atoms {
|
||||
rustler_atoms! {
|
||||
@ -36,49 +28,18 @@ rustler_export_nifs!(
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
fn on_load(env: Env, _load_info: Term) -> bool {
|
||||
fn on_load(_env: Env, _load_info: Term) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn nif_ecrecover<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {
|
||||
pub fn nif_ecrecover<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {
|
||||
let input: Binary = args[0].decode()?;
|
||||
let mut byte_ref = Vec::new();
|
||||
let ecrecover = EcRecover { };
|
||||
let result = match ecrecover.execute(input.as_slice(),
|
||||
let _result = match ecrecover.execute(input.as_slice(),
|
||||
&mut BytesRef::Flexible(&mut byte_ref)) {
|
||||
Ok(_) => (),
|
||||
Err(e) => return Err(rustler::Error::Atom("ecrecover failed")),
|
||||
Err(_e) => return Err(rustler::Error::Atom("ecrecover failed")),
|
||||
};
|
||||
Ok(byte_ref.as_slice().encode(env))
|
||||
}
|
||||
|
||||
/**
|
||||
* C interface to the ethereum ecrecover implementation. Returns 1 on success,
|
||||
* 0 on failure (in which case there are no guarantees as to what is in output.
|
||||
*
|
||||
* Memory allocated by caller, expected to be an array of bytes, as above-- 512 in,
|
||||
* 32 out.
|
||||
*/
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ecrecover(input: *const libc::c_uchar, output: *mut libc::c_uchar) -> i16 {
|
||||
let ecrecover = EcRecover { };
|
||||
let mut byte_ref = Vec::new();
|
||||
ecrecover.execute(unsafe { std::slice::from_raw_parts(input as *const u8, INPUT_LENGTH) },
|
||||
&mut BytesRef::Flexible(&mut byte_ref));
|
||||
let mut ptr: &mut[u8] = unsafe { std::slice::from_raw_parts_mut(output as *mut u8, OUTPUT_LENGTH) };
|
||||
if byte_ref.len() != OUTPUT_LENGTH {
|
||||
return 0;
|
||||
}
|
||||
ptr.copy_from_slice(byte_ref.as_slice());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert_eq!(2 + 2, 4);
|
||||
}
|
||||
Ok((atoms::ok(), byte_ref.as_slice()).encode(env))
|
||||
}
|
||||
|
@ -23,7 +23,17 @@ ecrecover_hex(Input) ->
|
||||
|
||||
|
||||
load() ->
|
||||
ok = erlang:load_nif("/home/newby/projects/ethereum/ecrecover/target/release/libecrecover", 0).
|
||||
erlang:display(file:get_cwd()),
|
||||
Dir = case code:priv_dir(nifecrecover) of
|
||||
{error, bad_name} ->
|
||||
filename:join(
|
||||
filename:dirname(
|
||||
filename:dirname(
|
||||
code:which(?MODULE))), "priv");
|
||||
D -> D
|
||||
end,
|
||||
SoName = filename:join(Dir, atom_to_list(?MODULE)),
|
||||
ok = erlang:load_nif(SoName, 0).
|
||||
|
||||
not_loaded(Line) ->
|
||||
erlang:nif_error({error, {not_loaded, [{module, ?MODULE}, {line, Line}]}}).
|
||||
|
Loading…
x
Reference in New Issue
Block a user