Skip to content

Commit

Permalink
Add test in browser env (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid authored Mar 13, 2021
1 parent c372dac commit f436690
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 181 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/coverage
/node_modules
/target
/tests/browser

lib/secp256k1.wasm
package-lock.json
tests/bundle.js
/lib/secp256k1.wasm
/package-lock.json
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/target
/tests/browser
/tests/fixtures
/util/gen-fixtures/secp256k1

package-lock.json
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
build-wasm-cp = cp -f target/wasm32-unknown-unknown/$(1)/tiny_secp256k1_wasm.wasm lib/secp256k1.wasm
build-wasm-location = lib/secp256k1.wasm
build-wasm-cp = mkdir -p lib && cp -f target/wasm32-unknown-unknown/$(1)/secp256k1_wasm.wasm $(build-wasm-location)

build-wasm:
cargo build --target wasm32-unknown-unknown --release
$(call build-wasm-cp,release)
wasm-opt --strip-debug --strip-producers --output lib/secp256k1.wasm lib/secp256k1.wasm
node ./util/wasm-strip.js lib/secp256k1.wasm
wasm-opt -O4 --output lib/secp256k1.wasm lib/secp256k1.wasm
wasm-opt --strip-debug --strip-producers --output $(build-wasm-location) $(build-wasm-location)
node ./util/wasm-strip.js $(build-wasm-location)
wasm-opt -O4 --output $(build-wasm-location) $(build-wasm-location)

build-wasm-debug:
cargo build --target wasm32-unknown-unknown
$(call build-wasm-cp,debug)

clean:
rm -rf target node_modules tests/browser

format:
cargo-fmt
npx prettier -w .
Expand All @@ -20,5 +24,13 @@ lint:
cargo clippy --target wasm32-unknown-unknown
npx prettier -c .

test:
npx tape tests/index.js | npx tap-difflet -p
test: test-browser test-node

test-browser-build:
npx webpack build -c tests/browser.webpack.js

test-browser: build-wasm-debug test-browser-build
cat tests/browser/index.js | npx browser-run --static tests/browser | npx tap-difflet -p

test-node: build-wasm-debug
node --experimental-json-modules tests/index.js | npx tap-difflet -p
53 changes: 19 additions & 34 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const validate = require("./validate");
const wasm = require("./secp256k1");
import * as validate from "./validate.js";
import wasm from "./secp256k1.js";

export const __initializeContext = wasm.initializeContext;

const WASM_BUFFER = new Uint8Array(wasm.memory.buffer);
const WASM_PRIVATE_KEY_PTR = wasm.PRIVATE_INPUT.value;
Expand Down Expand Up @@ -56,19 +58,19 @@ function __isPoint(p) {
}
}

function isPoint(p) {
export function isPoint(p) {
return validate.isPoint(p) && __isPoint(p);
}

function isPointCompressed(p) {
export function isPointCompressed(p) {
return validate.isPointCompressed(p) && __isPoint(p);
}

function isPrivate(x) {
export function isPrivate(x) {
return validate.isPrivate(x);
}

function pointAdd(pA, pB, compressed) {
export function pointAdd(pA, pB, compressed) {
validate.validatePoint(pA);
validate.validatePoint(pB);
const outputlen = assumeCompression(compressed, pA);
Expand All @@ -85,7 +87,7 @@ function pointAdd(pA, pB, compressed) {
}
}

function pointAddScalar(p, tweak, compressed) {
export function pointAddScalar(p, tweak, compressed) {
validate.validatePoint(p);
validate.validateTweak(tweak);
let outputlen = assumeCompression(compressed, p);
Expand All @@ -102,7 +104,7 @@ function pointAddScalar(p, tweak, compressed) {
}
}

function pointCompress(p, compressed) {
export function pointCompress(p, compressed) {
validate.validatePoint(p);
const outputlen = assumeCompression(compressed, p);

Expand All @@ -115,7 +117,7 @@ function pointCompress(p, compressed) {
}
}

function pointFromScalar(d, compressed = true) {
export function pointFromScalar(d, compressed = true) {
validate.validatePrivate(d);
const outputlen = assumeCompression(compressed);

Expand All @@ -130,7 +132,7 @@ function pointFromScalar(d, compressed = true) {
}
}

function pointMultiply(p, tweak, compressed) {
export function pointMultiply(p, tweak, compressed) {
validate.validatePoint(p);
validate.validateTweak(tweak);
let outputlen = assumeCompression(compressed, p);
Expand All @@ -147,7 +149,7 @@ function pointMultiply(p, tweak, compressed) {
}
}

function privateAdd(d, tweak) {
export function privateAdd(d, tweak) {
validate.validatePrivate(d);
validate.validateTweak(tweak);

Expand All @@ -163,12 +165,12 @@ function privateAdd(d, tweak) {
}
}

function privateSub(d, tweak) {
export function privateSub(d, tweak) {
validate.validatePrivate(d);
validate.validateTweak(tweak);

// We can not pass zero tweak to WASM, because use `secp256k1_ec_seckey_negate` for tweak negate.
// (because zero is not valid seckey)
// We can not pass zero tweak to WASM, because WASM use `secp256k1_ec_seckey_negate` for tweak negate.
// (zero is not valid seckey)
if (validate.isZero(tweak)) {
return new Uint8Array(d);
}
Expand All @@ -185,11 +187,11 @@ function privateSub(d, tweak) {
}
}

function sign(h, d) {
export function sign(h, d) {
return signWithEntropy(h, d);
}

function signWithEntropy(h, d, e) {
export function signWithEntropy(h, d, e) {
validate.validateHash(h);
validate.validatePrivate(d);
validate.validateExtraData(e);
Expand All @@ -208,7 +210,7 @@ function signWithEntropy(h, d, e) {
}
}

function verify(h, Q, signature, strict = false) {
export function verify(h, Q, signature, strict = false) {
validate.validateHash(h);
validate.validatePoint(Q);
validate.validateSignature(signature);
Expand All @@ -224,20 +226,3 @@ function verify(h, Q, signature, strict = false) {
SIGNATURE_INPUT.fill(0);
}
}

module.exports = {
__initializeContext: wasm.initializeContext,
isPoint,
isPointCompressed,
isPrivate,
pointAdd,
pointAddScalar,
pointCompress,
pointFromScalar,
pointMultiply,
privateAdd,
privateSub,
sign,
signWithEntropy,
verify,
};
2 changes: 2 additions & 0 deletions lib/secp256k1.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as wasm from "./secp256k1.wasm";
export default wasm;
14 changes: 8 additions & 6 deletions lib/secp256k1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { readFileSync } = require("fs");
const { join } = require("path");
const validate_wasm = require("./validate_wasm");
import { readFileSync } from "fs";
import { URL } from "url";
import * as wasm_error from "./wasm_error.js";
import * as wasm_rand from "./wasm_rand.js";

const binary = readFileSync(join(__dirname, "secp256k1.wasm"));
const binary = readFileSync(new URL("secp256k1.wasm", import.meta.url));
const imports = {
"./validate_wasm.js": validate_wasm,
"./wasm_error.js": wasm_error,
"./wasm_rand.js": wasm_rand,
};

const mod = new WebAssembly.Module(binary);
const instance = new WebAssembly.Instance(mod, imports);

module.exports = instance.exports;
export default instance.exports;
60 changes: 19 additions & 41 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const {
import {
ERROR_BAD_PRIVATE,
ERROR_BAD_POINT,
ERROR_BAD_TWEAK,
throwError,
ERROR_BAD_HASH,
ERROR_BAD_EXTRA_DATA,
ERROR_BAD_SIGNATURE,
} = require("./validate_wasm");
} from "./wasm_error.js";

const PRIVATE_KEY_SIZE = 32;
const PUBLIC_KEY_COMPRESSED_SIZE = 33;
const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65;
const TWEAK_SIZE = 32;
const HASH_SIZE = 32;
const EXTRA_DATA_SIZE = 32;
const SIGNATURE_SIZE = 64;
export const PRIVATE_KEY_SIZE = 32;
export const PUBLIC_KEY_COMPRESSED_SIZE = 33;
export const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65;
export const TWEAK_SIZE = 32;
export const HASH_SIZE = 32;
export const EXTRA_DATA_SIZE = 32;
export const SIGNATURE_SIZE = 64;

const BN32_ZERO = new Uint8Array(32);
const BN32_N = new Uint8Array([
Expand Down Expand Up @@ -65,11 +65,11 @@ function cmpBN32(data1, data2) {
return 0;
}

function isZero(x) {
export function isZero(x) {
return cmpBN32(x, BN32_ZERO) === 0;
}

function isPrivate(x) {
export function isPrivate(x) {
return (
isUint8Array(x) &&
x.length === PRIVATE_KEY_SIZE &&
Expand All @@ -78,15 +78,15 @@ function isPrivate(x) {
);
}

function isPoint(p) {
export function isPoint(p) {
return (
isUint8Array(p) &&
(p.length === PUBLIC_KEY_COMPRESSED_SIZE ||
p.length === PUBLIC_KEY_UNCOMPRESSED_SIZE)
);
}

function isPointCompressed(p) {
export function isPointCompressed(p) {
return isUint8Array(p) && p.length === PUBLIC_KEY_COMPRESSED_SIZE;
}

Expand Down Expand Up @@ -115,48 +115,26 @@ function isSignature(signature) {
);
}

function validatePrivate(d) {
export function validatePrivate(d) {
if (!isPrivate(d)) throwError(ERROR_BAD_PRIVATE);
}

function validatePoint(p) {
export function validatePoint(p) {
if (!isPoint(p)) throwError(ERROR_BAD_POINT);
}

function validateTweak(tweak) {
export function validateTweak(tweak) {
if (!isTweak(tweak)) throwError(ERROR_BAD_TWEAK);
}

function validateHash(h) {
export function validateHash(h) {
if (!isHash(h)) throwError(ERROR_BAD_HASH);
}

function validateExtraData(e) {
export function validateExtraData(e) {
if (!isExtraData(e)) throwError(ERROR_BAD_EXTRA_DATA);
}

function validateSignature(signature) {
export function validateSignature(signature) {
if (!isSignature(signature)) throwError(ERROR_BAD_SIGNATURE);
}

module.exports = {
PRIVATE_KEY_SIZE,
PUBLIC_KEY_COMPRESSED_SIZE,
PUBLIC_KEY_UNCOMPRESSED_SIZE,
TWEAK_SIZE,
HASH_SIZE,
EXTRA_DATA_SIZE,
SIGNATURE_SIZE,

isZero,
isPrivate,
isPoint,
isPointCompressed,

validatePrivate,
validatePoint,
validateTweak,
validateHash,
validateExtraData,
validateSignature,
};
38 changes: 0 additions & 38 deletions lib/validate_wasm.js

This file was deleted.

Loading

0 comments on commit f436690

Please sign in to comment.