Exemplo n.º 1
0
func benchmarkMod(b *testing.B, w, exp uint32) {
	b.StopTimer()
	var n, mod big.Int
	n.Rand(rand.New(rand.NewSource(1)), New(w))
	n.SetBit(&n, int(w), 1)
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		Mod(&mod, &n, exp)
	}
}
Exemplo n.º 2
0
func GenExcerciseRandomNumber(lower, upper string) string {

	lower = strings.TrimSpace(lower)
	upper = strings.TrimSpace(upper)

	var remainlowerlen int
	if lowerpointpos := strings.IndexRune(lower, '.'); lowerpointpos > 0 {
		remainlowerlen = len(lower[lowerpointpos+1:])
		lower = lower[:lowerpointpos] + lower[lowerpointpos+1:]
	}

	var remainupperlen int
	if upperpointpos := strings.IndexRune(upper, '.'); upperpointpos > 0 {
		remainupperlen = len(upper[upperpointpos+1:])
		upper = upper[:upperpointpos] + upper[upperpointpos+1:]
	}

	if remainlowerlen > remainupperlen {
		upper += strings.Repeat("0", remainlowerlen-remainupperlen)
	} else if remainlowerlen < remainupperlen {
		lower += strings.Repeat("0", remainupperlen-remainlowerlen)
	}

	lowerbigint := new(big.Int)
	lowerbigint, _ = lowerbigint.SetString(lower, 10)
	upperbigint := new(big.Int)
	upperbigint, _ = upperbigint.SetString(upper, 10)

	diff := big.NewInt(0)
	diff = diff.Sub(upperbigint, lowerbigint)

	random := new(big.Int)
	random = random.Rand(rand.New(rand.NewSource(time.Now().Unix())), diff)
	randomnumstr := random.Add(random, lowerbigint).String()

	if remainlowerlen > 0 || remainupperlen > 0 {
		randompoint := len(randomnumstr) - max(remainupperlen, remainlowerlen)
		randomnumstr = randomnumstr[:randompoint] + "." + randomnumstr[randompoint:]
	}

	if len(randomnumstr) > 1 && randomnumstr[0] == '-' && randomnumstr[1] == '.' {
		randomnumstr = "-0." + randomnumstr[2:]
	}

	return randomnumstr
}
Exemplo n.º 3
0
// GenerateG implements [2.2.1.2. Generation of g] in page 9
func GenerateG(p, q *big.Int) (g *big.Int) {
	r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
	g = new(big.Int)
	for {
		j := new(big.Int)
		j.Sub(p, big.NewInt(1))
		j.Div(j, q)
		h := new(big.Int)
		h.Rand(r, p)
		if h.Cmp(big.NewInt(0)) == 0 {
			continue
		}
		g.Exp(h, j, p)
		if g.Cmp(big.NewInt(1)) != 0 {
			break
		}
	}
	return
}
Exemplo n.º 4
0
// bigIntRand sets a to a random number in [origin, origin+b].
func bigIntRand(a, b *big.Int) *big.Int {
	a.Rand(conf.Random(), b)
	return a.Add(a, conf.BigOrigin())
}
Exemplo n.º 5
0
Arquivo: unary.go Projeto: db47h/ivy
// bigIntRand sets a to a random number in [origin, origin+b].
func bigIntRand(c Context, a, b *big.Int) *big.Int {
	a.Rand(c.Config().Random(), b)
	return a.Add(a, c.Config().BigOrigin())
}
Exemplo n.º 6
0
func NewDiffieHellman(g CyclicGroup) DiffieHellman {
	z := new(big.Int)
	z.Rand(rnd, g.Size())
	return &diffieHellman{g: g, a: z}
}
Exemplo n.º 7
0
func PrivateKey(p *big.Int) *big.Int {
	private := new(big.Int).Sub(p, two)
	return private.Add(two, private.Rand(r, private))
}