Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
mie::Fp Class Reference

#include <zm2.h>

Inheritance diagram for mie::Fp:
Collaboration diagram for mie::Fp:

Classes

struct  Dbl
 

Public Types

enum  { N = 32 / sizeof(Unit) }
 
typedef mie::Unit value_type
 

Public Member Functions

 Fp ()
 
MIE_FORCE_INLINE Fp (int x)
 
MIE_FORCE_INLINE Fp (const std::string &str)
 
MIE_FORCE_INLINE Fp (const mie::Unit *x)
 
 Fp (const mie::Vuint &rhs)
 
void set (int x)
 
void set (const std::string &str)
 
void set (const mie::Vuint &x)
 
MIE_FORCE_INLINE bool isZero () const
 
MIE_FORCE_INLINE void clear ()
 
Unit getLow () const
 
bool isOdd () const
 
mie::Vuint get () const
 
void inverse ()
 
std::string toString (int base=10) const
 
MIE_FORCE_INLINE const Unitoperator[] (size_t i) const
 
MIE_FORCE_INLINE Unitoperator[] (size_t i)
 
MIE_FORCE_INLINE size_t size () const
 
- Public Member Functions inherited from mie::local::addsubmul< Fp, local::comparable< Fp, local::hasNegative< Fp, local::inversible< Fp > > > >
MIE_FORCE_INLINE Fpoperator+= (const N &rhs)
 
MIE_FORCE_INLINE Fpoperator-= (const Fp &rhs)
 
MIE_FORCE_INLINE Fpoperator*= (const Fp &rhs)
 
- Public Member Functions inherited from mie::local::hasNegative< Fp, local::inversible< Fp > >
MIE_FORCE_INLINE Fp operator- () const
 
- Public Member Functions inherited from mie::local::inversible< Fp >
MIE_FORCE_INLINE void inverse ()
 
MIE_FORCE_INLINE Fpoperator/= (const Fp &x)
 

Static Public Member Functions

static int compare (const Fp &x, const Fp &y)
 
static void _3z_add_2xC (Fp &z, const Fp &x)
 
static void _2z_add_3x (Fp &z, const Fp &x)
 
static void fromMont (Fp &y, const Fp &x)
 
static void toMont (Fp &y, const Fp &x)
 
static void inv (Fp &out, const Fp &x)
 
static void divBy2 (Fp &z, const Fp &x)
 
static void divBy4 (Fp &z, const Fp &x)
 
static void fast_modp (Fp &z)
 
template<class T >
static MIE_FORCE_INLINE void setDirect (Fp &out, const T &in)
 
static void setModulo (const mie::Vuint &p, int mode, bool useMulx=true, bool definedBN_SUPPORT_SNARK=false)
 
static const mie::VuintgetModulo ()
 
static const FpgetDirectP (int n)
 
static const mie::VuintgetMontgomeryR ()
 
static void square (Fp &out, const Fp &x)
 
static bool squareRoot (Fp &y, const Fp &x)
 

Static Public Attributes

static void(* add )(Fp &out, const Fp &x, const Fp &y)
 
static void(* addNC )(Fp &out, const Fp &x, const Fp &y) = &Fp_addNC_C
 
static void(* subNC )(Fp &out, const Fp &x, const Fp &y) = &Fp_subNC_C
 
static void(* shr1 )(Fp &out, const Fp &x)
 
static void(* shr2 )(Fp &out, const Fp &x)
 
static void(* sub )(Fp &out, const Fp &x, const Fp &y)
 
static void(* neg )(Fp &out, const Fp &x) = &Fp_negC
 
static void(* mul )(Fp &out, const Fp &x, const Fp &y)
 
static int(* preInv )(Fp &r, const Fp &x)
 
static mie::FphalfTbl_
 

Friends

std::ostream & operator<< (std::ostream &os, const Fp &x)
 
std::istream & operator>> (std::istream &is, Fp &x)
 

Detailed Description

Definition at line 15 of file zm2.h.

Member Typedef Documentation

◆ value_type

Definition at line 20 of file zm2.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator

Definition at line 25 of file zm2.h.

25 {
26 N = 32 / sizeof(Unit)
27 };
@ N
Definition zm2.h:26
uint32_t Unit
Definition zm.h:66

Constructor & Destructor Documentation

◆ Fp() [1/5]

mie::Fp::Fp ( )
inline

Definition at line 28 of file zm2.h.

29 {
30 }

◆ Fp() [2/5]

MIE_FORCE_INLINE mie::Fp::Fp ( int x)
inline

Definition at line 31 of file zm2.h.

32 {
33 set(x);
34 }
bool set

◆ Fp() [3/5]

MIE_FORCE_INLINE mie::Fp::Fp ( const std::string & str)
inlineexplicit

Definition at line 35 of file zm2.h.

36 {
37 set(str);
38 }

◆ Fp() [4/5]

MIE_FORCE_INLINE mie::Fp::Fp ( const mie::Unit * x)
inlineexplicit

Definition at line 39 of file zm2.h.

40 {
41 std::copy(x, x + N, v_);
42 }

◆ Fp() [5/5]

mie::Fp::Fp ( const mie::Vuint & rhs)
inline

Definition at line 43 of file zm2.h.

44 {
45 set(rhs);
46 }

Member Function Documentation

◆ _2z_add_3x()

static void mie::Fp::_2z_add_3x ( Fp & z,
const Fp & x )
inlinestatic

Definition at line 108 of file zm2.h.

109 {
110 addNC(z, x, z);
111 addNC(z, z, z);
112 addNC(z, z, x);
113 fast_modp(z);
114 }
static void(* addNC)(Fp &out, const Fp &x, const Fp &y)
Definition zm2.h:86
static void fast_modp(Fp &z)
Definition zm2.h:231
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _3z_add_2xC()

static void mie::Fp::_3z_add_2xC ( Fp & z,
const Fp & x )
inlinestatic

Definition at line 98 of file zm2.h.

99 {
100 addNC(z, z, x);
101 addNC(z, z, z);
102 addNC(z, z, x);
103 fast_modp(z);
104 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear()

MIE_FORCE_INLINE void mie::Fp::clear ( )
inline

Definition at line 135 of file zm2.h.

136 {
137 std::fill(v_, v_ + N, 0);
138 }
Here is the caller graph for this function:

◆ compare()

static int mie::Fp::compare ( const Fp & x,
const Fp & y )
inlinestatic

Definition at line 79 of file zm2.h.

80 {
81 return mie::local::PrimitiveFunction::compare(&x[0], N, &y[0], N);
82 }
static int compare(const Unit *x, size_t xn, const Unit *y, size_t yn)
Definition zm.h:183
Here is the call graph for this function:

◆ divBy2()

static void mie::Fp::divBy2 ( Fp & z,
const Fp & x )
inlinestatic

Definition at line 216 of file zm2.h.

217 {
218 unsigned int i = x[0] & 0x1;
219 shr1(z, x);
220 addNC(z, z, halfTbl_[i]);
221 }
static mie::Fp * halfTbl_
Definition zm2.h:270
static void(* shr1)(Fp &out, const Fp &x)
Definition zm2.h:88
Here is the caller graph for this function:

◆ divBy4()

static void mie::Fp::divBy4 ( Fp & z,
const Fp & x )
inlinestatic

Definition at line 223 of file zm2.h.

224 {
225 unsigned int i = x[0] & 0x3;
226 shr2(z, x);
227 addNC(z, z, quarterTbl_[i]);
228 }
static void(* shr2)(Fp &out, const Fp &x)
Definition zm2.h:89
Here is the caller graph for this function:

◆ fast_modp()

static void mie::Fp::fast_modp ( Fp & z)
inlinestatic

Definition at line 231 of file zm2.h.

232 {
233 uint64_t t = z.v_[3] >> 61;
234 z -= getDirectP((int)t);
235 }
static const Fp & getDirectP(int n)
Definition zm2.cpp:126
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromMont()

static void mie::Fp::fromMont ( Fp & y,
const Fp & x )
inlinestatic

Definition at line 139 of file zm2.h.

140 {
141 mul(y, x, one_);
142 }
static void(* mul)(Fp &out, const Fp &x, const Fp &y)
Definition zm2.h:93
Here is the caller graph for this function:

◆ get()

mie::Vuint mie::Fp::get ( ) const
inline

Definition at line 158 of file zm2.h.

159 {
160 Fp t;
161 fromMont(t, *this);
162 mie::Vuint ret(t.v_, N);
163 return ret;
164 }
static void fromMont(Fp &y, const Fp &x)
Definition zm2.h:139
CK_RV ret
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDirectP()

const Fp & Fp::getDirectP ( int n)
static

Definition at line 126 of file zm2.cpp.

127{
128 assert(0 <= n && (size_t)n < pTblSize);
129 return s_pTbl[n];
130}
const size_t pTblSize
Definition zm2.cpp:46
Here is the caller graph for this function:

◆ getLow()

Unit mie::Fp::getLow ( ) const
inline

Definition at line 148 of file zm2.h.

149 {
150 Fp t;
151 fromMont(t, *this);
152 return t.v_[0];
153 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getModulo()

static const mie::Vuint & mie::Fp::getModulo ( )
inlinestatic

Definition at line 262 of file zm2.h.

262{ return p_; }
Here is the caller graph for this function:

◆ getMontgomeryR()

static const mie::Vuint & mie::Fp::getMontgomeryR ( )
inlinestatic

Definition at line 264 of file zm2.h.

264{ return montgomeryR_; }
Here is the caller graph for this function:

◆ inv()

static void mie::Fp::inv ( Fp & out,
const Fp & x )
inlinestatic

Definition at line 165 of file zm2.h.

166 {
167#ifdef MIE_USE_X64ASM
168 Fp r;
169 int k = preInv(r, x);
170#else
171 static const Fp p(&p_[0]);
172 Fp u, v, r, s;
173 u = p;
174 v = x;
175 r.clear();
176 s.clear(); s[0] = 1; // s is real 1
177 int k = 0;
178 while (!v.isZero()) {
179 if ((u[0] & 1) == 0) {
180 shr1(u, u);
181 addNC(s, s, s);
182 } else if ((v[0] & 1) == 0) {
183 shr1(v, v);
184 addNC(r, r, r);
185 } else if (v >= u) {
186 subNC(v, v, u);
187 addNC(s, s, r);
188 shr1(v, v);
189 addNC(r, r, r);
190 } else {
191 subNC(u, u, v);
192 addNC(r, r, s);
193 shr1(u, u);
194 addNC(s, s, s);
195 }
196 k++;
197 }
198 if (r >= p) {
199 subNC(r, r, p);
200 }
201 assert(!r.isZero());
202 subNC(r, p, r);
203#endif
204 /*
205 xr = 2^k
206 R = 2^256
207 get r2^(-k)R^2 = r 2^(512 - k)
208 */
209 mul(out, r, invTbl_[k]);
210 }
const mie::Vuint & p
Definition bn.cpp:27
const mie::Vuint & r
Definition bn.cpp:28
static int(* preInv)(Fp &r, const Fp &x)
Definition zm2.h:94
static void(* subNC)(Fp &out, const Fp &x, const Fp &y)
Definition zm2.h:87
void clear()
Definition zm.h:406
bool isZero() const
Definition zm.h:521
char * s
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inverse()

void mie::Fp::inverse ( )
inline

Definition at line 211 of file zm2.h.

212 {
213 inv(*this, *this);
214 }
static void inv(Fp &out, const Fp &x)
Definition zm2.h:165
Here is the call graph for this function:

◆ isOdd()

bool mie::Fp::isOdd ( ) const
inline

Definition at line 154 of file zm2.h.

155 {
156 return (getLow() & 1) != 0;
157 }
Unit getLow() const
Definition zm2.h:148
Here is the call graph for this function:

◆ isZero()

MIE_FORCE_INLINE bool mie::Fp::isZero ( ) const
inline

Definition at line 127 of file zm2.h.

128 {
129 Unit t = 0;
130 for (size_t i = 0; i < N; i++) {
131 t |= v_[i];
132 }
133 return t == 0;
134 }
Here is the caller graph for this function:

◆ operator[]() [1/2]

MIE_FORCE_INLINE Unit & mie::Fp::operator[] ( size_t i)
inline

Definition at line 252 of file zm2.h.

252{ return v_[i]; }

◆ operator[]() [2/2]

MIE_FORCE_INLINE const Unit & mie::Fp::operator[] ( size_t i) const
inline

Definition at line 251 of file zm2.h.

251{ return v_[i]; }

◆ set() [1/3]

void mie::Fp::set ( const mie::Vuint & x)
inline

Definition at line 70 of file zm2.h.

71 {
72 assert(x < getModulo());
73 mie::Vuint y(x);
74// count++;std::cout << "count=" << count << ", x=" << x << std::endl;
75 y *= getMontgomeryR();
76 y %= getModulo();
77 setDirect(*this, y);
78 }
static const mie::Vuint & getModulo()
Definition zm2.h:262
static const mie::Vuint & getMontgomeryR()
Definition zm2.h:264
static MIE_FORCE_INLINE void setDirect(Fp &out, const T &in)
Definition zm2.h:238
uint64_t y
Definition sha3.cpp:34
Here is the call graph for this function:

◆ set() [2/3]

void mie::Fp::set ( const std::string & str)
inline

Definition at line 66 of file zm2.h.

67 {
68 set(mie::Vuint(str));
69 }

◆ set() [3/3]

void mie::Fp::set ( int x)
inline

Definition at line 47 of file zm2.h.

48 {
49 if (x == 0) {
50 clear();
51 } else if (x == 1) {
52 const mie::Vuint& r = getMontgomeryR();
53 assert(r.size() == N);
54 std::copy(&r[0], &r[0] + N, v_);
55 } else if (x > 0) {
56 v_[0] = x;
57 std::fill(v_ + 1, v_ + N, 0);
58 mul(*this, *this, montgomeryR2_);
59 } else {
60 v_[0] = -x;
61 std::fill(v_ + 1, v_ + N, 0);
62 mul(*this, *this, montgomeryR2_);
63 neg(*this, *this);
64 }
65 }
MIE_FORCE_INLINE void clear()
Definition zm2.h:135
static void(* neg)(Fp &out, const Fp &x)
Definition zm2.h:92
size_t size() const
Definition zm.h:519
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setDirect()

template<class T >
static MIE_FORCE_INLINE void mie::Fp::setDirect ( Fp & out,
const T & in )
inlinestatic

Definition at line 238 of file zm2.h.

239 {
240 const size_t n = in.size();
241// assert(n <= N);
242 if (n < N) {
243 std::copy(&in[0], &in[0] + n, out.v_);
244 std::fill(out.v_ + n, out.v_ + N, 0);
245 } else {
246 // ignore in[i] for i >= N
247 std::copy(&in[0], &in[0] + N, out.v_);
248 }
249 }
Here is the caller graph for this function:

◆ setModulo()

void Fp::setModulo ( const mie::Vuint & p,
int mode,
bool useMulx = true,
bool definedBN_SUPPORT_SNARK = false )
static

Definition at line 3592 of file zm2.cpp.

3593{
3594#ifdef DEBUG_COUNT
3595 puts("DEBUG_COUNT mode on!!!");
3596#endif
3597#ifdef BN_SUPPORT_SNARK
3598 const bool scipr = true;
3599#else
3600 const bool scipr = false;
3601#endif
3602 if (scipr != definedBN_SUPPORT_SNARK) {
3603 fprintf(stderr, "use -DBN_SUPPORT_SNARK for all sources\n");
3604 exit(1);
3605 }
3606 static bool init = false;
3607 if (init) return;
3608 init = true;
3609 if (p.size() != Fp::N) {
3610 mie::local::errExit("not support p for Fp::setModulo");
3611 }
3612 p_ = p;
3613
3614 // Fp_mul
3615 {
3617 ZN::setModulo(Vuint(1) << (sizeof(Unit) * 8));
3618 ZN t(p);
3619 t = -t;
3620 t.inverse();
3621 pp_mont = t[0];
3622 pN = p << 256;
3623 p_add1_div4_ = (p + 1) / 4;
3624 }
3625
3626 // we can't use Fp before setting Fp_mul* variables!!!
3627 montgomeryR_ = (Vuint(1) << 256) % p;
3628 {
3630 Z::setModulo(p);
3631 Z t(montgomeryR_);
3632 Fp::setDirect(montgomeryR2_, t * t);
3633 }
3634 one_.clear();
3635 one_[0] = 1;
3636
3638 try {
3639 // setup code and data area
3640 const int PageSize = 4096;
3641 const size_t codeSize = PageSize * 9;
3642 const size_t dataSize = PageSize * 1;
3643
3644 static std::vector<Xbyak::uint8> buf;
3645 buf.resize(codeSize + dataSize + PageSize);
3646 Xbyak::uint8 *const codeAddr = Xbyak::CodeArray::getAlignedAddress(&buf[0], PageSize);
3647 Xbyak::CodeArray::protect(codeAddr, codeSize, true);
3648 s_data = Xbyak::CastTo<Data*>(codeAddr + codeSize);
3649
3650// printf("codeAddr=%p, dataAddr=%p\n", codeAddr, s_data);
3651 if ((size_t)codeAddr & 0xffffffff00000000ULL || (size_t)s_data & 0xffffffff00000000ULL) {
3652 // printf("\naddress of code and data is over 4GB!!!\n");
3653 }
3654
3655 // setup data
3656 s_pTbl = s_data->pTbl;
3658 Fp::quarterTbl_ = s_data->quarterTbl;
3660
3661 for (size_t i = 0; i < pTblSize; i++) {
3662 Fp::setDirect(s_pTbl[i], p * int(i));
3663 }
3664 /*
3665 for option1
3666 lower 192-bits of pNTbl_[1] = 0
3667 lower 192-bits of pNTbl_[2] = 0
3668 */
3669 Fp::Dbl::pNTbl_[0].setDirect(pN);
3670 for (size_t h = 1; h < pNtblSize; ++h) {
3671 Fp::Dbl::pNTbl_[h].setDirect(pN >> h);
3672 }
3673 setTablesForDiv(p);
3674
3675 // setup code
3676 static PairingCode code(codeSize, codeAddr);
3677 code.init(p_, mode, useMulx);
3678 {
3679 Fp t(2);
3680 for (int i = 0; i < 512; i++) {
3681 invTbl_[511 - i] = t;
3682 t += t;
3683 }
3684 }
3685 return;
3686 } catch (std::exception& e) {
3687 fprintf(stderr, "setModulo ERR:%s\n", e.what());
3688 }
3689 ::exit(1);
3690}
static uint8 * getAlignedAddress(uint8 *addr, size_t alignedSize=16)
Definition xbyak.h:987
static bool protect(const void *addr, size_t size, bool canExec)
Definition xbyak.h:966
static void setModulo(const V &m)
Definition zm.h:1331
void init()
Definition lib_test.cpp:3
const To CastTo(From p)
Definition xbyak.h:279
VuintT< local::FixedBuffer< mie::Unit, MIE_ZM_VUINT_BIT_LEN > > Vuint
Definition zm.h:1161
Fp halfTbl[2]
Definition zm2.cpp:50
Fp quarterTbl[4]
Definition zm2.cpp:51
Fp pTbl[pTblSize]
Definition zm2.cpp:49
FpDbl pNTbl[pNtblSize]
Definition zm2.cpp:52
static MIE_FORCE_INLINE void setDirect(Dbl &out, const mie::Vuint &in)
Definition zm2.h:312
static Dbl * pNTbl_
Definition zm2.h:403
uint8_t buf[2048]
const size_t pNtblSize
Definition zm2.cpp:47
Data * s_data
Definition zm2.cpp:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

MIE_FORCE_INLINE size_t mie::Fp::size ( ) const
inline

Definition at line 253 of file zm2.h.

253{ return N; }

◆ square()

static void mie::Fp::square ( Fp & out,
const Fp & x )
inlinestatic

Definition at line 282 of file zm2.h.

282{ mul(out, x, x); }
Here is the caller graph for this function:

◆ squareRoot()

static bool mie::Fp::squareRoot ( Fp & y,
const Fp & x )
inlinestatic

Definition at line 297 of file zm2.h.

298 {
299 Fp t;
300 t = mie::power(x, p_add1_div4_);
301 if (t * t != x) return false;
302 y = t;
303 return true;
304 }
T power(const T &x, const S &y)
Definition zm.h:1389
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toMont()

static void mie::Fp::toMont ( Fp & y,
const Fp & x )
inlinestatic

Definition at line 143 of file zm2.h.

144 {
145 mul(y, x, montgomeryR2_);
146 }

◆ toString()

std::string mie::Fp::toString ( int base = 10) const
inline

Definition at line 250 of file zm2.h.

250{ return get().toString(base); }
mie::Vuint get() const
Definition zm2.h:158
std::string toString(int base=10) const
Definition zm.h:407
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Fp & x )
friend

Definition at line 116 of file zm2.h.

117 {
118 return os << x.toString(os.flags() & std::ios_base::hex ? 16 : 10);
119 }
std::string toString() const
Definition bn254_if.hpp:73
os_t os

◆ operator>>

std::istream & operator>> ( std::istream & is,
Fp & x )
friend

Definition at line 120 of file zm2.h.

121 {
122 std::string str;
123 mie::local::getDigits(is, str);
124 x.set(str);
125 return is;
126 }
void set(int x)
Definition bn254_if.hpp:68
return str
Definition CLI11.hpp:1359
std::istream & getDigits(std::istream &is, std::string &str, bool allowNegative=false)
Definition zm.h:84

Member Data Documentation

◆ add

void(* mie::Fp::add) (Fp &out, const Fp &x, const Fp &y)
static

Definition at line 83 of file zm2.h.

◆ addNC

void(* Fp::addNC)(Fp &out, const Fp &x, const Fp &y) = &Fp_addNC_C
static

Definition at line 86 of file zm2.h.

◆ halfTbl_

Fp * Fp::halfTbl_
static

Definition at line 270 of file zm2.h.

◆ mul

void(* mie::Fp::mul) (Fp &out, const Fp &x, const Fp &y)
static

Definition at line 93 of file zm2.h.

◆ neg

void(* Fp::neg)(Fp &out, const Fp &x) = &Fp_negC
static

Definition at line 92 of file zm2.h.

◆ preInv

int(* Fp::preInv)(Fp &r, const Fp &x)=0
static

Definition at line 94 of file zm2.h.

◆ shr1

void(* Fp::shr1)(Fp &out, const Fp &x)=0
static

Definition at line 88 of file zm2.h.

◆ shr2

void(* Fp::shr2)(Fp &out, const Fp &x)=0
static

Definition at line 89 of file zm2.h.

◆ sub

void(* mie::Fp::sub) (Fp &out, const Fp &x, const Fp &y)
static

Definition at line 91 of file zm2.h.

◆ subNC

void(* Fp::subNC)(Fp &out, const Fp &x, const Fp &y) = &Fp_subNC_C
static

Definition at line 87 of file zm2.h.


The documentation for this class was generated from the following files: