github.com/consensys/gnark-crypto@v0.14.0/internal/generator/ecdsa/template/marshal.test.go.tmpl (about)

     1  import (
     2  	"crypto/rand"
     3  	"crypto/subtle"
     4  	"testing"
     5  
     6  	"github.com/leanovate/gopter"
     7  	"github.com/leanovate/gopter/prop"
     8  )
     9  
    10  const (
    11  	nbFuzzShort = 10
    12  	nbFuzz      = 100
    13  )
    14  
    15  func TestSerialization(t *testing.T) {
    16  	t.Parallel()
    17  	parameters := gopter.DefaultTestParameters()
    18  	if testing.Short() {
    19  		parameters.MinSuccessfulTests = nbFuzzShort
    20  	} else {
    21  		parameters.MinSuccessfulTests = nbFuzz
    22  	}
    23  
    24  	properties := gopter.NewProperties(parameters)
    25  
    26  	properties.Property("[{{ toUpper .Name }}] ECDSA serialization: SetBytes(Bytes()) should stay the same", prop.ForAll(
    27  		func() bool {
    28  			privKey, _ := GenerateKey(rand.Reader)
    29  
    30  			var end PrivateKey
    31  			buf := privKey.Bytes()
    32  			n, err := end.SetBytes(buf[:])
    33  			if err != nil {
    34  				return false
    35  			}
    36  			if n != sizePrivateKey {
    37  				return false
    38  			}
    39  
    40  			return end.PublicKey.Equal(&privKey.PublicKey) && subtle.ConstantTimeCompare(end.scalar[:], privKey.scalar[:]) == 1
    41  
    42  		},
    43  	))
    44  
    45  	properties.TestingRun(t, gopter.ConsoleReporter(false))
    46  }