We just released an optimized Elliptic Curve Digital Signature Algorithm (ECDSA) library for iOS!

GoNetwork
3 min readJul 27, 2018

--

https://xkcd.com/1691/

Overview

Today we are open sourcing and releasing RNFastSecp256k1; a react-native module which implements native ECDSA signing and recovery for nist-secp256k1 curve in addition to keccak-256 hashing improving performance over pure javascript libraries. Alongside we have released a react-native benchmark app to showcase the performance improvements and illustrates how developers may use the React-Native library in existing projects.

Problem

The first version of gonetworks framework encountered performance limitations with respect to ECDSA operations on iOS. Wherein we observed thousands of transactions/sec during development and testing on desktop platforms, the same release build for mobile suffered degradation limiting the framework to less than 5 transactions/sec.

Profiling & Understanding the Bottleneck

premature optimization is the root of all evil.” — DonaldKnuth

Early development strives for correctness above performance. Considering our experience with mobile development, we expected performance variance when creating a release bundle.

Early testing of the framework was oblivious to the performance slow down as a majority of the core logic was tested on desktop platforms. Furthermore, the react-native port leveraged the default devtool chain which further masked performance bottlenecks. An important implication of this is outlined in React-natives’ debug documentation; React Native JS code runs as a web worker inside chrome. Essentially, our mobile application code was executed in the desktops chrome js runtime.

To identify the culprit we switched gears to profiling our codebase. We avoided using React-Native devtools to prevent code from being executed in the chrome js runtime context. Alternatively, we went oldschool and rolled a simple logger. It was immediately apparent the hot-code existed in the ECDSA message signing and recovery code paths!

Solution Implementation

Our framework makes use of 3rd party libraries for ECDSA operations, particularly ethereumjs-util. The sign() and ecrecover() methods are implemented in pure javascript which provide extensive portability across platforms and pose no noticeable performance degradation on desktop. However, running the same methods in mobile context is catastrophic from a latency perspective.

We took it upon ourselves to contribute a high performance ECDSA library for React-Native. We provide native implementations to override the following ethereumjs-util function definitions:

  1. keccak256(data: hex_string) : hex_string
  2. ecrecover(data: hex_string,r:hex_string,s:hex_string,v:uint) : hex_string
  3. ecsign(data: hex_string,private_key: hex_string) : hex_string

Never roll your own crypto! The Native implementation leverages cross platfrom builds of:

  1. https://github.com/bitcoin-core/secp256k1
  2. https://github.com/coruus/keccak-tiny

The React-Native module includes build support for iphoneos and iphone-simulator; developers may use our library with either the ios simulator or physical iphone devices. Furthermore, we include release scripts to strip unnecessary compiled binaries from the FAT file during submission to the app store.

Currently, the module only supports iOS, android builds are in the works

Results

As mentioned earlier, we took the liberty to build a simple react-native benchmarking app that compares the performance of the pure js implementation in ethereumjs-util versus our native implementation. Feel free to run and compile the code to observe the difference in performance locally.

Benchmark Results

The results are quite astonishing. With our library, we can achieve the same level of performance on mobile as we have on desktop.

Overall, gonetwork-framework is capable of processing thousands off-chain transactions per second regardless of the target platform!

--

--

GoNetwork

A highly scalable, low cost mobile first network infrastructure for Ethereum. Winning team at #ETHWaterloo, the worlds largest Ethereum hackathon.