Skip to content

Commit 2b01410

Browse files
authored
Merge pull request #1312 from Workiva/updatingCrypto
[go1.20] Updating generics overrides for crypto
2 parents 5b0b675 + f9cbb03 commit 2b01410

File tree

7 files changed

+218
-26
lines changed

7 files changed

+218
-26
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build js
2+
// +build js
3+
4+
package ecdh
5+
6+
import (
7+
"crypto/internal/nistec"
8+
"io"
9+
)
10+
11+
//gopherjs:purge for go1.20 without generics
12+
type nistPoint[T any] interface{}
13+
14+
// temporarily replacement of `nistCurve[Point nistPoint[Point]]` for go1.20 without generics.
15+
type nistCurve struct {
16+
name string
17+
newPoint func() nistec.WrappedPoint
18+
scalarOrder []byte
19+
}
20+
21+
//gopherjs:override-signature
22+
func (c *nistCurve) String() string
23+
24+
//gopherjs:override-signature
25+
func (c *nistCurve) GenerateKey(rand io.Reader) (*PrivateKey, error)
26+
27+
//gopherjs:override-signature
28+
func (c *nistCurve) NewPrivateKey(key []byte) (*PrivateKey, error)
29+
30+
//gopherjs:override-signature
31+
func (c *nistCurve) privateKeyToPublicKey(key *PrivateKey) *PublicKey
32+
33+
//gopherjs:override-signature
34+
func (c *nistCurve) NewPublicKey(key []byte) (*PublicKey, error)
35+
36+
//gopherjs:override-signature
37+
func (c *nistCurve) ecdh(local *PrivateKey, remote *PublicKey) ([]byte, error)
38+
39+
// temporarily replacement for go1.20 without generics.
40+
var p256 = &nistCurve{
41+
name: "P-256",
42+
newPoint: nistec.NewP256WrappedPoint,
43+
scalarOrder: p256Order,
44+
}
45+
46+
// temporarily replacement for go1.20 without generics.
47+
var p384 = &nistCurve{
48+
name: "P-384",
49+
newPoint: nistec.NewP384WrappedPoint,
50+
scalarOrder: p384Order,
51+
}
52+
53+
// temporarily replacement for go1.20 without generics.
54+
var p521 = &nistCurve{
55+
name: "P-521",
56+
newPoint: nistec.NewP521WrappedPoint,
57+
scalarOrder: p521Order,
58+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//go:build js
2+
// +build js
3+
4+
package ecdsa
5+
6+
import (
7+
"crypto/elliptic"
8+
"crypto/internal/bigmod"
9+
"crypto/internal/nistec"
10+
"io"
11+
"math/big"
12+
)
13+
14+
//gopherjs:override-signature
15+
func generateNISTEC(c *nistCurve, rand io.Reader) (*PrivateKey, error)
16+
17+
//gopherjs:override-signature
18+
func randomPoint(c *nistCurve, rand io.Reader) (k *bigmod.Nat, p nistec.WrappedPoint, err error)
19+
20+
//gopherjs:override-signature
21+
func signNISTEC(c *nistCurve, priv *PrivateKey, csprng io.Reader, hash []byte) (sig []byte, err error)
22+
23+
//gopherjs:override-signature
24+
func inverse(c *nistCurve, kInv, k *bigmod.Nat)
25+
26+
//gopherjs:override-signature
27+
func hashToNat(c *nistCurve, e *bigmod.Nat, hash []byte)
28+
29+
//gopherjs:override-signature
30+
func verifyNISTEC(c *nistCurve, pub *PublicKey, hash, sig []byte) bool
31+
32+
//gopherjs:purge for go1.20 without generics
33+
type nistPoint[T any] interface{}
34+
35+
// temporarily replacement of `nistCurve[Point nistPoint[Point]]` for go1.20 without generics.
36+
type nistCurve struct {
37+
newPoint func() nistec.WrappedPoint
38+
curve elliptic.Curve
39+
N *bigmod.Modulus
40+
nMinus2 []byte
41+
}
42+
43+
//gopherjs:override-signature
44+
func (curve *nistCurve) pointFromAffine(x, y *big.Int) (p nistec.WrappedPoint, err error)
45+
46+
//gopherjs:override-signature
47+
func (curve *nistCurve) pointToAffine(p nistec.WrappedPoint) (x, y *big.Int, err error)
48+
49+
var _p224 *nistCurve
50+
51+
func p224() *nistCurve {
52+
p224Once.Do(func() {
53+
_p224 = &nistCurve{
54+
newPoint: nistec.NewP224WrappedPoint,
55+
}
56+
precomputeParams(_p224, elliptic.P224())
57+
})
58+
return _p224
59+
}
60+
61+
var _p256 *nistCurve
62+
63+
func p256() *nistCurve {
64+
p256Once.Do(func() {
65+
_p256 = &nistCurve{
66+
newPoint: nistec.NewP256WrappedPoint,
67+
}
68+
precomputeParams(_p256, elliptic.P256())
69+
})
70+
return _p256
71+
}
72+
73+
var _p384 *nistCurve
74+
75+
func p384() *nistCurve {
76+
p384Once.Do(func() {
77+
_p384 = &nistCurve{
78+
newPoint: nistec.NewP384WrappedPoint,
79+
}
80+
precomputeParams(_p384, elliptic.P384())
81+
})
82+
return _p384
83+
}
84+
85+
var _p521 *nistCurve
86+
87+
func p521() *nistCurve {
88+
p521Once.Do(func() {
89+
_p521 = &nistCurve{
90+
newPoint: nistec.NewP521WrappedPoint,
91+
}
92+
precomputeParams(_p521, elliptic.P521())
93+
})
94+
return _p521
95+
}
96+
97+
//gopherjs:override-signature
98+
func precomputeParams(c *nistCurve, curve elliptic.Curve)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build js
2+
// +build js
3+
4+
package ecdsa
5+
6+
import "testing"
7+
8+
//gopherjs:override-signature
9+
func testRandomPoint(t *testing.T, c *nistCurve)
10+
11+
//gopherjs:override-signature
12+
func testHashToNat(t *testing.T, c *nistCurve)

compiler/natives/src/crypto/internal/boring/bcache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func (c *Cache) Put(k, v unsafe.Pointer) {}
2020
//gopherjs:purge
2121
func (c *Cache) table() *[cacheSize]unsafe.Pointer
2222

23+
//gopherjs:purge
24+
type cacheTable struct{}
25+
2326
//gopherjs:purge
2427
type cacheEntry struct{}
2528

compiler/natives/src/crypto/internal/boring/bcache/cache_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package bcache
55

66
import "testing"
77

8+
var registeredCache Cache
9+
810
func TestCache(t *testing.T) {
911
t.Skip(`This test uses runtime.GC(), which GopherJS doesn't support`)
1012
}

compiler/natives/src/crypto/internal/nistec/nistec_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,52 @@ type nistPoint[T any] interface{}
1818

1919
func TestEquivalents(t *testing.T) {
2020
t.Run("P224", func(t *testing.T) {
21-
testEquivalents(t, nistec.NewP224WrappedPoint, nistec.NewP224WrappedGenerator, elliptic.P224())
21+
testEquivalents(t, nistec.NewP224WrappedPoint, elliptic.P224())
2222
})
2323
t.Run("P256", func(t *testing.T) {
24-
testEquivalents(t, nistec.NewP256WrappedPoint, nistec.NewP256WrappedGenerator, elliptic.P256())
24+
testEquivalents(t, nistec.NewP256WrappedPoint, elliptic.P256())
2525
})
2626
t.Run("P384", func(t *testing.T) {
27-
testEquivalents(t, nistec.NewP384WrappedPoint, nistec.NewP384WrappedGenerator, elliptic.P384())
27+
testEquivalents(t, nistec.NewP384WrappedPoint, elliptic.P384())
2828
})
2929
t.Run("P521", func(t *testing.T) {
30-
testEquivalents(t, nistec.NewP521WrappedPoint, nistec.NewP521WrappedGenerator, elliptic.P521())
30+
testEquivalents(t, nistec.NewP521WrappedPoint, elliptic.P521())
3131
})
3232
}
3333

3434
//gopherjs:override-signature
35-
func testEquivalents(t *testing.T, newPoint, newGenerator func() nistec.WrappedPoint, c elliptic.Curve)
35+
func testEquivalents(t *testing.T, newPoint func() nistec.WrappedPoint, c elliptic.Curve)
3636

3737
func TestScalarMult(t *testing.T) {
3838
t.Run("P224", func(t *testing.T) {
39-
testScalarMult(t, nistec.NewP224WrappedPoint, nistec.NewP224WrappedGenerator, elliptic.P224())
39+
testScalarMult(t, nistec.NewP224WrappedPoint, elliptic.P224())
4040
})
4141
t.Run("P256", func(t *testing.T) {
42-
testScalarMult(t, nistec.NewP256WrappedPoint, nistec.NewP256WrappedGenerator, elliptic.P256())
42+
testScalarMult(t, nistec.NewP256WrappedPoint, elliptic.P256())
4343
})
4444
t.Run("P384", func(t *testing.T) {
45-
testScalarMult(t, nistec.NewP384WrappedPoint, nistec.NewP384WrappedGenerator, elliptic.P384())
45+
testScalarMult(t, nistec.NewP384WrappedPoint, elliptic.P384())
4646
})
4747
t.Run("P521", func(t *testing.T) {
48-
testScalarMult(t, nistec.NewP521WrappedPoint, nistec.NewP521WrappedGenerator, elliptic.P521())
48+
testScalarMult(t, nistec.NewP521WrappedPoint, elliptic.P521())
4949
})
5050
}
5151

5252
//gopherjs:override-signature
53-
func testScalarMult(t *testing.T, newPoint, newGenerator func() nistec.WrappedPoint, c elliptic.Curve)
53+
func testScalarMult(t *testing.T, newPoint func() nistec.WrappedPoint, c elliptic.Curve)
5454

5555
func BenchmarkScalarMult(b *testing.B) {
5656
b.Run("P224", func(b *testing.B) {
57-
benchmarkScalarMult(b, nistec.NewP224WrappedGenerator(), 28)
57+
benchmarkScalarMult(b, nistec.NewP224WrappedPoint().SetGenerator(), 28)
5858
})
5959
b.Run("P256", func(b *testing.B) {
60-
benchmarkScalarMult(b, nistec.NewP256WrappedGenerator(), 32)
60+
benchmarkScalarMult(b, nistec.NewP256WrappedPoint().SetGenerator(), 32)
6161
})
6262
b.Run("P384", func(b *testing.B) {
63-
benchmarkScalarMult(b, nistec.NewP384WrappedGenerator(), 48)
63+
benchmarkScalarMult(b, nistec.NewP384WrappedPoint().SetGenerator(), 48)
6464
})
6565
b.Run("P521", func(b *testing.B) {
66-
benchmarkScalarMult(b, nistec.NewP521WrappedGenerator(), 66)
66+
benchmarkScalarMult(b, nistec.NewP521WrappedPoint().SetGenerator(), 66)
6767
})
6868
}
6969

@@ -72,16 +72,16 @@ func benchmarkScalarMult(b *testing.B, p nistec.WrappedPoint, scalarSize int)
7272

7373
func BenchmarkScalarBaseMult(b *testing.B) {
7474
b.Run("P224", func(b *testing.B) {
75-
benchmarkScalarBaseMult(b, nistec.NewP224WrappedGenerator(), 28)
75+
benchmarkScalarBaseMult(b, nistec.NewP224WrappedPoint().SetGenerator(), 28)
7676
})
7777
b.Run("P256", func(b *testing.B) {
78-
benchmarkScalarBaseMult(b, nistec.NewP256WrappedGenerator(), 32)
78+
benchmarkScalarBaseMult(b, nistec.NewP256WrappedPoint().SetGenerator(), 32)
7979
})
8080
b.Run("P384", func(b *testing.B) {
81-
benchmarkScalarBaseMult(b, nistec.NewP384WrappedGenerator(), 48)
81+
benchmarkScalarBaseMult(b, nistec.NewP384WrappedPoint().SetGenerator(), 48)
8282
})
8383
b.Run("P521", func(b *testing.B) {
84-
benchmarkScalarBaseMult(b, nistec.NewP521WrappedGenerator(), 66)
84+
benchmarkScalarBaseMult(b, nistec.NewP521WrappedPoint().SetGenerator(), 66)
8585
})
8686
}
8787

compiler/natives/src/crypto/internal/nistec/wrapper.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
package nistec
55

6+
// temporarily replacement of `nistPoint[T any]` for go1.20 without generics.
67
type WrappedPoint interface {
8+
SetGenerator() WrappedPoint
79
Bytes() []byte
10+
BytesX() ([]byte, error)
811
SetBytes(b []byte) (WrappedPoint, error)
912
Add(w1, w2 WrappedPoint) WrappedPoint
1013
Double(w1 WrappedPoint) WrappedPoint
@@ -24,14 +27,18 @@ func NewP224WrappedPoint() WrappedPoint {
2427
return wrapP224(NewP224Point())
2528
}
2629

27-
func NewP224WrappedGenerator() WrappedPoint {
28-
return wrapP224(NewP224Generator())
30+
func (w p224Wrapper) SetGenerator() WrappedPoint {
31+
return wrapP224(w.point.SetGenerator())
2932
}
3033

3134
func (w p224Wrapper) Bytes() []byte {
3235
return w.point.Bytes()
3336
}
3437

38+
func (w p224Wrapper) BytesX() ([]byte, error) {
39+
return w.point.BytesX()
40+
}
41+
3542
func (w p224Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
3643
p, err := w.point.SetBytes(b)
3744
return wrapP224(p), err
@@ -67,14 +74,18 @@ func NewP256WrappedPoint() WrappedPoint {
6774
return wrapP256(NewP256Point())
6875
}
6976

70-
func NewP256WrappedGenerator() WrappedPoint {
71-
return wrapP256(NewP256Generator())
77+
func (w p256Wrapper) SetGenerator() WrappedPoint {
78+
return wrapP256(w.point.SetGenerator())
7279
}
7380

7481
func (w p256Wrapper) Bytes() []byte {
7582
return w.point.Bytes()
7683
}
7784

85+
func (w p256Wrapper) BytesX() ([]byte, error) {
86+
return w.point.BytesX()
87+
}
88+
7889
func (w p256Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
7990
p, err := w.point.SetBytes(b)
8091
return wrapP256(p), err
@@ -110,14 +121,18 @@ func NewP521WrappedPoint() WrappedPoint {
110121
return wrapP521(NewP521Point())
111122
}
112123

113-
func NewP521WrappedGenerator() WrappedPoint {
114-
return wrapP521(NewP521Generator())
124+
func (w p521Wrapper) SetGenerator() WrappedPoint {
125+
return wrapP521(w.point.SetGenerator())
115126
}
116127

117128
func (w p521Wrapper) Bytes() []byte {
118129
return w.point.Bytes()
119130
}
120131

132+
func (w p521Wrapper) BytesX() ([]byte, error) {
133+
return w.point.BytesX()
134+
}
135+
121136
func (w p521Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
122137
p, err := w.point.SetBytes(b)
123138
return wrapP521(p), err
@@ -153,14 +168,18 @@ func NewP384WrappedPoint() WrappedPoint {
153168
return wrapP384(NewP384Point())
154169
}
155170

156-
func NewP384WrappedGenerator() WrappedPoint {
157-
return wrapP384(NewP384Generator())
171+
func (w p384Wrapper) SetGenerator() WrappedPoint {
172+
return wrapP384(w.point.SetGenerator())
158173
}
159174

160175
func (w p384Wrapper) Bytes() []byte {
161176
return w.point.Bytes()
162177
}
163178

179+
func (w p384Wrapper) BytesX() ([]byte, error) {
180+
return w.point.BytesX()
181+
}
182+
164183
func (w p384Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
165184
p, err := w.point.SetBytes(b)
166185
return wrapP384(p), err

0 commit comments

Comments
 (0)