asmcrypto / asmcrypto.js

JavaScript Cryptographic Library with performance in mind.
MIT License
661 stars 182 forks source link

Am I doing something wrong: asmcrypto way slower than other libs with sha256 #146

Closed ericmorand closed 6 years ago

ericmorand commented 6 years ago

Hi.

I made a little benchmark to compare asmcrypto with two other popular node.js crypto libs and my tests show that it's way slower than them, which is unexpected considering that performance is the main point of your lib:

let asm = require('asmcrypto.js');

start = process.hrtime();

for (let i = 0; i < 100000; i++) {
    asm.SHA256
        .hex('abcdefghijklmnopqrstuvwxyz')
    ;
}

diff = process.hrtime(start);

console.log((diff[0] * 1e9 + diff[1]) / 1e6 + 'ms');

let shajs = require('sha.js');

start = process.hrtime();

for (let i = 0; i < 100000; i++) {
    shajs('sha256')
        .update('abcdefghijklmnopqrstuvwxyz')
        .digest('hex')
    ;
}

diff = process.hrtime(start);

console.log((diff[0] * 1e9 + diff[1]) / 1e6 + 'ms');

let crypto = require('crypto');

start = process.hrtime();

for (let i = 0; i < 100000; i++) {
    crypto
        .createHmac('sha256', '')
        .update('abcdefghijklmnopqrstuvwxyz')
        .digest('hex')
    ;
}

diff = process.hrtime(start);

console.log((diff[0] * 1e9 + diff[1]) / 1e6 + 'ms');

On my system:

asmcrypto: ~650ms sha.js: ~370ms crypto: ~300ms

Is there something I'm doing wrong here? I'm already thinkng that other crypto libs are extremely slow (for comparison, PHP hash function execute that test in ~70ms - yes, seventy!) and was hoping asmcrypto would solve that issue.

jochenonline commented 6 years ago

OS, Browser, Version? Did you check it with other browsers?

ericmorand commented 6 years ago

@jochenonline, no browser, server-side, nodejs 9.8.0.

jochenonline commented 6 years ago

Why don't you use the internal crypto functions?

https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options

ericmorand commented 6 years ago

@jochenonline, I do. But I was assuming your implementation would be faster.

vibornoff commented 6 years ago

It was... until 2014 when Crome and FF released WebCrypto API support.

jochenonline commented 6 years ago

@ericmorand : asmcrypto is asm.js / WebCrypto is binary. So on server side any binary implementation will beat asmcrypto.

ericmorand commented 6 years ago

Ooooh, sorry, I missed that part. Thanks for your clarification and patience.