github.com/consensys/gnark-crypto@v0.14.0/utils/unsafe/dump_slice_test.go (about)

     1  package unsafe_test
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/consensys/gnark-crypto/ecc/bn254"
     9  	"github.com/consensys/gnark-crypto/utils/unsafe"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPointDump(t *testing.T) {
    14  	assert := require.New(t)
    15  	samplePoints := make([]bn254.G2Affine, 10)
    16  	fillBenchBasesG2(samplePoints)
    17  
    18  	var buf bytes.Buffer
    19  
    20  	err := unsafe.WriteSlice(&buf, samplePoints)
    21  	assert.NoError(err)
    22  
    23  	readPoints, _, err := unsafe.ReadSlice[[]bn254.G2Affine](&buf)
    24  	assert.NoError(err)
    25  
    26  	assert.Equal(samplePoints, readPoints)
    27  }
    28  
    29  func TestMarker(t *testing.T) {
    30  	assert := require.New(t)
    31  	var buf bytes.Buffer
    32  
    33  	err := unsafe.WriteMarker(&buf)
    34  	assert.NoError(err)
    35  
    36  	err = unsafe.ReadMarker(&buf)
    37  	assert.NoError(err)
    38  }
    39  
    40  func fillBenchBasesG2(samplePoints []bn254.G2Affine) {
    41  	var r big.Int
    42  	r.SetString("340444420969191673093399857471996460938405", 10)
    43  	samplePoints[0].ScalarMultiplication(&samplePoints[0], &r)
    44  
    45  	one := samplePoints[0].X
    46  	one.SetOne()
    47  
    48  	for i := 1; i < len(samplePoints); i++ {
    49  		samplePoints[i].X.Add(&samplePoints[i-1].X, &one)
    50  		samplePoints[i].Y.Sub(&samplePoints[i-1].Y, &one)
    51  	}
    52  }