github.com/consensys/gnark-crypto@v0.14.0/ecc/secp256k1/marshal_test.go (about)

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // FOO
    16  
    17  package secp256k1
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/leanovate/gopter"
    24  	"github.com/leanovate/gopter/prop"
    25  
    26  	"github.com/consensys/gnark-crypto/ecc/secp256k1/fp"
    27  )
    28  
    29  func TestG1AffineSerialization(t *testing.T) {
    30  	t.Parallel()
    31  	// test round trip serialization of infinity
    32  	{
    33  		// uncompressed
    34  		{
    35  			var p1, p2 G1Affine
    36  			p2.X.SetRandom()
    37  			p2.Y.SetRandom()
    38  			buf := p1.RawBytes()
    39  			n, err := p2.SetBytes(buf[:])
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  			if n != SizeOfG1AffineUncompressed {
    44  				t.Fatal("invalid number of bytes consumed in buffer")
    45  			}
    46  			if !(p2.X.IsZero() && p2.Y.IsZero()) {
    47  				t.Fatal("deserialization of uncompressed infinity point is not infinity")
    48  			}
    49  		}
    50  	}
    51  
    52  	parameters := gopter.DefaultTestParameters()
    53  	if testing.Short() {
    54  		parameters.MinSuccessfulTests = nbFuzzShort
    55  	} else {
    56  		parameters.MinSuccessfulTests = nbFuzz
    57  	}
    58  
    59  	properties := gopter.NewProperties(parameters)
    60  
    61  	properties.Property("[G1] Affine SetBytes(RawBytes) should stay the same", prop.ForAll(
    62  		func(a fp.Element) bool {
    63  			var start, end G1Affine
    64  			var ab big.Int
    65  			a.BigInt(&ab)
    66  			start.ScalarMultiplication(&g1GenAff, &ab)
    67  
    68  			buf := start.RawBytes()
    69  			n, err := end.SetBytes(buf[:])
    70  			if err != nil {
    71  				return false
    72  			}
    73  			if n != SizeOfG1AffineUncompressed {
    74  				return false
    75  			}
    76  			return start.X.Equal(&end.X) && start.Y.Equal(&end.Y)
    77  		},
    78  		GenFp(),
    79  	))
    80  
    81  	properties.TestingRun(t, gopter.ConsoleReporter(false))
    82  }