当前位置: 首页>>代码示例>>Golang>>正文


Golang Int.DivMod方法代码示例

本文整理汇总了Golang中math/big.Int.DivMod方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.DivMod方法的具体用法?Golang Int.DivMod怎么用?Golang Int.DivMod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在math/big.Int的用法示例。


在下文中一共展示了Int.DivMod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: encodeBase58Check

func encodeBase58Check(val []byte) []byte {
	const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

	// payload
	p := make([]byte, 1+len(val)) // version byte (0x00) + val
	copy(p[1:], val)
	h1 := sha256.Sum256(p)
	h2 := sha256.Sum256(h1[:])

	// value as []byte
	v := make([]byte, len(p)+4) // payload + first 4 bytes of h2
	copy(v, p)
	copy(v[len(p):], h2[:4])

	var res []byte
	x := new(big.Int).SetBytes(v)
	y := big.NewInt(58)
	m, zero := new(big.Int), new(big.Int)

	// convert to base58
	for x.Cmp(zero) > 0 {
		x, m = x.DivMod(x, y, m)
		res = append(res, alphabet[m.Int64()])
	}
	// append '1' for each leading zero byte in value
	for i := 0; v[i] == 0; i++ {
		res = append(res, alphabet[0])
	}
	// reverse
	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
		res[i], res[j] = res[j], res[i]
	}

	return res
}
开发者ID:go-gophers,项目名称:gophers,代码行数:35,代码来源:bitcoin.go

示例2: 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:]
}
开发者ID:psyvisions,项目名称:bitwrk,代码行数:30,代码来源:base58.go

示例3: 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)
}
开发者ID:robcat,项目名称:ripple,代码行数:28,代码来源:base58.go

示例4: Verify

//Verify verifies the proof file server send.
func (sw *Swizzle) Verify(p Proof) (bool, error) {
	proof, ok := p.(*SwizzleProof)
	if !ok {
		return false, errors.New("use SwizzleProof instance for proof")
	}
	var rhs big.Int
	var dummy big.Int
	ch := sw.lastChallenge
	for i := 0; i < sw.chunks; i++ {
		f, err := keyedPRFBig(sw.fKey, sw.prime, i)
		if err != nil {
			return false, err
		}
		v, err := keyedPRFBig(ch.Key, ch.Vmax, i)
		if err != nil {
			return false, err
		}
		rhs.Add(&rhs, dummy.Mul(v, f))
	}
	for j := 0; j < ch.Sectors; j++ {
		alpha, err := keyedPRFBig(sw.alphaKey, sw.prime, j)
		if err != nil {
			return false, err
		}
		rhs.Add(&rhs, dummy.Mul(alpha, &proof.Mu[j]))
	}
	dummy.DivMod(&rhs, sw.prime, &rhs)
	if proof.Sigma.Cmp(&rhs) == 0 {
		return true, nil
	}
	return false, nil
}
开发者ID:utamaro,项目名称:go-heartbeat,代码行数:33,代码来源:swizzle.go

示例5: 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)], ",")
}
开发者ID:satyamkotakonda,项目名称:rack,代码行数:30,代码来源:comma.go

示例6: 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
}
开发者ID:jmptrader,项目名称:base62,代码行数:26,代码来源:base62.go

示例7: Sub11

func Sub11(xx *big.Int) big.Int {
	mm := new(big.Int)
	kk := big.NewInt(10)
	yy := new(big.Int)
	yy.DivMod(xx, kk, mm)
	yy.Sub(yy, mm)
	return *yy
}
开发者ID:ezralalonde,项目名称:fuzzy-ninja,代码行数:8,代码来源:div.go

示例8: ratCeil

// Returns a new big.Int set to the ceiling of x.
func ratCeil(x *big.Rat) *big.Int {
	z := new(big.Int)
	m := new(big.Int)
	z.DivMod(x.Num(), x.Denom(), m)
	if m.Cmp(intZero) == 1 {
		z.Add(z, intOne)
	}
	return z
}
开发者ID:awesome-docker,项目名称:scaleway-cli,代码行数:10,代码来源:utils.go

示例9: oom

// total order of magnitude
// (same as above, but with no upper limit)
func oom(n, b *big.Int) (float64, int) {
	mag := 0
	m := &big.Int{}
	for n.Cmp(b) >= 0 {
		n.DivMod(n, b, m)
		mag++
	}
	return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
}
开发者ID:ChongFeng,项目名称:beats,代码行数:11,代码来源:big.go

示例10: SumBig

func SumBig(n *big.Int, base int) (sum int) {
	i := new(big.Int).Set(n)
	b := new(big.Int).SetUint64(uint64(base))
	r := new(big.Int)
	for i.BitLen() > 0 {
		i.DivMod(i, b, r)
		sum += int(r.Uint64())
	}
	return
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:10,代码来源:sum-digits-of-an-integer-1.go

示例11: EncodeToString

// EncodeToString returns the base-encoded string representation
// of the given bytes.
func (enc *Encoding) EncodeToString(b []byte) string {
	n := new(big.Int)
	r := new(big.Int)
	n.SetBytes(b)
	var result []byte
	for n.Cmp(zero) > 0 {
		n, r = n.DivMod(n, enc.base, r)
		result = append([]byte{enc.alphabet[r.Int64()]}, result...)
	}
	return string(result)
}
开发者ID:cmars,项目名称:oo,代码行数:13,代码来源:basen.go

示例12: oomm

// order of magnitude (to a max order)
func oomm(n, b *big.Int, maxmag int) (float64, int) {
	mag := 0
	m := &big.Int{}
	for n.Cmp(b) >= 0 {
		n.DivMod(n, b, m)
		mag++
		if mag == maxmag && maxmag >= 0 {
			break
		}
	}
	return float64(n.Int64()) + (float64(m.Int64()) / float64(b.Int64())), mag
}
开发者ID:ChongFeng,项目名称:beats,代码行数:13,代码来源:big.go

示例13: reverse

// TODO: Pass in result, so it can be reused.
func reverse(n *big.Int) (result *big.Int) {
	result = big.NewInt(0)
	var work big.Int
	work.Set(n)
	tmp := big.NewInt(0)

	for work.Cmp(zero) != 0 {
		work.DivMod(&work, ten, tmp)
		result.Mul(result, ten)
		result.Add(result, tmp)
	}
	return
}
开发者ID:d3zd3z,项目名称:euler,代码行数:14,代码来源:pr055.go

示例14: NumberEncode

func NumberEncode(number string, alphabet []byte) string {
	token := make([]byte, 0, 12)
	x, ok := new(big.Int).SetString(number, 10)
	if !ok {
		return ""
	}
	y := big.NewInt(int64(len(alphabet)))
	m := new(big.Int)
	for x.Sign() > 0 {
		x, m = x.DivMod(x, y, m)
		token = append(token, alphabet[int(m.Int64())])
	}
	return string(token)
}
开发者ID:ming-hai,项目名称:account,代码行数:14,代码来源:tools.go

示例15: EncodeAlphabet

func EncodeAlphabet(alphabet, bs []byte) string {
	base := big.NewInt(int64(len(alphabet)))
	result := make([]byte, 0)

	num := new(big.Int).SetBytes(bs)
	rem := new(big.Int)

	for num.Sign() != 0 {
		num.DivMod(num, base, rem)

		result = append(result, alphabet[rem.Int64()])
	}

	return string(result)
}
开发者ID:rovaughn,项目名称:nimbus,代码行数:15,代码来源:alphanum.go


注:本文中的math/big.Int.DivMod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。