...

Source file src/github.com/decred/dcrd/dcrec/secp256k1/v4/field_bench_test.go

Documentation: github.com/decred/dcrd/dcrec/secp256k1/v4

     1  // Copyright (c) 2020-2023 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package secp256k1
     6  
     7  import (
     8  	"math/big"
     9  	"testing"
    10  )
    11  
    12  // BenchmarkFieldNormalize benchmarks how long it takes the internal field
    13  // to perform normalization (which includes modular reduction).
    14  func BenchmarkFieldNormalize(b *testing.B) {
    15  	// The function is constant time so any value is fine.
    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  // BenchmarkFieldSqrt benchmarks calculating the square root of an unsigned
    29  // 256-bit big-endian integer modulo the field prime  with the specialized type.
    30  func BenchmarkFieldSqrt(b *testing.B) {
    31  	// The function is constant time so any value is fine.
    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  // BenchmarkBigSqrt benchmarks calculating the square root of an unsigned
    44  // 256-bit big-endian integer modulo the field prime with stdlib big integers.
    45  func BenchmarkBigSqrt(b *testing.B) {
    46  	// The function is constant time so any value is fine.
    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  // BenchmarkFieldIsGtOrEqPrimeMinusOrder benchmarks determining whether a value
    61  // is greater than or equal to the field prime minus the group order with the
    62  // specialized type.
    63  func BenchmarkFieldIsGtOrEqPrimeMinusOrder(b *testing.B) {
    64  	// The function is constant time so any value is fine.
    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  // BenchmarkBigIsGtOrEqPrimeMinusOrder benchmarks determining whether a value
    76  // is greater than or equal to the field prime minus the group order with stdlib
    77  // big integers.
    78  func BenchmarkBigIsGtOrEqPrimeMinusOrder(b *testing.B) {
    79  	// Same value used in field val version.
    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  		// In practice, the internal value to compare would have to be converted
    91  		// to a big integer from bytes, so it's a fair comparison to allocate a
    92  		// new big int here and set all bytes.
    93  		_ = new(big.Int).SetBytes(val.Bytes()).Cmp(bigPMinusN) >= 0
    94  	}
    95  }
    96  

View as plain text