BANKEX / BulletproofJS

Set of JavaScript based tools and Ethereum Solidity contracts for BulletProof based range proofs and confidential transactions
https://bankexfoundation.org
Other
29 stars 16 forks source link

React Native bulletproof generation slowness #3

Closed darioAnongba closed 6 years ago

darioAnongba commented 6 years ago

Hi,

I've been using pretty heavily your library to create bulletproofs in web and mobile applications for my Master's thesis. The proof generation is quite fast on the browser and Node but extremely slow on mobile devices (I know it's not ideal at all to do cryptography in JS on mobile but rewriting the library in Native code would be too much work).

What I noticed is that it takes around 1 min to generate a proof of 16 bits on a Samsung S8. The vast majority of the time is spent in the "generateFromWitness" function. I think the reason is that it uses recursion, which may not be optimized in all JS engines. Do you think it would make sense not to use recursion if trying to increase performances, even if less elegant?

Here is a benchmark showing that you should never use recursion in JavaScript: https://stackoverflow.com/questions/24915428/does-javascript-performance-suffer-from-deep-recursion

Cheers, Dario

shamatar commented 6 years ago

Hello Dario.

I was using a Node to write and debug the code, so mobile behavior can suffer. I'll check your link and see what I can do. May be some modern JS transpilers can help you too?

In a meantime I'm working on crypto library in Swift language that will be able to do everything starting from fast modular arithmetics and up to pairing calculations, so you may have it just in time for your thesis.

Sincerely, Alex

shamatar commented 6 years ago

As I see people suggest to use .strict JS mode, that should be on by default due to library being written in TypeScript

P.S. Are you trying to write some confidential voting system?

darioAnongba commented 6 years ago

Hi Alex,

Thanks for the quick answer. I will try to modify the function and check the performances.

Native libraries for Android and iOS would be great! I will integrate them in React Native if you get them done.

I am working in a general purpose confidential transaction token on Ethereum, aiming to only hide the amounts and not necessarily the participants (at first). I also built a mobile application enabling the minting, transfer, withdrawal and burning of tokens. This token could be used for voting indeed. Major problems I am facing now is the gas cost and the performance issues.

shamatar commented 6 years ago

@darioAnongba

By the way, Dario, can you provide a little better trace for performance? May be the hottest code is located in elliptic.js library, that does most of EC arithmetics?

Sincerely, Alex

darioAnongba commented 6 years ago

@shamatar

Sadly I won't have time to do a complete benchmark of this as I won't have time before my project ends. Maybe after but I will start working on a Native implementation for mobile anyways.

What I did though is that I created an iterative method "generateProof" to replace the recursive one but sadly that didn't solve the performance issues so recursion may not be the problem here...

Sincerely, Dario

shamatar commented 6 years ago

@darioAnongba

I've pushed the initial working code to https://github.com/BANKEX/EllipticSwift, now I just need to implement bulletproofs. Should be easier to implement it the second time.

shamatar commented 6 years ago

@darioAnongba

By the way, what type of range prover do you need more? I can try to write it by the end of the day, quite curious what you can achieve in your project.

Sincerely, Alex

darioAnongba commented 6 years ago

@shamatar,

I currently use the MultiRangeProver most of the time because when creating a new transaction I need to prove 2 values, the value I'm sending and the change back to myself (UTXO system).

The only moment I use the RangeProver (single) is when I mint tokens. When minting tokens I only need to prove 1 value. I could mint in batches but I can't because of the 16 bits limit at the moment (otherwise it costs too much gas).

Cheers, Dario

shamatar commented 6 years ago

@darioAnongba

Please give it a try https://github.com/BANKEX/EllipticSwift

I did not test it yet, just implemented some basic functions for you. Start with a single RangeProof, I'll add more (and tests too) over the next few days

Cheers, Alex

darioAnongba commented 6 years ago

Hi Alex,

Thanks for the Swift library!

shamatar commented 6 years ago

@darioAnongba

Hello Dario.

You have closed this, although I was not satisfied with performance (and bugs) of the previous implementation, so here is a HUGE rewrite. Please use it instead.

P. S. Can you also send me a copy of your thesis after the defence, I'm quite curious what you have achieved.

Sincerely, Alex

darioAnongba commented 6 years ago

Hi @shamatar,

I closed this because a native implementation would solve the slowness problem. Sadly I won't be able to test the Swift implementation before finishing the project as I'm already late in redaction and I would need to write a React Native module that uses BulletproofSwift, implement and test it in the app.

I will not only send you a copy but add your work as a main reference (BulletproofJS and smart contracts for BulletproofVerifier). I will contact you in due time for this (if you are interested).

Cheers, Dario

shamatar commented 6 years ago

Hi @darioAnongba

I'm sorry that you couldn't make this cool project in time. Feel free to contact me for references, I'll definitely grant you all required formal permissions as well. If at some point you will want to continue you project I can try to give you few hints. At least for iOS devices you can use my swift library + web3swift to make a purely native implementation.

Sincerely, Alex