Пример #1
0
func q2() {
	n := new(big.Int)
	a := new(big.Int)
	asquared := new(big.Int)
	one := new(big.Int)
	x := new(big.Int)
	xsquared := new(big.Int)
	p := new(big.Int)
	q := new(big.Int)
	candidate := new(big.Int)

	n.SetString("648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877", 10)
	one.SetString("1", 10)

	a = mathutil.SqrtBig(n)
	for {
		a.Add(a, one)
		asquared.Mul(a, a)
		xsquared.Sub(asquared, n)
		x = mathutil.SqrtBig(xsquared)
		p.Sub(a, x)
		q.Add(a, x)
		if candidate.Mul(p, q).Cmp(n) == 0 {
			fmt.Println(p.String())
			break
		}
	}
}
Пример #2
0
func main() {

	// Challenge #1
	N := new(big.Int)
	fmt.Sscan("179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581", N)

	A := mathutil.SqrtBig(N)
	A = A.Add(A, big.NewInt(1))

	q, p := factor(A, N)

	if checkFactors(p, q, N) {
		fmt.Println("Challenge #1 Solution")
		fmt.Println("q:", q)
		fmt.Println("p:", p)
	}

	// Challenge #2
	N2 := new(big.Int)
	fmt.Sscan("648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877", N2)

	A2 := mathutil.SqrtBig(N2)

	for i := A2; i.Cmp(N2) <= 0; i.Add(i, big.NewInt(1)) {
		q2, p2 := factor(A2, N2)
		if checkFactors(p2, q2, N2) {
			fmt.Println("Challenge #2 Solution")
			fmt.Println("q:", q2)
			fmt.Println("p:", p2)
			break
		}
	}

	// Challenge #4
	cipher := new(big.Int)
	fmt.Sscan("22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540", cipher)
	e := big.NewInt(65537)
	phi := big.NewInt(1)
	phi = phi.Mul(p.Sub(p, big.NewInt(1)), q.Sub(q, big.NewInt(1)))

	d := big.NewInt(0)
	d.ModInverse(e, phi)

	cipher.Exp(cipher, d, N)
	hexEncoded := fmt.Sprintf("%x", cipher)

	ind := strings.Index(hexEncoded, "00")
	if ind != -1 {
		fmt.Println("Challenge #4 Solution")
		res, _ := hex.DecodeString(hexEncoded[ind+2:])
		fmt.Println(string(res))
	}

}
Пример #3
0
func q1() {
	n := new(big.Int)
	sqrtN := new(big.Int)
	a := new(big.Int)
	asquared := new(big.Int)
	one := new(big.Int)
	x := new(big.Int)
	xsquared := new(big.Int)
	//p := new(big.Int)

	n.SetString("179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581", 10)
	one.SetString("1", 10)

	sqrtN = mathutil.SqrtBig(n)
	a.Add(sqrtN, one)
	asquared.Mul(a, a)

	xsquared.Sub(asquared, n)

	x = mathutil.SqrtBig(xsquared)

	p := new(big.Int)
	q := new(big.Int)
	p.Sub(a, x)
	q.Add(a, x)

	fmt.Println(a.Sub(a, x).String())

	d := new(big.Int)
	phiN := new(big.Int)
	phiN.Mul(p.Sub(p, one), q.Sub(q, one))
	e := new(big.Int)
	e.SetString("65537", 10)

	//get my decryption exponent
	d.ModInverse(e, phiN)

	c := new(big.Int)
	c.SetString("22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540", 10)

	m := new(big.Int)
	m.Exp(c, d, n)

	fmt.Println("decrypted message:")
	fmt.Printf("%x\n", m.Bytes())

	//020805907610b524330594e51d5dbbf643f09603731e9817111392d0c64e2739959a092d4daf979d387520ea7e577af9eb50a29f736925e810ab2fb4640e091a0f73252cb669d5b62b26764190ed188239fe71e1a7cb9e935d2db55c98b024e1dae46d00
	answer, _ := hex.DecodeString("466163746f72696e67206c65747320757320627265616b205253412e")
	fmt.Printf("%s\n", answer)
}
Пример #4
0
func get_factor(N *big.Int) (*big.Int, *big.Int) {
	A := mathutil.SqrtBig(N)
	A.Add(A, big.NewInt(1))

	x := new(big.Int).Mul(A, A)
	x = mathutil.SqrtBig(x.Sub(x, N))

	p := new(big.Int).Sub(A, x)
	q := new(big.Int).Add(A, x)

	if new(big.Int).Mul(p, q).Cmp(N) == 0 {
		return p, q
	}

	return nil, nil
}
Пример #5
0
func q3() {
	n := new(big.Int)
	a := new(big.Int)
	sixN := new(big.Int)
	one := new(big.Int)
	two := new(big.Int)
	four := new(big.Int)
	six := new(big.Int)
	twentyFour := new(big.Int)
	p := new(big.Int)
	q := new(big.Int)
	candidate := new(big.Int)
	asquared := new(big.Int)
	twentyFourN := new(big.Int)
	xsquared := new(big.Int)
	x := new(big.Int)

	n.SetString("720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929", 10)
	one.SetString("1", 10)
	two.SetString("2", 10)
	four.SetString("4", 10)
	six.SetString("6", 10)
	twentyFour.SetString("24", 10)

	twentyFourN.Mul(n, twentyFour)
	sixN.Mul(n, six)
	a = mathutil.SqrtBig(sixN)
	a.Mul(a, two)
	for {
		a.Add(a, one)

		asquared.Mul(a, a)

		xsquared.Sub(asquared, twentyFourN)
		x = mathutil.SqrtBig(xsquared)
		p.Sub(a, x)
		p.Div(p, six)

		q.Add(a, x)
		q.Div(q, four)
		if candidate.Mul(p, q).Cmp(n) == 0 {
			fmt.Println(p.String())
			break
		}
	}
}
Пример #6
0
func main() {
	raw, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatalln(err)
	}

	N, _ := new(big.Int).SetString(strings.Trim(string(raw), " \n"), 0)

	A := mathutil.SqrtBig(N)
	A.Add(A, big.NewInt(1))

	x := new(big.Int).Mul(A, A)
	x = mathutil.SqrtBig(x.Sub(x, N))

	p := new(big.Int).Sub(A, x)
	q := new(big.Int).Add(A, x)

	if new(big.Int).Mul(p, q).Cmp(N) == 0 {
		fmt.Println("Result:", p)
	}
}
Пример #7
0
func check_factor(A, N *big.Int) int {
	x := new(big.Int).Mul(A, A)
	x = mathutil.SqrtBig(x.Sub(x, N))

	p := new(big.Int).Sub(A, x)
	q := new(big.Int).Add(A, x)

	res := new(big.Int).Mul(p, q).Cmp(N)
	if res == 0 {
		fmt.Println("Result:", p)
	}

	return res
}
Пример #8
0
func factor(A, N *big.Int) (*big.Int, *big.Int) {

	t2 := big.NewInt(0)
	t2.Exp(A, big.NewInt(2), nil)
	t2.Sub(t2, N)
	if t2.Cmp(big.NewInt(0)) <= 0 {
		return big.NewInt(0), big.NewInt(0)
	}
	x := mathutil.SqrtBig(t2)

	q := big.NewInt(0)
	p := big.NewInt(0)

	q = q.Sub(A, x)
	p = p.Add(A, x)

	return q, p
}
Пример #9
0
func main() {
	raw, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatalln(err)
	}

	N, _ := new(big.Int).SetString(strings.Trim(string(raw), " \n"), 0)

	A := mathutil.SqrtBig(N)
	A.Add(A, big.NewInt(1))

	res := -1
	one := big.NewInt(1)
	for A.Cmp(N) == -1 && res != 0 {
		res = check_factor(A, N)
		A.Add(A, one)
	}
}
Пример #10
0
// newDashboardRow returns a new dashboard row with it's items calculated from
// the given aggregation event and timespan events (the returned row represents
// the whole aggregation event).
//
// The returned row does not have the URL field set.
func newDashboardRow(a appdash.AggregateEvent, timespans []appdash.TimespanEvent) dashboardRow {
	row := dashboardRow{
		Name:      a.Name,
		Timespans: len(timespans),
	}

	// Calculate sum and mean (row.Average), while determining min/max.
	sum := big.NewInt(0)
	for _, ts := range timespans {
		d := ts.End().Sub(ts.Start())
		sum.Add(sum, big.NewInt(int64(d)))
		if row.Min == 0 || d < row.Min {
			row.Min = d
		}
		if row.Max == 0 || d > row.Max {
			row.Max = d
		}
	}
	avg := big.NewInt(0).Div(sum, big.NewInt(int64(len(timespans))))
	row.Average = time.Duration(avg.Int64())

	// Calculate std. deviation.
	sqDiffSum := big.NewInt(0)
	for _, ts := range timespans {
		d := ts.End().Sub(ts.Start())
		diff := big.NewInt(0).Sub(big.NewInt(int64(d)), avg)
		sqDiffSum.Add(sqDiffSum, diff.Mul(diff, diff))
	}
	stdDev := big.NewInt(0).Div(sqDiffSum, big.NewInt(int64(len(timespans))))
	stdDev = mathutil.SqrtBig(stdDev)
	row.StdDev = time.Duration(stdDev.Int64())

	// TODO(slimsag): if we can make the table display the data as formatted by
	// Go (row.Average.String), we'll get much nicer display. But it means we'll
	// need to perform custom sorting on the table (it will think "9ms" > "1m",
	// for example).

	// Divide into milliseconds.
	row.Average = row.Average / time.Millisecond
	row.Min = row.Min / time.Millisecond
	row.Max = row.Max / time.Millisecond
	row.StdDev = row.StdDev / time.Millisecond
	return row
}
func main() {
	one := big.NewInt(1)
	remainder := big.NewInt(0)

	for n := big.NewInt(2); ; n = n.Add(n, one) {
		sqrtn := mathutil.SqrtBig(n)
		divisible := false
		for divisor := big.NewInt(2); divisor.Cmp(sqrtn) != 1; divisor = divisor.Add(divisor, one) {
			remainder = remainder.Mod(n, divisor)
			if remainder.Cmp(big.NewInt(0)) == 0 {
				divisible = true
				break
			}
		}
		if divisible {
			//fmt.Printf("%v is not prime\n", n.String())
		} else {
			fmt.Printf("%v is prime\n", n.String())
		}
	}
}
Пример #12
0
func chudPi(n int64) *big.Int {
	onebase := big.NewInt(10)
	onebase.Exp(onebase, big.NewInt(n), nil)
	C := big.NewInt(640320)
	C3Div24 := big.NewInt(0).Exp(C, big.NewInt(3), nil)
	C3Div24.Div(C3Div24, big.NewInt(24))
	zero, one, three, five, six := big.NewInt(0), big.NewInt(1), big.NewInt(3), big.NewInt(5), big.NewInt(6)

	var bs func(a, b *big.Int) (*big.Int, *big.Int, *big.Int)

	bs = func(a, b *big.Int) (*big.Int, *big.Int, *big.Int) {
		//fmt.Println("bs ", a, b)
		Pab, Qab, Tab := big.NewInt(1), big.NewInt(1), big.NewInt(0)
		bminusa := big.NewInt(0).Sub(b, a)
		if one.Cmp(bminusa) == 0 {
			if zero.Cmp(a) != 0 {
				sixa := big.NewInt(0).Mul(a, six)
				Pab.Sub(sixa, one)
				sixa.Sub(sixa, five)
				Pab.Mul(Pab, sixa) // *= 6*a-5
				sixa.Lsh(a, 1)
				sixa.Sub(sixa, one)
				Pab.Mul(Pab, sixa)

				Qab.Exp(a, three, nil)
				Qab.Mul(Qab, C3Div24)
			}
			Tab.SetInt64(545140134)
			Tab.Mul(Tab, a)
			Tab.Add(Tab, big.NewInt(13591409))
			Tab.Mul(Tab, Pab)
			if a.Bit(0) == 1 {
				Tab.Neg(Tab)
			}
		} else {
			// m is the midpoint between a & b
			m := big.NewInt(0).Add(a, b)
			m.Rsh(m, 1)
			Pam, Qam, Tam := bs(a, m)
			Pmb, Qmb, Tmb := bs(m, b)
			Pab.Mul(Pam, Pmb)
			Qab.Mul(Qam, Qmb)

			// Tab = Qmb * Tam + Pam * Tmb
			Qmb.Mul(Qmb, Tam)
			Pam.Mul(Pam, Tmb)
			Tab.Add(Qmb, Pam)
		}
		return Pab, Qab, Tab
	}
	DigitsPerTerm := int64(14)
	startB := big.NewInt(n/DigitsPerTerm + 1)

	_, q, t := bs(big.NewInt(0), startB)

	tmp := big.NewInt(10005)
	tmp.Mul(tmp, onebase)
	tmp.Mul(tmp, onebase) // tmp = 10005*onebase^2
	// so sqrt(tmp) = sqrt(10005)*onebase with precision maintained
	tmp = mathutil.SqrtBig(tmp)
	tmp.Mul(tmp, big.NewInt(426880))
	tmp.Mul(tmp, q)
	tmp.Div(tmp, t)
	return tmp
}