github.com/incognitochain/go-incognito-sdk@v1.0.1/privacy/point_test.go (about)

     1  package privacy
     2  
     3  import (
     4  	"crypto/subtle"
     5  	"fmt"
     6  
     7  	//C25519 "github.com/deroproject/derosuite/crypto"
     8  	C25519 "github.com/incognitochain/go-incognito-sdk/privacy/curve25519"
     9  	"testing"
    10  )
    11  
    12  func BenchmarkPoint_AddPedersen(b *testing.B) {
    13  	a := RandomScalar()
    14  	c := RandomScalar()
    15  
    16  	A := RandomPoint()
    17  	C := RandomPoint()
    18  
    19  	b.ResetTimer()
    20  
    21  	for i := 0; i < b.N; i++ {
    22  		new(Point).AddPedersen(a, A, c, C)
    23  	}
    24  }
    25  
    26  func TestPoint_ScalarMultPRIME(t *testing.T) {
    27  	for i := 0; i < 10000; i++ {
    28  		a := RandomScalar()
    29  		pa := RandomPoint()
    30  		b := RandomScalar()
    31  
    32  		res := new(Point).ScalarMult(pa, a)
    33  		res.ScalarMult(res, b)
    34  		tmpres := res.MarshalText()
    35  
    36  		tmp := new(Scalar).Mul(a, b)
    37  		tmpP := new(Point).ScalarMult(pa, tmp)
    38  
    39  		resPrime := C25519.ScalarMultKey(&pa.key, &a.key)
    40  		resPrime = C25519.ScalarMultKey(resPrime, &b.key)
    41  
    42  		tmpresPrime := resPrime.MarshalText()
    43  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
    44  		if !ok {
    45  			t.Fatalf("expected Scalar Mul Base correct !")
    46  		}
    47  
    48  		ok1 := subtle.ConstantTimeCompare(tmpP.MarshalText(), tmpresPrime) == 1
    49  		if !ok1 {
    50  			t.Fatalf("expected Scalar Mul Base correct !")
    51  		}
    52  	}
    53  }
    54  
    55  func TestPoint_MarshalText(t *testing.T) {
    56  	p := RandomPoint()
    57  	fmt.Println(p)
    58  	pByte := p.MarshalText()
    59  	fmt.Println(len(pByte))
    60  	pPrime, _ := new(Point).UnmarshalText(pByte)
    61  	fmt.Println(pPrime)
    62  }
    63  
    64  func TestScalarMul(t *testing.T) {
    65  	for i := 0; i < 1000; i++ {
    66  		a := RandomScalar()
    67  		pa := RandomPoint()
    68  		b := RandomScalar()
    69  
    70  		res := new(Point).ScalarMult(pa, a)
    71  		res.ScalarMult(res, b)
    72  		res = new(Point).ScalarMult(res, a)
    73  		tmpres := res.MarshalText()
    74  
    75  		resPrime := C25519.ScalarMultKey(&pa.key, &a.key)
    76  		resPrime = C25519.ScalarMultKey(resPrime, &b.key)
    77  		resPrime = C25519.ScalarMultKey(resPrime, &a.key)
    78  
    79  		tmpresPrime := resPrime.MarshalText()
    80  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
    81  		if !ok {
    82  			t.Fatalf("expected Scalar Mul Base correct !")
    83  		}
    84  	}
    85  }
    86  
    87  func TestScalarMulBase(t *testing.T) {
    88  
    89  	Gbytes := PedCom.G[0].ToBytesS()
    90  	fmt.Printf("Gbytes: %v\n", Gbytes)
    91  
    92  	array := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
    93  	aScalar := new(Scalar)
    94  	aScalar.FromBytesS(array)
    95  	res1 := new(Point).ScalarMultBase(aScalar)
    96  	res2 := new(Point).ScalarMult(PedCom.G[0], aScalar)
    97  	fmt.Printf("Res1: %v\n", res1.ToBytesS())
    98  	fmt.Printf("Res2: %v\n", res2.ToBytesS())
    99  
   100  	for i := 0; i < 1000; i++ {
   101  		a := RandomScalar()
   102  		b := RandomScalar()
   103  
   104  		res1 := new(Point).ScalarMultBase(a)
   105  		res2 := new(Point).ScalarMultBase(b)
   106  		res := new(Point).Add(res1, res2)
   107  		tmpres := res.MarshalText()
   108  
   109  		resPrime1 := C25519.ScalarmultBase(&a.key)
   110  		resPrime2 := C25519.ScalarmultBase(&b.key)
   111  		var resPrime C25519.Key
   112  
   113  		C25519.AddKeys(&resPrime, resPrime1, resPrime2)
   114  
   115  		tmpresPrime := resPrime.MarshalText()
   116  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
   117  		if !ok {
   118  			t.Fatalf("expected Scalar Mul Base correct !")
   119  		}
   120  	}
   121  }
   122  
   123  func TestPoint_Add(t *testing.T) {
   124  	count := 0
   125  	for i := 0; i < 1000; i++ {
   126  		pa := RandomPoint()
   127  		pb := RandomPoint()
   128  		pc := RandomPoint()
   129  
   130  		res := new(Point).Add(pa, pb)
   131  		res.Add(res, pc)
   132  
   133  		tmpres := res.MarshalText()
   134  
   135  		var resPrime C25519.Key
   136  		C25519.AddKeys(&resPrime, &pa.key, &pb.key)
   137  		C25519.AddKeys(&resPrime, &resPrime, &pc.key)
   138  
   139  		tmpresPrime := resPrime.MarshalText()
   140  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
   141  		if !ok {
   142  			t.Fatalf("expected Add correct !")
   143  		}
   144  		resPrimePrime, _ := new(Point).SetKey(&resPrime)
   145  		okk := IsPointEqual(res, resPrimePrime)
   146  		if !okk {
   147  			t.Fatalf("expected Add correct !")
   148  		}
   149  	}
   150  
   151  	fmt.Printf("Count wrong: %v\n", count)
   152  }
   153  
   154  func TestPoint_Sub(t *testing.T) {
   155  	for i := 0; i < 1000; i++ {
   156  		pa := RandomPoint()
   157  		pb := RandomPoint()
   158  		pc := RandomPoint()
   159  
   160  		res := new(Point).Sub(pa, pb)
   161  		res.Sub(res, pc)
   162  		tmpres := res.MarshalText()
   163  
   164  		var resPrime C25519.Key
   165  		C25519.SubKeys(&resPrime, &pa.key, &pb.key)
   166  		C25519.SubKeys(&resPrime, &resPrime, &pc.key)
   167  
   168  		tmpresPrime := resPrime.MarshalText()
   169  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
   170  		if !ok {
   171  			t.Fatalf("expected Sub correct !")
   172  		}
   173  		resPrimePrime, _ := new(Point).SetKey(&resPrime)
   174  		okk := IsPointEqual(res, resPrimePrime)
   175  		if !okk {
   176  			t.Fatalf("expected Sub correct !")
   177  		}
   178  	}
   179  }
   180  
   181  func TestPoint_InvertScalarMul(t *testing.T) {
   182  	for i := 0; i < 1000; i++ {
   183  		a := RandomScalar()
   184  		pa := RandomPoint()
   185  
   186  		// compute (pa^a)^1/a = pa
   187  		res := new(Point).ScalarMult(pa, a)
   188  		res.InvertScalarMult(res, a)
   189  		tmpres := res.MarshalText()
   190  
   191  		tmpresPrime := pa.MarshalText()
   192  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
   193  		if !ok {
   194  			t.Fatalf("expected Invert Scalar Mul correct !")
   195  		}
   196  	}
   197  }
   198  
   199  func TestPoint_InvertScalarMultBase(t *testing.T) {
   200  	for i := 0; i < 1000; i++ {
   201  		a := RandomScalar()
   202  
   203  		// compute (g^1/a)^a = g
   204  		res := new(Point).InvertScalarMultBase(a)
   205  		res.ScalarMult(res, a)
   206  		tmpres := res.MarshalText()
   207  
   208  		tmpresPrime := C25519.GBASE.MarshalText()
   209  		ok := subtle.ConstantTimeCompare(tmpres, tmpresPrime) == 1
   210  		if !ok {
   211  			t.Fatalf("expected Invert Scalar Mul Base correct !")
   212  		}
   213  	}
   214  }
   215  
   216  func TestHashToPoint(t *testing.T) {
   217  	for i := 0; i < 10; i++ {
   218  		for j := 0; j < 6; j++ {
   219  			p := HashToPointFromIndex(int64(j), CStringBulletProof)
   220  			fmt.Println(p.key)
   221  		}
   222  		fmt.Println()
   223  	}
   224  }
   225  
   226  func TestPoint_FromBytes(t *testing.T) {
   227  	//bytes := [32]byte{}
   228  	//bytes[0] = 12
   229  	//point, err := new(Point).FromBytes(bytes)
   230  	//
   231  	//ok := point.PointValid()
   232  	//
   233  	//if !ok {
   234  	//	t.Fatalf("expected point is valid!")
   235  	//}
   236  }