Extension of the ecdsa gem
This library is an extension of the ecdsa gem, which mainly speeds up the computation of points on elliptic curves by using projective rather than affine coordinates.
This gem was not written by a cryptography expert and has not been carefully checked as with the original gem. It is provided "as is" and it is the user's responsibility to make sure it will be suitable for the desired purpose.
In particular, this gem does not provide resistance against timing or other side-channel attacks.
Scalar multiplication uses a data-dependent NAF add/subtract pattern, the modular inverse is computed with
a variable-time algorithm, and Ruby's Integer arithmetic itself is not constant time.
Do not use this gem in an environment where an attacker can observe the timing of signing operations.
Installation
Add this line to your application's Gemfile:
gem 'ecdsa_ext'And then execute:
$ bundle install
Or install it yourself as:
$ gem install ecdsa_ext
Usage
Convert coordinate from affine to projective
require 'ecdsa_ext'
require 'securerandom'
group = ECDSA::Group::Secp256k1
private_key = 1 + SecureRandom.random_number(group.order - 1)
affine_point = group.generator * private_key
#<ECDSA::Point: secp256k1, 0x22a7d03cd6fec52e13d2713da6921cf8f374631ecea7d575d31c3f338a410ad, 0x530b82285b951582bc330fc0b1d26df56bf93277d1229676ab9c2d4749098a7c>
# convert to projective point
projective_point = affine_point.to_projective
#<ECDSA::Ext::ProjectivePoint:0x00007f45baa7f5b0 @group=#<ECDSA::Group:secp256k1>, @x=979696094695476041658010915065787178569931130816884020506645009594358960301, @y=37562300065191370074864991137132392549749230653372621152572375247509483260540, @z=1>Create directory
require 'ecdsa_ext'
require 'securerandom'
group = ECDSA::Group::Secp256k1
private_key = 1 + SecureRandom.random_number(group.order - 1)
projective_point = group.generator.to_projective * private_key
#<ECDSA::Ext::ProjectivePoint:0x00007f45baa7f5b0 @group=#<ECDSA::Group:secp256k1>, @x=979696094695476041658010915065787178569931130816884020506645009594358960301, @y=37562300065191370074864991137132392549749230653372621152572375247509483260540, @z=1>Operation
ECDSA::Ext::ProjectivePoint instance supports point addition, scalar multiplication and negation.
require 'ecdsa_ext'
# addition
projective_point3 = projective_point1 + projective_point2
# multiplication
projective_point4 = projective_point3 * 123
# negation
projective_point4_neg = projective_point4.negateConvert coordinate from projective to affine
require 'ecdsa_ext'
affine_point = projective_point4.to_affineUse jacobian coordinates
Jacobian coordinates have been supported since 0.3.0.
When using Jacobian coordinates, use ECDSA::Ext::JacobianPoint instead of ECDSA::Ext::ProjectivePoint.
In addition, ECDSA::Point now has a to_jacobian method that convert affine coordinates to jacobian coordinates.
Apply jacobian coordinates to existing ECDSA sign/verify
If you want the existing ECDSA gem to generate and verify signatures in Jacobian coordinates, add the following code. This code is a monkey patch to do the existing process in Jacobian coordinates.
require 'ecdsa/ext/sign_verify'Note that ECDSA.sign provided by this patch is not constant time (see the disclaimer at the top of this README).
It only pads the ephemeral key so that the number of loop iterations in scalar multiplication is fixed,
which is a partial mitigation and not a substitute for a constant-time implementation.