Skip to content

Commit

Permalink
Merge pull request #38 from ethereumjs/bn128-update
Browse files Browse the repository at this point in the history
Update upstream ethereum-bn128
  • Loading branch information
axic authored Feb 26, 2020
2 parents 8ac7be0 + 8bdd3bf commit 7cc22ab
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
rustflags = [
"-Clink-args=-s NO_DYNAMIC_EXECUTION=1",
"-Clink-args=-s NODEJS_CATCH_EXIT=0",
"-Clink-args=-s EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap']",
]
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ authors = ["holgerd77 <[email protected]>",
publish = false

[dependencies]
ethereum-bn128 = { git = "https://github.com/ewasm/ethereum-bn128.rs" }
parity-bytes = { git = "https://github.com/paritytech/parity-common" }
ethereum-bn128 = { git = "https://github.com/ewasm/ethereum-bn128.rs", version = "0.1.0" }
rustc-hex = "1.0"
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ all:
cargo build --target=asmjs-unknown-emscripten --release
mkdir -p lib
find target/asmjs-unknown-emscripten/release -type f -name "rustbn-js.js" | xargs -I {} cp {} lib/index.asm.js
@res=$$(sed -n '/run()$$/p' lib/index.asm.js | wc -l); \
@res=$$(sed -n '/run();$$/p' lib/index.asm.js | wc -l); \
if [ $$res == "0" ]; then \
echo "ERROR: could not find run() function in generated code"; \
exit 1; \
fi\

sed -ibak 's/run()$$/Module\["arguments"\]=\[\];run();module\.exports=Module;/' lib/index.asm.js
sed -ibak 's/run();$$/Module\["arguments"\]=\[\];run();module\.exports=Module;/' lib/index.asm.js

wasm:
cargo build --target=wasm32-unknown-emscripten --release
Expand Down
34 changes: 25 additions & 9 deletions lib/index.asm.js

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate ethereum_bn128;
extern crate parity_bytes as bytes;
extern crate rustc_hex;

use std::ffi::CStr;
Expand All @@ -8,16 +7,14 @@ use std::os::raw::c_char;
use rustc_hex::FromHex;
use rustc_hex::ToHex;

use bytes::BytesRef;

#[no_mangle]
pub fn ec_mul(input_hex_ptr: *const c_char) -> *const c_char {
let input_hex = unsafe { CStr::from_ptr(input_hex_ptr) };
let input_str: &str = input_hex.to_str().unwrap();
let input_parsed = FromHex::from_hex(input_str).unwrap();

let mut output = vec![0u8; 64];
match ethereum_bn128::bn128_mul(&input_parsed[..], &mut BytesRef::Fixed(&mut output[..])) {
let mut output = [0u8; 64];
match ethereum_bn128::bn128_mul(&input_parsed[..], &mut output) {
Ok(_) => {
let mut output_hex = output.to_hex();
output_hex.push_str("\0");
Expand All @@ -33,8 +30,8 @@ pub fn ec_add(input_hex_ptr: *const c_char) -> *const c_char {
let input_str: &str = input_hex.to_str().unwrap();
let input_parsed = FromHex::from_hex(input_str).unwrap();

let mut output = vec![0u8; 64];
match ethereum_bn128::bn128_add(&input_parsed[..], &mut BytesRef::Fixed(&mut output[..])) {
let mut output = [0u8; 64];
match ethereum_bn128::bn128_add(&input_parsed[..], &mut output) {
Ok(_) => {
let mut output_hex = output.to_hex();
output_hex.push_str("\0");
Expand All @@ -50,8 +47,8 @@ pub fn ec_pairing(input_hex_ptr: *const c_char) -> *const c_char {
let input_str: &str = input_hex.to_str().unwrap();
let input_parsed = FromHex::from_hex(input_str).unwrap();

let mut output = vec![0u8; 32];
match ethereum_bn128::bn128_pairing(&input_parsed[..], &mut BytesRef::Fixed(&mut output[..])) {
let mut output = [0u8; 32];
match ethereum_bn128::bn128_pairing(&input_parsed[..], &mut output) {
Ok(_) => {
let mut output_hex = output.to_hex();
output_hex.push_str("\0");
Expand Down

0 comments on commit 7cc22ab

Please sign in to comment.