package ec256

import (
	"crypto/rand"
	"fmt"
	"testing"
	"time"
)

func TestFuzz(t *testing.T) {

	ec := CurveSM2()
	ge := ec.Params()

	var scalar1 [32]byte
	var scalar2 [32]byte
	var timeout *time.Timer

	timeout = time.NewTimer(10 * time.Second)
	count := 0

loop:
	for {
		select {
		case <-timeout.C:
			break loop
		default:
			count++
			if count%100 == 0 {
				fmt.Println("Tested for", count, "times")
			}

			rand.Read(scalar1[:])
			rand.Read(scalar2[:])

			x, y := ec.ScalarBaseMult(scalar1[:])
			x2, y2 := ge.ScalarBaseMult(scalar1[:])

			xx, yy := ec.ScalarMult(x, y, scalar2[:])
			xx2, yy2 := ge.ScalarMult(x2, y2, scalar2[:])

			if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 {
				t.Fatalf("ScalarBaseMult does not match reference result with scalar: %x, please report this error to security@golang.org", scalar1)
			}

			if xx.Cmp(xx2) != 0 || yy.Cmp(yy2) != 0 {
				t.Fatalf("ScalarMult does not match reference result with scalars: %x and %x, please report this error to security@golang.org", scalar1, scalar2)
			}
		}
	}

	fmt.Printf("Total test %d times\n", count)
}
