本文整理汇总了Golang中math/big.Int.Sign方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Sign方法的具体用法?Golang Int.Sign怎么用?Golang Int.Sign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Sign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encodeBlock
// encodeBlock fills the dst buffer with the encoding of src.
// It is assumed the buffers are appropriately sized, and no
// bounds checks are performed. In particular, the dst buffer will
// be zero-padded from right to left in all remaining bytes.
func (enc *Encoding) encodeBlock(dst, src []byte) {
// Interpret the block as a big-endian number (Go's default)
num := new(big.Int).SetBytes(src)
rem := new(big.Int)
quo := new(big.Int)
encodedLen := enc.EncodedLen(len(src))
// Shift over the given number of extra Bits, so that all of our
// wasted bits are on the right.
num = num.Lsh(num, enc.extraBits(len(src), encodedLen))
p := encodedLen - 1
for num.Sign() != 0 {
num, rem = quo.QuoRem(num, enc.baseBig, rem)
dst[p] = enc.encode[rem.Uint64()]
p--
}
// Pad the remainder of the buffer with 0s
for p >= 0 {
dst[p] = enc.encode[0]
p--
}
}
示例2: validateECPublicKey
// validateECPublicKey checks that the point is a valid public key for
// the given curve. See [SEC1], 3.2.2
func validateECPublicKey(curve elliptic.Curve, x, y *big.Int) bool {
if x.Sign() == 0 && y.Sign() == 0 {
return false
}
if x.Cmp(curve.Params().P) >= 0 {
return false
}
if y.Cmp(curve.Params().P) >= 0 {
return false
}
if !curve.IsOnCurve(x, y) {
return false
}
// We don't check if N * PubKey == 0, since
//
// - the NIST curves have cofactor = 1, so this is implicit.
// (We don't foresee an implementation that supports non NIST
// curves)
//
// - for ephemeral keys, we don't need to worry about small
// subgroup attacks.
return true
}
示例3: FromFactorBigInt
/*
FromFactorBigInt returns n such that d | Mn if n <= max and d is odd. In other
cases zero is returned.
It is conjectured that every odd d ∊ N divides infinitely many Mersenne numbers.
The returned n should be the exponent of smallest such Mn.
NOTE: The computation of n from a given d performs roughly in O(n). It is
thus highly recomended to use the 'max' argument to limit the "searched"
exponent upper bound as appropriate. Otherwise the computation can take a long
time as a large factor can be a divisor of a Mn with exponent above the uint32
limits.
The FromFactorBigInt function is a modification of the original Will
Edgington's "reverse method", discussed here:
http://tech.groups.yahoo.com/group/primenumbers/message/15061
*/
func FromFactorBigInt(d *big.Int, max uint32) (n uint32) {
if d.Bit(0) == 0 {
return
}
var m big.Int
for n < max {
m.Add(&m, d)
i := 0
for ; m.Bit(i) == 1; i++ {
if n == math.MaxUint32 {
return 0
}
n++
}
m.Rsh(&m, uint(i))
if m.Sign() == 0 {
if n > max {
n = 0
}
return
}
}
return 0
}
示例4: NewMaster
// NewMaster creates a new master node for use in creating a hierarchical
// deterministic key chain. The seed must be between 128 and 512 bits and
// should be generated by a cryptographically secure random generation source.
//
// NOTE: There is an extremely small chance (< 1 in 2^127) the provided seed
// will derive to an unusable secret key. The ErrUnusable error will be
// returned if this should occur, so the caller must check for it and generate a
// new seed accordingly.
func NewMaster(seed []byte, net *chaincfg.Params) (*ExtendedKey, error) {
// Per [BIP32], the seed must be in range [MinSeedBytes, MaxSeedBytes].
if len(seed) < MinSeedBytes || len(seed) > MaxSeedBytes {
return nil, ErrInvalidSeedLen
}
// First take the HMAC-SHA512 of the master key and the seed data:
// I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)
hmac512 := hmac.New(sha512.New, masterKey)
hmac512.Write(seed)
lr := hmac512.Sum(nil)
// Split "I" into two 32-byte sequences Il and Ir where:
// Il = master secret key
// Ir = master chain code
secretKey := lr[:len(lr)/2]
chainCode := lr[len(lr)/2:]
// Ensure the key in usable.
secretKeyNum := new(big.Int).SetBytes(secretKey)
if secretKeyNum.Cmp(btcec.S256().N) >= 0 || secretKeyNum.Sign() == 0 {
return nil, ErrUnusableSeed
}
parentFP := []byte{0x00, 0x00, 0x00, 0x00}
return newExtendedKey(net.HDPrivateKeyID[:], secretKey, chainCode,
parentFP, 0, 0, true), nil
}
示例5: Verify
// Verify verifies the signature in r, s of hash using the public key, pub. It
// reports whether the signature is valid.
//
// Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
// to the byte-length of the subgroup. This function does not perform that
// truncation itself.
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
// FIPS 186-3, section 4.7
if pub.P.Sign() == 0 {
return false
}
if r.Sign() < 1 || r.Cmp(pub.Q) >= 0 {
return false
}
if s.Sign() < 1 || s.Cmp(pub.Q) >= 0 {
return false
}
w := new(big.Int).ModInverse(s, pub.Q)
n := pub.Q.BitLen()
if n&7 != 0 {
return false
}
z := new(big.Int).SetBytes(hash)
u1 := new(big.Int).Mul(z, w)
u1.Mod(u1, pub.Q)
u2 := w.Mul(r, w)
u2.Mod(u2, pub.Q)
v := u1.Exp(pub.G, u1, pub.P)
u2.Exp(pub.Y, u2, pub.P)
v.Mul(v, u2)
v.Mod(v, pub.P)
v.Mod(v, pub.Q)
return v.Cmp(r) == 0
}
示例6: EncodeBase58
func EncodeBase58(ba []byte) []byte {
if len(ba) == 0 {
return nil
}
// Expected size increase from base58 conversion is approximately 137%, use 138% to be safe
ri := len(ba) * 138 / 100
ra := make([]byte, ri+1)
x := new(big.Int).SetBytes(ba) // ba is big-endian
x.Abs(x)
y := big.NewInt(58)
m := new(big.Int)
for x.Sign() > 0 {
x, m = x.DivMod(x, y, m)
ra[ri] = base58[int32(m.Int64())]
ri--
}
// Leading zeroes encoded as base58 zeros
for i := 0; i < len(ba); i++ {
if ba[i] != 0 {
break
}
ra[ri] = '1'
ri--
}
return ra[ri+1:]
}
示例7: BigComma
// BigComma produces a string form of the given big.Int in base 10
// with commas after every three orders of magnitude.
func BigComma(b *big.Int) string {
sign := ""
if b.Sign() < 0 {
sign = "-"
b.Abs(b)
}
athousand := big.NewInt(1000)
c := (&big.Int{}).Set(b)
_, m := oom(c, athousand)
parts := make([]string, m+1)
j := len(parts) - 1
mod := &big.Int{}
for b.Cmp(athousand) >= 0 {
b.DivMod(b, athousand, mod)
parts[j] = strconv.FormatInt(mod.Int64(), 10)
switch len(parts[j]) {
case 2:
parts[j] = "0" + parts[j]
case 1:
parts[j] = "00" + parts[j]
}
j--
}
parts[j] = strconv.Itoa(int(b.Int64()))
return sign + strings.Join(parts[j:len(parts)], ",")
}
示例8: StrongMillerRabin
// StrongMillerRabin checks if N is a
// strong Miller-Rabin pseudoprime in base a.
// That is, it checks if a is a witness
// for compositeness of N or if N is a strong
// pseudoprime base a.
//
// Use builtin ProbablyPrime if you want to do a lot
// of random tests, this is for one specific
// base value.
func StrongMillerRabin(N *big.Int, a int64) int {
// Step 0: parse input
if N.Sign() < 0 || N.Bit(0) == 0 || a < 2 {
panic("MR is for positive odd integers with a >= 2")
}
A := big.NewInt(a)
if (a == 2 && N.Bit(0) == 0) || new(big.Int).GCD(nil, nil, N, A).Cmp(one) != 0 {
return IsComposite
}
// Step 1: find d,s, so that n - 1 = d*2^s
// with d odd
d := new(big.Int).Sub(N, one)
s := trailingZeroBits(d)
d.Rsh(d, s)
// Step 2: compute powers a^d
// and then a^(d*2^r) for 0<r<s
nm1 := new(big.Int).Sub(N, one)
Ad := new(big.Int).Exp(A, d, N)
if Ad.Cmp(one) == 0 || Ad.Cmp(nm1) == 0 {
return Undetermined
}
for r := uint(1); r < s; r++ {
Ad.Exp(Ad, two, N)
if Ad.Cmp(nm1) == 0 {
return Undetermined
}
}
// Step 3: a is a witness for compositeness
return IsComposite
}
示例9: pSub
// addJacition and subtraction
func pSub(a, b *big.Int) *big.Int {
x := new(big.Int).Sub(a, b)
if x.Sign() == -1 {
x.Add(x, curveP)
}
return x
}
示例10: BPSW
// BPSW runs the Baillie-PSW primality test on N.
// An undetermined result is likely prime.
//
// For more see http://www.trnicely.net/misc/bpsw.html
func BPSW(N *big.Int) int {
//Step 0: parse input
if N.Sign() <= 0 {
panic("BPSW is for positive integers only")
}
// Step 1: check all small primes
switch SmallPrimeTest(N) {
case IsPrime:
return IsPrime
case IsComposite:
return IsComposite
}
// Step 2: Miller-Rabin test
// returns false if composite
if StrongMillerRabin(N, 2) == IsComposite {
return IsComposite
}
// Step 3: Lucas-Selfridge test
// returns false if composite
if StrongLucasSelfridge(N) == IsComposite {
return IsComposite
}
// Step 4: If didn't fail other tests
// return true, i.e. this passed
return Undetermined
}
示例11: 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
}
示例12: MulBigPow10
// MulBigPow10 computes 10 * x ** n.
// It reuses x.
func MulBigPow10(x *big.Int, n int32) *big.Int {
if x.Sign() == 0 || n <= 0 {
return x
}
b := pow.BigTen(int64(n))
return x.Mul(x, &b)
}
示例13: Verify
// Verify a hash value with public key.
// [http://www.nsa.gov/ia/_files/ecdsa.pdf, page 15f]
func Verify(key *PublicKey, hash []byte, r, s *big.Int) bool {
// sanity checks for arguments
if r.Sign() == 0 || s.Sign() == 0 {
return false
}
if r.Cmp(curveN) >= 0 || s.Cmp(curveN) >= 0 {
return false
}
// check signature
e := convertHash(hash)
w := nInv(s)
u1 := e.Mul(e, w)
u2 := w.Mul(r, w)
p1 := ScalarMultBase(u1)
p2 := scalarMult(key.Q, u2)
if p1.x.Cmp(p2.x) == 0 {
return false
}
p3 := add(p1, p2)
rr := nMod(p3.x)
return rr.Cmp(r) == 0
}
示例14: encode_int
func (self *Encoder) encode_int(number *big.Int, size uint) []byte {
if size == 1 {
return []byte{uint8(int8(number.Int64()))}
} else if size == 2 {
number_buf := uint16(int16(number.Int64()))
return []byte{
uint8(number_buf >> 8),
uint8(number_buf),
}
} else if size == 4 {
number_buf := uint32(int32(number.Int64()))
return []byte{
uint8(number_buf >> 24),
uint8(number_buf >> 16),
uint8(number_buf >> 8),
uint8(number_buf),
}
} else if size == 0 {
if number.Sign() < 0 {
panic("jksn: number < 0")
}
result := []byte{uint8(new(big.Int).And(number, big.NewInt(0x7f)).Uint64())}
number.Rsh(number, 7)
for number.Sign() != 0 {
result = append(result, uint8(new(big.Int).And(number, big.NewInt(0x7f)).Uint64())|0x80)
number.Rsh(number, 7)
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return result
} else {
panic("jksn: size not in (1, 2, 4, 0)")
}
}
示例15: Int
// Int returns a uniform random value in [0, max). It panics if max <= 0.
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) {
if max.Sign() <= 0 {
panic("crypto/rand: argument to Int is <= 0")
}
k := (max.BitLen() + 7) / 8
// Jeffrey hack
return big.NewInt(1), nil
// b is the number of bits in the most significant byte of max.
b := uint(max.BitLen() % 8)
if b == 0 {
b = 8
}
bytes := make([]byte, k)
n = new(big.Int)
for {
_, err = io.ReadFull(rand, bytes)
if err != nil {
return nil, err
}
// Clear bits in the first byte to increase the probability
// that the candidate is < max.
bytes[0] &= uint8(int(1<<b) - 1)
n.SetBytes(bytes)
if n.Cmp(max) < 0 {
return
}
}
}