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

     1  package privacy
     2  
     3  import (
     4  	"crypto/rand"
     5  	"math/big"
     6  )
     7  
     8  // RandBytes generates random bytes with length
     9  func RandBytes(length int) []byte {
    10  	rbytes := make([]byte, length)
    11  	rand.Read(rbytes)
    12  	return rbytes
    13  }
    14  
    15  // ConvertIntToBinary represents a integer number in binary array with little endian with size n
    16  func ConvertIntToBinary(inum int, n int) []byte {
    17  	binary := make([]byte, n)
    18  
    19  	for i := 0; i < n; i++ {
    20  		binary[i] = byte(inum % 2)
    21  		inum = inum / 2
    22  	}
    23  
    24  	return binary
    25  }
    26  
    27  // ConvertIntToBinary represents a integer number in binary
    28  func ConvertUint64ToBinary(number uint64, n int) []*Scalar {
    29  	if number == 0 {
    30  		res := make([]*Scalar, n)
    31  		for i := 0; i < n; i++ {
    32  			res[i] = new(Scalar).FromUint64(0)
    33  		}
    34  		return res
    35  	}
    36  
    37  	binary := make([]*Scalar, n)
    38  
    39  	for i := 0; i < n; i++ {
    40  		binary[i] = new(Scalar).FromUint64(number % 2)
    41  		number = number / 2
    42  	}
    43  	return binary
    44  }
    45  
    46  // isOdd check a big int is odd or not
    47  func isOdd(a *big.Int) bool {
    48  	return a.Bit(0) == 1
    49  }
    50  
    51  // padd1Div4 computes (p + 1) / 4
    52  func padd1Div4(p *big.Int) (res *big.Int) {
    53  	res = new(big.Int).Add(p, big.NewInt(1))
    54  	res.Div(res, big.NewInt(4))
    55  	return
    56  }
    57  
    58  // paddedAppend appends the src byte slice to dst, returning the new slice.
    59  // If the length of the source is smaller than the passed size, leading zero
    60  // bytes are appended to the dst slice before appending src.
    61  func paddedAppend(size uint, dst, src []byte) []byte {
    62  	for i := 0; i < int(size)-len(src); i++ {
    63  		dst = append(dst, 0)
    64  	}
    65  	return append(dst, src...)
    66  }
    67  
    68  func ConvertScalarArrayToBigIntArray(scalarArr []*Scalar) []*big.Int {
    69  	res := make([]*big.Int, len(scalarArr))
    70  
    71  	for i := 0; i < len(res); i++ {
    72  		tmp := Reverse(scalarArr[i].GetKey())
    73  		res[i] = new(big.Int).SetBytes(ArrayToSlice(tmp.ToBytes()))
    74  	}
    75  
    76  	return res
    77  }