...

Source file src/crypto/elliptic/fuzz_test.go

Documentation: crypto/elliptic

		 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  	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 [email protected]", 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 [email protected]", scalar1, scalar2)
		52  		}
		53  	}
		54  }
		55  

View as plain text