...
1
2
3
4
5 package secp256k1
6
7 import (
8 "math/big"
9 "testing"
10 )
11
12
13
14 func BenchmarkFieldNormalize(b *testing.B) {
15
16 f := &FieldVal{n: [10]uint32{
17 0x000148f6, 0x03ffffc0, 0x03ffffff, 0x03ffffff, 0x03ffffff,
18 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x00000007,
19 }}
20
21 b.ReportAllocs()
22 b.ResetTimer()
23 for i := 0; i < b.N; i++ {
24 f.Normalize()
25 }
26 }
27
28
29
30 func BenchmarkFieldSqrt(b *testing.B) {
31
32 valHex := "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca"
33 f := new(FieldVal).SetHex(valHex).Normalize()
34
35 b.ReportAllocs()
36 b.ResetTimer()
37 for i := 0; i < b.N; i++ {
38 var result FieldVal
39 _ = result.SquareRootVal(f)
40 }
41 }
42
43
44
45 func BenchmarkBigSqrt(b *testing.B) {
46
47 valHex := "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca"
48 val, ok := new(big.Int).SetString(valHex, 16)
49 if !ok {
50 b.Fatalf("failed to parse hex %s", valHex)
51 }
52
53 b.ReportAllocs()
54 b.ResetTimer()
55 for i := 0; i < b.N; i++ {
56 _ = new(big.Int).ModSqrt(val, curveParams.P)
57 }
58 }
59
60
61
62
63 func BenchmarkFieldIsGtOrEqPrimeMinusOrder(b *testing.B) {
64
65 valHex := "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca"
66 f := new(FieldVal).SetHex(valHex).Normalize()
67
68 b.ReportAllocs()
69 b.ResetTimer()
70 for i := 0; i < b.N; i++ {
71 _ = f.IsGtOrEqPrimeMinusOrder()
72 }
73 }
74
75
76
77
78 func BenchmarkBigIsGtOrEqPrimeMinusOrder(b *testing.B) {
79
80 valHex := "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca"
81 val, ok := new(big.Int).SetString(valHex, 16)
82 if !ok {
83 b.Fatalf("failed to parse hex %s", valHex)
84 }
85 bigPMinusN := new(big.Int).Sub(curveParams.P, curveParams.N)
86
87 b.ReportAllocs()
88 b.ResetTimer()
89 for i := 0; i < b.N; i++ {
90
91
92
93 _ = new(big.Int).SetBytes(val.Bytes()).Cmp(bigPMinusN) >= 0
94 }
95 }
96
View as plain text