Debian Codegraph @

libgo/go/crypto/elliptic/internal/nistec/nistec_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package nistec_test
6
7 import (
8 "crypto/elliptic/internal/nistec"
9 "math/rand"
10 "os"
11 "runtime"
12 "strings"
13 "testing"
14 )
15
16 func TestAllocations(t *testing.T) {
17 if runtime.Compiler == "gccgo" {
18 t.Skip("gofrontend escape analysis not good enough")
19 }
20 if strings.HasSuffix(os.Getenv("GO_BUILDER_NAME"), "-noopt") {
21 t.Skip("skipping allocations test without relevant optimizations")
22 }
23 t.Run("P224", func(t *testing.T) {
24 if allocs := testing.AllocsPerRun(100, func() {
25 p := nistec.NewP224Generator()
26 scalar := make([]byte, 66)
27 rand.Read(scalar)
28 p.ScalarMult(p, scalar)
29 out := p.Bytes()
30 if _, err := p.SetBytes(out); err != nil {
31 t.Fatal(err)
32 }
33 }); allocs > 0 {
34 t.Errorf("expected zero allocations, got %0.1f", allocs)
35 }
36 })
37 t.Run("P384", func(t *testing.T) {
38 if allocs := testing.AllocsPerRun(100, func() {
39 p := nistec.NewP384Generator()
40 scalar := make([]byte, 66)
41 rand.Read(scalar)
42 p.ScalarMult(p, scalar)
43 out := p.Bytes()
44 if _, err := p.SetBytes(out); err != nil {
45 t.Fatal(err)
46 }
47 }); allocs > 0 {
48 t.Errorf("expected zero allocations, got %0.1f", allocs)
49 }
50 })
51 t.Run("P521", func(t *testing.T) {
52 if allocs := testing.AllocsPerRun(100, func() {
53 p := nistec.NewP521Generator()
54 scalar := make([]byte, 66)
55 rand.Read(scalar)
56 p.ScalarMult(p, scalar)
57 out := p.Bytes()
58 if _, err := p.SetBytes(out); err != nil {
59 t.Fatal(err)
60 }
61 }); allocs > 0 {
62 t.Errorf("expected zero allocations, got %0.1f", allocs)
63 }
64 })
65 }
66
67 func BenchmarkScalarMult(b *testing.B) {
68 b.Run("P224", func(b *testing.B) {
69 scalar := make([]byte, 66)
70 rand.Read(scalar)
71 p := nistec.NewP224Generator()
72 b.ReportAllocs()
73 b.ResetTimer()
74 for i := 0; i < b.N; i++ {
75 p.ScalarMult(p, scalar)
76 }
77 })
78 b.Run("P384", func(b *testing.B) {
79 scalar := make([]byte, 66)
80 rand.Read(scalar)
81 p := nistec.NewP384Generator()
82 b.ReportAllocs()
83 b.ResetTimer()
84 for i := 0; i < b.N; i++ {
85 p.ScalarMult(p, scalar)
86 }
87 })
88 b.Run("P521", func(b *testing.B) {
89 scalar := make([]byte, 66)
90 rand.Read(scalar)
91 p := nistec.NewP521Generator()
92 b.ReportAllocs()
93 b.ResetTimer()
94 for i := 0; i < b.N; i++ {
95 p.ScalarMult(p, scalar)
96 }
97 })
98 }