github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/crypto/elliptic/internal/nistec/nistec_test.go (about) 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 "strings" 12 "testing" 13 ) 14 15 func TestAllocations(t *testing.T) { 16 if strings.HasSuffix(os.Getenv("GO_BUILDER_NAME"), "-noopt") { 17 t.Skip("skipping allocations test without relevant optimizations") 18 } 19 t.Run("P224", func(t *testing.T) { 20 if allocs := testing.AllocsPerRun(100, func() { 21 p := nistec.NewP224Generator() 22 scalar := make([]byte, 66) 23 rand.Read(scalar) 24 p.ScalarMult(p, scalar) 25 out := p.Bytes() 26 if _, err := p.SetBytes(out); err != nil { 27 t.Fatal(err) 28 } 29 }); allocs > 0 { 30 t.Errorf("expected zero allocations, got %0.1f", allocs) 31 } 32 }) 33 t.Run("P384", func(t *testing.T) { 34 if allocs := testing.AllocsPerRun(100, func() { 35 p := nistec.NewP384Generator() 36 scalar := make([]byte, 66) 37 rand.Read(scalar) 38 p.ScalarMult(p, scalar) 39 out := p.Bytes() 40 if _, err := p.SetBytes(out); err != nil { 41 t.Fatal(err) 42 } 43 }); allocs > 0 { 44 t.Errorf("expected zero allocations, got %0.1f", allocs) 45 } 46 }) 47 t.Run("P521", func(t *testing.T) { 48 if allocs := testing.AllocsPerRun(100, func() { 49 p := nistec.NewP521Generator() 50 scalar := make([]byte, 66) 51 rand.Read(scalar) 52 p.ScalarMult(p, scalar) 53 out := p.Bytes() 54 if _, err := p.SetBytes(out); err != nil { 55 t.Fatal(err) 56 } 57 }); allocs > 0 { 58 t.Errorf("expected zero allocations, got %0.1f", allocs) 59 } 60 }) 61 } 62 63 func BenchmarkScalarMult(b *testing.B) { 64 b.Run("P224", func(b *testing.B) { 65 scalar := make([]byte, 66) 66 rand.Read(scalar) 67 p := nistec.NewP224Generator() 68 b.ReportAllocs() 69 b.ResetTimer() 70 for i := 0; i < b.N; i++ { 71 p.ScalarMult(p, scalar) 72 } 73 }) 74 b.Run("P384", func(b *testing.B) { 75 scalar := make([]byte, 66) 76 rand.Read(scalar) 77 p := nistec.NewP384Generator() 78 b.ReportAllocs() 79 b.ResetTimer() 80 for i := 0; i < b.N; i++ { 81 p.ScalarMult(p, scalar) 82 } 83 }) 84 b.Run("P521", func(b *testing.B) { 85 scalar := make([]byte, 66) 86 rand.Read(scalar) 87 p := nistec.NewP521Generator() 88 b.ReportAllocs() 89 b.ResetTimer() 90 for i := 0; i < b.N; i++ { 91 p.ScalarMult(p, scalar) 92 } 93 }) 94 }