...

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

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

     1  // Copyright (c) 2020-2022 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  // benchmarkVals returns the raw bytes for a couple of unsigned 256-bit
    13  // big-endian integers used throughout the benchmarks.
    14  func benchmarkVals() [2][]byte {
    15  	return [2][]byte{
    16  		hexToBytes("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364143"),
    17  		hexToBytes("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364144"),
    18  	}
    19  }
    20  
    21  // BenchmarkBigIntModN benchmarks setting and reducing an unsigned 256-bit
    22  // big-endian integer modulo the group order with stdlib big integers.
    23  func BenchmarkBigIntModN(b *testing.B) {
    24  	buf := benchmarkVals()[0]
    25  
    26  	b.ReportAllocs()
    27  	b.ResetTimer()
    28  	for i := 0; i < b.N; i++ {
    29  		v := new(big.Int).SetBytes(buf)
    30  		v.Mod(v, curveParams.N)
    31  	}
    32  }
    33  
    34  // BenchmarkModNScalar benchmarks setting and reducing an unsigned 256-bit
    35  // big-endian integer modulo the group order with the specialized type.
    36  func BenchmarkModNScalar(b *testing.B) {
    37  	slice := benchmarkVals()[0]
    38  	var buf [32]byte
    39  	copy(buf[:], slice)
    40  
    41  	b.ReportAllocs()
    42  	b.ResetTimer()
    43  	for i := 0; i < b.N; i++ {
    44  		var s ModNScalar
    45  		s.SetBytes(&buf)
    46  	}
    47  }
    48  
    49  // BenchmarkBigIntZero benchmarks zeroing an unsigned 256-bit big-endian
    50  // integer modulo the group order with stdlib big integers.
    51  func BenchmarkBigIntZero(b *testing.B) {
    52  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
    53  
    54  	b.ReportAllocs()
    55  	b.ResetTimer()
    56  	for i := 0; i < b.N; i++ {
    57  		v1.SetUint64(0)
    58  	}
    59  }
    60  
    61  // BenchmarkModNScalarZero benchmarks zeroing an unsigned 256-bit big-endian
    62  // integer modulo the group order with the specialized type.
    63  func BenchmarkModNScalarZero(b *testing.B) {
    64  	var s1 ModNScalar
    65  	s1.SetByteSlice(benchmarkVals()[0])
    66  
    67  	b.ReportAllocs()
    68  	b.ResetTimer()
    69  	for i := 0; i < b.N; i++ {
    70  		s1.Zero()
    71  	}
    72  }
    73  
    74  // BenchmarkBigIntIsZero benchmarks determining if an unsigned 256-bit
    75  // big-endian integer modulo the group order is zero with stdlib big integers.
    76  func BenchmarkBigIntIsZero(b *testing.B) {
    77  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
    78  
    79  	b.ReportAllocs()
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		_ = v1.Sign() == 0
    83  	}
    84  }
    85  
    86  // BenchmarkModNScalarIsZero benchmarks determining if an unsigned 256-bit
    87  // big-endian integer modulo the group order is zero with the specialized type.
    88  func BenchmarkModNScalarIsZero(b *testing.B) {
    89  	var s1 ModNScalar
    90  	s1.SetByteSlice(benchmarkVals()[0])
    91  
    92  	b.ReportAllocs()
    93  	b.ResetTimer()
    94  	for i := 0; i < b.N; i++ {
    95  		_ = s1.IsZero()
    96  	}
    97  }
    98  
    99  // BenchmarkBigIntEquals benchmarks determining equality between two unsigned
   100  // 256-bit big-endian integers modulo the group order with stdlib big integers.
   101  func BenchmarkBigIntEquals(b *testing.B) {
   102  	bufs := benchmarkVals()
   103  	v1 := new(big.Int).SetBytes(bufs[0])
   104  	v2 := new(big.Int).SetBytes(bufs[1])
   105  
   106  	b.ReportAllocs()
   107  	b.ResetTimer()
   108  	for i := 0; i < b.N; i++ {
   109  		v1.Cmp(v2)
   110  	}
   111  }
   112  
   113  // BenchmarkModNScalarEquals benchmarks determining equality between two
   114  // unsigned 256-bit big-endian integers modulo the group order with the
   115  // specialized type.
   116  func BenchmarkModNScalarEquals(b *testing.B) {
   117  	bufs := benchmarkVals()
   118  	var s1, s2 ModNScalar
   119  	s1.SetByteSlice(bufs[0])
   120  	s2.SetByteSlice(bufs[1])
   121  
   122  	b.ReportAllocs()
   123  	b.ResetTimer()
   124  	for i := 0; i < b.N; i++ {
   125  		s1.Equals(&s2)
   126  	}
   127  }
   128  
   129  // BenchmarkBigIntAddModN benchmarks adding two unsigned 256-bit big-endian
   130  // integers modulo the group order with stdlib big integers.
   131  func BenchmarkBigIntAddModN(b *testing.B) {
   132  	bufs := benchmarkVals()
   133  	v1 := new(big.Int).SetBytes(bufs[0])
   134  	v2 := new(big.Int).SetBytes(bufs[1])
   135  
   136  	b.ReportAllocs()
   137  	b.ResetTimer()
   138  	for i := 0; i < b.N; i++ {
   139  		result := new(big.Int).Add(v1, v2)
   140  		result.Mod(result, curveParams.N)
   141  	}
   142  }
   143  
   144  // BenchmarkModNScalarAdd benchmarks adding two unsigned 256-bit big-endian
   145  // integers modulo the group order with the specialized type.
   146  func BenchmarkModNScalarAdd(b *testing.B) {
   147  	bufs := benchmarkVals()
   148  	var s1, s2 ModNScalar
   149  	s1.SetByteSlice(bufs[0])
   150  	s2.SetByteSlice(bufs[1])
   151  
   152  	b.ReportAllocs()
   153  	b.ResetTimer()
   154  	for i := 0; i < b.N; i++ {
   155  		_ = new(ModNScalar).Add2(&s1, &s2)
   156  	}
   157  }
   158  
   159  // BenchmarkBigIntMulModN benchmarks multiplying two unsigned 256-bit big-endian
   160  // integers modulo the group order with stdlib big integers.
   161  func BenchmarkBigIntMulModN(b *testing.B) {
   162  	bufs := benchmarkVals()
   163  	v1 := new(big.Int).SetBytes(bufs[0])
   164  	v2 := new(big.Int).SetBytes(bufs[1])
   165  
   166  	b.ReportAllocs()
   167  	b.ResetTimer()
   168  	for i := 0; i < b.N; i++ {
   169  		result := new(big.Int).Mul(v1, v2)
   170  		result.Mod(result, curveParams.N)
   171  	}
   172  }
   173  
   174  // BenchmarkModNScalarMul benchmarks multiplying two unsigned 256-bit big-endian
   175  // integers modulo the group order with the specialized type.
   176  func BenchmarkModNScalarMul(b *testing.B) {
   177  	bufs := benchmarkVals()
   178  	var s1, s2 ModNScalar
   179  	s1.SetByteSlice(bufs[0])
   180  	s2.SetByteSlice(bufs[1])
   181  
   182  	b.ReportAllocs()
   183  	b.ResetTimer()
   184  	for i := 0; i < b.N; i++ {
   185  		_ = new(ModNScalar).Mul2(&s1, &s2)
   186  	}
   187  }
   188  
   189  // BenchmarkBigIntSquareModN benchmarks squaring an unsigned 256-bit big-endian
   190  // integer modulo the group order is zero with stdlib big integers.
   191  func BenchmarkBigIntSquareModN(b *testing.B) {
   192  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
   193  
   194  	b.ReportAllocs()
   195  	b.ResetTimer()
   196  	for i := 0; i < b.N; i++ {
   197  		result := new(big.Int).Mul(v1, v1)
   198  		result.Mod(result, curveParams.N)
   199  	}
   200  }
   201  
   202  // BenchmarkModNScalarSquare benchmarks squaring an unsigned 256-bit big-endian
   203  // integer modulo the group order is zero with the specialized type.
   204  func BenchmarkModNScalarSquare(b *testing.B) {
   205  	var s1 ModNScalar
   206  	s1.SetByteSlice(benchmarkVals()[0])
   207  
   208  	b.ReportAllocs()
   209  	b.ResetTimer()
   210  	for i := 0; i < b.N; i++ {
   211  		_ = new(ModNScalar).SquareVal(&s1)
   212  	}
   213  }
   214  
   215  // BenchmarkBigIntNegateModN benchmarks negating an unsigned 256-bit big-endian
   216  // integer modulo the group order is zero with stdlib big integers.
   217  func BenchmarkBigIntNegateModN(b *testing.B) {
   218  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
   219  
   220  	b.ReportAllocs()
   221  	b.ResetTimer()
   222  	for i := 0; i < b.N; i++ {
   223  		result := new(big.Int).Neg(v1)
   224  		result.Mod(result, curveParams.N)
   225  	}
   226  }
   227  
   228  // BenchmarkModNScalarNegate benchmarks negating an unsigned 256-bit big-endian
   229  // integer modulo the group order is zero with the specialized type.
   230  func BenchmarkModNScalarNegate(b *testing.B) {
   231  	var s1 ModNScalar
   232  	s1.SetByteSlice(benchmarkVals()[0])
   233  
   234  	b.ReportAllocs()
   235  	b.ResetTimer()
   236  	for i := 0; i < b.N; i++ {
   237  		_ = new(ModNScalar).NegateVal(&s1)
   238  	}
   239  }
   240  
   241  // BenchmarkBigIntInverseModN benchmarks calculating the multiplicative inverse
   242  // of an unsigned 256-bit big-endian integer modulo the group order is zero with
   243  // stdlib big integers.
   244  func BenchmarkBigIntInverseModN(b *testing.B) {
   245  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
   246  
   247  	b.ReportAllocs()
   248  	b.ResetTimer()
   249  	for i := 0; i < b.N; i++ {
   250  		new(big.Int).ModInverse(v1, curveParams.N)
   251  	}
   252  }
   253  
   254  // BenchmarkModNScalarInverse benchmarks calculating the multiplicative inverse
   255  // of an unsigned 256-bit big-endian integer modulo the group order is zero with
   256  // the specialized type.
   257  func BenchmarkModNScalarInverse(b *testing.B) {
   258  	var s1 ModNScalar
   259  	s1.SetByteSlice(benchmarkVals()[0])
   260  
   261  	b.ReportAllocs()
   262  	b.ResetTimer()
   263  	for i := 0; i < b.N; i++ {
   264  		_ = new(ModNScalar).InverseValNonConst(&s1)
   265  	}
   266  }
   267  
   268  // BenchmarkBigIntIsOverHalfOrder benchmarks determining if an unsigned 256-bit
   269  // big-endian integer modulo the group order exceeds half the group order with
   270  // stdlib big integers.
   271  func BenchmarkBigIntIsOverHalfOrder(b *testing.B) {
   272  	v1 := new(big.Int).SetBytes(benchmarkVals()[0])
   273  	bigHalfOrder := new(big.Int).Rsh(curveParams.N, 1)
   274  
   275  	b.ReportAllocs()
   276  	b.ResetTimer()
   277  	for i := 0; i < b.N; i++ {
   278  		_ = v1.Cmp(bigHalfOrder)
   279  	}
   280  }
   281  
   282  // BenchmarkModNScalarIsOverHalfOrder benchmarks determining if an unsigned
   283  // 256-bit big-endian integer modulo the group order exceeds half the group
   284  // order with the specialized type.
   285  func BenchmarkModNScalarIsOverHalfOrder(b *testing.B) {
   286  	var s1 ModNScalar
   287  	s1.SetByteSlice(benchmarkVals()[0])
   288  
   289  	b.ReportAllocs()
   290  	b.ResetTimer()
   291  	for i := 0; i < b.N; i++ {
   292  		s1.IsOverHalfOrder()
   293  	}
   294  }
   295  

View as plain text