本文整理汇总了Golang中math/big.Int.Exp方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Exp方法的具体用法?Golang Int.Exp怎么用?Golang Int.Exp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Exp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ComputeKey
// ComputeKey computes the session key given the value of A.
func (ss *ServerSession) ComputeKey(A []byte) ([]byte, error) {
err := ss.setA(A)
if err != nil {
return nil, err
}
// S = (Av^u) mod N
S := new(big.Int).Exp(ss._v, ss._u, ss.SRP.Group.Prime)
S.Mul(ss._A, S).Mod(S, ss.SRP.Group.Prime)
// Reject A*v^u == 0,1 (mod N)
one := big.NewInt(1)
if S.Cmp(one) <= 0 {
return nil, fmt.Errorf("Av^u) mod N <= 0")
}
// Reject A*v^u == -1 (mod N)
t1 := new(big.Int).Add(S, one)
if t1.BitLen() == 0 {
return nil, fmt.Errorf("Av^u) mod N == -1")
}
// S = (S ^ b) mod N (computes session key)
S.Exp(S, ss._b, ss.SRP.Group.Prime)
// K = H(S)
ss.key = quickHash(ss.SRP.HashFunc, S.Bytes())
return ss.key, nil
}
示例2: main
func main() {
// Initialize two big ints with the first two numbers in the sequence.
a := big.NewInt(0)
b := big.NewInt(1)
// Initialize limit as 10^99, the smallest integer with 100 digits.
var limit big.Int
limit.Exp(big.NewInt(10), big.NewInt(99), nil)
fmt.Println(&limit)
// Loop while a is smaller than 1e100.
for a.Cmp(&limit) < 0 {
// Compute the next Fibonacci number, storing it in a.
a.Add(a, b)
// Swap a and b so that b is the next number in the sequence.
a, b = b, a
}
fmt.Println(a) // 100-digit Fibonacci number
// Test a for primality.
// (ProbablyPrimes' argument sets the number of Miller-Rabin
// rounds to be performed. 20 is a good value.)
fmt.Println(a.ProbablyPrime(20))
}
示例3: newPasswd
func newPasswd() {
ClearScreen()
wr("Make new password\n")
t:
key := ins("Name of password: ")
if in := validate(key); in != "" {
wr("You used an invalid character in your name: ", in)
goto t
}
strlen := ins("Password length: ")
ig := in("Characters to not include: ")
ilen, err := strconv.Atoi(strlen)
if err != nil {
pause("That wasn't a number")
return
}
passes[key] = NewPass(ilen, ig)
if Debug {
fmt.Println("Debug dump:\n\t", passes)
}
a, b := big.NewInt(int64(Mod-len(ig))), big.NewInt(int64(ilen))
var z big.Int
z.Exp(a, b, nil)
fmt.Printf("Made new password; Approx chance of guessing = 1/%s\n\n", z.String())
pause("note that changes have not been saved.")
}
示例4: main
func main() {
var count int // initialize count at zero
distinct_powers := make(map[string]bool)
for a := 2; a < 101; a++ {
for b := 2; b < 101; b++ {
// have to use big.Int because the exponents exceed 20 digits fast
var ex big.Int
ex.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), nil)
// have to convert back to string because
// map won't accept big.Int as a key
term := ex.String()
if !distinct_powers[term] {
distinct_powers[term] = true
count++
}
}
}
fmt.Println(count)
}
示例5: DecodeToBigInt
// DecodeToBigInt returns an arbitrary precision integer from the base62 encoded string
func (e *Encoding) DecodeToBigInt(s string) *big.Int {
var (
n = new(big.Int)
c = new(big.Int)
idx = new(big.Int)
power = new(big.Int)
exp = new(big.Int)
bse = new(big.Int)
)
bse.SetInt64(base)
// Run through each character to decode
for i, v := range s {
// Get index/position of the rune as a big int
idx.SetInt64(int64(strings.IndexRune(e.encode, v)))
// Work downwards through exponents
exp.SetInt64(int64(len(s) - (i + 1)))
// Calculate power for this exponent
power.Exp(bse, exp, nil)
// Multiplied by our index, gives us the value for this character
c = c.Mul(idx, power)
// Finally add to running total
n.Add(n, c)
}
return n
}
示例6: TestPad
func TestPad(t *testing.T) {
private, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil || private == nil {
t.Fatal("Can't gen private key %s\n", err)
}
public := &private.PublicKey
var a [9]byte
copy(a[0:8], "IDENTITY")
seed := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
encrypted_secret, err := rsa.EncryptOAEP(sha1.New(), rand.Reader,
public, seed, a[0:9])
if err != nil {
t.Fatal("Can't encrypt ", err)
}
fmt.Printf("encrypted_secret: %x\n", encrypted_secret)
decrypted_secret, err := rsa.DecryptOAEP(sha1.New(), rand.Reader,
private, encrypted_secret, a[0:9])
if err != nil {
t.Fatal("Can't decrypt ", err)
}
fmt.Printf("decrypted_secret: %x\n", decrypted_secret)
var N *big.Int
var D *big.Int
var x *big.Int
var z *big.Int
N = public.N
D = private.D
x = new(big.Int)
z = new(big.Int)
x.SetBytes(encrypted_secret)
z = z.Exp(x, D, N)
decrypted_pad := z.Bytes()
fmt.Printf("decrypted_pad : %x\n", decrypted_pad)
}
示例7: StrongMillerRabin
// StrongMillerRabin checks if N is a
// strong Miller-Rabin pseudoprime in base a.
// That is, it checks if a is a witness
// for compositeness of N or if N is a strong
// pseudoprime base a.
//
// Use builtin ProbablyPrime if you want to do a lot
// of random tests, this is for one specific
// base value.
func StrongMillerRabin(N *big.Int, a int64) int {
// Step 0: parse input
if N.Sign() < 0 || N.Bit(0) == 0 || a < 2 {
panic("MR is for positive odd integers with a >= 2")
}
A := big.NewInt(a)
if (a == 2 && N.Bit(0) == 0) || new(big.Int).GCD(nil, nil, N, A).Cmp(one) != 0 {
return IsComposite
}
// Step 1: find d,s, so that n - 1 = d*2^s
// with d odd
d := new(big.Int).Sub(N, one)
s := trailingZeroBits(d)
d.Rsh(d, s)
// Step 2: compute powers a^d
// and then a^(d*2^r) for 0<r<s
nm1 := new(big.Int).Sub(N, one)
Ad := new(big.Int).Exp(A, d, N)
if Ad.Cmp(one) == 0 || Ad.Cmp(nm1) == 0 {
return Undetermined
}
for r := uint(1); r < s; r++ {
Ad.Exp(Ad, two, N)
if Ad.Cmp(nm1) == 0 {
return Undetermined
}
}
// Step 3: a is a witness for compositeness
return IsComposite
}
示例8: validPoW
func (h *Header) validPoW() error {
if h.Nonces[0] == h.Nonces[1] ||
h.Nonces[0] == h.Nonces[2] ||
h.Nonces[1] == h.Nonces[2] {
return ErrInvalidPoW
}
dInt := new(big.Int).SetUint64(h.Difficulty)
mInt := new(big.Int).SetUint64(2)
mInt.Exp(mInt, dInt, nil)
a := h.SumNonce(0)
b := h.SumNonce(1)
c := h.SumNonce(2)
aInt := new(big.Int).SetBytes(a[:])
bInt := new(big.Int).SetBytes(b[:])
cInt := new(big.Int).SetBytes(c[:])
aInt.Mod(aInt, mInt)
bInt.Mod(bInt, mInt)
cInt.Mod(cInt, mInt)
if aInt.Cmp(bInt) != 0 || aInt.Cmp(cInt) != 0 {
return ErrInvalidPoW
}
return nil
}
示例9: threeValueCalculation
func (calc Calculator) threeValueCalculation(a, b, c *big.Int, operator string) *big.Int {
switch operator {
case "&":
return a.Exp(a, b, c)
}
return nil
}
示例10: calcLastFinger
// (n - 2^(k-1)) mod 2^m
func calcLastFinger(n []byte, k int) (string, []byte) {
// convert the n to a bigint
nBigInt := big.Int{}
nBigInt.SetBytes(n)
// get the right addend, i.e. 2^(k-1)
two := big.NewInt(2)
addend := big.Int{}
addend.Exp(two, big.NewInt(int64(k-1)), nil)
addend.Mul(&addend, big.NewInt(-1))
//Soustraction
neg := big.Int{}
neg.Add(&addend, &nBigInt)
// calculate 2^m
m := 160
ceil := big.Int{}
ceil.Exp(two, big.NewInt(int64(m)), nil)
// apply the mod
result := big.Int{}
result.Mod(&neg, &ceil)
resultBytes := result.Bytes()
resultHex := fmt.Sprintf("%x", resultBytes)
return resultHex, resultBytes
}
示例11: privateEncrypt
// privateEncrypt implements OpenSSL's RSA_private_encrypt function
func privateEncrypt(key *rsa.PrivateKey, data []byte) (enc []byte, err error) {
k := (key.N.BitLen() + 7) / 8
tLen := len(data)
// rfc2313, section 8:
// The length of the data D shall not be more than k-11 octets
if tLen > k-11 {
err = errors.New("Data too long")
return
}
em := make([]byte, k)
em[1] = 1
for i := 2; i < k-tLen-1; i++ {
em[i] = 0xff
}
copy(em[k-tLen:k], data)
c := new(big.Int).SetBytes(em)
if c.Cmp(key.N) > 0 {
err = nil
return
}
var m *big.Int
var ir *big.Int
if key.Precomputed.Dp == nil {
m = new(big.Int).Exp(c, key.D, key.N)
} else {
// We have the precalculated values needed for the CRT.
m = new(big.Int).Exp(c, key.Precomputed.Dp, key.Primes[0])
m2 := new(big.Int).Exp(c, key.Precomputed.Dq, key.Primes[1])
m.Sub(m, m2)
if m.Sign() < 0 {
m.Add(m, key.Primes[0])
}
m.Mul(m, key.Precomputed.Qinv)
m.Mod(m, key.Primes[0])
m.Mul(m, key.Primes[1])
m.Add(m, m2)
for i, values := range key.Precomputed.CRTValues {
prime := key.Primes[2+i]
m2.Exp(c, values.Exp, prime)
m2.Sub(m2, m)
m2.Mul(m2, values.Coeff)
m2.Mod(m2, prime)
if m2.Sign() < 0 {
m2.Add(m2, prime)
}
m2.Mul(m2, values.R)
m.Add(m, m2)
}
}
if ir != nil {
// Unblind.
m.Mul(m, ir)
m.Mod(m, key.N)
}
enc = m.Bytes()
return
}
示例12: calcFinger
// (n + 2^(k-1)) mod (2^m)
func calcFinger(n []byte, k int, m int) (string, []byte) {
// convert the n to a bigint
nBigInt := big.Int{}
nBigInt.SetBytes(n)
// get the right addend, i.e. 2^(k-1)
two := big.NewInt(2)
addend := big.Int{}
addend.Exp(two, big.NewInt(int64(k-1)), nil)
// calculate sum
sum := big.Int{}
sum.Add(&nBigInt, &addend)
// calculate 2^m
ceil := big.Int{}
ceil.Exp(two, big.NewInt(int64(m)), nil)
// apply the mod
result := big.Int{}
result.Mod(&sum, &ceil)
resultBytes := result.Bytes()
resultHex := fmt.Sprintf("%x", resultBytes)
return resultHex, resultBytes
}
示例13: processSMP4
func (c *Conversation) processSMP4(mpis []*big.Int) error {
if len(mpis) != 3 {
return errors.New("otr: incorrect number of arguments in SMP4 message")
}
rb := mpis[0]
cr := mpis[1]
d7 := mpis[2]
h := sha256.New()
r := new(big.Int).Exp(c.smp.qaqb, d7, p)
s := new(big.Int).Exp(rb, cr, p)
r.Mul(r, s)
r.Mod(r, p)
s.Exp(g, d7, p)
t := new(big.Int).Exp(c.smp.g3b, cr, p)
s.Mul(s, t)
s.Mod(s, p)
t.SetBytes(hashMPIs(h, 8, s, r))
if t.Cmp(cr) != 0 {
return errors.New("otr: ZKP cR failed in SMP4 message")
}
r.Exp(rb, c.smp.a3, p)
if r.Cmp(c.smp.papb) != 0 {
return smpFailureError
}
return nil
}
示例14: 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")
}
示例15: Join
/*
The Join function
The first fuction to be executed
Alaows it gives the hash and id and request the finger table for the boost node
*/
func Join(test bool) { //
//generate id
me.nodeId = Index(generateNodeId(), BITS)
nodeIdInt := me.nodeId
me.storage = map[string]string{}
//prepare finger table
if test { //firstnode in the ring
me.offset = 0
for i, _ := range me.FingerTable {
//compute the ith node
two := big.NewInt(2)
dist := big.Int{}
dist.Exp(two, big.NewInt(int64(i)), nil)
var ithNode big.Int
ithNode.Add(&dist, &nodeIdInt)
x := ringSize(BITS)
ithNode.Mod(&ithNode, &x)
//fill the fingr table row
me.FingerTable[i][0] = ithNode.String()
me.FingerTable[i][1] = me.nodeId.String()
me.FingerTable[i][2] = me.address //this node address
}
} else { //not first node in the ring
//initialize offset
GetOffsetClient(me.target)
updateFingerTable(nodeIdInt, me.target) //target server required
go infornMySuccessor()
go AutoUpdate() // initialize auto update
}
}