本文整理汇总了Golang中math/big.Int.SetUint64方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.SetUint64方法的具体用法?Golang Int.SetUint64怎么用?Golang Int.SetUint64使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.SetUint64方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkPe1bloop
func BenchmarkPe1bloop(b *testing.B) {
bi := new(big.Int)
bi.SetUint64(uint64(1000000))
for i := 0; i < b.N; i++ {
pe.Pe1bloop(bi)
}
}
示例2: TestDecodeBigNumber
func TestDecodeBigNumber(t *testing.T) {
var value big.Int
value.SetUint64(^uint64(0))
value.Mul(&value, big.NewInt(32))
b := bytes.Buffer{}
e := NewEncoder(&b)
err := e.Encode(value)
if err != nil {
t.Fatal(err)
}
t.Log(hex.Dump(b.Bytes()))
d := NewDecoder(&b)
found, err := d.DecodeNext()
if err != nil {
t.Fatal(err)
}
i := found.(big.Int)
if i.Cmp(&value) != 0 {
t.Fatalf("expected %v but %v found", value, found)
}
}
示例3: calcDifficultyFrontier
func calcDifficultyFrontier(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
diff := new(big.Int)
adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
bigTime := new(big.Int)
bigParentTime := new(big.Int)
bigTime.SetUint64(time)
bigParentTime.SetUint64(parentTime)
if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
diff.Add(parentDiff, adjust)
} else {
diff.Sub(parentDiff, adjust)
}
if diff.Cmp(params.MinimumDifficulty) < 0 {
diff.Set(params.MinimumDifficulty)
}
periodCount := new(big.Int).Add(parentNumber, common.Big1)
periodCount.Div(periodCount, ExpDiffPeriod)
if periodCount.Cmp(common.Big1) > 0 {
// diff = diff + 2^(periodCount - 2)
expDiff := periodCount.Sub(periodCount, common.Big2)
expDiff.Exp(common.Big2, expDiff, nil)
diff.Add(diff, expDiff)
diff = common.BigMax(diff, params.MinimumDifficulty)
}
return diff
}
示例4: bigIntFromFloat
// "stolen" from https://golang.org/pkg/math/big/#Rat.SetFloat64
// Removed non-finite case because we already check for
// Inf/NaN values
func bigIntFromFloat(f float64) *big.Int {
const expMask = 1<<11 - 1
bits := math.Float64bits(f)
mantissa := bits & (1<<52 - 1)
exp := int((bits >> 52) & expMask)
if exp == 0 { // denormal
exp -= 1022
} else { // normal
mantissa |= 1 << 52
exp -= 1023
}
shift := 52 - exp
// Optimization (?): partially pre-normalise.
for mantissa&1 == 0 && shift > 0 {
mantissa >>= 1
shift--
}
if shift < 0 {
shift = -shift
}
var a big.Int
a.SetUint64(mantissa)
return a.Lsh(&a, uint(shift))
}
示例5: Info
func (m *Material) Info() [][][2]string {
var info [][][2]string
maybe := func(name string, stat world.Stat) {
var total, volume, tmp big.Int
for _, c := range m.components {
tmp.SetUint64(c.volume)
volume.Add(&volume, &tmp)
total.Add(&total, tmp.Mul(c.data().Stat(stat), &tmp))
}
if volume.Sign() == 0 {
return
}
total.Div(tmp.Mul(&total, &m.quality), &volume)
switch total.Sign() {
case 0:
return
case 1:
info = append(info, [][2]string{
{"+" + Comma(&total), "#4f4"},
{name, "#ccc"},
})
case -1:
info = append(info, [][2]string{
{Comma(&total), "#f44"},
{name, "#ccc"},
})
}
}
maybe(" power", world.StatPower)
maybe(" magic", world.StatMagic)
maybe(" agility", world.StatAgility)
maybe(" luck", world.StatLuck)
maybe(" intelligence", world.StatIntelligence)
maybe(" stamina", world.StatStamina)
maybe(" integrity", world.StatIntegrity)
maybe(" melee damage", world.StatMeleeDamage)
maybe(" magic damage", world.StatMagicDamage)
maybe(" mana", world.StatMana)
maybe(" mana regen", world.StatManaRegen)
maybe(" crit chance", world.StatCritChance)
maybe(" attack speed", world.StatAttackSpeed)
maybe(" melee armor", world.StatMeleeArmor)
maybe(" magic armor", world.StatMagicArmor)
maybe(" health", world.StatHealth)
maybe(" health regen", world.StatHealthRegen)
maybe(" resistance", world.StatResistance)
maybe(" movement speed", world.StatMovementSpeed)
maybe(" gathering", world.StatGathering)
maybe(" structure health", world.StatStructureHealth)
return info
}
示例6: TestPe1bloop
func TestPe1bloop(t *testing.T) {
bi := new(big.Int)
bi.SetUint64(uint64(1000))
actual := pe.Pe1bloop(bi)
expected := new(big.Int).SetUint64(uint64(234168))
if actual.Cmp(expected) != 0 {
t.Errorf("got %v, want %v", actual, expected)
}
}
示例7: computeChildSecret
// computeChildSecret helper method that derives a child secret key from the
// intermediary state.
func (k *HDKey) computeChildSecret(ilInt *big.Int) *eckey.SecretKey {
keyInt := new(big.Int).SetBytes(k[childKeyOffset+1:])
defer keyInt.SetUint64(0)
ilInt.Add(ilInt, keyInt)
ilInt.Mod(ilInt, eckey.S256.N)
sk := new(eckey.SecretKey)
util.PaddedCopy(sk[:], ilInt.Bytes(), eckey.SecretSize)
return sk
}
示例8: Child
// Child computes the descendant of an HDKey at the specified child number.
func (k *HDKey) Child(i uint32) (*HDKey, error) {
// Verify that child derivation is possible
isChildHardened := i >= HardenedKeyStart
if !k.IsPrivate() && isChildHardened {
return nil, ErrDeriveHardenedFromPublic
}
// Assemble seed data for HMAC
seed := make([]byte, childKeySize+childNumberSize)
if isChildHardened {
copy(seed, k[childKeyOffset:]) // Copy 0x00 || 32-byte secret key
} else {
copy(seed, k.CompressedPublicKey()[:]) // Copy HEADER || 32-byte X-coord
}
// Copy child number as uint32
binary.BigEndian.PutUint32(seed[childKeySize:], i)
// il, ir = HMAC-512(chainCode, seed), clean up intermediary state
il, childChainCode := util.HMAC512Split(k.chainCode(), seed)
defer func() { util.Zero(il); util.Zero(childChainCode) }()
// Left 32 bytes becomes intermediate secret key, defer clean up
ilInt := new(big.Int).SetBytes(il)
defer ilInt.SetUint64(0)
// Check that ilInt creates valid SecretKey, clean up intermediary SecretKey
isk, err := eckey.NewSecretKeyInt(ilInt)
if err != nil {
return nil, ErrUnusableSeed
}
defer isk.Zero()
ver := k.version()
parentCPK := k.CompressedPublicKey()
fpBytes := util.Hash256d(parentCPK[:])[:fingerprintSize]
fp := binary.BigEndian.Uint32(fpBytes)
// If key is private, derive a child secret key
if k.IsPrivate() {
sk := k.computeChildSecret(ilInt)
return newHDSecretKey(ver, k.depth()+1, fp, i, childChainCode, sk), nil
}
// Otherwise, derive child public key
cpk, err := computeChildPublic(parentCPK, isk)
if err != nil {
return nil, err
}
return newHDPublicKey(ver, k.depth()+1, fp, i, childChainCode, cpk), nil
}
示例9: TestModSqr
func TestModSqr(t *testing.T) {
A := new(big.Int)
B := new(big.Int)
for _, a := range numbers {
A.SetUint64(a)
b := mod_sqr(a)
B.Mul(A, A)
B.Mod(B, P)
expected := B.Uint64()
if b != expected {
t.Fatalf("%d**2: Expecting %d but got %d", a, expected, b)
}
}
}
示例10: pe1b
func pe1b() {
bi := new(big.Int)
if len(os.Args) > 1 {
if _, ok := bi.SetString(os.Args[1], 10); !ok {
fmt.Printf("couldn't interpret line %#v\n", os.Args[1])
}
} else {
bi.SetUint64(uint64(1000))
}
//ans := pe.Pe1bloop(bi)
ans := pe.Pe1b(bi)
fmt.Println("pe1: ", ans)
}
示例11: stat
func (m *Material) stat(stat, meta *world.Stat) *big.Int {
var total, volume, tmp big.Int
for _, c := range m.components {
tmp.SetUint64(c.volume)
volume.Add(&volume, &tmp)
total.Add(&total, tmp.Mul(c.data().Stat(*stat), &tmp))
if meta != nil {
total.Add(&total, tmp.Div(tmp.Mul(c.data().Stat(*meta), tmp.SetUint64(c.volume)), world.TuningMetaStatDivisor))
}
}
if volume.Sign() == 0 {
return &volume
}
total.Div(tmp.Mul(&total, &m.quality), &volume)
return &total
}
示例12: NewSecretKey
func NewSecretKey(b []byte) (*SecretKey, error) {
if len(b) != SecretSize {
return nil, ErrInvalidSecretLength
}
// Copy secret bytes and clean up
s := new(big.Int).SetBytes(b)
defer s.SetUint64(0)
sk, err := NewSecretKeyInt(s)
if err != nil {
return nil, err
}
return sk, nil
}
示例13: TestModInv
func TestModInv(t *testing.T) {
A := new(big.Int)
B := new(big.Int)
for _, a := range numbers {
if a == 0 {
continue
}
A.SetUint64(a)
b := mod_inv(a)
B.ModInverse(A, P)
expected := B.Uint64()
if b != expected {
t.Fatalf("inv(%d): Expecting %d but got %d", a, expected, b)
}
}
}
示例14: TestModShift
func TestModShift(t *testing.T) {
A := new(big.Int)
C := new(big.Int)
for _, a := range numbers {
A.SetUint64(a)
for b := uint8(0); b < 96; b++ {
c := mod_shift(a, b)
C.Lsh(A, uint(b))
C.Mod(C, P)
expected := C.Uint64()
if c != expected {
t.Fatalf("%d << %d: Expecting %d but got %d", a, b, expected, c)
}
}
}
}
示例15: TestModPow
func TestModPow(t *testing.T) {
A := new(big.Int)
B := new(big.Int)
C := new(big.Int)
for _, a := range numbers {
A.SetUint64(a)
for _, b := range numbers {
B.SetUint64(b)
c := mod_pow(a, b)
C.Exp(A, B, P)
expected := C.Uint64()
if c != expected {
t.Errorf("0x%X ** 0x%X: Expecting 0x%X but got 0x%X", a, b, expected, c)
}
}
}
}