github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm2/fuzz_test.go (about) 1 //go:build amd64 || arm64 || ppc64le 2 // +build amd64 arm64 ppc64le 3 4 package sm2 5 6 import ( 7 "crypto/elliptic" 8 "crypto/rand" 9 "io" 10 "testing" 11 "time" 12 ) 13 14 var _ = elliptic.P256() 15 16 func TestFuzz(t *testing.T) { 17 p256 := P256Sm2() 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 _, err := io.ReadFull(rand.Reader, scalar1[:]) 38 if err != nil { 39 t.Fatal(err) 40 } 41 _, err = io.ReadFull(rand.Reader, scalar2[:]) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 x, y := p256.ScalarBaseMult(scalar1[:]) 47 x2, y2 := p256Generic.ScalarBaseMult(scalar1[:]) 48 49 xx, yy := p256.ScalarMult(x, y, scalar2[:]) 50 xx2, yy2 := p256Generic.ScalarMult(x2, y2, scalar2[:]) 51 52 if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 { 53 t.Fatalf("ScalarBaseMult does not match reference result with scalar: %x, please report this error to https://github.com/hxx258456/ccgo/issues", scalar1) 54 } 55 56 if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 { 57 t.Fatalf("ScalarMult does not match reference result with scalars: %x and %x, please report this error to https://github.com/hxx258456/ccgo/issues", scalar1, scalar2) 58 } 59 } 60 }