Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec: p256 #55

Open
14 of 29 tasks
mmcloughlin opened this issue Jul 14, 2019 · 1 comment
Open
14 of 29 tasks

ec: p256 #55

mmcloughlin opened this issue Jul 14, 2019 · 1 comment

Comments

@mmcloughlin
Copy link
Owner

mmcloughlin commented Jul 14, 2019

Implement the P-256 curve as the first end-to-end curve implementation. Operations:

Sub-tasks:

Performance:

@mmcloughlin
Copy link
Owner Author

mmcloughlin commented Jul 14, 2019

The elliptic.Curve interface to implement is

type Curve interface {
    // Params returns the parameters for the curve.
    Params() *CurveParams
    // IsOnCurve reports whether the given (x,y) lies on the curve.
    IsOnCurve(x, y *big.Int) bool
    // Add returns the sum of (x1,y1) and (x2,y2)
    Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)
    // Double returns 2*(x,y)
    Double(x1, y1 *big.Int) (x, y *big.Int)
    // ScalarMult returns k*(Bx,By) where k is a number in big-endian form.
    ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)
    // ScalarBaseMult returns k*G, where G is the base point of the group
    // and k is an integer in big-endian form.
    ScalarBaseMult(k []byte) (x, y *big.Int)
}

Coordinates

Use Jacobian coordinates under the hood.

x = X/Z^2
y = Y/Z^3

(Note converting affine to Jacobian we just take X = x, Y = y, Z = 1).

Add

The existing implementation uses add-2007-bl.

https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl

id           g1p/shortw/jacobian-3/addition/add-2007-bl
tag          add-2007-bl
class        g1p
shape        shortw
repr         jacobian-3
operation    addition
cost         11M + 5S + 9add + 4*2
source       2007 Bernstein--Lange; note that the improvement from 12M+4S to 11M+5S was already mentioned in 2001 Bernstein http://cr.yp.to/talks.html#2001.10.29
compute      Z1Z1 = Z1^2
             Z2Z2 = Z2^2
             U1 = X1 Z2Z2
             U2 = X2 Z1Z1
             S1 = Y1 Z2 Z2Z2
             S2 = Y2 Z1 Z1Z1
             H = U2-U1
             I = (2 H)^2
             J = H I
             r = 2 (S2-S1)
             V = U1 I
             X3 = r^2-J-2 V
             Y3 = r (V-X3)-2 S1 J
             Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) H
op3          Z1Z1 = Z1^2
             Z2Z2 = Z2^2
             U1 = X1*Z2Z2
             U2 = X2*Z1Z1
             t0 = Z2*Z2Z2
             S1 = Y1*t0
             t1 = Z1*Z1Z1
             S2 = Y2*t1
             H = U2-U1
             t2 = 2*H
             I = t2^2
             J = H*I
             t3 = S2-S1
             r = 2*t3
             V = U1*I
             t4 = r^2
             t5 = 2*V
             t6 = t4-J
             X3 = t6-t5
             t7 = V-X3
             t8 = S1*J
             t9 = 2*t8
             t10 = r*t7
             Y3 = t10-t9
             t11 = Z1+Z2
             t12 = t11^2
             t13 = t12-Z1Z1
             t14 = t13-Z2Z2
             Z3 = t14*H
inputs       X1 X2 Y1 Y2 Z1 Z2

The cloudflare p384 implementation uses add-1998-cmo

https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo

id           g1p/shortw/jacobian-3/addition/add-1998-cmo
tag          add-1998-cmo
class        g1p
shape        shortw
repr         jacobian-3
operation    addition
url          https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo
cost         10M + 5S + 4^3 + 6add + 1*2
source       1998 Cohen--Miyaji--Ono "Efficient elliptic curve exponentiation using mixed coordinates", formula (5)
compute      U1 = X1 Z2^2
             U2 = X2 Z1^2
             S1 = Y1 Z2^3
             S2 = Y2 Z1^3
             H = U2-U1
             r = S2-S1
             X3 = r^2-H^3-2 U1 H^2
             Y3 = r (U1 H^2-X3)-S1 H^3
             Z3 = Z1 Z2 H
op3          t0 = Z2^2
             U1 = X1*t0
             t1 = Z1^2
             U2 = X2*t1
             t2 = Z2^3
             S1 = Y1*t2
             t3 = Z1^3
             S2 = Y2*t3
             H = U2-U1
             r = S2-S1
             t4 = r^2
             t5 = H^3
             t6 = H^2
             t7 = U1*t6
             t8 = 2*t7
             t9 = t4-t5
             X3 = t9-t8
             t10 = H^2
             t11 = U1*t10
             t12 = t11-X3
             t13 = H^3
             t14 = S1*t13
             t15 = r*t12
             Y3 = t15-t14
             t16 = Z2*H
             Z3 = Z1*t16
inputs       X1 X2 Y1 Y2 Z1 Z2

Mixed Add

A mixed addition adds an affine point to a Jacobian point. In the EFD this is specified as an assumption that Z2 = 1, that is the second point's Z-coordinate is 1.

I cannot easily tell which formula the crypto/elliptic P-256 uses. I think cloudflare p384 uses madd-2007-bl

https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-madd-2007-bl

id           g1p/shortw/jacobian-3/addition/madd-2007-bl
tag          madd-2007-bl
class        g1p
shape        shortw
repr         jacobian-3
operation    addition
url          https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-madd-2007-bl
cost         7M + 4S + 9add + 3*2 + 1*4
source       2007 Bernstein--Lange
assume       Z2=1
compute      Z1Z1 = Z1^2
             U2 = X2 Z1Z1
             S2 = Y2 Z1 Z1Z1
             H = U2-X1
             HH = H^2
             I = 4 HH
             J = H I
             r = 2 (S2-Y1)
             V = X1 I
             X3 = r^2-J-2 V
             Y3 = r (V-X3)-2 Y1 J
             Z3 = (Z1+H)^2-Z1Z1-HH
op3          Z1Z1 = Z1^2
             U2 = X2*Z1Z1
             t0 = Z1*Z1Z1
             S2 = Y2*t0
             H = U2-X1
             HH = H^2
             I = 4*HH
             J = H*I
             t1 = S2-Y1
             r = 2*t1
             V = X1*I
             t2 = r^2
             t3 = 2*V
             t4 = t2-J
             X3 = t4-t3
             t5 = V-X3
             t6 = Y1*J
             t7 = 2*t6
             t8 = r*t5
             Y3 = t8-t7
             t9 = Z1+H
             t10 = t9^2
             t11 = t10-Z1Z1
             Z3 = t11-HH
inputs       X1 X2 Y1 Y2 Z1

Doubling

I cannot easily tell which formula the crypto/elliptic P-256 uses.

I believe the cloudflare p384 implementation uses

https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b

id           g1p/shortw/jacobian-3/doubling/dbl-2001-b
tag          dbl-2001-b
class        g1p
shape        shortw
repr         jacobian-3
operation    doubling
url          https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
cost         3M + 5S + 8add + 1*3 + 1*4 + 2*8
source       2001 Bernstein "A software implementation of NIST P-224"
appliesto    jacobian-3
compute      delta = Z1^2
             gamma = Y1^2
             beta = X1 gamma
             alpha = 3 (X1-delta) (X1+delta)
             X3 = alpha^2-8 beta
             Z3 = (Y1+Z1)^2-gamma-delta
             Y3 = alpha (4 beta-X3)-8 gamma^2
op3          delta = Z1^2
             gamma = Y1^2
             beta = X1*gamma
             t0 = X1-delta
             t1 = X1+delta
             t2 = t0*t1
             alpha = 3*t2
             t3 = alpha^2
             t4 = 8*beta
             X3 = t3-t4
             t5 = Y1+Z1
             t6 = t5^2
             t7 = t6-gamma
             Z3 = t7-delta
             t8 = 4*beta
             t9 = t8-X3
             t10 = gamma^2
             t11 = 8*t10
             t12 = alpha*t9
             Y3 = t12-t11
inputs       X1 Y1 Z1

mmcloughlin added a commit that referenced this issue Jul 15, 2019
mmcloughlin added a commit that referenced this issue Aug 3, 2019
mmcloughlin added a commit that referenced this issue Aug 4, 2019
Adds function to prune away instructions that are not needed to produce
required outputs.

Updates #55 #41
mmcloughlin added a commit that referenced this issue Aug 4, 2019
mmcloughlin added a commit that referenced this issue Aug 5, 2019
mmcloughlin added a commit that referenced this issue Aug 5, 2019
mmcloughlin added a commit that referenced this issue Aug 6, 2019
mmcloughlin added a commit that referenced this issue Aug 6, 2019
mmcloughlin added a commit that referenced this issue Aug 6, 2019
mmcloughlin added a commit that referenced this issue Aug 12, 2019
Previously this was actually built with a chain for p-3, not p-2.

Updates #55 #65
mmcloughlin added a commit that referenced this issue Aug 12, 2019
Previously this was not correctly propogating carries, causing errors in
some edge cases.

Updates #55 #47
mmcloughlin added a commit that referenced this issue Aug 12, 2019
mmcloughlin added a commit that referenced this issue Aug 12, 2019
mmcloughlin added a commit that referenced this issue Aug 12, 2019
mmcloughlin added a commit that referenced this issue Aug 12, 2019
mmcloughlin added a commit that referenced this issue Aug 12, 2019
Code generation for point operations on `g1p/shortw/jacobian-3`.

Updates #57 #55
mmcloughlin added a commit that referenced this issue Aug 24, 2019
mmcloughlin added a commit that referenced this issue Aug 24, 2019
mmcloughlin added a commit that referenced this issue Aug 28, 2019
Implements basic AST-based template libary. Uses it to generate curve operations for short weierstrass curves.

Updates #55 #38 
Closes #63
mmcloughlin added a commit that referenced this issue Sep 3, 2019
Utilizes montgomery field asm generation to produce arithmetic modulo
the group order N.

Introduces a gen/name package to control naming of field functions,
preventing conflicts with the fp field functions.

Updates #77 #55
mmcloughlin added a commit that referenced this issue Sep 4, 2019
Provides the bulk of an implementation of scalar multiplication for short Weierstrass curves. Follows Algorithm 1 in "Selecting Elliptic Curves for Cryptography: An Efficiency and Security Analysis" by Bos et al.

Note this is not quite complete. The final add needs to use a complete addition formula, and ensure that the zero scalar is handled correctly.

Updates #67 #55
mmcloughlin added a commit that referenced this issue Sep 5, 2019
Starts a directory of additional formulae to add to the explicit
formulas database. First addition is the Renes--Costello--Batina
complete addition formula.

Updates #76 #67 #55
mmcloughlin added a commit that referenced this issue Sep 14, 2019
Implements scalar inversion, satisfying the `crypto/ecdsa.invertable` interface.

Fixes #85 
Updates #55
mmcloughlin added a commit that referenced this issue Sep 14, 2019
Basic implementation of fixed-base scalar multiplication using
ScalarMult.

Updates #55
mmcloughlin added a commit that referenced this issue Sep 15, 2019
Replace point_test.go with a file generated from a template.

Updates #55
mmcloughlin added a commit that referenced this issue Sep 15, 2019
Adds test for add used as double.

Updates #55 #89
mmcloughlin added a commit that referenced this issue Sep 15, 2019
mmcloughlin added a commit that referenced this issue Sep 15, 2019
mmcloughlin added a commit that referenced this issue Sep 24, 2019
Introduces an additional Component type AsmFunction for formula
functions backed by an underlying assembly implementation.

Swaps out the main addition and doubling operations for shortw with
assembly versions.

Updates #93 #55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant