本文整理汇总了Golang中math/big.Int.Int64方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Int64方法的具体用法?Golang Int.Int64怎么用?Golang Int.Int64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Int64方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UnmarshalJSON
func (n *Number) UnmarshalJSON(data []byte) error {
input := strings.TrimSpace(string(data))
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}
if len(input) == 0 {
*n = Number(latestBlockNumber.Int64())
return nil
}
in := new(big.Int)
_, ok := in.SetString(input, 0)
if !ok { // test if user supplied string tag
return fmt.Errorf(`invalid number %s`, data)
}
if in.Cmp(earliestBlockNumber) >= 0 && in.Cmp(maxBlockNumber) <= 0 {
*n = Number(in.Int64())
return nil
}
return fmt.Errorf("blocknumber not in range [%d, %d]", earliestBlockNumber, maxBlockNumber)
}
示例2: Base58Encode
// Base58Encode encodes a byte slice to a modified base58 string.
func Base58Encode(b []byte, alphabet string) string {
x := new(big.Int)
x.SetBytes(b)
answer := make([]byte, 0)
for x.Cmp(bigZero) > 0 {
mod := new(big.Int)
x.DivMod(x, bigRadix, mod)
answer = append(answer, alphabet[mod.Int64()])
}
// leading zero bytes
for _, i := range b {
if i != 0 {
break
}
answer = append(answer, alphabet[0])
}
// reverse
alen := len(answer)
for i := 0; i < alen/2; i++ {
answer[i], answer[alen-1-i] = answer[alen-1-i], answer[i]
}
return string(answer)
}
示例3: Encode
// Encode encodes src into EncodedMaxLen(len(src))
// or fewer bytes of dst. It returns the number of bytes written to dst.
func Encode(dst, src []byte) int {
zeros := 0
for _, b := range src {
if int(b) == 0 {
zeros++
} else {
break
}
}
i := new(big.Int).SetBytes(src)
big58 := big.NewInt(58)
big0 := big.NewInt(0)
var index int
for i.Cmp(big0) > 0 {
tmp := new(big.Int).Mod(i, big58)
i.Div(i, big58)
dst[index] = base58alphabet[tmp.Int64()]
index++
}
for ; zeros > 0; zeros-- {
dst[index] = base58alphabet[0]
index++
}
reverseInplace(dst[0:index])
return index
}
示例4: 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)], ",")
}
示例5: rand
func rand(max *big.Int) int64 {
i, err := urandom.Int(urandom.Reader, max)
if err != nil {
return prandom.Int63n(max.Int64())
}
return i.Int64()
}
示例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: Validate
func Validate(user string, pass string) bool {
sess := GetSession()
defer sess.Close()
authdb := sess.DB("prod").C("auth")
var r *big.Int
r, _ = rand.Int(rand.Reader, big.NewInt(5))
time.Sleep(time.Duration(r.Int64()) * time.Nanosecond)
var person S.User
err := authdb.Find(bson.M{"user": user}).One(&person)
if err != nil {
fmt.Println(err)
} else {
hasher := sha256.New()
hasher.Write([]byte(pass + string(person.Salt)))
hashed := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
if hashed == person.Pass {
return true
}
}
return false
}
示例8: Resolver
func (self *EthReg) Resolver(n *big.Int) *registrar.Registrar {
xe := self.backend
if n != nil {
xe = self.backend.AtStateNum(n.Int64())
}
return registrar.New(xe)
}
示例9: setBignum
func setBignum(rv reflect.Value, x *big.Int) error {
switch rv.Kind() {
case reflect.Ptr:
return setBignum(reflect.Indirect(rv), x)
case reflect.Interface:
rv.Set(reflect.ValueOf(*x))
return nil
case reflect.Int32:
if x.BitLen() < 32 {
rv.SetInt(x.Int64())
return nil
} else {
return fmt.Errorf("int too big for int32 target")
}
case reflect.Int, reflect.Int64:
if x.BitLen() < 64 {
rv.SetInt(x.Int64())
return nil
} else {
return fmt.Errorf("int too big for int64 target")
}
default:
return fmt.Errorf("cannot assign bignum into Kind=%s Type=%s %#v", rv.Kind().String(), rv.Type().String(), rv)
}
}
示例10: EncodeBigInt
// EncodeBigInt returns the base62 encoding of an arbitrary precision integer
func (e *Encoding) EncodeBigInt(n *big.Int) string {
var (
b = make([]byte, 0)
rem = new(big.Int)
bse = new(big.Int)
zero = new(big.Int)
)
bse.SetInt64(base)
zero.SetInt64(0)
// Progressively divide by base, until we hit zero
// store remainder each time
// Prepend as an additional character is the higher power
for n.Cmp(zero) == 1 {
n, rem = n.DivMod(n, bse, rem)
b = append([]byte{e.encode[rem.Int64()]}, b...)
}
s := string(b)
if e.padding > 0 {
s = e.pad(s, e.padding)
}
return s
}
示例11: GetInstr
func (c *StateObject) GetInstr(pc *big.Int) *common.Value {
if int64(len(c.code)-1) < pc.Int64() {
return common.NewValue(0)
}
return common.NewValueFromBytes([]byte{c.code[pc.Int64()]})
}
示例12: main
func main() {
lim := 1000
primes := allPrimes(lim)
mx_cyc := 0
mx_len := 0
for _, i := range primes {
j := 1
for j < i {
p := new(big.Int)
*p = bigPow(10, j)
res := new(big.Int)
res.Mod(p, big.NewInt(int64(i)))
if res.Int64() == 1 {
if mx_len < j {
mx_len = i
mx_cyc = j
}
break
}
j++
}
}
fmt.Printf("Found: 1/%d yields %d cycles\n", mx_len, mx_cyc)
}
示例13: UnmarshalJSON
func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) {
var obj []struct {
Payload string
To string
From string
Topics []string
Priority interface{}
Ttl interface{}
}
if err = json.Unmarshal(b, &obj); err != nil {
return shared.NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return shared.NewInsufficientParamsError(len(obj), 1)
}
args.Payload = obj[0].Payload
args.To = obj[0].To
args.From = obj[0].From
args.Topics = obj[0].Topics
var num *big.Int
if num, err = numString(obj[0].Priority); err != nil {
return err
}
args.Priority = uint32(num.Int64())
if num, err = numString(obj[0].Ttl); err != nil {
return err
}
args.Ttl = uint32(num.Int64())
return nil
}
示例14: bigRsh
func bigRsh(z, x, y *big.Int) *big.Int {
i := y.Int64()
if i < 0 {
panic("negative shift")
}
return z.Rsh(x, uint(i))
}
示例15: GetInstr
func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value {
if int64(len(c.Code)-1) < pc.Int64() {
return ethutil.NewValue(0)
}
return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]})
}