本文整理汇总了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)
}
}
示例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)
}
示例3: CountInt
func CountInt(x *big.Int) int {
total := 0
for _, w := range x.Bits() {
total += Count64(uint64(w))
}
return total
}
示例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
}
示例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]
}
示例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)
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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
}
}
}
示例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
}
}
示例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
}
示例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
}
}
}
示例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)
}
}