Пример #1
0
mpz to_mpz_ext(lua_State * L, int idx) {
    switch (lua_type(L, idx)) {
    case LUA_TNUMBER:       return mpz(static_cast<long>(lua_tointeger(L, idx)));
    case LUA_TSTRING:       return mpz(lua_tostring(L, idx));
    case LUA_TUSERDATA:     return *static_cast<mpz*>(luaL_checkudata(L, idx, mpz_mt));
    default: throw exception(sstream() << "arg #" << idx << " must be a number, string or mpz");
    }
}
Пример #2
0
void display_decimal(std::ostream & out, mpbq const & a, unsigned prec) {
    if (a.is_integer()) {
        out << a.m_num;
        return;
    } else {
        mpz two_k;
        mpz n1, v1;
        if (a.is_neg())
            out << "-";
        v1 = abs(a.m_num);
        power(two_k, mpz(2), a.m_k);
        n1 = rem(v1, two_k);
        v1 = v1/two_k;
        lean_assert(!n1.is_zero());
        out << v1;
        out << ".";
        for (unsigned i = 0; i < prec; i++) {
            n1 *= 10;
            v1  = n1/two_k;
            n1  = rem(n1, two_k);
            out << v1;
            if (n1.is_zero())
                return;
        }
        out << "?";
    }
}
Пример #3
0
static optional<mpz> to_num(expr const & e, bool first) {
    if (is_zero(e)) {
        return first ? some(mpz(0)) : optional<mpz>();
    } else if (is_one(e)) {
        return some(mpz(1));
    } else if (auto a = is_bit0(e)) {
        if (auto r = to_num(*a, false))
            return some(2*(*r));
    } else if (auto a = is_bit1(e)) {
        if (auto r = to_num(*a, false))
            return some(2*(*r)+1);
    } else if (auto a = is_neg(e)) {
        if (auto r = to_num(*a, false))
            return some(neg(*r));
    }
    return optional<mpz>();
}
Пример #4
0
expr from_string_core(std::string const & s) {
    expr r = *g_empty;
    for (unsigned i = 0; i < s.size(); i++) {
        expr n = to_nat_expr(mpz(static_cast<unsigned char>(s[i])));
        expr c = mk_app(*g_char_of_nat, n);
        r = mk_app(*g_str, c, r);
    }
    return r;
}
Пример #5
0
static mpz const & to_mpz(lua_State * L) {
    static LEAN_THREAD_LOCAL mpz arg;
    switch (lua_type(L, idx)) {
    case LUA_TNUMBER:       arg = static_cast<long>(lua_tointeger(L, idx)); return arg;
    case LUA_TSTRING:       arg = mpz(lua_tostring(L, idx)); return arg;
    case LUA_TUSERDATA:     return *static_cast<mpz*>(luaL_checkudata(L, idx, mpz_mt));
    default: throw exception(sstream() << "arg #" << idx << " must be a number, string or mpz");
    }
}
Пример #6
0
expr to_nat_expr_core(mpz const & n) {
    lean_assert(n >= 0);
    if (n == 1)
        return mk_nat_one();
    else if (n % mpz(2) == 0)
        return mk_nat_bit0(to_nat_expr(n / 2));
    else
        return mk_nat_bit1(to_nat_expr(n / 2));
}
Пример #7
0
static void ex1() {
    unsynch_mpq_manager        qm;
    polynomial::manager        pm(qm);
    polynomial_ref x(pm);
    polynomial_ref a(pm);
    polynomial_ref b(pm);
    polynomial_ref c(pm);
    x = pm.mk_polynomial(pm.mk_var());
    a = pm.mk_polynomial(pm.mk_var());
    b = pm.mk_polynomial(pm.mk_var());
    c = pm.mk_polynomial(pm.mk_var());
    polynomial_ref p(pm);
    p = (a + 2*b)*(x^3) + x*a + (b^2);
    polynomial_ref p1(pm);
    p1 = derivative(p, 0);
    polynomial_ref h2(pm);
    unsigned d;
    h2 = pseudo_remainder(p, p1, 0, d);
    std::cout << "d: " << d << "\n";
    std::cout << "p: "; pp(p, 0); std::cout << "\np': "; pp(p1, 0); std::cout << "\nh2: "; pp(h2, 0); std::cout << "\n";
    polynomial_ref h3(pm);
    h3 = pseudo_remainder(p1, h2, 0, d);
    std::cout << "d: " << d << "\n";
    std::cout << "h3: "; pp(h3, 0); std::cout << "\n";

    algebraic_numbers::manager am(qm);
    scoped_anum v1(am), v2(am);
    am.set(v1, 2);
    am.root(v1, 3, v1);
    am.set(v2, 3);
    am.root(v2, 3, v2);

    polynomial::simple_var2value<anum_manager> x2v(am);
    x2v.push_back(1, v1);
    x2v.push_back(2, v2);
    std::cout << "sign(h3(v1,v2)): " << am.eval_sign_at(h3, x2v) << "\n";
    scoped_anum v0(am);
    am.set(v0, -1);
    x2v.push_back(0, v0);
    std::cout << "sign(h2(v1,v2)): " << am.eval_sign_at(h2, x2v) << "\n";
    std::cout << "sign(p'(v1,v2)): " << am.eval_sign_at(p1, x2v) << "\n";
    std::cout << "sign(p(v1,v2)): " << am.eval_sign_at(p, x2v) << "\n";

    polynomial::simple_var2value<anum_manager> x2v2(am);
    x2v2.push_back(1, v1);
    x2v2.push_back(2, v2);
    scoped_mpq tmp(qm);
    qm.set(tmp, -1);
    qm.div(tmp, mpz(2), tmp);
    std::cout << "tmp: "; qm.display(std::cout, tmp); std::cout << " "; qm.display_decimal(std::cout, tmp, 10); std::cout << "\n";
    am.set(v0, tmp);
    x2v2.push_back(0, v0);
    std::cout << "v0: " << v0 << "\n";
    std::cout << "sign(h2(v1,v2)): " << am.eval_sign_at(h2, x2v2) << "\n";
    std::cout << "sign(p'(v1,v2)): " << am.eval_sign_at(p1, x2v2) << "\n";
    std::cout << "sign(p(v1,v2)): " << am.eval_sign_at(p, x2v2) << "\n";
}
Пример #8
0
int elgv1vrfy(const mpbarrett* p, const mpbarrett* n, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
{
	register size_t size = p->size;
	register mpw* temp;

	if (mpz(r->size, r->data))
		return 0;

	if (mpgex(r->size, r->data, size, p->modl))
		return 0;

	if (mpz(s->size, s->data))
		return 0;

	if (mpgex(s->size, s->data, n->size, n->modl))
		return 0;

	temp = (mpw*) malloc((6*size+2)*sizeof(mpw));

	if (temp)
	{
		register int rc;

		/* compute u1 = y^r mod p */
		mpbpowmod_w(p, y->size, y->data, r->size, r->data, temp, temp+2*size);

		/* compute u2 = r^s mod p */
		mpbpowmod_w(p, r->size, r->data, s->size, s->data, temp+size, temp+2*size);

		/* compute v1 = u1*u2 mod p */
		mpbmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);

		/* compute v2 = g^h(m) mod p */
		mpbpowmod_w(p, g->size, g->data, hm->size, hm->data, temp, temp+2*size);

		rc = mpeq(size, temp, temp+size);

		free(temp);

		return rc;
	}
	return 0;
}
Пример #9
0
static mpz const & to_mpz(lua_State * L) {
    LEAN_THREAD_PTR(mpz) arg;
    if (!arg.get())
        arg.reset(new mpz());
    switch (lua_type(L, idx)) {
    case LUA_TNUMBER:       *arg = static_cast<long>(lua_tointeger(L, idx)); return *arg;
    case LUA_TSTRING:       *arg = mpz(lua_tostring(L, idx)); return *arg;
    case LUA_TUSERDATA:     return *static_cast<mpz*>(luaL_checkudata(L, idx, mpz_mt));
    default: throw exception(sstream() << "arg #" << idx << " must be a number, string or mpz");
    }
}
Пример #10
0
static void tst2() {
    typedef basic_interval_manager<unsynch_mpz_manager, false> mpzi_manager;
    typedef mpzi_manager::scoped_interval scoped_mpzi;

    unsynch_mpz_manager nm;
    mpzi_manager m(nm);
    scoped_mpzi  x(m), y(m), z(m);

    m.set(x, mpz(1), mpz(2));
    m.set(y, mpz(-2), mpz(3));
    m.add(x, y, z);
    std::cout << "x: " << x << ", y: " << y << ", z: " << z << "\n";
    SASSERT(nm.eq(z.lower(), mpz(-1)));
    SASSERT(nm.eq(z.upper(), mpz(5)));
    m.mul(x, y, z);
    std::cout << "x: " << x << ", y: " << y << ", z: " << z << "\n";
    SASSERT(nm.eq(z.lower(), mpz(-4)));
    SASSERT(nm.eq(z.upper(), mpz(6)));
}
Пример #11
0
 equation * mk_eq(unsigned num, mpz const * as, var const * xs, mpz const & c, unsigned num_js, mpq const * bs, justification const * js,
                  bool sort = true) {
     equation * new_eq = alloc(equation);
     for (unsigned i = 0; i < num; i++) {
         m().set(m_as_buffer[xs[i]], as[i]);
         new_eq->m_as.push_back(mpz());
         new_eq->m_xs.push_back(xs[i]);
     }
     sort_core(new_eq->m_as, new_eq->m_xs, m_as_buffer);
     m().set(new_eq->m_c, c);
     for (unsigned i = 0; i < num_js; i++) {
         m().set(m_bs_buffer[js[i]], bs[i]);
         new_eq->m_bs.push_back(mpq());
         new_eq->m_js.push_back(js[i]);
     }
     if (sort)
         sort_core(new_eq->m_bs, new_eq->m_js, m_bs_buffer);
     return new_eq;
 }
Пример #12
0
        smt::theory::init(ctx);
        m_is_initialized = true;
    }

    app * theory_fpa::fpa_value_proc::mk_value(model_generator & mg, ptr_vector<expr> & values) {
        ast_manager & m = m_th.get_manager();

        TRACE("t_fpa_detail", for (unsigned i = 0; i < values.size(); i++)
                                  tout << "value[" << i << "] = " << mk_ismt2_pp(values[i], m) << std::endl;);

        mpf_manager & mpfm = m_fu.fm();
        unsynch_mpz_manager & mpzm = mpfm.mpz_manager();
        app * result;

        scoped_mpz bias(mpzm);
        mpzm.power(mpz(2), m_ebits - 1, bias);
        mpzm.dec(bias);

        scoped_mpz sgn_z(mpzm), sig_z(mpzm), exp_z(mpzm);
        unsigned bv_sz;

        if (values.size() == 1) {
            SASSERT(m_bu.is_bv(values[0]));
            SASSERT(m_bu.get_bv_size(values[0]) == (m_ebits + m_sbits));

            rational all_r(0);
            scoped_mpz all_z(mpzm);

            bool r = m_bu.is_numeral(values[0], all_r, bv_sz);
            SASSERT(r);
            SASSERT(bv_sz == (m_ebits + m_sbits));
Пример #13
0
mpz read_mpz(deserializer & d) {
    return mpz(d.read_string().c_str());
}
Пример #14
0
int dsavrfy(const mpbarrett* p, const mpbarrett* q, const mpnumber* g, const mpnumber* hm, const mpnumber* y, const mpnumber* r, const mpnumber* s)
{
	register size_t psize = p->size;
	register size_t qsize = q->size;

	register mpw* ptemp;
	register mpw* qtemp;

	register mpw* pwksp;
	register mpw* qwksp;

	register int rc = 0;

	/* h(m) shouldn't contain more bits than q */
	if (mpbits(hm->size, hm->data) > mpbits(q->size, q->modl))
		return rc;

	/* check 0 < r < q */
	if (mpz(r->size, r->data))
		return rc;

	if (mpgex(r->size, r->data, qsize, q->modl))
		return rc;

	/* check 0 < s < q */
	if (mpz(s->size, s->data))
		return rc;

	if (mpgex(s->size, s->data, qsize, q->modl))
		return rc;

	ptemp = (mpw*) malloc((6*psize+2)*sizeof(mpw));
	if (ptemp == (mpw*) 0)
		return rc;

	qtemp = (mpw*) malloc((8*qsize+6)*sizeof(mpw));
	if (qtemp == (mpw*) 0)
	{
		free(ptemp);
		return rc;
	}

	pwksp = ptemp+2*psize;
	qwksp = qtemp+2*qsize;

	mpsetx(qsize, qtemp+qsize, s->size, s->data);

	/* compute w = inv(s) mod q */
	if (mpextgcd_w(qsize, q->modl, qtemp+qsize, qtemp, qwksp))
	{
		/* compute u1 = h(m)*w mod q */
		mpbmulmod_w(q, hm->size, hm->data, qsize, qtemp, qtemp+qsize, qwksp);

		/* compute u2 = r*w mod q */
		mpbmulmod_w(q, r->size, r->data, qsize, qtemp, qtemp, qwksp);

		/* compute g^u1 mod p */
		mpbpowmod_w(p, g->size, g->data, qsize, qtemp+qsize, ptemp, pwksp);

		/* compute y^u2 mod p */
		mpbpowmod_w(p, y->size, y->data, qsize, qtemp, ptemp+psize, pwksp);

		/* multiply mod p */
		mpbmulmod_w(p, psize, ptemp, psize, ptemp+psize, ptemp, pwksp);

		/* modulo q */
		mpmod(ptemp+psize, psize, ptemp, qsize, q->modl, pwksp);

		rc = mpeqx(r->size, r->data, psize, ptemp+psize);
	}

	free(qtemp);
	free(ptemp);

	return rc;
}
Пример #15
0
mpz factorial(const mpz& n) {
  return (n == 1 || n == 0) ? mpz(1) : factorial(n - 1) * n;
}
Пример #16
0
vm_obj mk_vm_nat(unsigned n) {
    if (n < LEAN_MAX_SMALL_NAT)
        return mk_vm_simple(n);
    else
        return mk_vm_mpz(mpz(n));
}