本文整理汇总了Golang中math/big.Int.Xor方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Xor方法的具体用法?Golang Int.Xor怎么用?Golang Int.Xor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Xor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Mul
func (z *Polynomial) Mul(x, y *Polynomial) *Polynomial {
var zCoeffs *big.Int
if z == x || z == y {
// Ensure that we do not modify z if it's a parameter.
zCoeffs = new(big.Int)
} else {
zCoeffs = &z.coeffs
zCoeffs.SetInt64(0)
}
small, large := x, y
if y.degree < x.degree {
small, large = y, x
}
// Walk through small coeffs, shift large by the corresponding amount,
// and accumulate in z.
coeffs := new(big.Int).Set(&small.coeffs)
zero := new(big.Int)
for coeffs.Cmp(zero) > 0 {
deg := calcDegree(coeffs)
factor := new(big.Int).Lsh(&large.coeffs, deg)
zCoeffs.Xor(zCoeffs, factor)
// Prepare for next iteration.
coeffs.SetBit(coeffs, int(deg), 0)
}
z.degree = calcDegree(zCoeffs)
z.coeffs = *zCoeffs
return z
}
示例2: Xor
func (d PublicKeyDigest) Xor(n PublicKeyDigest) PublicKeyDigest {
var b, c big.Int
b.SetBytes(d[:])
c.SetBytes(n[:])
b.Xor(&b, &c)
var e PublicKeyDigest
copy(e[:], b.Bytes())
return e
}
示例3: hashFiles
func hashFiles(files []string) string {
hash := new(big.Int)
for _, file := range files {
val := new(big.Int)
val.SetBytes(hashFile(file))
hash = hash.Xor(hash, val)
}
return fmt.Sprintf("%x", hash)
}
示例4: binaryIntOp
func binaryIntOp(x *big.Int, op token.Token, y *big.Int) interface{} {
var z big.Int
switch op {
case token.ADD:
return z.Add(x, y)
case token.SUB:
return z.Sub(x, y)
case token.MUL:
return z.Mul(x, y)
case token.QUO:
return z.Quo(x, y)
case token.REM:
return z.Rem(x, y)
case token.AND:
return z.And(x, y)
case token.OR:
return z.Or(x, y)
case token.XOR:
return z.Xor(x, y)
case token.AND_NOT:
return z.AndNot(x, y)
case token.SHL:
// The shift length must be uint, or untyped int and
// convertible to uint.
// TODO 32/64bit
if y.BitLen() > 32 {
panic("Excessive shift length")
}
return z.Lsh(x, uint(y.Int64()))
case token.SHR:
if y.BitLen() > 32 {
panic("Excessive shift length")
}
return z.Rsh(x, uint(y.Int64()))
case token.EQL:
return x.Cmp(y) == 0
case token.NEQ:
return x.Cmp(y) != 0
case token.LSS:
return x.Cmp(y) < 0
case token.LEQ:
return x.Cmp(y) <= 0
case token.GTR:
return x.Cmp(y) > 0
case token.GEQ:
return x.Cmp(y) >= 0
}
panic("unreachable")
}
示例5: bigIntXor
func bigIntXor(oldValues [][]byte, newValues [][]byte, w float64) (result [][]byte) {
var sum *big.Int
if oldValues != nil {
sum = new(big.Int).SetBytes(oldValues[0])
for _, b := range newValues {
sum.Xor(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w))))
}
} else {
sum = new(big.Int).Mul(new(big.Int).SetBytes(newValues[0]), big.NewInt(int64(w)))
for _, b := range newValues[1:] {
sum.Xor(sum, new(big.Int).Mul(common.DecodeBigInt(b), big.NewInt(int64(w))))
}
}
return [][]byte{sum.Bytes()}
}
示例6: LastUsable
// LastUsable returns the last address in a given network.
func (ipv6 IPv6Addr) LastUsable() IPAddr {
addr := new(big.Int)
addr.Set(ipv6.Address)
mask := new(big.Int)
mask.Set(ipv6.Mask)
negMask := new(big.Int)
negMask.Xor(ipv6HostMask, mask)
lastAddr := new(big.Int)
lastAddr.And(addr, mask)
lastAddr.Or(lastAddr, negMask)
return IPv6Addr{
Address: IPv6Address(lastAddr),
Mask: ipv6HostMask,
}
}
示例7: computeClientAuthenticator
func computeClientAuthenticator(hf HashFunc, grp *SRPGroup, username, salt, A, B, K []byte) []byte {
//M = H(H(N) xor H(g), H(I), s, A, B, K)
// H(N) xor H(g)
hn := new(big.Int).SetBytes(quickHash(hf, grp.Prime.Bytes()))
hg := new(big.Int).SetBytes(quickHash(hf, grp.Generator.Bytes()))
hng := hn.Xor(hn, hg)
hi := quickHash(hf, []byte(username))
h := hf()
h.Write(hng.Bytes())
h.Write(hi)
h.Write(salt)
h.Write(A)
h.Write(B)
h.Write(K)
return h.Sum(nil)
}
示例8: binaryIntOp
func binaryIntOp(x *big.Int, op token.Token, y *big.Int) interface{} {
var z big.Int
switch op {
case token.ADD:
return z.Add(x, y)
case token.SUB:
return z.Sub(x, y)
case token.MUL:
return z.Mul(x, y)
case token.QUO:
return z.Quo(x, y)
case token.REM:
return z.Rem(x, y)
case token.AND:
return z.And(x, y)
case token.OR:
return z.Or(x, y)
case token.XOR:
return z.Xor(x, y)
case token.AND_NOT:
return z.AndNot(x, y)
case token.SHL:
panic("unimplemented")
case token.SHR:
panic("unimplemented")
case token.EQL:
return x.Cmp(y) == 0
case token.NEQ:
return x.Cmp(y) != 0
case token.LSS:
return x.Cmp(y) < 0
case token.LEQ:
return x.Cmp(y) <= 0
case token.GTR:
return x.Cmp(y) > 0
case token.GEQ:
return x.Cmp(y) >= 0
}
panic("unreachable")
}
示例9: binaryOpConst
// binaryOpConst returns the result of the constant evaluation x op y;
// both operands must be of the same "kind" (boolean, numeric, or string).
// If intDiv is true, division (op == token.QUO) is using integer division
// (and the result is guaranteed to be integer) rather than floating-point
// division. Division by zero leads to a run-time panic.
//
func binaryOpConst(x, y interface{}, op token.Token, intDiv bool) interface{} {
x, y = matchConst(x, y)
switch x := x.(type) {
case bool:
y := y.(bool)
switch op {
case token.LAND:
return x && y
case token.LOR:
return x || y
default:
unreachable()
}
case int64:
y := y.(int64)
switch op {
case token.ADD:
// TODO(gri) can do better than this
if is63bit(x) && is63bit(y) {
return x + y
}
return normalizeIntConst(new(big.Int).Add(big.NewInt(x), big.NewInt(y)))
case token.SUB:
// TODO(gri) can do better than this
if is63bit(x) && is63bit(y) {
return x - y
}
return normalizeIntConst(new(big.Int).Sub(big.NewInt(x), big.NewInt(y)))
case token.MUL:
// TODO(gri) can do better than this
if is32bit(x) && is32bit(y) {
return x * y
}
return normalizeIntConst(new(big.Int).Mul(big.NewInt(x), big.NewInt(y)))
case token.REM:
return x % y
case token.QUO:
if intDiv {
return x / y
}
return normalizeRatConst(new(big.Rat).SetFrac(big.NewInt(x), big.NewInt(y)))
case token.AND:
return x & y
case token.OR:
return x | y
case token.XOR:
return x ^ y
case token.AND_NOT:
return x &^ y
default:
unreachable()
}
case *big.Int:
y := y.(*big.Int)
var z big.Int
switch op {
case token.ADD:
z.Add(x, y)
case token.SUB:
z.Sub(x, y)
case token.MUL:
z.Mul(x, y)
case token.REM:
z.Rem(x, y)
case token.QUO:
if intDiv {
z.Quo(x, y)
} else {
return normalizeRatConst(new(big.Rat).SetFrac(x, y))
}
case token.AND:
z.And(x, y)
case token.OR:
z.Or(x, y)
case token.XOR:
z.Xor(x, y)
case token.AND_NOT:
z.AndNot(x, y)
default:
unreachable()
}
return normalizeIntConst(&z)
case *big.Rat:
y := y.(*big.Rat)
var z big.Rat
switch op {
case token.ADD:
z.Add(x, y)
case token.SUB:
z.Sub(x, y)
//.........这里部分代码省略.........
示例10: nxor
func nxor(z *big.Int, x *big.Int, y *big.Int) *big.Int {
z.Xor(x, y)
return z.Not(z)
}
示例11: xor
func xor(z *big.Int, x *big.Int, y *big.Int) *big.Int { return z.Xor(x, y) }
示例12: Run
//.........这里部分代码省略.........
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case EQ:
x, y := stack.pop(), stack.pop()
// x == y
if x.Cmp(y) == 0 {
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case ISZERO:
x := stack.pop()
if x.Cmp(common.BigFalse) > 0 {
stack.push(common.BigFalse)
} else {
stack.push(common.BigTrue)
}
case AND:
x, y := stack.pop(), stack.pop()
stack.push(base.And(x, y))
case OR:
x, y := stack.pop(), stack.pop()
stack.push(base.Or(x, y))
case XOR:
x, y := stack.pop(), stack.pop()
stack.push(base.Xor(x, y))
case BYTE:
th, val := stack.pop(), stack.pop()
if th.Cmp(big.NewInt(32)) < 0 {
byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
base.Set(byt)
} else {
base.Set(common.BigFalse)
}
stack.push(base)
case ADDMOD:
x := stack.pop()
y := stack.pop()
z := stack.pop()
if z.Cmp(Zero) > 0 {
add := new(big.Int).Add(x, y)
base.Mod(add, z)
base = U256(base)
}
stack.push(base)
case MULMOD:
x := stack.pop()
y := stack.pop()
z := stack.pop()
if z.Cmp(Zero) > 0 {
mul := new(big.Int).Mul(x, y)
示例13: Run
//.........这里部分代码省略.........
case EQ:
x, y := stack.pop(), stack.pop()
self.Printf(" %v == %v", y, x)
// x == y
if x.Cmp(y) == 0 {
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case ISZERO:
x := stack.pop()
if x.Cmp(common.BigFalse) > 0 {
stack.push(common.BigFalse)
} else {
stack.push(common.BigTrue)
}
// 0x10 range
case AND:
x, y := stack.pop(), stack.pop()
self.Printf(" %v & %v", y, x)
stack.push(base.And(x, y))
case OR:
x, y := stack.pop(), stack.pop()
self.Printf(" %v | %v", x, y)
stack.push(base.Or(x, y))
case XOR:
x, y := stack.pop(), stack.pop()
self.Printf(" %v ^ %v", x, y)
stack.push(base.Xor(x, y))
case BYTE:
th, val := stack.pop(), stack.pop()
if th.Cmp(big.NewInt(32)) < 0 {
byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
base.Set(byt)
} else {
base.Set(common.BigFalse)
}
self.Printf(" => 0x%x", base.Bytes())
stack.push(base)
case ADDMOD:
x := stack.pop()
y := stack.pop()
z := stack.pop()
if z.Cmp(Zero) > 0 {
add := new(big.Int).Add(x, y)
base.Mod(add, z)
base = U256(base)
}
self.Printf(" %v + %v %% %v = %v", x, y, z, base)
stack.push(base)
case MULMOD:
x := stack.pop()
y := stack.pop()
示例14: RunClosure
//.........这里部分代码省略.........
// x == y
if x.Cmp(y) == 0 {
stack.Push(ethutil.BigTrue)
} else {
stack.Push(ethutil.BigFalse)
}
case NOT:
require(1)
x := stack.Pop()
if x.Cmp(ethutil.BigFalse) > 0 {
stack.Push(ethutil.BigFalse)
} else {
stack.Push(ethutil.BigTrue)
}
// 0x10 range
case AND:
require(2)
x, y := stack.Popn()
self.Printf(" %v & %v", y, x)
stack.Push(base.And(y, x))
case OR:
require(2)
x, y := stack.Popn()
self.Printf(" %v | %v", y, x)
stack.Push(base.Or(y, x))
case XOR:
require(2)
x, y := stack.Popn()
self.Printf(" %v ^ %v", y, x)
stack.Push(base.Xor(y, x))
case BYTE:
require(2)
val, th := stack.Popn()
if th.Cmp(big.NewInt(32)) < 0 && th.Cmp(big.NewInt(int64(len(val.Bytes())))) < 0 {
byt := big.NewInt(int64(ethutil.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
stack.Push(byt)
self.Printf(" => 0x%x", byt.Bytes())
} else {
stack.Push(ethutil.BigFalse)
}
case ADDMOD:
require(3)
x := stack.Pop()
y := stack.Pop()
z := stack.Pop()
base.Add(x, y)
base.Mod(base, z)
ensure256(base)
self.Printf(" = %v", base)
stack.Push(base)
case MULMOD:
require(3)
x := stack.Pop()
y := stack.Pop()
z := stack.Pop()
示例15: Eval
// Post-order traversal, equivalent to postfix notation.
func Eval(node interface{}) (*big.Int, error) {
switch nn := node.(type) {
case *ast.BinaryExpr:
z := new(big.Int)
x, xerr := Eval(nn.X)
if xerr != nil {
return nil, xerr
}
y, yerr := Eval(nn.Y)
if yerr != nil {
return nil, yerr
}
switch nn.Op {
case token.ADD:
return z.Add(x, y), nil
case token.SUB:
return z.Sub(x, y), nil
case token.MUL:
return z.Mul(x, y), nil
case token.QUO:
if y.Sign() == 0 { // 0 denominator
return nil, DivideByZero
}
return z.Quo(x, y), nil
case token.REM:
if y.Sign() == 0 {
return nil, DivideByZero
}
return z.Rem(x, y), nil
case token.AND:
return z.And(x, y), nil
case token.OR:
return z.Or(x, y), nil
case token.XOR:
return z.Xor(x, y), nil
case token.SHL:
if y.Sign() < 0 { // negative shift
return nil, NegativeShift
}
return z.Lsh(x, uint(y.Int64())), nil
case token.SHR:
if y.Sign() < 0 {
return nil, NegativeShift
}
return z.Rsh(x, uint(y.Int64())), nil
case token.AND_NOT:
return z.AndNot(x, y), nil
default:
return nil, UnknownOpErr
}
case *ast.UnaryExpr:
var z *big.Int
var err error
if z, err = Eval(nn.X); err != nil {
return nil, err
}
switch nn.Op {
case token.SUB: // -x
return z.Neg(z), nil
case token.XOR: // ^x
return z.Not(z), nil
case token.ADD: // +x (useless)
return z, nil
}
case *ast.BasicLit:
z := new(big.Int)
switch nn.Kind {
case token.INT:
z.SetString(nn.Value, 0)
return z, nil
default:
return nil, UnknownLitErr
}
case *ast.ParenExpr:
z, err := Eval(nn.X)
if err != nil {
return nil, err
}
return z, nil
case *ast.CallExpr:
ident, ok := nn.Fun.(*ast.Ident)
if !ok {
return nil, UnknownTokenErr // quarter to four am; dunno correct error
}
var f Func
f, ok = FuncMap[ident.Name]
if !ok {
return nil, UnknownFuncErr
}
var aerr error
args := make([]*big.Int, len(nn.Args))
for i, a := range nn.Args {
if args[i], aerr = Eval(a); aerr != nil {
return nil, aerr
}
}
x, xerr := f(args...)
if xerr != nil {
return nil, xerr
//.........这里部分代码省略.........