本文整理汇总了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
}
示例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:]
}
示例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)
}
示例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
}
示例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)], ",")
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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)
}
示例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)
}