本文整理汇总了Golang中math/big.Int类的典型用法代码示例。如果您正苦于以下问题:Golang Int类的具体用法?Golang Int怎么用?Golang Int使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Int类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: New
// Creates a new STS session, ready to initiate or accept key exchanges. The group/generator pair defines the
// cyclic group on which STS will operate. cipher and bits are used during the authentication token's symmetric
// encryption, whilst hash is needed during RSA signing.
func New(random io.Reader, group, generator *big.Int, cipher func([]byte) (cipher.Block, error),
bits int, hash crypto.Hash) (*Session, error) {
// Generate a random secret exponent
expbits := group.BitLen()
secret := make([]byte, (expbits+7)/8)
n, err := io.ReadFull(random, secret)
if n != len(secret) || err != nil {
return nil, err
}
// Clear top bits if non-power was requested
clear := uint(expbits % 8)
if clear == 0 {
clear = 8
}
secret[0] &= uint8(int(1<<clear) - 1)
exp := new(big.Int).SetBytes(secret)
ses := new(Session)
ses.group = group
ses.generator = generator
ses.exponent = exp
ses.hash = hash
ses.crypter = cipher
ses.keybits = bits
return ses, nil
}
示例2: bigIntToNetIPv6
// bigIntToNetIPv6 is a helper function that correctly returns a net.IP with the
// correctly padded values.
func bigIntToNetIPv6(bi *big.Int) *net.IP {
x := make(net.IP, IPv6len)
ipv6Bytes := bi.Bytes()
// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If
// they are different sizes we to pad the size of response.
if len(ipv6Bytes) < IPv6len {
buf := new(bytes.Buffer)
buf.Grow(IPv6len)
for i := len(ipv6Bytes); i < IPv6len; i++ {
if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
}
}
for _, b := range ipv6Bytes {
if err := binary.Write(buf, binary.BigEndian, b); err != nil {
panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
}
}
ipv6Bytes = buf.Bytes()
}
i := copy(x, ipv6Bytes)
if i != IPv6len {
panic("IPv6 wrong size")
}
return &x
}
示例3: BigIntToEncodedBytes
// BigIntToEncodedBytes converts a big integer into its corresponding
// 32 byte little endian representation.
func BigIntToEncodedBytes(a *big.Int) *[32]byte {
s := new([32]byte)
if a == nil {
return s
}
// Caveat: a can be longer than 32 bytes.
aB := a.Bytes()
// If we have a short byte string, expand
// it so that it's long enough.
aBLen := len(aB)
if aBLen < fieldIntSize {
diff := fieldIntSize - aBLen
for i := 0; i < diff; i++ {
aB = append([]byte{0x00}, aB...)
}
}
for i := 0; i < fieldIntSize; i++ {
s[i] = aB[i]
}
// Reverse the byte string --> little endian after
// encoding.
reverse(s)
return s
}
示例4: parseECPrivateKey
// parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
// The OID for the named curve may be provided from another source (such as
// the PKCS8 container) - if it is provided then use this instead of the OID
// that may exist in the EC private key structure.
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *ecdsa.PrivateKey, err error) {
var privKey ecPrivateKey
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
}
if privKey.Version != ecPrivKeyVersion {
return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
}
var curve elliptic.Curve
if namedCurveOID != nil {
curve = namedCurveFromOID(*namedCurveOID)
} else {
curve = namedCurveFromOID(privKey.NamedCurveOID)
}
if curve == nil {
return nil, errors.New("x509: unknown elliptic curve")
}
k := new(big.Int).SetBytes(privKey.PrivateKey)
if k.Cmp(curve.Params().N) >= 0 {
return nil, errors.New("x509: invalid elliptic curve private key value")
}
priv := new(ecdsa.PrivateKey)
priv.Curve = curve
priv.D = k
priv.X, priv.Y = curve.ScalarBaseMult(privKey.PrivateKey)
return priv, nil
}
示例5: factor2
func factor2(n *big.Int) int {
// could be improved for large factors
f := 0
for ; n.Bit(f) == 0; f++ {
}
return f
}
示例6: factorial
func factorial(x *big.Int) *big.Int {
n := big.NewInt(1)
if x.Cmp(big.NewInt(0)) == 0 {
return n
}
return n.Mul(x, factorial(n.Sub(x, n)))
}
示例7: mipmapKey
// returns a formatted MIP mapped key by adding prefix, canonical number and level
//
// ex. fn(98, 1000) = (prefix || 1000 || 0)
func mipmapKey(num, level uint64) []byte {
lkey := make([]byte, 8)
binary.BigEndian.PutUint64(lkey, level)
key := new(big.Int).SetUint64(num / level * level)
return append(mipmapPre, append(lkey, key.Bytes()...)...)
}
示例8: BigMax
// Big max
//
// Returns the maximum size big integer
func BigMax(x, y *big.Int) *big.Int {
if x.Cmp(y) < 0 {
return y
}
return x
}
示例9: getWorkPackage
func getWorkPackage(difficulty *big.Int) string {
if currWork == nil {
return getErrorResponse("Current work unavailable")
}
// Our response object
response := &ResponseArray{
Id: currWork.Id,
Jsonrpc: currWork.Jsonrpc,
Result: currWork.Result[:],
}
// Calculte requested difficulty
diff := new(big.Int).Div(pow256, difficulty)
diffBytes := string(common.ToHex(diff.Bytes()))
// Adjust the difficulty for the miner
response.Result[2] = diffBytes
// Convert respone object to JSON
b, _ := json.Marshal(response)
return string(b)
}
示例10: main
func main() {
out, _ := os.Create("485.out")
defer out.Close()
for !done {
var one big.Int
one.SetInt64(1)
p = append(p, one)
l := len(p)
fmt.Fprint(out, &p[l-1])
for i := l - 2; i > 0; i-- {
p[i].Add(&p[i], &p[i-1])
fmt.Fprintf(out, " %v", &p[i])
s = fmt.Sprint(&p[i])
if len(s) > MAX {
done = true
break
}
}
if !done && l > 1 {
fmt.Fprintf(out, " %v", &p[0])
}
fmt.Fprintln(out)
}
}
示例11: baseCheck
// baseCheck checks for any stack error underflows
func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
// PUSH and DUP are a bit special. They all cost the same but we do want to have checking on stack push limit
// PUSH is also allowed to calculate the same price for all PUSHes
// DUP requirements are handled elsewhere (except for the stack limit check)
if op >= PUSH1 && op <= PUSH32 {
op = PUSH1
}
if op >= DUP1 && op <= DUP16 {
op = DUP1
}
if r, ok := _baseCheck[op]; ok {
err := stack.require(r.stackPop)
if err != nil {
return err
}
if r.stackPush > 0 && stack.len()-r.stackPop+r.stackPush > int(params.StackLimit.Int64()) {
return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64())
}
gas.Add(gas, r.gas)
}
return nil
}
示例12: bigAdd
func bigAdd(IntSlice []big.Int) *big.Int {
var sum *big.Int = big.NewInt(0)
for i := 0; i < len(IntSlice); i++ {
sum.Add(sum, &IntSlice[i])
}
return sum
}
示例13: String
func (v *Value) String() string {
if v.IsZero() {
return "0"
}
if v.Native && v.IsScientific() {
value := strconv.FormatUint(v.Num, 10)
offset := strconv.FormatInt(v.Offset, 10)
if v.Negative {
return "-" + value + "e" + offset
}
return value + "e" + offset
}
value := big.NewInt(int64(v.Num))
if v.Negative {
value.Neg(value)
}
var offset *big.Int
if v.Native {
offset = big.NewInt(-6)
} else {
offset = big.NewInt(v.Offset)
}
exp := offset.Exp(bigTen, offset.Neg(offset), nil)
rat := big.NewRat(0, 1).SetFrac(value, exp)
left := rat.FloatString(0)
if rat.IsInt() {
return left
}
length := len(left)
if v.Negative {
length -= 1
}
return strings.TrimRight(rat.FloatString(32-length), "0")
}
示例14: GetInstr
func (c *StateObject) GetInstr(pc *big.Int) *common.Value {
if int64(len(c.code)-1) < pc.Int64() {
return common.NewValue(0)
}
return common.NewValueFromBytes([]byte{c.code[pc.Int64()]})
}
示例15: addRemoteKey
func (p *PrivateKeys) addRemoteKey(remote []byte, clientPacket []byte, serverPacket []byte) SharedKeys {
remote_be := new(big.Int)
remote_be.SetBytes(remote)
shared_key := powm(remote_be, p.privateKey, p.prime)
data := make([]byte, 0, 100)
mac := hmac.New(sha1.New, shared_key.Bytes())
for i := 1; i < 6; i++ {
mac.Write(clientPacket)
mac.Write(serverPacket)
mac.Write([]byte{uint8(i)})
data = append(data, mac.Sum(nil)...)
mac.Reset()
}
mac = hmac.New(sha1.New, data[0:0x14])
mac.Write(clientPacket)
mac.Write(serverPacket)
return SharedKeys{
challenge: mac.Sum(nil),
sendKey: data[0x14:0x34],
recvKey: data[0x34:0x54],
}
}