當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Int.Bits方法代碼示例

本文整理匯總了Golang中math/big.Int.Bits方法的典型用法代碼示例。如果您正苦於以下問題:Golang Int.Bits方法的具體用法?Golang Int.Bits怎麽用?Golang Int.Bits使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在math/big.Int的用法示例。


在下文中一共展示了Int.Bits方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: p256FromBig

// p256FromBig sets out = R*in.
func p256FromBig(out *[p256Limbs]uint32, in *big.Int) {
	tmp := new(big.Int).Lsh(in, 257)
	tmp.Mod(tmp, p256.P)

	for i := 0; i < p256Limbs; i++ {
		if bits := tmp.Bits(); len(bits) > 0 {
			out[i] = uint32(bits[0]) & bottom29Bits
		} else {
			out[i] = 0
		}
		tmp.Rsh(tmp, 29)

		i++
		if i == p256Limbs {
			break
		}

		if bits := tmp.Bits(); len(bits) > 0 {
			out[i] = uint32(bits[0]) & bottom28Bits
		} else {
			out[i] = 0
		}
		tmp.Rsh(tmp, 28)
	}
}
開發者ID:ArtemL,項目名稱:GCC,代碼行數:26,代碼來源:p256.go

示例2: BigAbsCmp

func BigAbsCmp(x, y big.Int) int {
	// SetBits sets to |v|, thus giving an absolute comparison.
	var x0, y0 big.Int
	x0.SetBits(x.Bits())
	y0.SetBits(y.Bits())
	return x0.Cmp(&y0)
}
開發者ID:EricLagergren,項目名稱:decimal,代碼行數:7,代碼來源:abs.go

示例3: CountInt

func CountInt(x *big.Int) int {
	total := 0
	for _, w := range x.Bits() {
		total += Count64(uint64(w))
	}
	return total
}
開發者ID:egonelbre,項目名稱:spexs2,代碼行數:7,代碼來源:utils.go

示例4: SmallPrimeTest

// SmallPrimeTest determins if N is a small prime
// or divisible by a small prime.
func SmallPrimeTest(N *big.Int) int {
	if N.Sign() <= 0 {
		panic("SmallPrimeTest for positive integers only")
	}
	if N.BitLen() <= 10 {
		n := uint16(N.Uint64())
		i := sort.Search(len(primes10), func(i int) bool {
			return primes10[i] >= n
		})
		if i >= len(primes10) || n != primes10[i] {
			return IsComposite
		}
		return IsPrime
	}
	// quick test for N even
	if N.Bits()[0]&1 == 0 {
		return IsComposite
	}
	// compare several small gcds for efficency
	z := new(big.Int)
	if z.GCD(nil, nil, N, prodPrimes10A).Cmp(one) == 1 {
		return IsComposite
	}
	if z.GCD(nil, nil, N, prodPrimes10B).Cmp(one) == 1 {
		return IsComposite
	}
	if z.GCD(nil, nil, N, prodPrimes10C).Cmp(one) == 1 {
		return IsComposite
	}
	if z.GCD(nil, nil, N, prodPrimes10D).Cmp(one) == 1 {
		return IsComposite
	}
	return Undetermined
}
開發者ID:tscholl2,項目名稱:prime,代碼行數:36,代碼來源:tests.go

示例5: Bytes

// Bytes returns the bytes for the absolute value of x in little-
// endian binary representation; x must be an Int.
func Bytes(x Value) []byte {
	var val *big.Int
	switch x := x.(type) {
	case int64Val:
		val = new(big.Int).SetInt64(int64(x))
	case intVal:
		val = x.val
	default:
		panic(fmt.Sprintf("%v not an Int", x))
	}

	words := val.Bits()
	bytes := make([]byte, len(words)*wordSize)

	i := 0
	for _, w := range words {
		for j := 0; j < wordSize; j++ {
			bytes[i] = byte(w)
			w >>= 8
			i++
		}
	}
	// remove leading 0's
	for i > 0 && bytes[i-1] == 0 {
		i--
	}

	return bytes[:i]
}
開發者ID:xslonepiece,項目名稱:goios,代碼行數:31,代碼來源:exact.go

示例6: BigInt

// BigInt sets all bytes in the passed big int to zero and then sets the
// value to 0.  This differs from simply setting the value in that it
// specifically clears the underlying bytes whereas simply setting the value
// does not.  This is mostly useful to forcefully clear private keys.
func BigInt(x *big.Int) {
	b := x.Bits()
	for i := range b {
		b[i] = 0
	}
	x.SetInt64(0)
}
開發者ID:frankbraun,項目名稱:dcrwallet,代碼行數:11,代碼來源:slice.go

示例7: TrailingZerosBig

// TrailingZerosBig returns the number of trailing 0 bits in v.
//
// If v is 0, it returns 0.
func TrailingZerosBig(v *big.Int) int {
	for i, b := range v.Bits() {
		if b != 0 {
			return i*wordBits + tzw(b)
		}
	}
	return 0
}
開發者ID:pombredanne,項目名稱:integer,代碼行數:11,代碼來源:xmath.go

示例8: TrailingOnesBig

// TrailingOnesBig returns the number of trailing 1 bits in v.
func TrailingOnesBig(v *big.Int) int {
	words := v.Bits()
	for i, b := range words {
		if b != ^big.Word(0) {
			return i*wordBits + tzw(^b)
		}
	}
	return len(words) * wordBits
}
開發者ID:pombredanne,項目名稱:integer,代碼行數:10,代碼來源:xmath.go

示例9: BigInt

// BigInt sets all bytes in the passed big int to zero and then sets the
// value to 0.  This differs from simply setting the value in that it
// specifically clears the underlying bytes whereas simply setting the value
// does not.  This is mostly useful to forcefully clear private keys.
func BigInt(x *big.Int) {
	b := x.Bits()
	z := [16]big.Word{}
	n := uint(copy(b, z[:]))
	for n < uint(len(b)) {
		copy(b[n:], b[:n])
		n <<= 1
	}
	x.SetInt64(0)
}
開發者ID:D-bank,項目名稱:btcwallet,代碼行數:14,代碼來源:slice14.go

示例10: big2scalar

func big2scalar(out *[4]C.ulonglong, in *big.Int) error {
	b := in.Bits()
	if len(b) > 4 {
		return fmt.Errorf("big.Int needs %d words, cannot be converted to scalar_t", len(b))
	}
	for i, w := range b {
		out[i] = C.ulonglong(w)
	}
	return nil
}
開發者ID:asimshankar,項目名稱:bn256,代碼行數:10,代碼來源:bn256_64.go

示例11: readBits

// reads num into buf as big-endian bytes.
func readBits(buf []byte, num *big.Int) {
	const wordLen = int(unsafe.Sizeof(big.Word(0)))
	i := len(buf)
	for _, d := range num.Bits() {
		for j := 0; j < wordLen && i > 0; j++ {
			i--
			buf[i] = byte(d)
			d >>= 8
		}
	}
}
開發者ID:Raskal8,項目名稱:go-ethereum,代碼行數:12,代碼來源:secp256.go

示例12: Uint64FromBigInt

// Uint64FromBigInt returns (uint64 value of n, true) if 0 <= n <=
// math.MaxUint64.  Otherwise it returns  (undefined value, false).
//
// NOTE: This function is DEPRECATED with Go release 1.1 and will be REMOVED
// with Go release 1.1+1, b/c of
// http://code.google.com/p/go/source/detail?r=954a79ee3ea8
func Uint64FromBigInt(n *big.Int) (uint64, bool) {
	switch bits := n.BitLen(); {
	case bits == 0:
		return 0, true
	case n.Sign() < 0 || bits > 64:
		return 0, false
	case bits <= UintptrBits():
		return uint64(n.Bits()[0]), true
	default:
		b := n.Bits()
		return uint64(b[1])<<uint(uintptrBits) | uint64(b[0]), true
	}
}
開發者ID:newobject,項目名稱:camlistore,代碼行數:19,代碼來源:mathutil.go

示例13: big2scalar

func big2scalar(out *[4]C.ulonglong, in *big.Int) error {
	b := in.Bits()
	if len(b) > 8 {
		return fmt.Errorf("big.Int needs %d words, cannot be converted to scalar_t", len(b))
	}
	max := len(b) >> 1
	for i := 0; i < max; i++ {
		out[i] = C.ulonglong(b[i<<1]) | (C.ulonglong(b[i<<1+1]) << 32)
	}
	if len(b)&0x1 == 1 {
		out[max] = C.ulonglong(b[len(b)-1])
	}
	return nil
}
開發者ID:asimshankar,項目名稱:bn256,代碼行數:14,代碼來源:bn256_32.go

示例14: TestBigInt

func TestBigInt(t *testing.T) {
	tests := []string{
		// 16 0xFFFFFFFF 32-bit uintptrs
		strings.Repeat("FFFFFFFF", 16),

		// 17 32-bit uintptrs, minimum value which enters loop on 32-bit
		"01" + strings.Repeat("00000000", 16),

		// 32 0xFFFFFFFF 32-bit uintptrs, maximum value which enters loop exactly once on 32-bit
		strings.Repeat("FFFFFFFF", 32),

		// 33 32-bit uintptrs, minimum value which enters loop twice on 32-bit
		"01" + strings.Repeat("00000000", 32),

		// 16 0xFFFFFFFFFFFFFFFF 64-bit uintptrs
		strings.Repeat("FFFFFFFFFFFFFFFF", 16),

		// 17 64-bit uintptrs, minimum value which enters loop on 64-bit
		"01" + strings.Repeat("0000000000000000", 16),

		// 32 0xFFFFFFFFFFFFFFFF 64-bit uintptrs, maximum value which enters loop exactly once on 64-bit
		strings.Repeat("FFFFFFFFFFFFFFFF", 32),

		// 33 64-bit uintptrs, minimum value which enters loop twice on 64-bit
		"01" + strings.Repeat("0000000000000000", 32),
	}

	for i, s := range tests {
		v, ok := new(big.Int).SetString(s, 16)
		if !ok {
			t.Errorf("Test %d includes invalid hex number %s", i, s)
			continue
		}

		BigInt(v)
		err := checkZeroWords(v.Bits())
		if err != nil {
			t.Errorf("Test %d (s=%s) failed: %v", i, s, err)
			continue
		}
		if v.Cmp(bigZero) != 0 {
			t.Errorf("Test %d (s=%s) zeroed big.Int represents non-zero number %v", i, s, v)
			continue
		}
	}
}
開發者ID:Roasbeef,項目名稱:btcwallet,代碼行數:46,代碼來源:zero_test.go

示例15: fromBig

// fromBig converts a *big.Int into a format used by this code.
func fromBig(out []uint64, big *big.Int) {
	for i := range out {
		out[i] = 0
	}

	for i, v := range big.Bits() {
		out[i] = uint64(v)
	}
}
開發者ID:kraj,項目名稱:gcc,代碼行數:10,代碼來源:p256_amd64.go


注:本文中的math/big.Int.Bits方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。