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

     1  package privacy
     2  
     3  import (
     4  	"crypto/subtle"
     5  	"fmt"
     6  	"github.com/stretchr/testify/assert"
     7  	"math/big"
     8  	//C25519 "github.com/deroproject/derosuite/crypto"
     9  	C25519 "github.com/incognitochain/go-incognito-sdk/privacy/curve25519"
    10  	"testing"
    11  )
    12  
    13  func TestCompare(t *testing.T) {
    14  	a := new(Scalar).FromUint64(1001)
    15  	b := new(Scalar).FromUint64(1001)
    16  	fmt.Println(Compare(a, b))
    17  }
    18  
    19  func TestCheckDuplicateScalarArray(t *testing.T) {
    20  	a := RandomScalar()
    21  	b := RandomScalar()
    22  	c := RandomScalar()
    23  	d := RandomScalar()
    24  
    25  	flag := CheckDuplicateScalarArray([]*Scalar{a, b, c, d, a, b})
    26  	fmt.Println(flag)
    27  
    28  	data := []struct {
    29  		arr         []*Scalar
    30  		isDuplicate bool
    31  	}{
    32  		{[]*Scalar{new(Scalar).FromUint64(uint64(100)), new(Scalar).FromUint64(uint64(1000)), new(Scalar).FromUint64(uint64(10000)), new(Scalar).FromUint64(uint64(100000)), new(Scalar).FromUint64(uint64(100000))}, true},
    33  		{[]*Scalar{new(Scalar).FromUint64(uint64(100)), new(Scalar).FromUint64(uint64(1000)), new(Scalar).FromUint64(uint64(10000)), new(Scalar).FromUint64(uint64(100000)), new(Scalar).FromUint64(uint64(1000000))}, false},
    34  	}
    35  
    36  	for _, dataItem := range data {
    37  		isDuplicate := CheckDuplicateScalarArray(dataItem.arr)
    38  		assert.Equal(t, dataItem.isDuplicate, isDuplicate)
    39  	}
    40  }
    41  
    42  func TestScalar_Mul(t *testing.T) {
    43  	count := 0
    44  	for i := 0; i < 100; i++ {
    45  
    46  		a := RandomScalar()
    47  		b := RandomScalar()
    48  		c := RandomScalar()
    49  		res := new(Scalar).Mul(a, b)
    50  		res = res.Mul(res, c)
    51  
    52  		curveOrder := C25519.CurveOrder()
    53  
    54  		resBN := new(big.Int).SetBytes(res.ToBytesS())
    55  		curveOrderBN := new(big.Int).SetBytes(ArrayToSlice(curveOrder.ToBytes()))
    56  
    57  		if resBN.Cmp(curveOrderBN) == 1 {
    58  			count++
    59  			fmt.Printf("Wrong!!!!!\n")
    60  		}
    61  
    62  		var resPrime C25519.Key
    63  		C25519.ScMul(&resPrime, &a.key, &b.key)
    64  		C25519.ScMul(&resPrime, &resPrime, &c.key)
    65  		tmp := resPrime.MarshalText()
    66  		ok := subtle.ConstantTimeCompare(res.MarshalText(), tmp) == 1
    67  		if !ok {
    68  			t.Fatalf("expected Scalar Mul correct !")
    69  		}
    70  	}
    71  
    72  	fmt.Printf("Count : %v\n", count)
    73  
    74  }
    75  
    76  func TestScalar_Add(t *testing.T) {
    77  	count := 0
    78  	for i := 0; i < 100; i++ {
    79  		a := RandomScalar()
    80  		b := RandomScalar()
    81  		c := RandomScalar()
    82  
    83  		res := new(Scalar).Add(a, b)
    84  		res = res.Add(res, c)
    85  		res = res.Add(res, a)
    86  
    87  		var resPrime C25519.Key
    88  		C25519.ScAdd(&resPrime, &a.key, &b.key)
    89  		C25519.ScAdd(&resPrime, &resPrime, &c.key)
    90  		C25519.ScAdd(&resPrime, &resPrime, &a.key)
    91  
    92  		tmp := resPrime.MarshalText()
    93  		ok := subtle.ConstantTimeCompare(res.MarshalText(), tmp) == 1
    94  		if !ok {
    95  			t.Fatalf("expected Scalar Mul correct !")
    96  		}
    97  	}
    98  
    99  	fmt.Printf("Count : %v\n", count)
   100  }
   101  
   102  func TestScalar_Sub(t *testing.T) {
   103  
   104  	for i := 0; i < 100; i++ {
   105  		a := RandomScalar()
   106  		b := RandomScalar()
   107  		c := RandomScalar()
   108  
   109  		res := new(Scalar).Sub(a, b)
   110  		res = res.Sub(res, c)
   111  
   112  		var resPrime C25519.Key
   113  		C25519.ScSub(&resPrime, &a.key, &b.key)
   114  		C25519.ScSub(&resPrime, &resPrime, &c.key)
   115  		tmp := resPrime.MarshalText()
   116  		ok := subtle.ConstantTimeCompare(res.MarshalText(), tmp) == 1
   117  		if !ok {
   118  			t.Fatalf("expected Scalar Mul correct !")
   119  		}
   120  	}
   121  }
   122  func TestScalar_Exp(t *testing.T) {
   123  	for i := 0; i < 1; i++ {
   124  		a := RandomScalar()
   125  		b := uint64(15)
   126  
   127  		res := new(Scalar).Exp(a, b)
   128  		resPrime := new(Scalar).Mul(a, a)
   129  		resPrime.Mul(resPrime, a)
   130  		resPrime.Mul(resPrime, a)
   131  		resPrime.Mul(resPrime, a)
   132  
   133  		resPrime.Mul(resPrime, a)
   134  		resPrime.Mul(resPrime, a)
   135  		resPrime.Mul(resPrime, a)
   136  		resPrime.Mul(resPrime, a)
   137  		resPrime.Mul(resPrime, a)
   138  
   139  		resPrime.Mul(resPrime, a)
   140  		resPrime.Mul(resPrime, a)
   141  		resPrime.Mul(resPrime, a)
   142  		resPrime.Mul(resPrime, a)
   143  		resPrime.Mul(resPrime, a)
   144  
   145  		fmt.Println(resPrime)
   146  		fmt.Println(res.key)
   147  	}
   148  }
   149  
   150  func TestScalar_Invert(t *testing.T) {
   151  	for i := 0; i < 100; i++ {
   152  		a := RandomScalar()
   153  		inv_a := new(Scalar).Invert(a)
   154  
   155  		res := new(Scalar).Mul(a, inv_a)
   156  		ok := res.IsOne()
   157  		if !ok {
   158  			t.Fatalf("expected Scalar Invert correct !")
   159  		}
   160  	}
   161  
   162  	b := new(Scalar).FromUint64(1)
   163  	bInverse := b.Invert(b)
   164  	fmt.Printf("bInverse %v\n", bInverse)
   165  }
   166  
   167  //func Test(t *testing.T){
   168  //	a := new(Scalar).SetUint64(253)
   169  //	b := new(Scalar).SetUint64(253)
   170  //	c := new(Scalar).Mul(a,b)
   171  //	fmt.Println("c: ", c)
   172  //	cPrime  := Reverse(c.key)
   173  //
   174  //	cB := cPrime.ToBytes()
   175  //	fmt.Println("cB: ", cB)
   176  //	aI := new(big.Int).SetBytes(ArrayToSlice(cB))
   177  //	fmt.Println("aI: ", aI)
   178  //	fmt.Println("aI.Bytes(): ", aI.Bytes())
   179  //	fmt.Println("SliceToArray(aI.Bytes()): ", SliceToArray(aI.Bytes()))
   180  //
   181  //	key := new(C25519.Key)
   182  //	key.FromBytes(SliceToArray(common.AddPaddingBigInt(aI, 32)))
   183  //	fmt.Printf("Key: %v\n", key)
   184  //
   185  //	keyInverse := Reverse(*key)
   186  //	fmt.Printf("keyInverse: %v\n", keyInverse)
   187  //
   188  //	sc, err := new(Scalar).SetKey(key)
   189  //	fmt.Printf("sc: %v\n", sc)
   190  //	fmt.Printf("err: %v\n", err)
   191  //
   192  //}