github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/crypto/elliptic/fuzz_test.go (about)

     1  // Copyright 2018 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  //go:build amd64 || arm64 || ppc64le
     6  // +build amd64 arm64 ppc64le
     7  
     8  package elliptic
     9  
    10  import (
    11  	"crypto/rand"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestFuzz(t *testing.T) {
    17  
    18  	p256 := P256()
    19  	p256Generic := p256.Params()
    20  
    21  	var scalar1 [32]byte
    22  	var scalar2 [32]byte
    23  	var timeout *time.Timer
    24  
    25  	if testing.Short() {
    26  		timeout = time.NewTimer(10 * time.Millisecond)
    27  	} else {
    28  		timeout = time.NewTimer(2 * time.Second)
    29  	}
    30  
    31  	for {
    32  		select {
    33  		case <-timeout.C:
    34  			return
    35  		default:
    36  		}
    37  
    38  		rand.Read(scalar1[:])
    39  		rand.Read(scalar2[:])
    40  
    41  		x, y := p256.ScalarBaseMult(scalar1[:])
    42  		x2, y2 := p256Generic.ScalarBaseMult(scalar1[:])
    43  
    44  		xx, yy := p256.ScalarMult(x, y, scalar2[:])
    45  		xx2, yy2 := p256Generic.ScalarMult(x2, y2, scalar2[:])
    46  
    47  		if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 {
    48  			t.Fatalf("ScalarBaseMult does not match reference result with scalar: %x, please report this error to security@golang.org", scalar1)
    49  		}
    50  
    51  		if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 {
    52  			t.Fatalf("ScalarMult does not match reference result with scalars: %x and %x, please report this error to security@golang.org", scalar1, scalar2)
    53  		}
    54  	}
    55  }