Skip to content

Commit 9d0bfab

Browse files
update nistec
1 parent cbe21ec commit 9d0bfab

File tree

3 files changed

+280
-172
lines changed

3 files changed

+280
-172
lines changed

compiler/natives/src/crypto/elliptic/nistec.go

Lines changed: 7 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,10 @@ import (
1515
//gopherjs:purge for go1.19 without generics
1616
type nistPoint[T any] interface{}
1717

18-
type wrappedPoint interface {
19-
Bytes() []byte
20-
SetBytes(b []byte) (wrappedPoint, error)
21-
Add(w1, w2 wrappedPoint) wrappedPoint
22-
Double(w1 wrappedPoint) wrappedPoint
23-
ScalarMult(w1 wrappedPoint, scalar []byte) (wrappedPoint, error)
24-
ScalarBaseMult(scalar []byte) (wrappedPoint, error)
25-
}
26-
2718
// nistCurve replaces the generics with a version using the wrappedPoint
2819
// interface, then update all the method signatures to also use wrappedPoint.
2920
type nistCurve struct {
30-
newPoint func() wrappedPoint
21+
newPoint func() nistec.WrappedPoint
3122
params *CurveParams
3223
}
3324

@@ -38,10 +29,10 @@ func (curve *nistCurve) Params() *CurveParams
3829
func (curve *nistCurve) IsOnCurve(x, y *big.Int) bool
3930

4031
//gopherjs:override-signature
41-
func (curve *nistCurve) pointFromAffine(x, y *big.Int) (p wrappedPoint, err error)
32+
func (curve *nistCurve) pointFromAffine(x, y *big.Int) (p nistec.WrappedPoint, err error)
4233

4334
//gopherjs:override-signature
44-
func (curve *nistCurve) pointToAffine(p wrappedPoint) (x, y *big.Int)
35+
func (curve *nistCurve) pointToAffine(p nistec.WrappedPoint) (x, y *big.Int)
4536

4637
//gopherjs:override-signature
4738
func (curve *nistCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
@@ -68,46 +59,7 @@ func (curve *nistCurve) Unmarshal(data []byte) (x, y *big.Int)
6859
func (curve *nistCurve) UnmarshalCompressed(data []byte) (x, y *big.Int)
6960

7061
var p224 = &nistCurve{
71-
newPoint: newP224WrappedPoint,
72-
}
73-
74-
type p224Wrapper struct {
75-
point *nistec.P224Point
76-
}
77-
78-
func wrapP224(point *nistec.P224Point) wrappedPoint {
79-
return p224Wrapper{point: point}
80-
}
81-
82-
func newP224WrappedPoint() wrappedPoint {
83-
return wrapP224(nistec.NewP224Point())
84-
}
85-
86-
func (w p224Wrapper) Bytes() []byte {
87-
return w.point.Bytes()
88-
}
89-
90-
func (w p224Wrapper) SetBytes(b []byte) (wrappedPoint, error) {
91-
p, err := w.point.SetBytes(b)
92-
return wrapP224(p), err
93-
}
94-
95-
func (w p224Wrapper) Add(w1, w2 wrappedPoint) wrappedPoint {
96-
return wrapP224(w.point.Add(w1.(p224Wrapper).point, w2.(p224Wrapper).point))
97-
}
98-
99-
func (w p224Wrapper) Double(w1 wrappedPoint) wrappedPoint {
100-
return wrapP224(w.point.Double(w1.(p224Wrapper).point))
101-
}
102-
103-
func (w p224Wrapper) ScalarMult(w1 wrappedPoint, scalar []byte) (wrappedPoint, error) {
104-
p, err := w.point.ScalarMult(w1.(p224Wrapper).point, scalar)
105-
return wrapP224(p), err
106-
}
107-
108-
func (w p224Wrapper) ScalarBaseMult(scalar []byte) (wrappedPoint, error) {
109-
p, err := w.point.ScalarBaseMult(scalar)
110-
return wrapP224(p), err
62+
newPoint: nistec.NewP224WrappedPoint,
11163
}
11264

11365
type p256Curve struct {
@@ -116,131 +68,14 @@ type p256Curve struct {
11668

11769
var p256 = &p256Curve{
11870
nistCurve: nistCurve{
119-
newPoint: newP256WrappedPoint,
71+
newPoint: nistec.NewP256WrappedPoint,
12072
},
12173
}
12274

123-
type p256Wrapper struct {
124-
point *nistec.P256Point
125-
}
126-
127-
func wrapP256(point *nistec.P256Point) wrappedPoint {
128-
return p256Wrapper{point: point}
129-
}
130-
131-
func newP256WrappedPoint() wrappedPoint {
132-
return wrapP256(nistec.NewP256Point())
133-
}
134-
135-
func (w p256Wrapper) Bytes() []byte {
136-
return w.point.Bytes()
137-
}
138-
139-
func (w p256Wrapper) SetBytes(b []byte) (wrappedPoint, error) {
140-
p, err := w.point.SetBytes(b)
141-
return wrapP256(p), err
142-
}
143-
144-
func (w p256Wrapper) Add(w1, w2 wrappedPoint) wrappedPoint {
145-
return wrapP256(w.point.Add(w1.(p256Wrapper).point, w2.(p256Wrapper).point))
146-
}
147-
148-
func (w p256Wrapper) Double(w1 wrappedPoint) wrappedPoint {
149-
return wrapP256(w.point.Double(w1.(p256Wrapper).point))
150-
}
151-
152-
func (w p256Wrapper) ScalarMult(w1 wrappedPoint, scalar []byte) (wrappedPoint, error) {
153-
p, err := w.point.ScalarMult(w1.(p256Wrapper).point, scalar)
154-
return wrapP256(p), err
155-
}
156-
157-
func (w p256Wrapper) ScalarBaseMult(scalar []byte) (wrappedPoint, error) {
158-
p, err := w.point.ScalarBaseMult(scalar)
159-
return wrapP256(p), err
160-
}
161-
16275
var p521 = &nistCurve{
163-
newPoint: newP521WrappedPoint,
164-
}
165-
166-
type p521Wrapper struct {
167-
point *nistec.P521Point
168-
}
169-
170-
func wrapP521(point *nistec.P521Point) wrappedPoint {
171-
return p521Wrapper{point: point}
172-
}
173-
174-
func newP521WrappedPoint() wrappedPoint {
175-
return wrapP521(nistec.NewP521Point())
176-
}
177-
178-
func (w p521Wrapper) Bytes() []byte {
179-
return w.point.Bytes()
180-
}
181-
182-
func (w p521Wrapper) SetBytes(b []byte) (wrappedPoint, error) {
183-
p, err := w.point.SetBytes(b)
184-
return wrapP521(p), err
185-
}
186-
187-
func (w p521Wrapper) Add(w1, w2 wrappedPoint) wrappedPoint {
188-
return wrapP521(w.point.Add(w1.(p521Wrapper).point, w2.(p521Wrapper).point))
189-
}
190-
191-
func (w p521Wrapper) Double(w1 wrappedPoint) wrappedPoint {
192-
return wrapP521(w.point.Double(w1.(p521Wrapper).point))
193-
}
194-
195-
func (w p521Wrapper) ScalarMult(w1 wrappedPoint, scalar []byte) (wrappedPoint, error) {
196-
p, err := w.point.ScalarMult(w1.(p521Wrapper).point, scalar)
197-
return wrapP521(p), err
198-
}
199-
200-
func (w p521Wrapper) ScalarBaseMult(scalar []byte) (wrappedPoint, error) {
201-
p, err := w.point.ScalarBaseMult(scalar)
202-
return wrapP521(p), err
76+
newPoint: nistec.NewP521WrappedPoint,
20377
}
20478

20579
var p384 = &nistCurve{
206-
newPoint: newP384WrappedPoint,
207-
}
208-
209-
type p384Wrapper struct {
210-
point *nistec.P384Point
211-
}
212-
213-
func wrapP384(point *nistec.P384Point) wrappedPoint {
214-
return p384Wrapper{point: point}
215-
}
216-
217-
func newP384WrappedPoint() wrappedPoint {
218-
return wrapP384(nistec.NewP384Point())
219-
}
220-
221-
func (w p384Wrapper) Bytes() []byte {
222-
return w.point.Bytes()
223-
}
224-
225-
func (w p384Wrapper) SetBytes(b []byte) (wrappedPoint, error) {
226-
p, err := w.point.SetBytes(b)
227-
return wrapP384(p), err
228-
}
229-
230-
func (w p384Wrapper) Add(w1, w2 wrappedPoint) wrappedPoint {
231-
return wrapP384(w.point.Add(w1.(p384Wrapper).point, w2.(p384Wrapper).point))
232-
}
233-
234-
func (w p384Wrapper) Double(w1 wrappedPoint) wrappedPoint {
235-
return wrapP384(w.point.Double(w1.(p384Wrapper).point))
236-
}
237-
238-
func (w p384Wrapper) ScalarMult(w1 wrappedPoint, scalar []byte) (wrappedPoint, error) {
239-
p, err := w.point.ScalarMult(w1.(p384Wrapper).point, scalar)
240-
return wrapP384(p), err
241-
}
242-
243-
func (w p384Wrapper) ScalarBaseMult(scalar []byte) (wrappedPoint, error) {
244-
p, err := w.point.ScalarBaseMult(scalar)
245-
return wrapP384(p), err
80+
newPoint: nistec.NewP384WrappedPoint,
24681
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//go:build js
2+
// +build js
3+
4+
package nistec_test
5+
6+
import (
7+
"crypto/elliptic"
8+
"testing"
9+
)
10+
11+
func TestAllocations(t *testing.T) {
12+
t.Skip("testing.AllocsPerRun not supported in GopherJS")
13+
}
14+
15+
//gopherjs:purge
16+
type nistPoint[T any] interface{}
17+
18+
func TestEquivalents(t *testing.T) {
19+
t.Run("P224", func(t *testing.T) {
20+
testEquivalents(t, nistec.NewP224WrappedPoint, nistec.NewP224WrappedGenerator, elliptic.P224())
21+
})
22+
t.Run("P256", func(t *testing.T) {
23+
testEquivalents(t, nistec.NewP256WrappedPoint, nistec.NewP256WrappedGenerator, elliptic.P256())
24+
})
25+
t.Run("P384", func(t *testing.T) {
26+
testEquivalents(t, nistec.NewP384WrappedPoint, nistec.NewP384WrappedGenerator, elliptic.P384())
27+
})
28+
t.Run("P521", func(t *testing.T) {
29+
testEquivalents(t, nistec.NewP521WrappedPoint, nistec.NewP521WrappedGenerator, elliptic.P521())
30+
})
31+
}
32+
33+
//gopherjs:override-signature
34+
func testEquivalents(t *testing.T, newPoint, newGenerator func() WrappedPoint, c elliptic.Curve) {}
35+
36+
func TestScalarMult(t *testing.T) {
37+
t.Run("P224", func(t *testing.T) {
38+
testScalarMult(t, nistec.NewP224WrappedPoint, nistec.NewP224WrappedGenerator, elliptic.P224())
39+
})
40+
t.Run("P256", func(t *testing.T) {
41+
testScalarMult(t, nistec.NewP256WrappedPoint, nistec.NewP256WrappedGenerator, elliptic.P256())
42+
})
43+
t.Run("P384", func(t *testing.T) {
44+
testScalarMult(t, nistec.NewP384WrappedPoint, nistec.NewP384WrappedGenerator, elliptic.P384())
45+
})
46+
t.Run("P521", func(t *testing.T) {
47+
testScalarMult(t, nistec.NewP521WrappedPoint, nistec.NewP521WrappedGenerator, elliptic.P521())
48+
})
49+
}
50+
51+
//gopherjs:override-signature
52+
func testScalarMult(t *testing.T, newPoint, newGenerator func() WrappedPoint, c elliptic.Curve)
53+
54+
func BenchmarkScalarMult(b *testing.B) {
55+
b.Run("P224", func(b *testing.B) {
56+
benchmarkScalarMult(b, nistec.NewP224WrappedGenerator(), 28)
57+
})
58+
b.Run("P256", func(b *testing.B) {
59+
benchmarkScalarMult(b, nistec.NewP256GWrappedenerator(), 32)
60+
})
61+
b.Run("P384", func(b *testing.B) {
62+
benchmarkScalarMult(b, nistec.NewP384WrappedGenerator(), 48)
63+
})
64+
b.Run("P521", func(b *testing.B) {
65+
benchmarkScalarMult(b, nistec.NewP521WrappedGenerator(), 66)
66+
})
67+
}
68+
69+
//gopherjs:override-signature
70+
func benchmarkScalarMult(b *testing.B, p WrappedPoint, scalarSize int)
71+
72+
func BenchmarkScalarBaseMult(b *testing.B) {
73+
b.Run("P224", func(b *testing.B) {
74+
benchmarkScalarBaseMult(b, nistec.NewP22Wrapped4Generator(), 28)
75+
})
76+
b.Run("P256", func(b *testing.B) {
77+
benchmarkScalarBaseMult(b, nistec.NewP256WrappedGenerator(), 32)
78+
})
79+
b.Run("P384", func(b *testing.B) {
80+
benchmarkScalarBaseMult(b, nistec.NewP384WrappedGenerator(), 48)
81+
})
82+
b.Run("P521", func(b *testing.B) {
83+
benchmarkScalarBaseMult(b, nistec.NewP521GWrappedenerator(), 66)
84+
})
85+
}
86+
87+
//gopherjs:override-signature
88+
func benchmarkScalarBaseMult(b *testing.B, p WrappedPoint, scalarSize int)

0 commit comments

Comments
 (0)