Home

Awesome

Ristretto255.js Build Status

Ristretto255.js is a pure-JS implementation of the Ristretto255 group operations, built on top of the popular TweetNaCl.js crypto library.

Overview

This project gives a high-level javascript API for operations in the ristretto255 prime-order group. The ristretto255 group enjoys the speed and safety of Curve25519 while also being prime-order, so that cryptographic protocols built on top of it will be resistant to cofactor-related attacks.

Installation

To install with the yarn package manager, simply run:

yarn add ristretto255

Usage

The main files of this repository include:

This library exports the following types of arithmetic operations:

Operations over a prime order group (ristretto255) of order L

The inputs to all of the functions below should be valid ristretto255 points (which can be checked by calling ristretto255.isValid()); otherwise, the behavior is undefined, and functions may throw exceptions.

All ristretto255 elements are stored in the serialized format as 32-element byte arrays of type Uint8Array(32).

Operations over scalars - big integers modulo L, where

L = 2^252 + 27742317777372353535851937790883648493.

Each scalar (a big integer modulo L) is of type Float64Array(32). Each of the 32 elements is at most 8 bits (auxiliary bits are needed to accommodate overflows during arithmetic operations).

Scalar operations implement simple school-book methods to optimize for a minimal javascript binary size.

Unsafe operations over Edwards EC points

Unsafe operations give a way to use the ristretto255 group more efficiently, but these APIs should be used with great care. It is recommended in the ristretto255 RFC that an implementation that does a number of group operations first transforms ristretto255 elements to EC points (unsafe.point.fromBytes), then performs necessary operations in the EC group (e.g. unsafe.point.scalarMult) and transforms the resulting EC point back to ristretto255 group (unsafe.point.toBytes). However since JavaScript does not provide type safety, it is easier to mix-up different types, thus we highly recommend the use of safe operations described in the previous sections instead. The implementations must not operate on internal (EC) representations other than in between unsafe.point.fromBytes and unsafe.point.toBytes functions.

The format for the EC point is four coordinates: [gf(), gf(), gf(), gf()], where each coordinate gf() = Float64Array(16) is a 16-element array, where each element has 16 least significant bits used.

Development and testing

  1. Clone this repository with git clone.

  2. cd ristretto255-js/

  3. To install the necessary dependencies, run yarn. (Note: Building this library requires node version >=6.9.0)

  4. To build ristretto255.min.js, run yarn build.

  5. To lint and test, run yarn lint and yarn test.

Benchmarks

To run the benchmarks in a browser, open ristretto255.benchmarks.html. Here are the benchmarks from a MacBook Pro (15-inch, 2018) with 2.9 GHz Intel Core i9:

ristretto255 groupscalar group
getRandom0.48 msscalar.getRandom0.01 ms
fromHash0.53 msscalar.invert2.60 ms
add0.41 msscalar.negate0.01 ms
sub0.41 msscalar.add0.02 ms
scalarMultBase3.47 msscalar.sub0.03 ms
scalarMult3.61 msscalar.mul0.01 ms
UNSAFE - Edwards EC group
unsafe.point.toBytes0.13 ms
unsafe.point.fromBytes0.13 ms
unsafe.point.getRandom0.26 ms
unsafe.point.fromHash0.27 ms
unsafe.point.add0.01 ms
unsafe.point.sub0.01 ms
unsafe.point.scalarMultBase3.30 ms
unsafe.point.scalarMult3.27 ms

System requirements

We inherit the limitations of TweetNaCl.js and support modern browsers that support the Window.crypto API and can generate cryptographically secure random numbers (which can be checked here).

This code can also be run with Node.js.

Contributors

The authors of this code are Valeria Nikolaenko (@valerini) and Kevin Lewi (@kevinlewi).

Acknowledgments

Special thanks go to Kostas Chalkias (@kchalkias) and David Wong (@mimoo) for reviewing and giving feedback, Henry de Valence (@hdevalence) for answering questions about ristretto255, Dmitry Chestnykh (@dchest) for extending TweetNaCl.js to support this library, and Kyle Summers (@KyleJSummers) for recommending javascript build tooling.

License

This project is MIT licensed.