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