herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
450 stars 152 forks source link

Cache init params #134

Closed aguycalled closed 2 years ago

aguycalled commented 2 years ago

Is there any possibility to cache the params generated on Init() or any drawdown from doing so?

iOS' JavaScriptCore does not support WASM, so I'm being forced to build mcl-wasm with WASM=0 in order to be able to use it in a React Native app, but the initialisation time is too slow (other operations execute with an acceptable speed). Thinking that hardcoding the values could help speeding up boot time.

Thanks!

herumi commented 2 years ago

What parameters do you want to increase the speed of? If it is BLS12_381, then I think that some pre-computed values can be embedded.

aguycalled commented 2 years ago

Yes it would be BLS12-381. Could you provide me some guidance on how to do it? Thx

herumi commented 2 years ago

How can I build mcl without wasm and check it? On Linux, I modified mcl-wasm/Makefile and tested it.

-EMCC_OPT+=-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0
+EMCC_OPT+=-s WASM=0 -s NO_EXIT_RUNTIME=1 -s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0
>cat t.js
'use strict'
const mcl = require('./dist/index.js')
const assert = require('assert')
const { performance } = require('perf_hooks')

const curveTest = (curveType, name) => {
  const start = performance.now()
  mcl.init(curveType)
    .then(() => {
      try {
        const end = performance.now()
        console.log('time=' + (end - start))
      } catch (e) {
        console.log(`TEST FAIL ${e}`)
        assert(false)
      }
    })
}

curveTest(mcl.BLS12_381)
>node t.js
time=89.65

It does not look so slow.

aguycalled commented 2 years ago

Yes, that's the way I build it too.

On a iOS device with a React Native app, await mcl.init(mcl.BLS12_381) takes around 1 minute. The iOS simulator runs fast, so it must be something specific to running on iOS.

herumi commented 2 years ago

await mcl.init(mcl.BLS12_381) takes around 1 minute.

Can you profile which functions are slow?

aguycalled commented 2 years ago

I will try to do it, but not sure what's the right way to profile C++ code compiled as JavaScript (no WASM) running on an iOS device.

aguycalled commented 2 years ago

I've been able to look into this with more detail and the real cause of the slowness are some later calls to deserializeHexStrToG1, which takes 60ms each time, and not mcl.init.

herumi commented 2 years ago

May I close this issue because the reason for the slow initialization is not mcl.init?