Skip to content

Commit dea4768

Browse files
authored
Merge pull request #1262 from Workiva/updateNatives19
[go1.19] Updating natives
2 parents 1faba3b + a83c5fa commit dea4768

File tree

12 files changed

+504
-9
lines changed

12 files changed

+504
-9
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//go:build js
2+
// +build js
3+
4+
package elliptic
5+
6+
import (
7+
"crypto/internal/nistec"
8+
"math/big"
9+
)
10+
11+
// nistPoint uses generics so must be removed for generic-less GopherJS.
12+
// All the following code changes in this file are to make p224, p256,
13+
// p521, and p384 still function correctly without this generic struct.
14+
//
15+
//gopherjs:purge for go1.19 without generics
16+
type nistPoint[T any] interface{}
17+
18+
// nistCurve replaces the generics with a version using the wrappedPoint
19+
// interface, then update all the method signatures to also use wrappedPoint.
20+
type nistCurve struct {
21+
newPoint func() nistec.WrappedPoint
22+
params *CurveParams
23+
}
24+
25+
//gopherjs:override-signature
26+
func (curve *nistCurve) Params() *CurveParams
27+
28+
//gopherjs:override-signature
29+
func (curve *nistCurve) IsOnCurve(x, y *big.Int) bool
30+
31+
//gopherjs:override-signature
32+
func (curve *nistCurve) pointFromAffine(x, y *big.Int) (p nistec.WrappedPoint, err error)
33+
34+
//gopherjs:override-signature
35+
func (curve *nistCurve) pointToAffine(p nistec.WrappedPoint) (x, y *big.Int)
36+
37+
//gopherjs:override-signature
38+
func (curve *nistCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
39+
40+
//gopherjs:override-signature
41+
func (curve *nistCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int)
42+
43+
//gopherjs:override-signature
44+
func (curve *nistCurve) normalizeScalar(scalar []byte) []byte
45+
46+
//gopherjs:override-signature
47+
func (curve *nistCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int)
48+
49+
//gopherjs:override-signature
50+
func (curve *nistCurve) ScalarBaseMult(scalar []byte) (*big.Int, *big.Int)
51+
52+
//gopherjs:override-signature
53+
func (curve *nistCurve) CombinedMult(Px, Py *big.Int, s1, s2 []byte) (x, y *big.Int)
54+
55+
//gopherjs:override-signature
56+
func (curve *nistCurve) Unmarshal(data []byte) (x, y *big.Int)
57+
58+
//gopherjs:override-signature
59+
func (curve *nistCurve) UnmarshalCompressed(data []byte) (x, y *big.Int)
60+
61+
var p224 = &nistCurve{
62+
newPoint: nistec.NewP224WrappedPoint,
63+
}
64+
65+
type p256Curve struct {
66+
nistCurve
67+
}
68+
69+
var p256 = &p256Curve{
70+
nistCurve: nistCurve{
71+
newPoint: nistec.NewP256WrappedPoint,
72+
},
73+
}
74+
75+
var p521 = &nistCurve{
76+
newPoint: nistec.NewP521WrappedPoint,
77+
}
78+
79+
var p384 = &nistCurve{
80+
newPoint: nistec.NewP384WrappedPoint,
81+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build js
2+
// +build js
3+
4+
package bbig
5+
6+
import (
7+
"crypto/internal/boring"
8+
"math/big"
9+
)
10+
11+
func Enc(b *big.Int) boring.BigInt {
12+
if b == nil {
13+
return nil
14+
}
15+
x := b.Bits()
16+
if len(x) == 0 {
17+
return boring.BigInt{}
18+
}
19+
// Replacing original which uses unsafe:
20+
// return unsafe.Slice((*uint)(&x[0]), len(x))
21+
b2 := make(boring.BigInt, len(x))
22+
for i, w := range x {
23+
b2[i] = uint(w)
24+
}
25+
return b2
26+
}
27+
28+
func Dec(b boring.BigInt) *big.Int {
29+
if b == nil {
30+
return nil
31+
}
32+
if len(b) == 0 {
33+
return new(big.Int)
34+
}
35+
// Replacing original which uses unsafe:
36+
//x := unsafe.Slice((*big.Word)(&b[0]), len(b))
37+
x := make([]big.Word, len(b))
38+
for i, w := range b {
39+
x[i] = big.Word(w)
40+
}
41+
return new(big.Int).SetBits(x)
42+
}
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)
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//go:build js
2+
// +build js
3+
4+
package nistec
5+
6+
type WrappedPoint interface {
7+
Bytes() []byte
8+
SetBytes(b []byte) (WrappedPoint, error)
9+
Add(w1, w2 WrappedPoint) WrappedPoint
10+
Double(w1 WrappedPoint) WrappedPoint
11+
ScalarMult(w1 WrappedPoint, scalar []byte) (WrappedPoint, error)
12+
ScalarBaseMult(scalar []byte) (WrappedPoint, error)
13+
}
14+
15+
type p224Wrapper struct {
16+
point *P224Point
17+
}
18+
19+
func wrapP224(point *P224Point) WrappedPoint {
20+
return p224Wrapper{point: point}
21+
}
22+
23+
func NewP224WrappedPoint() WrappedPoint {
24+
return wrapP224(NewP224Point())
25+
}
26+
27+
func NewP224WrappedGenerator() WrappedPoint {
28+
return wrapP224(NewP224Generator())
29+
}
30+
31+
func (w p224Wrapper) Bytes() []byte {
32+
return w.point.Bytes()
33+
}
34+
35+
func (w p224Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
36+
p, err := w.point.SetBytes(b)
37+
return wrapP224(p), err
38+
}
39+
40+
func (w p224Wrapper) Add(w1, w2 WrappedPoint) WrappedPoint {
41+
return wrapP224(w.point.Add(w1.(p224Wrapper).point, w2.(p224Wrapper).point))
42+
}
43+
44+
func (w p224Wrapper) Double(w1 WrappedPoint) WrappedPoint {
45+
return wrapP224(w.point.Double(w1.(p224Wrapper).point))
46+
}
47+
48+
func (w p224Wrapper) ScalarMult(w1 WrappedPoint, scalar []byte) (WrappedPoint, error) {
49+
p, err := w.point.ScalarMult(w1.(p224Wrapper).point, scalar)
50+
return wrapP224(p), err
51+
}
52+
53+
func (w p224Wrapper) ScalarBaseMult(scalar []byte) (WrappedPoint, error) {
54+
p, err := w.point.ScalarBaseMult(scalar)
55+
return wrapP224(p), err
56+
}
57+
58+
type p256Wrapper struct {
59+
point *P256Point
60+
}
61+
62+
func wrapP256(point *P256Point) WrappedPoint {
63+
return p256Wrapper{point: point}
64+
}
65+
66+
func NewP256WrappedPoint() WrappedPoint {
67+
return wrapP256(NewP256Point())
68+
}
69+
70+
func NewP256WrappedGenerator() WrappedPoint {
71+
return wrapP256(NewP256Generator())
72+
}
73+
74+
func (w p256Wrapper) Bytes() []byte {
75+
return w.point.Bytes()
76+
}
77+
78+
func (w p256Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
79+
p, err := w.point.SetBytes(b)
80+
return wrapP256(p), err
81+
}
82+
83+
func (w p256Wrapper) Add(w1, w2 WrappedPoint) WrappedPoint {
84+
return wrapP256(w.point.Add(w1.(p256Wrapper).point, w2.(p256Wrapper).point))
85+
}
86+
87+
func (w p256Wrapper) Double(w1 WrappedPoint) WrappedPoint {
88+
return wrapP256(w.point.Double(w1.(p256Wrapper).point))
89+
}
90+
91+
func (w p256Wrapper) ScalarMult(w1 WrappedPoint, scalar []byte) (WrappedPoint, error) {
92+
p, err := w.point.ScalarMult(w1.(p256Wrapper).point, scalar)
93+
return wrapP256(p), err
94+
}
95+
96+
func (w p256Wrapper) ScalarBaseMult(scalar []byte) (WrappedPoint, error) {
97+
p, err := w.point.ScalarBaseMult(scalar)
98+
return wrapP256(p), err
99+
}
100+
101+
type p521Wrapper struct {
102+
point *P521Point
103+
}
104+
105+
func wrapP521(point *P521Point) WrappedPoint {
106+
return p521Wrapper{point: point}
107+
}
108+
109+
func NewP521WrappedPoint() WrappedPoint {
110+
return wrapP521(NewP521Point())
111+
}
112+
113+
func NewP521WrappedGenerator() WrappedPoint {
114+
return wrapP521(NewP521Generator())
115+
}
116+
117+
func (w p521Wrapper) Bytes() []byte {
118+
return w.point.Bytes()
119+
}
120+
121+
func (w p521Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
122+
p, err := w.point.SetBytes(b)
123+
return wrapP521(p), err
124+
}
125+
126+
func (w p521Wrapper) Add(w1, w2 WrappedPoint) WrappedPoint {
127+
return wrapP521(w.point.Add(w1.(p521Wrapper).point, w2.(p521Wrapper).point))
128+
}
129+
130+
func (w p521Wrapper) Double(w1 WrappedPoint) WrappedPoint {
131+
return wrapP521(w.point.Double(w1.(p521Wrapper).point))
132+
}
133+
134+
func (w p521Wrapper) ScalarMult(w1 WrappedPoint, scalar []byte) (WrappedPoint, error) {
135+
p, err := w.point.ScalarMult(w1.(p521Wrapper).point, scalar)
136+
return wrapP521(p), err
137+
}
138+
139+
func (w p521Wrapper) ScalarBaseMult(scalar []byte) (WrappedPoint, error) {
140+
p, err := w.point.ScalarBaseMult(scalar)
141+
return wrapP521(p), err
142+
}
143+
144+
type p384Wrapper struct {
145+
point *P384Point
146+
}
147+
148+
func wrapP384(point *P384Point) WrappedPoint {
149+
return p384Wrapper{point: point}
150+
}
151+
152+
func NewP384WrappedPoint() WrappedPoint {
153+
return wrapP384(NewP384Point())
154+
}
155+
156+
func NewP384WrappedGenerator() WrappedPoint {
157+
return wrapP384(NewP384Generator())
158+
}
159+
160+
func (w p384Wrapper) Bytes() []byte {
161+
return w.point.Bytes()
162+
}
163+
164+
func (w p384Wrapper) SetBytes(b []byte) (WrappedPoint, error) {
165+
p, err := w.point.SetBytes(b)
166+
return wrapP384(p), err
167+
}
168+
169+
func (w p384Wrapper) Add(w1, w2 WrappedPoint) WrappedPoint {
170+
return wrapP384(w.point.Add(w1.(p384Wrapper).point, w2.(p384Wrapper).point))
171+
}
172+
173+
func (w p384Wrapper) Double(w1 WrappedPoint) WrappedPoint {
174+
return wrapP384(w.point.Double(w1.(p384Wrapper).point))
175+
}
176+
177+
func (w p384Wrapper) ScalarMult(w1 WrappedPoint, scalar []byte) (WrappedPoint, error) {
178+
p, err := w.point.ScalarMult(w1.(p384Wrapper).point, scalar)
179+
return wrapP384(p), err
180+
}
181+
182+
func (w p384Wrapper) ScalarBaseMult(scalar []byte) (WrappedPoint, error) {
183+
p, err := w.point.ScalarBaseMult(scalar)
184+
return wrapP384(p), err
185+
}

0 commit comments

Comments
 (0)