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

     1  package privacy
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/incognitochain/go-incognito-sdk/common"
     6  	"github.com/stretchr/testify/assert"
     7  	"math/big"
     8  	"testing"
     9  )
    10  
    11  var _ = func() (_ struct{}) {
    12  	fmt.Println("This runs before init()!")
    13  	return
    14  }()
    15  
    16  func TestUtilsRandBytes(t *testing.T) {
    17  	data := []int{
    18  		0,
    19  		10,
    20  		45,
    21  		100,
    22  		1000,
    23  	}
    24  
    25  	for _, item := range data {
    26  		res := RandBytes(item)
    27  		fmt.Printf("Res: %v\n", res)
    28  		assert.Equal(t, item, len(res))
    29  	}
    30  }
    31  
    32  func TestUtilsConvertIntToBinary(t *testing.T) {
    33  	data := []struct {
    34  		number int
    35  		size   int
    36  		binary []byte
    37  	}{
    38  		{64, 8, []byte{0, 0, 0, 0, 0, 0, 1, 0}},
    39  		{100, 10, []byte{0, 0, 1, 0, 0, 1, 1, 0, 0, 0}},
    40  		{1, 8, []byte{1, 0, 0, 0, 0, 0, 0, 0}},
    41  	}
    42  
    43  	for _, item := range data {
    44  		res := ConvertIntToBinary(item.number, item.size)
    45  		assert.Equal(t, item.binary, res)
    46  	}
    47  }
    48  
    49  //func TestUtilsConvertBigIntToBinary(t *testing.T) {
    50  //	data := []struct {
    51  //		number *big.Int
    52  //		size   int
    53  //		binary []*big.Int
    54  //	}{
    55  //		{new(big.Int).FromUint64(uint64(64)), 8, []*big.Int{new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(1), new(big.Int).SetInt64(0)}},
    56  //		{new(big.Int).FromUint64(uint64(100)), 10, []*big.Int{new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(1), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(1), new(big.Int).SetInt64(1), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0), new(big.Int).SetInt64(0)}},
    57  //	}
    58  //
    59  //	for _, item := range data {
    60  //		res := ConvertBigIntToBinary(item.number, item.size)
    61  //		assert.Equal(t, item.binary, res)
    62  //	}
    63  //}
    64  
    65  func TestUtilsAddPaddingBigInt(t *testing.T) {
    66  	data := []struct {
    67  		number *big.Int
    68  		size   int
    69  	}{
    70  		{new(big.Int).SetBytes(RandBytes(12)), common.BigIntSize},
    71  		{new(big.Int).SetBytes(RandBytes(42)), 50},
    72  		{new(big.Int).SetBytes(RandBytes(0)), 10},
    73  	}
    74  
    75  	for _, item := range data {
    76  		res := common.AddPaddingBigInt(item.number, item.size)
    77  		assert.Equal(t, item.size, len(res))
    78  	}
    79  }
    80  
    81  func TestUtilsIntToByteArr(t *testing.T) {
    82  	data := []struct {
    83  		number int
    84  		bytes  []byte
    85  	}{
    86  		{12345, []byte{48, 57}},
    87  		{123, []byte{0, 123}},
    88  		{0, []byte{0, 0}},
    89  	}
    90  
    91  	for _, item := range data {
    92  		res := common.IntToBytes(item.number)
    93  		assert.Equal(t, item.bytes, res)
    94  
    95  		number := common.BytesToInt(res)
    96  		assert.Equal(t, item.number, number)
    97  	}
    98  }
    99  
   100  func TestInterface(t *testing.T) {
   101  	a := make(map[string]interface{})
   102  	a["x"] = "10"
   103  
   104  	value, ok := a["y"].(string)
   105  	if !ok {
   106  		fmt.Printf("Param is invalid\n")
   107  	}
   108  
   109  	value2, ok := a["y"]
   110  	if !ok {
   111  		fmt.Printf("Param is invalid\n")
   112  	}
   113  
   114  	value3, ok := a["x"].(string)
   115  	if !ok {
   116  		fmt.Printf("Param is invalid\n")
   117  	}
   118  
   119  	fmt.Printf("Value: %v\n", value)
   120  	fmt.Printf("Value2: %v\n", value2)
   121  	fmt.Printf("Value2: %v\n", value3)
   122  }
   123  
   124  func TestFee(t *testing.T) {
   125  	inValue := uint64(50000)
   126  	outValue1 := uint64(23000)
   127  	fee := -1
   128  	fee2 := uint64(fee)
   129  	outValue2 := int64(inValue - outValue1 - fee2)
   130  
   131  	fmt.Printf("Fee uint64: %v\n", uint64(fee))
   132  	fmt.Printf("outValue2: %v\n", outValue2)
   133  
   134  	comInputValueSum := new(Point).ScalarMult(PedCom.G[PedersenValueIndex], new(Scalar).FromUint64(uint64(inValue)))
   135  	comOutputValue1 := new(Point).ScalarMult(PedCom.G[PedersenValueIndex], new(Scalar).FromUint64(uint64(outValue1)))
   136  	comOutputValue2 := new(Point).ScalarMult(PedCom.G[PedersenValueIndex], new(Scalar).FromUint64(uint64(outValue2)))
   137  	comOutputValueSum := new(Point).Add(comOutputValue1, comOutputValue2)
   138  
   139  	comFee := new(Point)
   140  	if fee2 > 0 {
   141  		fmt.Printf("fee2 > 0\n")
   142  		comFee = comFee.ScalarMult(PedCom.G[PedersenValueIndex], new(Scalar).FromUint64(uint64(fee2)))
   143  	}
   144  
   145  	tmp1 := new(Point).Add(comOutputValueSum, comFee)
   146  
   147  	if IsPointEqual(tmp1, comInputValueSum) {
   148  		fmt.Printf("Equal\n")
   149  	} else {
   150  		fmt.Printf(" Not Equal\n")
   151  	}
   152  
   153  	//fee := -10
   154  	//output := -9
   155  	//aUint64 := uint64(a)
   156  	//bUint64 := uint64(b)
   157  	//
   158  	//fmt.Printf("aUint64: %v\n", aUint64)
   159  	//fmt.Printf("bUint64: %v\n", bUint64)
   160  
   161  	//comOutputValueSum.Add(comOutputValueSum, new(privacy.Point).ScalarMult(privacy.PedCom.G[privacy.PedersenValueIndex], new(privacy.Scalar).FromUint64(uint64(fee))))
   162  }
   163  
   164  func TestEncryptByXorOperator(t *testing.T) {
   165  	v := new(big.Int).SetUint64(100)
   166  
   167  	randomness := RandomScalar()
   168  	randomnessBytes := randomness.ToBytesS()
   169  
   170  	// encrypt
   171  	ciphertext := v.Uint64()
   172  
   173  	for i := 0; i < 4; i++ {
   174  		randSlice := randomnessBytes[i*8 : i*8+8]
   175  		randSliceUint64 := new(big.Int).SetBytes(randSlice).Uint64()
   176  		ciphertext = ciphertext ^ randSliceUint64
   177  	}
   178  	fmt.Printf("ciphertext %v\n", ciphertext)
   179  
   180  	// decrypt
   181  	plaintext := ciphertext
   182  	for i := 0; i < 4; i++ {
   183  		randSlice := randomnessBytes[i*8 : i*8+8]
   184  		randSliceUint64 := new(big.Int).SetBytes(randSlice).Uint64()
   185  		plaintext = plaintext ^ randSliceUint64
   186  	}
   187  	fmt.Printf("plaintext %v\n", plaintext)
   188  }