Source file src/crypto/elliptic/fuzz_test.go

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

View as plain text