本文整理汇总了Golang中math/big.Int.Lsh方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Lsh方法的具体用法?Golang Int.Lsh怎么用?Golang Int.Lsh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Lsh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: unsigned_to_signed
func (self *Decoder) unsigned_to_signed(x *big.Int, bits uint) *big.Int {
// return x - ((x >> (bits - 1)) << bits)
temp := new(big.Int)
temp.Rsh(x, bits-1)
temp.Lsh(temp, bits)
return temp.Sub(x, temp)
}
示例2: 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--
}
}
示例3: TestP256BaseMult
func TestP256BaseMult(t *testing.T) {
p256 := P256()
p256Generic := p256.Params()
scalars := make([]*big.Int, 0, len(p224BaseMultTests)+1)
for _, e := range p224BaseMultTests {
k, _ := new(big.Int).SetString(e.k, 10)
scalars = append(scalars, k)
}
k := new(big.Int).SetInt64(1)
k.Lsh(k, 500)
scalars = append(scalars, k)
for i, k := range scalars {
x, y := p256.ScalarBaseMult(k.Bytes())
x2, y2 := p256Generic.ScalarBaseMult(k.Bytes())
if x.Cmp(x2) != 0 || y.Cmp(y2) != 0 {
t.Errorf("#%d: got (%x, %x), want (%x, %x)", i, x, y, x2, y2)
}
if testing.Short() && i > 5 {
break
}
}
}
示例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: Shift
// Shift returns the result of the shift expression x op s
// with op == token.SHL or token.SHR (<< or >>). x must be
// an Int.
//
func Shift(x Value, op token.Token, s uint) Value {
switch x := x.(type) {
case unknownVal:
return x
case int64Val:
if s == 0 {
return x
}
switch op {
case token.SHL:
z := big.NewInt(int64(x))
return normInt(z.Lsh(z, s))
case token.SHR:
return x >> s
}
case intVal:
if s == 0 {
return x
}
var z big.Int
switch op {
case token.SHL:
return normInt(z.Lsh(x.val, s))
case token.SHR:
return normInt(z.Rsh(x.val, s))
}
}
panic(fmt.Sprintf("invalid shift %v %s %d", x, op, s))
}
示例6: bigLsh
func bigLsh(z, x, y *big.Int) *big.Int {
i := y.Int64()
if i < 0 {
panic("negative shift")
}
return z.Lsh(x, uint(i))
}
示例7: MakeRandom
func MakeRandom(gen *rand.Rand, degree int) *Polynomial {
if degree == 0 {
return NewPolynomialFromInt(gen.Int63n(2))
}
coeffs := new(big.Int)
// x^0 + x^1 + ... + x^n => n + 1 terms
// However, only the first n terms are variable. (x^n is fixed to
// have degree n.) Thus, we randomly generate the first n terms
// and fix the final term x^n.
numBits := degree
numBlocks := numBits / 32
for ii := 0; ii < numBlocks; ii++ {
v := gen.Uint32()
// Merge.
bigV := big.NewInt(int64(v))
coeffs.Lsh(coeffs, 32).Or(coeffs, bigV)
}
// Handle the remainder.
numRemainingBits := uint(numBits % 32)
if numRemainingBits > 0 {
mask := (int64(1) << numRemainingBits) - 1
v := int64(gen.Uint32()) & mask
coeffs.Lsh(coeffs, numRemainingBits).Or(coeffs, big.NewInt(v))
}
coeffs.SetBit(coeffs, degree, 1)
return NewPolynomial(uint(degree), coeffs)
}
示例8: ISqrt
// ISqrt returns the greatest number x such that x^2 <= n. n must be
// non-negative.
//
// See https://www.akalin.com/computing-isqrt for an analysis.
func ISqrt(n *big.Int) *big.Int {
s := n.Sign()
if s < 0 {
panic("negative radicand")
}
if s == 0 {
return &big.Int{}
}
// x = 2^ceil(Bits(n)/2)
var x big.Int
x.Lsh(big.NewInt(1), (uint(n.BitLen())+1)/2)
for {
// y = floor((x + floor(n/x))/2)
var y big.Int
y.Div(n, &x)
y.Add(&y, &x)
y.Rsh(&y, 1)
if y.Cmp(&x) >= 0 {
return &x
}
x = y
}
}
示例9: CompactToBig
// CompactToBig converts a compact representation of a whole number N to an
// unsigned 32-bit number. The representation is similar to IEEE754 floating
// point numbers.
//
// Like IEEE754 floating point, there are three basic components: the sign,
// the exponent, and the mantissa. They are broken out as follows:
//
// * the most significant 8 bits represent the unsigned base 256 exponent
// * bit 23 (the 24th bit) represents the sign bit
// * the least significant 23 bits represent the mantissa
//
// -------------------------------------------------
// | Exponent | Sign | Mantissa |
// -------------------------------------------------
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
// -------------------------------------------------
//
// The formula to calculate N is:
// N = (-1^sign) * mantissa * 256^(exponent-3)
//
// This compact form is only used in bitcoin to encode unsigned 256-bit numbers
// which represent difficulty targets, thus there really is not a need for a
// sign bit, but it is implemented here to stay consistent with bitcoind.
func CompactToBig(compact uint32) *big.Int {
// Extract the mantissa, sign bit, and exponent.
mantissa := compact & 0x007fffff
isNegative := compact&0x00800000 != 0
exponent := uint(compact >> 24)
// Since the base for the exponent is 256, the exponent can be treated
// as the number of bytes to represent the full 256-bit number. So,
// treat the exponent as the number of bytes and shift the mantissa
// right or left accordingly. This is equivalent to:
// N = mantissa * 256^(exponent-3)
var bn *big.Int
if exponent <= 3 {
mantissa >>= 8 * (3 - exponent)
bn = big.NewInt(int64(mantissa))
} else {
bn = big.NewInt(int64(mantissa))
bn.Lsh(bn, 8*(exponent-3))
}
// Make it negative if the sign bit is set.
if isNegative {
bn = bn.Neg(bn)
}
return bn
}
示例10: doubleJacobian
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
// returns its double, also in Jacobian form.
func (curve *CurveParams) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
delta := new(big.Int).Mul(z, z)
delta.Mod(delta, curve.P)
gamma := new(big.Int).Mul(y, y)
gamma.Mod(gamma, curve.P)
alpha := new(big.Int).Sub(x, delta)
if alpha.Sign() == -1 {
alpha.Add(alpha, curve.P)
}
alpha2 := new(big.Int).Add(x, delta)
alpha.Mul(alpha, alpha2)
alpha2.Set(alpha)
alpha.Lsh(alpha, 1)
alpha.Add(alpha, alpha2)
beta := alpha2.Mul(x, gamma)
x3 := new(big.Int).Mul(alpha, alpha)
beta8 := new(big.Int).Lsh(beta, 3)
x3.Sub(x3, beta8)
for x3.Sign() == -1 {
x3.Add(x3, curve.P)
}
x3.Mod(x3, curve.P)
z3 := new(big.Int).Add(y, z)
z3.Mul(z3, z3)
z3.Sub(z3, gamma)
if z3.Sign() == -1 {
z3.Add(z3, curve.P)
}
z3.Sub(z3, delta)
if z3.Sign() == -1 {
z3.Add(z3, curve.P)
}
z3.Mod(z3, curve.P)
beta.Lsh(beta, 2)
beta.Sub(beta, x3)
if beta.Sign() == -1 {
beta.Add(beta, curve.P)
}
y3 := alpha.Mul(alpha, beta)
gamma.Mul(gamma, gamma)
gamma.Lsh(gamma, 3)
gamma.Mod(gamma, curve.P)
y3.Sub(y3, gamma)
if y3.Sign() == -1 {
y3.Add(y3, curve.P)
}
y3.Mod(y3, curve.P)
return x3, y3, z3
}
示例11: base64ToInt
//base64ToInt makes int from string.
func base64ToInt(s string) *big.Int {
var tmp big.Int
sb := []byte(s)
for i := len(sb) - 1; i >= 0; i-- {
b := big.NewInt(base64de[sb[i]])
tmp.Lsh(&tmp, 6).Or(&tmp, b)
}
return &tmp
}
示例12: main
func main() {
i := new(big.Int)
// 2^1000 == 1 << 1000
i.SetString("1", 10)
i.Lsh(i, 1000)
fmt.Println(misc.DigitSum(i))
}
示例13: TestIRootLargeP
// TestIRootLargeP tests IRoot() with p >= n.BitLen().
func TestIRootLargeP(t *testing.T) {
var p uint = 100
var n big.Int
n.Lsh(big.NewInt(1), p-1)
r := IRoot(&n, p)
if r.Cmp(big.NewInt(1)) != 0 {
t.Errorf("For n=%s and p=%d, expected r=1, got r=%s",
&n, p, r)
}
}
示例14: p224AlternativeToBig
func p224AlternativeToBig(in *p224FieldElement) *big.Int {
ret := new(big.Int)
tmp := new(big.Int)
for i := uint(0); i < 8; i++ {
tmp.SetInt64(int64(in[i]))
tmp.Lsh(tmp, 28*i)
ret.Add(ret, tmp)
}
ret.Mod(ret, p224.P)
return ret
}
示例15: StakePoolTicketFee
// StakePoolTicketFee determines the stake pool ticket fee for a given ticket
// from the passed percentage. Pool fee as a percentage is truncated from 0.01%
// to 100.00%. This all must be done with integers, so bear with the big.Int
// usage below.
//
// See the included doc.go of this package for more information about the
// calculation of this fee.
func StakePoolTicketFee(stakeDiff dcrutil.Amount, relayFee dcrutil.Amount,
height int32, poolFee float64, params *chaincfg.Params) dcrutil.Amount {
// Shift the decimal two places, e.g. 1.00%
// to 100. This assumes that the proportion
// is already multiplied by 100 to give a
// percentage, thus making the entirety
// be a multiplication by 10000.
poolFeeAbs := math.Floor(poolFee * 100.0)
poolFeeInt := int64(poolFeeAbs)
// Subsidy is fetched from the blockchain package, then
// pushed forward a number of adjustment periods for
// compensation in gradual subsidy decay. Recall that
// the average time to claiming 50% of the tickets as
// votes is the approximately the same as the ticket
// pool size (params.TicketPoolSize), so take the
// ceiling of the ticket pool size divided by the
// reduction interval.
adjs := int(math.Ceil(float64(params.TicketPoolSize) /
float64(params.ReductionInterval)))
initSubsidyCacheOnce.Do(func() {
subsidyCache = blockchain.NewSubsidyCache(int64(height), params)
})
subsidy := blockchain.CalcStakeVoteSubsidy(subsidyCache, int64(height),
params)
for i := 0; i < adjs; i++ {
subsidy *= 100
subsidy /= 101
}
// The numerator is (p*10000*s*(v+z)) << 64.
shift := uint(64)
s := new(big.Int).SetInt64(subsidy)
v := new(big.Int).SetInt64(int64(stakeDiff))
z := new(big.Int).SetInt64(int64(relayFee))
num := new(big.Int).SetInt64(poolFeeInt)
num.Mul(num, s)
vPlusZ := new(big.Int).Add(v, z)
num.Mul(num, vPlusZ)
num.Lsh(num, shift)
// The denominator is 10000*(s+v).
// The extra 10000 above cancels out.
den := new(big.Int).Set(s)
den.Add(den, v)
den.Mul(den, new(big.Int).SetInt64(10000))
// Divide and shift back.
num.Div(num, den)
num.Rsh(num, shift)
return dcrutil.Amount(num.Int64())
}