diff --git a/.travis.yml b/.travis.yml index e7d3020abb..2ebf2ea7df 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,21 +27,6 @@ script: - ITERS=10 py.test - mkdir _build && cd _build && cmake .. && make && cd .. -matrix: - include: - - env: BUILD=emscripten - script: - - cd emscripten && rm trezor-crypto.js && make docker-build && make test-correctness && cd .. - services: - - docker - sudo: required - install: true - compiler: true - addons: - apt: - packages: - - nodejs - notifications: webhooks: urls: diff --git a/emscripten/.gitignore b/emscripten/.gitignore deleted file mode 100644 index 84f814fcc0..0000000000 --- a/emscripten/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -benchmark-browserify.js diff --git a/emscripten/Makefile b/emscripten/Makefile deleted file mode 100644 index 11c07169f8..0000000000 --- a/emscripten/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -EMFLAGS = \ - -Os --closure 1 \ - --memory-init-file 0 \ - --pre-js pre.js --post-js post.js \ - -I ../ed25519-donna \ - -s EXPORTED_FUNCTIONS='["_hdnode_public_ckd_address_optimized", "_ecdsa_read_pubkey"]' - -SRC = ../bignum.c ../ecdsa.c ../secp256k1.c ../hmac.c ../bip32.c \ - ../base58.c ../ripemd160.c ../sha2.c ../rand.c ../address.c - -all: trezor-crypto.js benchmark-browserify.js - -trezor-crypto.js: $(SRC) - emcc $(EMFLAGS) -o $@ $^ - -benchmark-browserify.js: node_modules trezor-crypto.js benchmark.js - $(shell npm bin)/browserify benchmark.js -o $@ --noparse=`pwd`/trezor-crypto.js - @echo "open benchmark.html in your favourite browser" - -benchmark-node: node_modules trezor-crypto.js benchmark.js - node benchmark.js - -node_modules: - npm install - npm install browserify - -clean: - rm -f trezor-crypto.js benchmark-browserify.js - -docker: - docker run --rm -i -v $(shell pwd)/..:/src -t apiaryio/emcc /bin/bash - -docker-build: - docker run --rm -v $(shell pwd)/..:/src apiaryio/emcc /bin/bash -c 'cd emscripten && make' - -test-correctness: node_modules - node test-correctness.js test-correctness-segwit-p2sh.js diff --git a/emscripten/benchmark.html b/emscripten/benchmark.html deleted file mode 100644 index be2acc00ec..0000000000 --- a/emscripten/benchmark.html +++ /dev/null @@ -1,8 +0,0 @@ - -
- - - - - - diff --git a/emscripten/benchmark.js b/emscripten/benchmark.js deleted file mode 100644 index ed7ed8cfc4..0000000000 --- a/emscripten/benchmark.js +++ /dev/null @@ -1,98 +0,0 @@ -var crypto = require('./trezor-crypto'); -var bitcoin = require('bitcoinjs-lib'); - -var XPUB = - 'xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRm' + - 'F8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU'; -var node = bitcoin.HDNode.fromBase58(XPUB).derive(0); - -var nodeStruct = { - depth: node.depth, - child_num: node.index, - fingerprint: node.parentFingerprint, - chain_code: node.chainCode, - public_key: node.keyPair.getPublicKeyBuffer() -}; - -var suite; -var worker; - -if (typeof Worker !== 'undefined') { - console.log('enabling web worker benchmark'); - worker = new Worker('./trezor-crypto.js'); - worker.onerror = function (error) { - console.error('worker:', error); - }; - suite = [ - // benchBitcoinJS, - // benchBrowserify, - benchWorker - ]; -} else { - suite = [ - benchBitcoinJS, - benchBrowserify - ]; -} - -benchmark(suite, 1000, 1000); - -function benchmark(suite, delay, ops) { - (function cycle(i) { - setTimeout(function () { - var benchmark = suite[i]; - runBenchmark(benchmark, ops, function (runtime) { - printResult(benchmark, ops, runtime); - cycle(i+1 < suite.length ? i+1 : 0); - }); - }, delay); - }(0)); -} - -function benchBitcoinJS(ops, fn) { - var i; - for (i = 0; i < ops; i++) { - node.derive(i).getAddress(); - } - fn(); -} - -function benchBrowserify(ops, fn) { - var i; - crypto.serializeNode(nodeStruct); - for (i = 0; i < ops; i++) { - crypto.deriveAddress(i, 0); - } - fn(); -} - -function benchWorker(ops, fn) { - worker.onmessage = function (event) { - fn(); - }; - worker.postMessage({ - type: 'deriveAddressRange', - node: nodeStruct, - from: 0, - to: ops - 1, - version: 0 - }); -} - -function runBenchmark(benchmark, ops, fn) { - var start = new Date(); - benchmark(ops, function () { - var end = new Date(); - fn(end - start); - }); -} - -function printResult(benchmark, ops, runtime) { - var opssec = (ops / runtime) * 1000; - console.log( - benchmark.name, - 'ops #', ops, - 'runtime', runtime / 1000, - 'sec, ops/sec', opssec - ); -} diff --git a/emscripten/package.json b/emscripten/package.json deleted file mode 100644 index 16ad3c5cea..0000000000 --- a/emscripten/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "trezor-crypto-test", - "version": "1.0.0", - "description": "", - "dependencies": { - "bitcoinjs-lib": "^2.1.1" - } -} diff --git a/emscripten/post.js b/emscripten/post.js deleted file mode 100644 index ce2dfb7b06..0000000000 --- a/emscripten/post.js +++ /dev/null @@ -1,115 +0,0 @@ -/* - typedef struct { - uint32_t depth; - uint32_t fingerprint; - uint32_t child_num; - uint8_t chain_code[32]; - uint8_t private_key[32]; - uint8_t public_key[33]; - } HDNode; - */ - -var HEAPU8 = Module['HEAPU8']; -var _malloc = Module['_malloc']; -var _hdnode_public_ckd_address_optimized = Module['_hdnode_public_ckd_address_optimized']; -var _ecdsa_read_pubkey = Module['_ecdsa_read_pubkey']; -var Pointer_stringify = Module['Pointer_stringify']; - -// HDNode structs global -var PUBPOINT_SIZE = 2 * 9 * 4; // (2 * bignum256 (= 9 * uint32_t)) -var _pubpoint = _malloc(PUBPOINT_SIZE); -var PUBKEY_SIZE = 33; -var _pubkey = _malloc(PUBKEY_SIZE); -var CHAINCODE_SIZE = 32; -var _chaincode = _malloc(CHAINCODE_SIZE); - -// address string global -var ADDRESS_SIZE = 60; // maximum size -var _address = _malloc(ADDRESS_SIZE); - -/* - * public library interface - */ - -/** - * @param {HDNode} node HDNode struct, see the definition above - */ -function serializeNode(node) { - var u8_pubkey = new Uint8Array(33); - u8_pubkey.set(node['public_key'], 0); - HEAPU8.set(u8_pubkey, _pubkey); - - var u8_chaincode = new Uint8Array(32); - u8_chaincode.set(node['chain_code'], 0); - HEAPU8.set(u8_chaincode, _chaincode); - - _ecdsa_read_pubkey(0, _pubkey, _pubpoint); -} - -/** - * @param {Number} index BIP32 index of the address - * @param {Number} version address version byte - * @return {String} - */ -function deriveAddress(index, version, segwit) { - _hdnode_public_ckd_address_optimized(_pubpoint, _chaincode, index, version, _address, ADDRESS_SIZE, segwit); - return Pointer_stringify(_address); -} - -/** - * @param {HDNode} node HDNode struct, see the definition above - * @param {Number} firstIndex index of the first address - * @param {Number} lastIndex index of the last address - * @param {Number} version address version byte - * @return {Array>>0):0){p=b+72|0;o=p;o=qc(c[o>>2]|0,c[o+4>>2]|0,1,0)|0;c[p>>2]=o;c[p+4>>2]=C}}while(0);return}function Ib(b,d){b=b|0;d=d|0;var e=0,f=0,g=0,h=0,i=0;if(d){h=b+64|0;f=h;f=mc(c[f>>2]|0,c[f+4>>2]|0,3)|0;f=f&127;e=f+1|0;g=b+80|0;a[g+f>>0]=-128;if(e>>>0>112){lc(g+e|0,0,f^127|0)|0;e=0;do{f=b+80+(e<<3)|0;i=f;i=ma(c[i>>2]|0,c[i+4>>2]|0)|0;c[f>>2]=i;c[f+4>>2]=C;e=e+1|0}while((e|0)!=16);Gb(b,g,b);e=0}lc(g+e|0,0,112-e|0)|0;e=0;do{i=b+80+(e<<3)|0;f=i;f=ma(c[f>>2]|0,c[f+4>>2]|0)|0;c[i>>2]=f;c[i+4>>2]=C;e=e+1|0}while((e|0)!=14);f=b+72|0;e=c[f+4>>2]|0;i=b+192|0;c[i>>2]=c[f>>2];c[i+4>>2]=e;i=c[h+4>>2]|0;e=b+200|0;c[e>>2]=c[h>>2];c[e+4>>2]=i;Gb(b,g,b);e=0;do{i=b+(e<<3)|0;h=i;h=ma(c[h>>2]|0,c[h+4>>2]|0)|0;c[i>>2]=h;c[i+4>>2]=C;e=e+1|0}while((e|0)!=8);e=d;f=b;g=e+64|0;do{a[e>>0]=a[f>>0]|0;e=e+1|0;f=f+1|0}while((e|0)<(g|0))}lc(b|0,0,208)|0;return}function Jb(a,b,d){a=a|0;b=b|0;d=d|0;var e=0,f=0,g=0,h=0,j=0;g=i;i=i+208|0;e=g;f=e;h=8;j=f+64|0;do{c[f>>2]=c[h>>2];f=f+4|0;h=h+4|0}while((f|0)<(j|0));lc(e+64|0,0,144)|0;Hb(e,a,b);Ib(e,d);i=g;return}function Kb(){var a=0,b=0,d=0;d=i;i=i+16|0;b=d;a=c[9714]|0;if(!a){a=bc(38797,38810)|0;c[9714]=a}if((gc(b,1,4,a)|0)==4){i=d;return c[b>>2]|0}else pa(38812,38828,60,38838);return 0}function Lb(a){a=a|0;if(a>>>0>=256)if(a>>>0<65536)a=2;else a=a>>>0<16777216?3:4;else a=1;return a|0}function Mb(b,c){b=b|0;c=c|0;var d=0;if(b>>>0<=16777215)if(b>>>0<=65535){if(b>>>0>255)d=6}else d=4;else{a[c>>0]=b>>>24;c=c+1|0;d=4}if((d|0)==4){a[c>>0]=b>>>16;c=c+1|0;d=6}if((d|0)==6){a[c>>0]=b>>>8;c=c+1|0}a[c>>0]=b;return}function Nb(a){a=a|0;var b=0,d=0;b=i;i=i+16|0;d=b;c[d>>2]=c[a+60>>2];a=Qb(ha(6,d|0)|0)|0;i=b;return a|0}function Ob(b,d,e){b=b|0;d=d|0;e=e|0;var f=0,g=0,h=0,j=0,k=0,l=0,m=0,n=0;m=i;i=i+48|0;h=m+16|0;g=m;f=m+32|0;c[f>>2]=d;j=f+4|0;l=b+48|0;n=c[l>>2]|0;c[j>>2]=e-((n|0)!=0&1);k=b+44|0;c[f+8>>2]=c[k>>2];c[f+12>>2]=n;if(!(c[9715]|0)){c[h>>2]=c[b+60>>2];c[h+4>>2]=f;c[h+8>>2]=2;f=Qb(qa(145,h|0)|0)|0}else{ga(1,b|0);c[g>>2]=c[b+60>>2];c[g+4>>2]=f;c[g+8>>2]=2;f=Qb(qa(145,g|0)|0)|0;ba(0)}if((f|0)>=1){j=c[j>>2]|0;if(f>>>0>j>>>0){g=c[k>>2]|0;h=b+4|0;c[h>>2]=g;c[b+8>>2]=g+(f-j);if(!(c[l>>2]|0))f=e;else{c[h>>2]=g+1;a[d+(e+-1)>>0]=a[g>>0]|0;f=e}}}else{c[b>>2]=c[b>>2]|f&48^16;c[b+8>>2]=0;c[b+4>>2]=0}i=m;return f|0}function Pb(a,b,d){a=a|0;b=b|0;d=d|0;var e=0,f=0,g=0;f=i;i=i+32|0;g=f;e=f+20|0;c[g>>2]=c[a+60>>2];c[g+4>>2]=0;c[g+8>>2]=b;c[g+12>>2]=e;c[g+16>>2]=d;if((Qb(ja(140,g|0)|0)|0)<0){c[e>>2]=-1;a=-1}else a=c[e>>2]|0;i=f;return a|0}function Qb(a){a=a|0;if(a>>>0>4294963200){c[(Rb()|0)>>2]=0-a;a=-1}return a|0}function Rb(){var a=0;if(!(c[9715]|0))a=38904;else a=c[(uc()|0)+64>>2]|0;return a|0}function Sb(a){a=a|0;if(!(c[a+68>>2]|0))Tb(a);return}function Tb(a){a=a|0;return}function Ub(a,b,d){a=a|0;b=b|0;d=d|0;var e=0,f=0,g=0,h=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0;q=i;i=i+48|0;n=q+16|0;m=q;j=q+32|0;o=a+28|0;h=c[o>>2]|0;c[j>>2]=h;p=a+20|0;h=(c[p>>2]|0)-h|0;c[j+4>>2]=h;c[j+8>>2]=b;c[j+12>>2]=d;k=a+60|0;l=a+44|0;g=2;b=h+d|0;while(1){if(!(c[9715]|0)){c[n>>2]=c[k>>2];c[n+4>>2]=j;c[n+8>>2]=g;f=Qb(ra(146,n|0)|0)|0}else{ga(2,a|0);c[m>>2]=c[k>>2];c[m+4>>2]=j;c[m+8>>2]=g;f=Qb(ra(146,m|0)|0)|0;ba(0)}if((b|0)==(f|0)){b=6;break}if((f|0)<0){b=8;break}b=b-f|0;e=c[j+4>>2]|0;if(f>>>0<=e>>>0)if((g|0)==2){c[o>>2]=(c[o>>2]|0)+f;g=2;h=j}else h=j;else{h=c[l>>2]|0;c[o>>2]=h;c[p>>2]=h;f=f-e|0;g=g+-1|0;h=j+8|0;e=c[j+12>>2]|0}c[h>>2]=(c[h>>2]|0)+f;c[h+4>>2]=e-f;j=h}if((b|0)==6){n=c[l>>2]|0;c[a+16>>2]=n+(c[a+48>>2]|0);a=n;c[o>>2]=a;c[p>>2]=a}else if((b|0)==8){c[a+16>>2]=0;c[o>>2]=0;c[p>>2]=0;c[a>>2]=c[a>>2]|32;if((g|0)==2)d=0;else d=d-(c[j+4>>2]|0)|0}i=q;return d|0}function Vb(a){a=a|0;if(!(c[a+68>>2]|0))Tb(a);return}function Wb(b,d,e){b=b|0;d=d|0;e=e|0;var f=0,g=0;g=i;i=i+80|0;f=g;c[b+36>>2]=3;if((c[b>>2]&64|0)==0?(c[f>>2]=c[b+60>>2],c[f+4>>2]=21505,c[f+8>>2]=g+12,da(54,f|0)|0):0)a[b+75>>0]=-1;f=Ub(b,d,e)|0;i=g;return f|0}function Xb(b){b=b|0;var d=0,e=0;d=b+74|0;e=a[d>>0]|0;a[d>>0]=e+255|e;d=b+20|0;e=b+44|0;if((c[d>>2]|0)>>>0>(c[e>>2]|0)>>>0)va[c[b+36>>2]&7](b,0,0)|0;c[b+16>>2]=0;c[b+28>>2]=0;c[d>>2]=0;d=c[b>>2]|0;if(d&20)if(!(d&4))d=-1;else{c[b>>2]=d|32;d=-1}else{d=c[e>>2]|0;c[b+8>>2]=d;c[b+4>>2]=d;d=0}return d|0}function Yb(a){a=a|0;return 0}function Zb(b,d,e){b=b|0;d=d|0;e=e|0;var f=0,g=0,h=0,i=0;h=d&255;f=(e|0)!=0;a:do if(f&(b&3|0)!=0){g=d&255;while(1){if((a[b>>0]|0)==g<<24>>24){i=6;break a}b=b+1|0;e=e+-1|0;f=(e|0)!=0;if(!(f&(b&3|0)!=0)){i=5;break}}}else i=5;while(0);if((i|0)==5)if(f)i=6;else e=0;b:do if((i|0)==6){g=d&255;if((a[b>>0]|0)!=g<<24>>24){f=R(h,16843009)|0;c:do if(e>>>0>3)while(1){h=c[b>>2]^f;if((h&-2139062144^-2139062144)&h+-16843009|0)break;b=b+4|0;e=e+-4|0;if(e>>>0<=3){i=11;break c}}else i=11;while(0);if((i|0)==11)if(!e){e=0;break}while(1){if((a[b>>0]|0)==g<<24>>24)break b;b=b+1|0;e=e+-1|0;if(!e){e=0;break}}}}while(0);return (e|0?b:0)|0}function _b(b){b=b|0;var d=0,e=0,f=0;f=b;a:do if(!(f&3))e=4;else{d=f;while(1){if(!(a[b>>0]|0)){b=d;break a}b=b+1|0;d=b;if(!(d&3)){e=4;break}}}while(0);if((e|0)==4){while(1){d=c[b>>2]|0;if(!((d&-2139062144^-2139062144)&d+-16843009))b=b+4|0;else break}if((d&255)<<24>>24)do b=b+1|0;while((a[b>>0]|0)!=0)}return b-f|0}function $b(b,c){b=b|0;c=c|0;b=ac(b,c)|0;return ((a[b>>0]|0)==(c&255)<<24>>24?b:0)|0}function ac(b,d){b=b|0;d=d|0;var e=0,f=0,g=0;f=d&255;a:do if(!f)b=b+(_b(b)|0)|0;else{if(b&3){e=d&255;do{g=a[b>>0]|0;if(g<<24>>24==0?1:g<<24>>24==e<<24>>24)break a;b=b+1|0}while((b&3|0)!=0)}f=R(f,16843009)|0;e=c[b>>2]|0;b:do if(!((e&-2139062144^-2139062144)&e+-16843009))do{g=e^f;if((g&-2139062144^-2139062144)&g+-16843009|0)break b;b=b+4|0;e=c[b>>2]|0}while(!((e&-2139062144^-2139062144)&e+-16843009|0));while(0);e=d&255;while(1){g=a[b>>0]|0;if(g<<24>>24==0?1:g<<24>>24==e<<24>>24)break;else b=b+1|0}}while(0);return b|0}function bc(b,d){b=b|0;d=d|0;var e=0,f=0,g=0,h=0;g=i;i=i+32|0;f=g+16|0;e=g;if(Zb(38847,a[d>>0]|0,4)|0){h=cc(d)|0|32768;c[e>>2]=b;c[e+4>>2]=h;c[e+8>>2]=438;e=Qb(ka(5,e|0)|0)|0;if((e|0)>=0){b=dc(e,d)|0;if(!b){c[f>>2]=e;ha(6,f|0)|0;b=0}}else b=0}else{c[(Rb()|0)>>2]=22;b=0}i=g;return b|0}function cc(b){b=b|0;var c=0,d=0,e=0;d=($b(b,43)|0)==0;c=a[b>>0]|0;d=d?c<<24>>24!=114&1:2;e=($b(b,120)|0)==0;d=e?d:d|128;b=($b(b,101)|0)==0;b=b?d:d|524288;b=c<<24>>24==114?b:b|64;b=c<<24>>24==119?b|512:b;return (c<<24>>24==97?b|1024:b)|0}function dc(b,d){b=b|0;d=d|0;var e=0,f=0,g=0,h=0,j=0,k=0,l=0,m=0,n=0,o=0;o=i;i=i+112|0;n=o+40|0;l=o+24|0;k=o+16|0;g=o;m=o+52|0;f=a[d>>0]|0;if(Zb(38847,f<<24>>24,4)|0){e=hc(1144)|0;if(!e)e=0;else{h=e;j=h+112|0;do{c[h>>2]=0;h=h+4|0}while((h|0)<(j|0));if(!($b(d,43)|0))c[e>>2]=f<<24>>24==114?8:4;if($b(d,101)|0){c[g>>2]=b;c[g+4>>2]=2;c[g+8>>2]=1;ca(221,g|0)|0;f=a[d>>0]|0}if(f<<24>>24==97){c[k>>2]=b;c[k+4>>2]=3;f=ca(221,k|0)|0;if(!(f&1024)){c[l>>2]=b;c[l+4>>2]=4;c[l+8>>2]=f|1024;ca(221,l|0)|0}d=c[e>>2]|128;c[e>>2]=d}else d=c[e>>2]|0;c[e+60>>2]=b;c[e+44>>2]=e+120;c[e+48>>2]=1024;f=e+75|0;a[f>>0]=-1;if((d&8|0)==0?(c[n>>2]=b,c[n+4>>2]=21505,c[n+8>>2]=m,(da(54,n|0)|0)==0):0)a[f>>0]=10;c[e+32>>2]=4;c[e+36>>2]=3;c[e+40>>2]=2;c[e+12>>2]=1;if(!(c[9716]|0))c[e+76>>2]=-1;ea(38888);f=c[9721]|0;c[e+56>>2]=f;if(f|0)c[f+52>>2]=e;c[9721]=e;na(38888)}}else{c[(Rb()|0)>>2]=22;e=0}i=o;return e|0}function ec(a){a=a|0;var b=0,d=0;do if(a){if((c[a+76>>2]|0)<=-1){b=fc(a)|0;break}d=(Yb(a)|0)==0;b=fc(a)|0;if(!d)Tb(a)}else{if(!(c[9549]|0))b=0;else b=ec(c[9549]|0)|0;ea(38888);a=c[9721]|0;if(a)do{if((c[a+76>>2]|0)>-1)d=Yb(a)|0;else d=0;if((c[a+20>>2]|0)>>>0>(c[a+28>>2]|0)>>>0)b=fc(a)|0|b;if(d|0)Tb(a);a=c[a+56>>2]|0}while((a|0)!=0);na(38888)}while(0);return b|0}function fc(a){a=a|0;var b=0,d=0,e=0,f=0,g=0,h=0;b=a+20|0;h=a+28|0;if((c[b>>2]|0)>>>0>(c[h>>2]|0)>>>0?(va[c[a+36>>2]&7](a,0,0)|0,(c[b>>2]|0)==0):0)a=-1;else{d=a+4|0;e=c[d>>2]|0;f=a+8|0;g=c[f>>2]|0;if(e>>>0>>16&2;k=14-(q|r|k)+(D<