/* * gfpvar_.cpp * */ #include "gfpvar.h" #include "Setup.h" #include "Protocols/Share.h" #include "gfp.hpp" template Zp_Data gfpvar_::ZpD; template string gfpvar_::type_string() { return "gfp"; } template string gfpvar_::type_short() { return "p"; } template char gfpvar_::type_char() { return 'p'; } template void gfpvar_::specification(octetStream& os) { os.store(pr()); } template int gfpvar_::length() { return ZpD.pr_bit_length; } template int gfpvar_::size() { return ZpD.get_t() * sizeof(mp_limb_t); } template int gfpvar_::size_in_bits() { return size() * 8; } template bool gfpvar_::allows(Dtype dtype) { return gfp_<0, 0>::allows(dtype); } template DataFieldType gfpvar_::field_type() { return gfp_<0, 0>::field_type(); } template void gfpvar_::init_field(bigint prime, bool montgomery) { ZpD.init(prime, montgomery); if (ZpD.get_t() > N_LIMBS) throw wrong_gfp_size("gfpvar_", prime, "MAX_MOD_SZ", ZpD.get_t() * 2); } template void gfpvar_::init_default(int lgp, bool montgomery) { init_field(SPDZ_Data_Setup_Primes(lgp), montgomery); } template inline void gfpvar_::reset() { ZpD = {}; } template const Zp_Data& gfpvar_::get_ZpD() { return ZpD; } template const bigint& gfpvar_::pr() { return ZpD.pr; } template void gfpvar_::check_setup(string dir) { ::check_setup(dir, pr()); } template void gfpvar_::write_setup(string dir) { write_online_setup(dir, pr()); } template gfpvar_::gfpvar_() { } template gfpvar_::gfpvar_(int other) { to_modp(a, other, ZpD); } template gfpvar_::gfpvar_(const bigint& other) { to_modp(a, other, ZpD); } template gfpvar_::gfpvar_(int128 other) : gfpvar_( (bigint::tmp = other.get_lower() + ((bigint::tmp2 = other.get_upper()) << 64))) { } template gfpvar_::gfpvar_(BitVec_ other) : gfpvar_(bigint::tmp = other.get()) { } template void gfpvar_::assign(const void* buffer) { a.assign(buffer, ZpD.get_t()); } template void gfpvar_::assign_zero() { *this = {}; } template void gfpvar_::assign_one() { assignOne(a, ZpD); } template bool gfpvar_::is_zero() { return isZero(a, ZpD); } template bool gfpvar_::is_one() { return isOne(a, ZpD); } template bool gfpvar_::is_bit() { return is_zero() or is_one(); } template typename gfpvar_::modp_type gfpvar_::get() const { return a; } template const void* gfpvar_::get_ptr() const { return a.get(); } template void* gfpvar_::get_ptr() { return &a; } template void gfpvar_::zero_overhang() { a.zero_overhang(ZpD); } template void gfpvar_::check() { assert(mpn_cmp(a.get(), ZpD.get_prA(), ZpD.get_t()) < 0); } template gfpvar_ gfpvar_::operator +(const gfpvar_& other) const { gfpvar_ res; Add(res.a, a, other.a, ZpD); return res; } template gfpvar_ gfpvar_::operator -(const gfpvar_& other) const { gfpvar_ res; Sub(res.a, a, other.a, ZpD); return res; } template gfpvar_ gfpvar_::operator *(const gfpvar_& other) const { gfpvar_ res; Mul(res.a, a, other.a, ZpD); return res; } template gfpvar_ gfpvar_::operator /(const gfpvar_& other) const { return *this * other.invert(); } template gfpvar_ gfpvar_::operator <<(int other) const { return bigint::tmp = (bigint::tmp = *this) << other; } template gfpvar_ gfpvar_::operator >>(int other) const { return bigint::tmp = (bigint::tmp = *this) >> other; } template gfpvar_& gfpvar_::operator +=(const gfpvar_& other) { Add(a, a, other.a, ZpD); return *this; } template gfpvar_& gfpvar_::operator -=(const gfpvar_& other) { Sub(a, a, other.a, ZpD); return *this; } template gfpvar_& gfpvar_::operator *=(const gfpvar_& other) { Mul(a, a, other.a, ZpD); return *this; } template gfpvar_& gfpvar_::operator &=(const gfpvar_& other) { *this = bigint::tmp = (bigint::tmp = *this) & (bigint::tmp2 = other); return *this; } template gfpvar_& gfpvar_::operator >>=(int other) { return *this = *this >> other; } template bool gfpvar_::operator ==(const gfpvar_& other) const { return areEqual(a, other.a, ZpD); } template bool gfpvar_::operator !=(const gfpvar_& other) const { return not (*this == other); } template void gfpvar_::negate() { *this = gfpvar_() - *this; } template gfpvar_ gfpvar_::invert() const { gfpvar_ res; Inv(res.a, a, ZpD); return res; } template gfpvar_ gfpvar_::sqrRoot() const { bigint ti; ti = sqrRootMod(*this); if (!isOdd(ti)) ti = ZpD.pr - ti; return ti; } template void gfpvar_::randomize(PRNG& G, int) { a.randomize(G, ZpD); } template void gfpvar_::almost_randomize(PRNG& G) { randomize(G); } template void gfpvar_::pack(octetStream& os, int) const { a.pack(os, ZpD); } template void gfpvar_::unpack(octetStream& os, int) { a.unpack(os, ZpD); } template void gfpvar_::output(ostream& o, bool human) const { a.output(o, ZpD, human); } template void gfpvar_::input(istream& i, bool human) { a.input(i, ZpD, human); } template class gfpvar_<0, MAX_MOD_SZ / 2>; template class gfpvar_<1, MAX_MOD_SZ>; template class gfpvar_<2, MAX_MOD_SZ>;