15class Fp :
public local::addsubmul<Fp,
17 local::hasNegative<Fp,
18 local::inversible<Fp> > > > {
41 std::copy(x, x +
N, v_);
54 std::copy(&
r[0], &
r[0] +
N, v_);
57 std::fill(v_ + 1, v_ +
N, 0);
58 mul(*
this, *
this, montgomeryR2_);
61 std::fill(v_ + 1, v_ +
N, 0);
62 mul(*
this, *
this, montgomeryR2_);
66 void set(
const std::string& str)
83 static void (*
add)(
Fp& out,
const Fp& x,
const Fp& y);
91 static void (*
sub)(
Fp& out,
const Fp& x,
const Fp& y);
93 static void (*
mul)(
Fp& out,
const Fp& x,
const Fp& y);
118 return os << x.
toString(
os.flags() & std::ios_base::hex ? 16 : 10);
130 for (
size_t i = 0; i <
N; i++) {
137 std::fill(v_, v_ +
N, 0);
145 mul(y, x, montgomeryR2_);
156 return (
getLow() & 1) != 0;
171 static const Fp p(&p_[0]);
179 if ((u[0] & 1) == 0) {
182 }
else if ((v[0] & 1) == 0) {
209 mul(out,
r, invTbl_[k]);
218 unsigned int i = x[0] & 0x1;
225 unsigned int i = x[0] & 0x3;
227 addNC(z, z, quarterTbl_[i]);
240 const size_t n = in.size();
243 std::copy(&in[0], &in[0] + n, out.v_);
244 std::fill(out.v_ + n, out.v_ +
N, 0);
247 std::copy(&in[0], &in[0] +
N, out.v_);
255 static void setModulo(
const mie::Vuint&
p,
int mode,
bool useMulx =
true,
bool definedBN_SUPPORT_SNARK =
256#ifdef BN_SUPPORT_SNARK
268 static
mie::
Fp invTbl_[512];
272 static
mie::
Fp *quarterTbl_;
275 static
mie::
Fp montgomeryR2_;
279 static
void setTablesForDiv(const
mie::
Vuint&
p);
283#ifdef MIE_ATE_USE_GMP
284 static void toMpz(mpz_class& y,
const Fp& x)
286 mpz_import(y.get_mpz_t(),
N, -1,
sizeof(
Unit), 0, 0, x.v_);
288 static void fromMpz(
Fp& y,
const mpz_class& x)
291 mpz_export(y.v_, &
size, -1,
sizeof(
Unit), 0, 0, x.get_mpz_t());
292 for (
size_t i =
size; i <
N; i++) {
301 if (t * t != x)
return false;
306 struct Dbl :
public local::addsubmul<Dbl,
307 local::comparable<Dbl,
308 local::hasNegative<Dbl> > > {
314 const size_t n = in.size();
316 std::copy(&in[0], &in[0] + n, out.v_);
317 std::fill(out.v_ + n, out.v_ +
N * 2, 0);
320 std::copy(&in[0], &in[0] +
N * 2, out.v_);
334 std::fill(v_, v_ +
N * 2, 0);
350 return os << x.
toString(
os.flags() & std::ios_base::hex ? 16 : 10);
356 mul(*
this, x, montgomeryR2_);
425 printf(
"Use mie::Vuint intead of Fp for the 3rd parameter for ScalarMulti\n");
static void(* add)(Fp &out, const Fp &x, const Fp &y)
MIE_FORCE_INLINE size_t size() const
static void(* addNC)(Fp &out, const Fp &x, const Fp &y)
MIE_FORCE_INLINE Fp(int x)
void set(const std::string &str)
static void(* shr2)(Fp &out, const Fp &x)
MIE_FORCE_INLINE Fp(const mie::Unit *x)
static void(* mul)(Fp &out, const Fp &x, const Fp &y)
void set(const mie::Vuint &x)
static int(* preInv)(Fp &r, const Fp &x)
static void inv(Fp &out, const Fp &x)
MIE_FORCE_INLINE const Unit & operator[](size_t i) const
static int compare(const Fp &x, const Fp &y)
MIE_FORCE_INLINE Unit & operator[](size_t i)
MIE_FORCE_INLINE Fp(const std::string &str)
static mie::Fp * halfTbl_
static const mie::Vuint & getModulo()
friend std::istream & operator>>(std::istream &is, Fp &x)
static const Fp & getDirectP(int n)
static void _3z_add_2xC(Fp &z, const Fp &x)
static void divBy2(Fp &z, const Fp &x)
friend std::ostream & operator<<(std::ostream &os, const Fp &x)
static void fromMont(Fp &y, const Fp &x)
MIE_FORCE_INLINE bool isZero() const
MIE_FORCE_INLINE void clear()
static void(* neg)(Fp &out, const Fp &x)
static const mie::Vuint & getMontgomeryR()
static void toMont(Fp &y, const Fp &x)
static void divBy4(Fp &z, const Fp &x)
static void(* sub)(Fp &out, const Fp &x, const Fp &y)
static void(* subNC)(Fp &out, const Fp &x, const Fp &y)
Fp(const mie::Vuint &rhs)
static void(* shr1)(Fp &out, const Fp &x)
std::string toString(int base=10) const
static void fast_modp(Fp &z)
static MIE_FORCE_INLINE void setDirect(Fp &out, const T &in)
static void _2z_add_3x(Fp &z, const Fp &x)
static bool squareRoot(Fp &y, const Fp &x)
static void setModulo(const mie::Vuint &p, int mode, bool useMulx=true, bool definedBN_SUPPORT_SNARK=false)
static void square(Fp &out, const Fp &x)
std::istream & getDigits(std::istream &is, std::string &str, bool allowNegative=false)
T power(const T &x, const S &y)
VuintT< local::FixedBuffer< mie::Unit, MIE_ZM_VUINT_BIT_LEN > > Vuint
#define T(meth, val, expected)
unsigned __int64 uint64_t
MIE_FORCE_INLINE void clear()
MIE_FORCE_INLINE const Unit & operator[](size_t i) const
static void subOpt1(Dbl &z, const Dbl &x, const Dbl &y)
static int compare(const Dbl &x, const Dbl &y)
static MIE_FORCE_INLINE void setDirect(Dbl &out, const std::string &in)
static MIE_FORCE_INLINE void setDirect(Dbl &out, const mie::Vuint &in)
std::string toString(int base=10) const
friend std::ostream & operator<<(std::ostream &os, const Dbl &x)
const Unit * const_ptr() const
MIE_FORCE_INLINE size_t size() const
static void(* mod)(Fp &z, const Dbl &x)
void bin_op(Dbl &z, const Dbl &x, const Dbl &y)
mie::Vuint getDirect() const
void setDirect(const T &in)
Dbl(const std::string &str)
static void(* mul)(Dbl &z, const Fp &x, const Fp &y)
void uni_op(Dbl &z, const Dbl &x)
MIE_FORCE_INLINE Unit & operator[](size_t i)
std::string toString(int base=10) const
static int compare(const Unit *x, size_t xn, const Unit *y, size_t yn)
static value_type getBlock(const mie::Fp &, size_t)
static size_t getBlockSize(const mie::Fp &)