func TestCRLCreation(t *testing.T) { block, _ := pem.Decode([]byte(pemPrivateKey)) priv, _ := ParsePKCS1PrivateKey(block.Bytes) block, _ = pem.Decode([]byte(pemCertificate)) cert, _ := ParseCertificate(block.Bytes) now := time.SecondsToUTC(1000) expiry := time.SecondsToUTC(10000) revokedCerts := []pkix.RevokedCertificate{ { SerialNumber: big.NewInt(1), RevocationTime: now, }, { SerialNumber: big.NewInt(42), RevocationTime: now, }, } crlBytes, err := cert.CreateCRL(rand.Reader, priv, revokedCerts, now, expiry) if err != nil { t.Errorf("error creating CRL: %s", err) } _, err = ParseDERCRL(crlBytes) if err != nil { t.Errorf("error reparsing CRL: %s", err) } }
func spiral_side_len(iter int64) *big.Int { //The big package is incredibly awkward, so in case you can't tell, // side_len = iter*2 - 1 n := big.NewInt(0) n = n.Add(n.Mul(big.NewInt(iter), big.NewInt(2)), big.NewInt(-1)) return n }
func main() { ss := new(s_a) ss.a = 24 ss.b = "45" ss.b = ss.b + fmt.Sprint(ss.a) ss.test() n, err := fmt.Print("hello") n = n + 1 fmt.Print(err) ss.a, err = strconv.Atoi(ss.b) fmt.Print(ss) iTest(ss) f, err := os.Open("c:\\my.txt", os.O_RDONLY, 0) if err != nil { return } defer f.Close() var s string // n1, err := fmt.Fscanln(f,"%s",s) fr := bufio.NewReader(f) s, err = fr.ReadString('\n') fmt.Println(s) log.Println("log message") summa := big.NewInt(0) summa.SetString("123454567891234545678912345456789", 0) summa2 := big.NewInt(0) summa2.SetString("7", 0) summa3 := big.NewInt(0) summa3.Exp(summa, summa2, nil) fmt.Println(summa3) _, _ = 4*5, "test" _ = "Test" }
func fibo(n *big.Int) *big.Int { s := n.String() if r, ok := fiboCache[s]; ok { return r } i0 := big.NewInt(0) if n.Cmp(i0) <= 0 { return i0 } if n.Cmp(big.NewInt(2)) <= 0 { return big.NewInt(1) } n1 := big.NewInt(-1) n1.Add(n1, n) n1 = fibo(n1) n2 := big.NewInt(-2) n2.Add(n2, n) n2 = fibo(n2) i0.Add(n1, n2) fiboCache[s] = i0 return i0 }
func readRSAKey(x PrivateKey) (*rsa.PrivateKey, os.Error) { var k rsa.PrivateKey var Ns, Ds, Ps, Qs string _, e := fmt.Sscanf(string(x), "N = %s\nE = %d\nD = %s\nP = %s\nQ = %s\n", &Ns, &k.E, &Ds, &Ps, &Qs) if e != nil { return nil, os.NewError("Couldn't read file: " + e.String()) } var ok = true k.N, ok = big.NewInt(0).SetString(Ns, 0) if !ok { return nil, os.NewError("Unable to read k.N: " + Ns) } k.D, ok = big.NewInt(0).SetString(Ds, 0) if !ok { return nil, os.NewError("Unable to read k.D: " + Ds) } k.P, ok = big.NewInt(0).SetString(Ps, 0) if !ok { return nil, os.NewError("Unable to read k.P: " + Ps) } k.Q, ok = big.NewInt(0).SetString(Qs, 0) if !ok { return nil, os.NewError("Unable to read k.Q: " + Qs) } return &k, nil }
func (h *Hash) l2(m []byte, length, iter int) []byte { tmpt := h.kdf(192, 128*(iter+1)) t := tmpt[16*iter : 16*(iter+1)] for i := 0; i < 16; i += 4 { t[i] &= 31 } k := newInt(t) y := big.NewInt(1) n := len(m) / 16 if n != 0 { for i := 0; i < n; i++ { mi := newInt(m[16*i : 16*(i+1)]) y.Mod(y.Add(y.Mul(y, k), mi), p127) } } else { y = k } y.Add(y, new(big.Int).Lsh(big.NewInt(int64(length%l1keylen)), 64)) y.Mod(y, p127) Y := make([]byte, 16) copy(Y[16-len(y.Bytes()):], y.Bytes()) return Y }
func bitwise_arithmetic_shift_left(x, y Obj) Obj { xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag if !yfx { panic("bad shift amount") } amount := uint(uintptr(unsafe.Pointer(y)) >> fixnum_shift) if xfx && amount < 32-fixnum_shift { i := int64(int(uintptr(unsafe.Pointer(x)) >> fixnum_shift)) r := i << amount if r >= int64(fixnum_min) && r <= int64(fixnum_max) { return Obj(unsafe.Pointer(uintptr((r << fixnum_shift) | fixnum_tag))) } else { return wrap(big.NewInt(r)) } } else if xfx { x = wrap(big.NewInt(int64(fixnum_to_int(x)))) } else if (uintptr(unsafe.Pointer(x)) & heap_mask) != heap_tag { panic("bad type") } switch vx := (*x).(type) { case *big.Int: var z *big.Int = big.NewInt(0) return wrap(z.Lsh(vx, amount)) } panic("bad type") }
func bitwise_and(x, y Obj) Obj { xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag if xfx && yfx { i1 := uintptr(unsafe.Pointer(x)) i2 := uintptr(unsafe.Pointer(y)) return Obj(unsafe.Pointer(uintptr(i1 & i2))) } if (!xfx && (uintptr(unsafe.Pointer(x))&heap_mask) != heap_tag) || (!yfx && (uintptr(unsafe.Pointer(y))&heap_mask) != heap_tag) { panic("bad type") } if xfx { return bitwise_and(y, x) } switch vx := (*x).(type) { case *big.Int: var z *big.Int = big.NewInt(0) if yfx { vy := big.NewInt(int64(fixnum_to_int(y))) return wrap(z.And(vx, vy)) } switch vy := (*y).(type) { case *big.Int: return wrap(z.And(vx, vy)) default: panic("bad type") } } panic("bad type") }
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and // returns its double, also in Jacobian form. func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) { // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l a := new(big.Int).Mul(x, x) //X1² b := new(big.Int).Mul(y, y) //Y1² c := new(big.Int).Mul(b, b) //B² d := new(big.Int).Add(x, b) //X1+B d.Mul(d, d) //(X1+B)² d.Sub(d, a) //(X1+B)²-A d.Sub(d, c) //(X1+B)²-A-C d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C) e := new(big.Int).Mul(big.NewInt(3), a) //3*A f := new(big.Int).Mul(e, e) //E² x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D x3.Sub(f, x3) //F-2*D x3.Mod(x3, BitCurve.P) y3 := new(big.Int).Sub(d, x3) //D-X3 y3.Mul(e, y3) //E*(D-X3) y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C y3.Mod(y3, BitCurve.P) z3 := new(big.Int).Mul(y, z) //Y1*Z1 z3.Mul(big.NewInt(2), z3) //3*Y1*Z1 z3.Mod(z3, BitCurve.P) return x3, y3, z3 }
func RabinMiller(p *big.Int) bool { pdec := new(big.Int).Sub(p, big.NewInt(1)) // = p - 1 big2 := big.NewInt(2) for i := 0; i < 20; i++ { x := RandNumSmaller(p) stg := new(big.Int).Exp(x, pdec, p) // = x^(p-1) mod p if stg.Cmp(big1) != 0 { return false } // test na Carmichaelova cisla (kontrola zda x^[(p-1)/2] je +1 nebo -1) p2 := new(big.Int).Rsh(p, 1) // = (p - 1)/2 for { stg.Exp(x, p2, p) if stg.Cmp(pdec) == 0 { break } if stg.Cmp(big1) != 0 { return false } _, res := p2.Div(p2, big2) if res.Cmp(big1) == 0 { break } } } return true }
func factorial(num int) (ret *big.Int) { ret = big.NewInt(1) for i := num; i > 0; i-- { ret = ret.Mul(ret, big.NewInt(int64(i))) } return }
func main() { flag.Parse() m := int64(*nrows) //Setup profiling if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal(err) } pprof.WriteHeapProfile(f) f.Close() return } prev := make([]*big.Int, 1) one := big.NewInt(int64(1)) prev[0] = one x := big.NewInt(int64(0)) for row := int64(1); row < m; row++ { curr := make([]*big.Int, 1, row+1) curr[0] = one mid := int64(row/2) + 1 var right int64 if math.Fmod(float64(row), 2.0) == 0 { right = mid - 1 } else { right = mid } for j := int64(1); j < mid; j++ { x = x.Add(prev[j-1], prev[j]) curr = append(curr, x) x = big.NewInt(int64(0)) } // Take a slice of the first half of the row, reverse it, and // append to the current row. s := make([]*big.Int, right) r := curr[0:right] copy(s, r) rev := reverse(s) curr = append(curr, rev...) //fmt.Println(curr) prev = curr[:] } }
func (b Base58) Base582Big() *big.Int { answer := new(big.Int) for i := 0; i < len(b); i++ { answer.Mul(answer, big.NewInt(58)) //multiply current value by 58 answer.Add(answer, big.NewInt(int64(revalp[string(b[i:i+1])]))) //add value of the current letter } return answer }
func main() { for x := 0; x < 100000; x++ { a := big.NewInt(1) for i := 200; i < 221; i++ { a.Mul(big.NewInt(int64(i+x)), a) } } }
func Factorial(val *big.Int) *big.Int { result := big.NewInt(int64(1)) for mult := big.NewInt(int64(1)); mult.Cmp(val) != 1; mult = mult.Add(mult, big.NewInt(int64(1))) { result = result.Mul(result, mult) } return result }
func prod(n int) *big.Int { prod := big.NewInt(1) i, inc := big.NewInt(1), big.NewInt(1) for ; n > 0; n-- { prod.Mul(prod, i) i.Add(i, inc) } return prod }
func TestRatSub(t *testing.T) { fmt.Println("Testing RatSub") x := big.NewRat(0, 1).Sub(big.NewRat(1, 3), big.NewRat(1, 2)) fmt.Print("x ") fmt.Println(x) if (x.Num().Cmp(big.NewInt(-1)) != 0) || (x.Denom().Cmp(big.NewInt(6)) != 0) { t.Error("Num and Denom don't match") } }
func make_fibonacci() func() *big.Int { last, next := big.NewInt(1), big.NewInt(1) return func() *big.Int { tmp := new(big.Int).Set(last) last.Set(next) next.Add(tmp, next) return tmp } }
func bigfact(n *big.Int) *big.Int { one := big.NewInt(1) if one.Cmp(n) == 0 { return n } o := big.NewInt(0).Set(n) p := big.NewInt(0).Set(n) return o.Mul(o, bigfact(p.Sub(p, one))) }
func fact(n int) *big.Int { if n <= 1 { return big.NewInt(1) } r := big.NewInt(int64(n)) return r.Mul(r, fact(n-1)) }
func make_factorial(limit int) []*big.Int { factorial := make([]*big.Int, limit+1) factorial[0] = big.NewInt(1) mult, one := big.NewInt(1), big.NewInt(1) for i := 1; i <= limit; i++ { factorial[i] = new(big.Int).Mul(mult, factorial[i-1]) mult.Add(mult, one) } return factorial }
func Hex2Big(b []byte) *big.Int { answer := big.NewInt(0) for i := 0; i < len(b); i++ { answer.Lsh(answer, 8) answer.Add(answer, big.NewInt(int64(b[i]))) } return answer }
func Euler29() string { distinct := make(map[string]int) for a := 2; a <= 100; a++ { for b := a; b <= 100; b++ { distinct[bigextension.Pow(big.NewInt(int64(a)), int64(b)).String()] = 0 distinct[bigextension.Pow(big.NewInt(int64(b)), int64(a)).String()] = 0 } } return fmt.Sprint(len(distinct)) }
func main() { a, b, c, counter := big.NewInt(int64(1)), big.NewInt(int64(2)), big.NewInt(int64(0)), 2 for len(a.String()) < 1000 { c.Neg(a) //c is a temp variable a.Add(a, b) b.Neg(c) counter++ } println(counter) }
func main() { x, y := big.NewInt(0), big.NewInt(1) var i int for i = 1; len(y.String()) < 1000; i++ { x, y = y, x y.Add(x, y) } fmt.Println(i) }
func main() { input := flag.Int64("num", 100, "num!") flag.Parse() a := big.NewInt(*input) b := big.NewInt(1) for ; a.Cmp(big.NewInt(0)) > 0; a.Sub(a, big.NewInt(1)) { b.Mul(a, b) } c := b.String() fmt.Println(c) }
func TestNewInt(t *testing.T) { x := big.NewInt(int64(int64(1<<62) + 646213)) x.Mul(x, big.NewInt(int64(1<<37))) fmt.Print("Bitlen ") fmt.Println(x.BitLen()) fmt.Print("Binomial ") fmt.Print(x.Binomial(int64(5), int64(2))) fmt.Print(" ") fmt.Println(x) }
func number_divide(x, y Obj) Obj { xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag if xfx && yfx { i1 := int(uintptr(unsafe.Pointer(x))) >> fixnum_shift i2 := int(uintptr(unsafe.Pointer(y))) >> fixnum_shift // A good optimizer will combine the div and mod into // one instruction. r, m := i1/i2, i1%i2 if m == 0 && r > fixnum_min && r < fixnum_max { return Make_fixnum(r) } else { return wrap(big.NewRat(int64(i1), int64(i2))) } } if (!xfx && (uintptr(unsafe.Pointer(x))&heap_mask) != heap_tag) || (!yfx && (uintptr(unsafe.Pointer(y))&heap_mask) != heap_tag) { panic("bad type") } if xfx { x = wrap(big.NewInt(int64(fixnum_to_int(x)))) } if yfx { y = wrap(big.NewInt(int64(fixnum_to_int(y)))) //return wrap(z.Div(vx,vy)) } switch vx := (*x).(type) { case *big.Int: var z *big.Int = big.NewInt(0) switch vy := (*y).(type) { case *big.Int: return simpBig(z.Div(vx, vy)) case *big.Rat: z := big.NewRat(1, 1) z.SetInt(vx) return simpRat(z.Quo(z, vy)) default: panic("bad type") } case *big.Rat: z := big.NewRat(1, 1) switch vy := (*y).(type) { case *big.Int: z.SetInt(vy) return simpRat(z.Quo(vx, z)) case *big.Rat: return simpRat(z.Quo(vx, vy)) } } panic("bad type") }
func main() { result := big.NewInt(0) power := big.NewInt(0) var number *big.Int for i := 1; i < 1001; i++ { number = big.NewInt(int64(i)) power.Exp(number, number, nil) result.Add(result, power) } str := result.String() println(str[len(str)-10 : len(str)]) }
func main() { strMap := make(map[string]int) for a := 2; a < 101; a++ { for b := 2; b < 101; b++ { bigA := big.NewInt(int64(a)) bigB := big.NewInt(int64(b)) bigA.Exp(bigA, bigB, nil) strMap[bigA.String()] = 0 } } println(len(strMap)) }