...
1
2
3
4
5 package secp256k1
6
7 import (
8 "math/big"
9 "testing"
10 )
11
12
13
14 func benchmarkVals() [2][]byte {
15 return [2][]byte{
16 hexToBytes("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364143"),
17 hexToBytes("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364144"),
18 }
19 }
20
21
22
23 func BenchmarkBigIntModN(b *testing.B) {
24 buf := benchmarkVals()[0]
25
26 b.ReportAllocs()
27 b.ResetTimer()
28 for i := 0; i < b.N; i++ {
29 v := new(big.Int).SetBytes(buf)
30 v.Mod(v, curveParams.N)
31 }
32 }
33
34
35
36 func BenchmarkModNScalar(b *testing.B) {
37 slice := benchmarkVals()[0]
38 var buf [32]byte
39 copy(buf[:], slice)
40
41 b.ReportAllocs()
42 b.ResetTimer()
43 for i := 0; i < b.N; i++ {
44 var s ModNScalar
45 s.SetBytes(&buf)
46 }
47 }
48
49
50
51 func BenchmarkBigIntZero(b *testing.B) {
52 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
53
54 b.ReportAllocs()
55 b.ResetTimer()
56 for i := 0; i < b.N; i++ {
57 v1.SetUint64(0)
58 }
59 }
60
61
62
63 func BenchmarkModNScalarZero(b *testing.B) {
64 var s1 ModNScalar
65 s1.SetByteSlice(benchmarkVals()[0])
66
67 b.ReportAllocs()
68 b.ResetTimer()
69 for i := 0; i < b.N; i++ {
70 s1.Zero()
71 }
72 }
73
74
75
76 func BenchmarkBigIntIsZero(b *testing.B) {
77 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
78
79 b.ReportAllocs()
80 b.ResetTimer()
81 for i := 0; i < b.N; i++ {
82 _ = v1.Sign() == 0
83 }
84 }
85
86
87
88 func BenchmarkModNScalarIsZero(b *testing.B) {
89 var s1 ModNScalar
90 s1.SetByteSlice(benchmarkVals()[0])
91
92 b.ReportAllocs()
93 b.ResetTimer()
94 for i := 0; i < b.N; i++ {
95 _ = s1.IsZero()
96 }
97 }
98
99
100
101 func BenchmarkBigIntEquals(b *testing.B) {
102 bufs := benchmarkVals()
103 v1 := new(big.Int).SetBytes(bufs[0])
104 v2 := new(big.Int).SetBytes(bufs[1])
105
106 b.ReportAllocs()
107 b.ResetTimer()
108 for i := 0; i < b.N; i++ {
109 v1.Cmp(v2)
110 }
111 }
112
113
114
115
116 func BenchmarkModNScalarEquals(b *testing.B) {
117 bufs := benchmarkVals()
118 var s1, s2 ModNScalar
119 s1.SetByteSlice(bufs[0])
120 s2.SetByteSlice(bufs[1])
121
122 b.ReportAllocs()
123 b.ResetTimer()
124 for i := 0; i < b.N; i++ {
125 s1.Equals(&s2)
126 }
127 }
128
129
130
131 func BenchmarkBigIntAddModN(b *testing.B) {
132 bufs := benchmarkVals()
133 v1 := new(big.Int).SetBytes(bufs[0])
134 v2 := new(big.Int).SetBytes(bufs[1])
135
136 b.ReportAllocs()
137 b.ResetTimer()
138 for i := 0; i < b.N; i++ {
139 result := new(big.Int).Add(v1, v2)
140 result.Mod(result, curveParams.N)
141 }
142 }
143
144
145
146 func BenchmarkModNScalarAdd(b *testing.B) {
147 bufs := benchmarkVals()
148 var s1, s2 ModNScalar
149 s1.SetByteSlice(bufs[0])
150 s2.SetByteSlice(bufs[1])
151
152 b.ReportAllocs()
153 b.ResetTimer()
154 for i := 0; i < b.N; i++ {
155 _ = new(ModNScalar).Add2(&s1, &s2)
156 }
157 }
158
159
160
161 func BenchmarkBigIntMulModN(b *testing.B) {
162 bufs := benchmarkVals()
163 v1 := new(big.Int).SetBytes(bufs[0])
164 v2 := new(big.Int).SetBytes(bufs[1])
165
166 b.ReportAllocs()
167 b.ResetTimer()
168 for i := 0; i < b.N; i++ {
169 result := new(big.Int).Mul(v1, v2)
170 result.Mod(result, curveParams.N)
171 }
172 }
173
174
175
176 func BenchmarkModNScalarMul(b *testing.B) {
177 bufs := benchmarkVals()
178 var s1, s2 ModNScalar
179 s1.SetByteSlice(bufs[0])
180 s2.SetByteSlice(bufs[1])
181
182 b.ReportAllocs()
183 b.ResetTimer()
184 for i := 0; i < b.N; i++ {
185 _ = new(ModNScalar).Mul2(&s1, &s2)
186 }
187 }
188
189
190
191 func BenchmarkBigIntSquareModN(b *testing.B) {
192 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
193
194 b.ReportAllocs()
195 b.ResetTimer()
196 for i := 0; i < b.N; i++ {
197 result := new(big.Int).Mul(v1, v1)
198 result.Mod(result, curveParams.N)
199 }
200 }
201
202
203
204 func BenchmarkModNScalarSquare(b *testing.B) {
205 var s1 ModNScalar
206 s1.SetByteSlice(benchmarkVals()[0])
207
208 b.ReportAllocs()
209 b.ResetTimer()
210 for i := 0; i < b.N; i++ {
211 _ = new(ModNScalar).SquareVal(&s1)
212 }
213 }
214
215
216
217 func BenchmarkBigIntNegateModN(b *testing.B) {
218 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
219
220 b.ReportAllocs()
221 b.ResetTimer()
222 for i := 0; i < b.N; i++ {
223 result := new(big.Int).Neg(v1)
224 result.Mod(result, curveParams.N)
225 }
226 }
227
228
229
230 func BenchmarkModNScalarNegate(b *testing.B) {
231 var s1 ModNScalar
232 s1.SetByteSlice(benchmarkVals()[0])
233
234 b.ReportAllocs()
235 b.ResetTimer()
236 for i := 0; i < b.N; i++ {
237 _ = new(ModNScalar).NegateVal(&s1)
238 }
239 }
240
241
242
243
244 func BenchmarkBigIntInverseModN(b *testing.B) {
245 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
246
247 b.ReportAllocs()
248 b.ResetTimer()
249 for i := 0; i < b.N; i++ {
250 new(big.Int).ModInverse(v1, curveParams.N)
251 }
252 }
253
254
255
256
257 func BenchmarkModNScalarInverse(b *testing.B) {
258 var s1 ModNScalar
259 s1.SetByteSlice(benchmarkVals()[0])
260
261 b.ReportAllocs()
262 b.ResetTimer()
263 for i := 0; i < b.N; i++ {
264 _ = new(ModNScalar).InverseValNonConst(&s1)
265 }
266 }
267
268
269
270
271 func BenchmarkBigIntIsOverHalfOrder(b *testing.B) {
272 v1 := new(big.Int).SetBytes(benchmarkVals()[0])
273 bigHalfOrder := new(big.Int).Rsh(curveParams.N, 1)
274
275 b.ReportAllocs()
276 b.ResetTimer()
277 for i := 0; i < b.N; i++ {
278 _ = v1.Cmp(bigHalfOrder)
279 }
280 }
281
282
283
284
285 func BenchmarkModNScalarIsOverHalfOrder(b *testing.B) {
286 var s1 ModNScalar
287 s1.SetByteSlice(benchmarkVals()[0])
288
289 b.ReportAllocs()
290 b.ResetTimer()
291 for i := 0; i < b.N; i++ {
292 s1.IsOverHalfOrder()
293 }
294 }
295
View as plain text