field_bench_test.go raw
1 //go:build !js && !wasm && !tinygo && !wasm32
2
3 package p256k1
4
5 import (
6 "testing"
7 )
8
9 var benchFieldA = FieldElement{
10 n: [5]uint64{0x4567890abcdef, 0xcba9876543210, 0x3456789abcdef, 0xcba0987654321, 0x123456789ab},
11 magnitude: 1,
12 normalized: true,
13 }
14
15 var benchFieldB = FieldElement{
16 n: [5]uint64{0xdef1234567890, 0x6543210fedcba, 0xcba1234567890, 0x7654321abcdef, 0xfedcba98765},
17 magnitude: 1,
18 normalized: true,
19 }
20
21 // BenchmarkFieldMulAsm benchmarks the assembly field multiplication
22 func BenchmarkFieldMulAsm(b *testing.B) {
23 if !hasFieldAsm() {
24 b.Skip("Assembly not available")
25 }
26
27 var r FieldElement
28 for i := 0; i < b.N; i++ {
29 fieldMulAsm(&r, &benchFieldA, &benchFieldB)
30 }
31 }
32
33 // BenchmarkFieldMulPureGo benchmarks the pure Go field multiplication
34 func BenchmarkFieldMulPureGo(b *testing.B) {
35 var r FieldElement
36 for i := 0; i < b.N; i++ {
37 fieldMulPureGo(&r, &benchFieldA, &benchFieldB)
38 }
39 }
40
41 // BenchmarkFieldSqrAsm benchmarks the assembly field squaring
42 func BenchmarkFieldSqrAsm(b *testing.B) {
43 if !hasFieldAsm() {
44 b.Skip("Assembly not available")
45 }
46
47 var r FieldElement
48 for i := 0; i < b.N; i++ {
49 fieldSqrAsm(&r, &benchFieldA)
50 }
51 }
52
53 // BenchmarkFieldSqrPureGo benchmarks the pure Go field squaring (via mul)
54 func BenchmarkFieldSqrPureGo(b *testing.B) {
55 var r FieldElement
56 for i := 0; i < b.N; i++ {
57 fieldMulPureGo(&r, &benchFieldA, &benchFieldA)
58 }
59 }
60
61 // BenchmarkFieldMul benchmarks the full mul method (which uses assembly when available)
62 func BenchmarkFieldMul(b *testing.B) {
63 r := new(FieldElement)
64 a := benchFieldA
65 bb := benchFieldB
66 for i := 0; i < b.N; i++ {
67 r.mul(&a, &bb)
68 }
69 }
70
71 // BenchmarkFieldSqr benchmarks the full sqr method (which uses assembly when available)
72 func BenchmarkFieldSqr(b *testing.B) {
73 r := new(FieldElement)
74 a := benchFieldA
75 for i := 0; i < b.N; i++ {
76 r.sqr(&a)
77 }
78 }
79
80 // BMI2 benchmarks
81
82 // BenchmarkFieldMulAsmBMI2 benchmarks the BMI2 assembly field multiplication
83 func BenchmarkFieldMulAsmBMI2(b *testing.B) {
84 if !hasFieldAsmBMI2() {
85 b.Skip("BMI2+ADX assembly not available")
86 }
87
88 var r FieldElement
89 for i := 0; i < b.N; i++ {
90 fieldMulAsmBMI2(&r, &benchFieldA, &benchFieldB)
91 }
92 }
93
94 // BenchmarkFieldSqrAsmBMI2 benchmarks the BMI2 assembly field squaring
95 func BenchmarkFieldSqrAsmBMI2(b *testing.B) {
96 if !hasFieldAsmBMI2() {
97 b.Skip("BMI2+ADX assembly not available")
98 }
99
100 var r FieldElement
101 for i := 0; i < b.N; i++ {
102 fieldSqrAsmBMI2(&r, &benchFieldA)
103 }
104 }
105