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

     1  package privacy_util
     2  
     3  import (
     4  	"github.com/incognitochain/go-incognito-sdk/privacy"
     5  	"github.com/incognitochain/go-incognito-sdk/privacy/curve25519"
     6  	"math/big"
     7  
     8  	"github.com/incognitochain/go-incognito-sdk/common"
     9  )
    10  
    11  func ScalarToBigInt(sc *privacy.Scalar) *big.Int {
    12  	keyR := privacy.Reverse(sc.GetKey())
    13  	keyRByte := keyR.ToBytes()
    14  	bi := new(big.Int).SetBytes(keyRByte[:])
    15  	return bi
    16  }
    17  
    18  func BigIntToScalar(bi *big.Int) *privacy.Scalar {
    19  	biByte := common.AddPaddingBigInt(bi, privacy.Ed25519KeySize)
    20  	var key curve25519.Key
    21  	key.FromBytes(SliceToArray(biByte))
    22  	keyR := privacy.Reverse(key)
    23  	sc, err := new(privacy.Scalar).SetKey(&keyR)
    24  	if err != nil {
    25  		return nil
    26  	}
    27  	return sc
    28  }
    29  
    30  // ConvertIntToBinary represents a integer number in binary array with little endian with size n
    31  func ConvertIntToBinary(inum int, n int) []byte {
    32  	binary := make([]byte, n)
    33  
    34  	for i := 0; i < n; i++ {
    35  		binary[i] = byte(inum % 2)
    36  		inum = inum / 2
    37  	}
    38  
    39  	return binary
    40  }
    41  
    42  // ConvertIntToBinary represents a integer number in binary
    43  func ConvertUint64ToBinary(number uint64, n int) []*privacy.Scalar {
    44  	if number == 0 {
    45  		res := make([]*privacy.Scalar, n)
    46  		for i := 0; i < n; i++ {
    47  			res[i] = new(privacy.Scalar).FromUint64(0)
    48  		}
    49  		return res
    50  	}
    51  
    52  	binary := make([]*privacy.Scalar, n)
    53  
    54  	for i := 0; i < n; i++ {
    55  		binary[i] = new(privacy.Scalar).FromUint64(number % 2)
    56  		number = number / 2
    57  	}
    58  	return binary
    59  }
    60  
    61  // isOdd check a big int is odd or not
    62  func isOdd(a *big.Int) bool {
    63  	return a.Bit(0) == 1
    64  }
    65  
    66  // padd1Div4 computes (p + 1) / 4
    67  func padd1Div4(p *big.Int) (res *big.Int) {
    68  	res = new(big.Int).Add(p, big.NewInt(1))
    69  	res.Div(res, big.NewInt(4))
    70  	return
    71  }
    72  
    73  // paddedAppend appends the src byte slice to dst, returning the new slice.
    74  // If the length of the source is smaller than the passed size, leading zero
    75  // bytes are appended to the dst slice before appending src.
    76  func paddedAppend(size uint, dst, src []byte) []byte {
    77  	for i := 0; i < int(size)-len(src); i++ {
    78  		dst = append(dst, 0)
    79  	}
    80  	return append(dst, src...)
    81  }
    82  
    83  func ConvertScalarArrayToBigIntArray(scalarArr []*privacy.Scalar) []*big.Int {
    84  	res := make([]*big.Int, len(scalarArr))
    85  
    86  	for i := 0; i < len(res); i++ {
    87  		tmp := privacy.Reverse(scalarArr[i].GetKey())
    88  		res[i] = new(big.Int).SetBytes(ArrayToSlice(tmp.ToBytes()))
    89  	}
    90  
    91  	return res
    92  }
    93  
    94  func SliceToArray(slice []byte) [privacy.Ed25519KeySize]byte {
    95  	var array [privacy.Ed25519KeySize]byte
    96  	copy(array[:], slice)
    97  	return array
    98  }
    99  
   100  func ArrayToSlice(array [privacy.Ed25519KeySize]byte) []byte {
   101  	var slice []byte
   102  	slice = array[:]
   103  	return slice
   104  }