github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/FakeZZ.cpp (about)

     1  #include "FakeZZ.h"
     2  
     3  #include <assert.h>
     4  
     5  #if USE_NTL
     6  
     7  # include <NTL/ZZ.h>
     8  
     9  #else
    10  
    11  // independent of ZZ
    12  
    13  # include <iostream>
    14  # include <math.h>
    15  # include <stdexcept>
    16  
    17  using namespace std;
    18  
    19  # include <string.h>
    20  # include "edgamal.h"
    21  
    22  // dependent on ZZ
    23  
    24  # include <NTL/ZZ.h>
    25  
    26  # ifdef NTL_CLIENT
    27  #  undef NTL_CLIENT
    28  # endif
    29  # define NTL_CLIENT
    30  
    31  // TODO refactor this into better place
    32  
    33  int GROUPMOD = 1;
    34  int SUBGROUPMOD = 0;
    35  
    36  NTL::ZZ subgroup_order = NTL::conv<NTL::ZZ>("7237005577332262213973186563042994240857116359379907606001950938285454250989");
    37  // old one:         1257206741114416297422800737364843764556936223541
    38  // other old one:   2093940378184301311653365957372856779274958817946641127345598909177821235333110899157852449358735758089191470831461169154289110965924549400975552759536367817772197222736877807377880197200409316970791234520514702977005806082978079032920444679504632247059010175405894645810064101337094360118559702814823284408560044493630320638017495213077621340331881796467607713650957219938583
    39  
    40  ZZ old_gen = ZZ(NTL::conv<NTL::ZZ>("1929181099559129674691211513194785872536670409492790905276619913671396722443243145931673445424440902236760877484211441680348197072495215150053603001343967365713940597148603897520835948403066356627154482171157913975934174689003578096019980791028264452409955094293631742810957258379488668086855090084223965396993821991583550151470397960480522495500106360092070361350077271147228"));
    41  ZZ old_ord = ZZ(NTL::conv<NTL::ZZ>("1257206741114416297422800737364843764556936223541"));
    42  ZZ old_mod = ZZ(NTL::conv<NTL::ZZ>("2093940378184301311653365957372856779274958817946641127345598909177821235333110899157852449358735758089191470831461169154289110965924549400975552759536367817772197222736877807377880197200409316970791234520514702977005806082978079032920444679504632247059010175405894645810064101337094360118559702814823284408560044493630320638017495213077621340331881796467607713650957219938583"));
    43  
    44  
    45  // class stuff
    46  
    47  // either
    48  ZZ::ZZ() {
    49    is_initialized = false;
    50  }
    51  
    52  // TODO self-implemented; should never be publicly called
    53  ZZ::ZZ(NTL::ZZ xzz) {
    54    this->zz = xzz;
    55    is_initialized = true;
    56    is_scalar = true;
    57  }
    58  
    59  // TODO self-implemented; should never be publicly called
    60  NTL::ZZ ZZ::get() const {
    61    return this->zz;
    62  }
    63  
    64  // TODO self-implemented; should never be publicly called
    65  void ZZ::set(NTL::ZZ xzz) {
    66    this->zz = xzz;
    67    is_initialized = true;
    68    this->is_scalar = true;
    69  }
    70  
    71  ZZ::~ZZ() {}
    72  
    73  // both: Cipher_elg constructor uses point, Mod_p * and != uses number, Verifier_toom.check_challenge uses number
    74  bool ZZ::operator !=(const ZZ& b) const {
    75    assert(is_initialized);
    76    assert(b.is_initialized);
    77  
    78    assert(is_scalar && b.is_scalar);
    79  
    80    return zz != b.zz;
    81  }
    82  
    83  // both: Cipher_elg constructor uses point, Mod_p * and != uses number, Verifier_toom.check_challenge uses number
    84  bool ZZ::operator !=(const long b) const {
    85    assert(is_initialized);
    86    assert(is_scalar);
    87  
    88    return zz != b;
    89  }
    90  
    91  bool ZZ::operator ==(const ZZ& b) const {
    92    assert(is_initialized);
    93    assert(b.is_initialized);
    94  
    95    assert(is_scalar && b.is_scalar);
    96  
    97    return zz == b.zz;
    98  }
    99  
   100  // number: Mod_p uses to compare with 0
   101  bool ZZ::operator ==(const long b) const {
   102    assert(is_initialized);
   103    assert(is_scalar);
   104  
   105    return zz == b;
   106  }
   107  
   108  // either? func_pro uses to initialize matrix to 1, 2, 3, ..., N and initialize to 0, func_ver.check_Delta_op uses for some arithmetic t_3 = n*(i-1)+j and initialize to 1, Functions.bilinearMap uses to initialize to 0, multi_expo to initialize to 1, ...
   109  void ZZ::operator =(const long c) {
   110    zz = c;
   111    is_initialized = true;
   112    is_scalar = true;
   113  }
   114  void ZZ::operator =(const ZZ& c) {
   115    zz = c.zz;
   116    is_initialized = true;
   117  
   118    assert(c.is_scalar);
   119    if (!c.is_initialized) {
   120      this->is_scalar = true;
   121    } else {
   122      this->is_scalar = c.is_scalar;
   123    }
   124  }
   125  
   126  ostream& operator <<(ostream& os, const ZZ a) {
   127    assert(a.is_initialized);
   128    assert(a.is_scalar); // point serialization should go through point_serialize and point_deserialize
   129  
   130    return (os << a.zz);
   131  }
   132  istream& operator >>(istream& is, ZZ& x) {
   133    x.is_initialized = true;
   134    x.is_scalar = true;
   135  
   136    return (is >> x.zz);
   137  }
   138  
   139  // data format and representation
   140  
   141  ZZ to_ZZ(long val) {
   142    return ZZ(NTL::to_ZZ(val));
   143  }
   144  // number: called on everything (including 0, 1, 60, x, p, v, ...)
   145  ZZ to_ZZ(const ZZ& a) {
   146    assert(a.is_initialized);
   147    assert(a.is_scalar);
   148  
   149    return ZZ(NTL::to_ZZ(a.get()));   // TODO this is just the identity function
   150  }
   151  
   152  // number: (other than print statement for config) only ever called on order
   153  long NumBits(const ZZ& a) {
   154    assert(a.is_initialized);
   155    assert(a.is_scalar);
   156  
   157    return NTL::NumBits(a.get());
   158  }
   159  long NumBits(long a) {
   160    return NTL::NumBits(a);
   161  }
   162  
   163  // number: Utils.cpp uses point (not true?), Verifier_toom.cpp uses number
   164  ZZ ZZFromBytes(const unsigned char *p, long n) {
   165    return ZZ(NTL::ZZFromBytes(p, n));
   166  }
   167  
   168  // number: only used in multi_expo
   169  long bit(const ZZ& a, long k) {
   170    assert(a.is_initialized);
   171    assert(a.is_scalar);
   172  
   173    return NTL::bit(a.get(), k);
   174  }
   175  
   176  long bit(long a, long k) {
   177    return NTL::bit(a, k);
   178  }
   179  
   180  
   181  // arithmetic
   182  
   183  ZZ prevmod[30];
   184  int nextprevmod = 0;
   185  
   186  void CMPME(ZZ n) {
   187    return;
   188    //   int yes = 1;
   189    //   for (int i = 0; i < nextprevmod; i++) {
   190    //     if (prevmod[i].zz == n.zz) {
   191    //       yes = 0;
   192    //     }
   193    //   }
   194    //   if (yes) {
   195    //     prevmod[nextprevmod].zz = n.zz;
   196    //     nextprevmod++;
   197    //     cout << "[";
   198    //     for (int j = 0; j < nextprevmod; j++) {
   199    //       cout << prevmod[j].zz << ", ";
   200    //     }
   201    //     cout << "]" << endl;
   202    //   }
   203  }
   204  
   205  
   206  #include <assert.h>
   207  
   208  // number
   209  void AddMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n) { CMPME(n);
   210    assert(a.is_initialized);
   211    assert(b.is_initialized);
   212    assert(n.is_initialized);
   213  
   214    assert(a.is_scalar && b.is_scalar && n.is_scalar);
   215    assert(n.zz == old_ord.zz);
   216  
   217    NTL::ZZ xzz;
   218    NTL::AddMod(xzz, a.get(), b.get(), n.get());
   219    x.set(xzz);
   220  }
   221  void SubMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n) { CMPME(n);
   222    assert(a.is_initialized);
   223    assert(b.is_initialized);
   224    assert(n.is_initialized);
   225  
   226    assert(a.is_scalar && b.is_scalar && n.is_scalar);
   227    assert(n.zz == old_ord.zz);
   228  
   229    NTL::ZZ xzz;
   230    NTL::SubMod(xzz, a.get(), b.get(), n.get());
   231    x.set(xzz);
   232  }
   233  void NegateMod(ZZ& x, const ZZ& a, const ZZ& n) { CMPME(n);
   234    assert(a.is_initialized);
   235    assert(n.is_initialized);
   236  
   237    assert(a.is_scalar && n.is_scalar);
   238    assert(n.zz == old_ord.zz);
   239  
   240    NTL::ZZ xzz;
   241    NTL::NegateMod(xzz, a.get(), n.get());
   242    x.set(xzz);
   243  }
   244  
   245  // both
   246  void MulMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n) { CMPME(n);
   247    assert(a.is_initialized);
   248    assert(b.is_initialized);
   249    assert(n.is_initialized);
   250  
   251    assert(a.is_scalar && b.is_scalar);
   252    if (a.is_scalar) {
   253      assert(n.zz == old_ord.zz);
   254    } else {
   255      assert(n.zz == old_mod.zz);
   256    }
   257  
   258    bool scalar_flag = a.is_scalar;
   259    NTL::ZZ xzz;
   260    NTL::MulMod(xzz, a.get(), b.get(), n.get());
   261    x.set(xzz);
   262    x.is_scalar = scalar_flag;
   263  }
   264  // number
   265  void MulMod(ZZ& x, const ZZ& a, long b, const ZZ& n) { CMPME(n);
   266    assert(a.is_initialized);
   267    assert(n.is_initialized);
   268  
   269    assert(a.is_scalar);
   270    if (a.is_scalar) {
   271      assert(n.zz == old_ord.zz);
   272    } else {
   273      assert(n.zz == old_mod.zz);
   274    }
   275  
   276    NTL::ZZ xzz;
   277    NTL::MulMod(xzz, a.get(), b, n.get());
   278    x.set(xzz);
   279  }
   280  // both (but point only used during verification while checking E)
   281  ZZ MulMod(const ZZ& a, const ZZ& b, const ZZ& n) { CMPME(n);
   282    assert(a.is_initialized);
   283    assert(b.is_initialized);
   284    assert(n.is_initialized);
   285  
   286    assert(a.is_scalar && b.is_scalar);
   287    if (a.is_scalar) {
   288      assert(n.zz == old_ord.zz);
   289    } else {
   290      assert(n.zz == old_mod.zz);
   291    }
   292  
   293    ZZ ret = ZZ(NTL::MulMod(a.get(), b.get(), n.get()));
   294    ret.is_scalar = a.is_scalar;
   295    return ret;
   296  }
   297  
   298  // point: only used in multi_expo
   299  void SqrMod(ZZ& x, const ZZ& a, const ZZ& n) { CMPME(n);
   300    assert(a.is_initialized);
   301    assert(n.is_initialized);
   302  
   303    assert(a.is_scalar);
   304    assert(n.zz == old_mod.zz);
   305  
   306    bool scalar_flag = a.is_scalar;
   307    NTL::ZZ xzz;
   308    NTL::SqrMod(xzz, a.get(), n.get());
   309    x.set(xzz);
   310    x.is_scalar = scalar_flag;
   311  }
   312  
   313  // point: only used in Pedersen precomp
   314  ZZ sqr(const ZZ& a) {
   315    assert(a.is_initialized);
   316  
   317    ZZ ret = ZZ(NTL::sqr(a.get()));
   318    ret.is_scalar = a.is_scalar;
   319    return ret;
   320  }
   321  
   322  // point
   323  void PowerMod(ZZ& x, const ZZ& a, const ZZ& e, const ZZ& n) { CMPME(n);
   324    assert(a.is_initialized);
   325    assert(e.is_initialized);
   326    assert(n.is_initialized);
   327  
   328    assert(e.is_scalar);
   329  
   330    assert(a.is_scalar);
   331    assert(n.zz == old_mod.zz);
   332  
   333    bool scalar_flag = a.is_scalar;
   334    NTL::ZZ xzz;
   335    NTL::PowerMod(xzz, a.get(), e.get(), n.get());
   336    x.set(xzz);
   337    x.is_scalar = scalar_flag;
   338  }
   339  // point
   340  void PowerMod(ZZ& x, const ZZ& a, long e, const ZZ& n) { CMPME(n);
   341    assert(a.is_initialized);
   342    assert(n.is_initialized);
   343  
   344    assert(a.is_scalar);
   345    assert(n.zz == old_mod.zz);
   346  
   347    bool scalar_flag = a.is_scalar;
   348    NTL::ZZ xzz;
   349    NTL::PowerMod(xzz, a.get(), e, n.get());
   350    x.set(xzz);
   351    x.is_scalar = scalar_flag;
   352  }
   353  // point: only used in G_q and ElGammal
   354  ZZ PowerMod(const ZZ& a, const ZZ& e, const ZZ& n) { CMPME(n);
   355    assert(a.is_initialized);
   356    assert(e.is_initialized);
   357    assert(n.is_initialized);
   358  
   359    assert(a.is_scalar);
   360    assert(e.is_scalar);
   361  
   362    assert(n.zz == old_mod.zz);
   363  
   364    ZZ ret = ZZ(NTL::PowerMod(a.get(), e.get(), n.get()));
   365    ret.is_scalar = a.is_scalar;
   366    return ret;
   367  }
   368  
   369  // both
   370  void InvMod(ZZ& x, const ZZ& a, const ZZ& n) { CMPME(n);
   371    assert(a.is_initialized);
   372    assert(n.is_initialized);
   373  
   374    if (a.is_scalar) {
   375      assert(n.zz == old_ord.zz);
   376    } else {
   377      assert(n.zz == old_mod.zz);
   378    }
   379  
   380    bool scalar_flag = a.is_scalar;
   381    NTL::ZZ xzz;
   382    NTL::InvMod(xzz, a.get(), n.get());
   383    x.set(xzz);
   384    x.is_scalar = scalar_flag;
   385  }
   386  
   387  // point: only used in ElGammal
   388  ZZ InvMod(const ZZ& a, const ZZ& n) { CMPME(n);
   389    assert(a.is_initialized);
   390    assert(n.is_initialized);
   391  
   392    assert(a.is_scalar);
   393  
   394    assert(n.zz == old_mod.zz);
   395  
   396    ZZ ret = ZZ(NTL::InvMod(a.get(), n.get()));
   397    ret.is_scalar = a.is_scalar;
   398    return ret;
   399  }
   400  
   401  // number: only used in Verifier_toom
   402  ZZ operator%(const ZZ& a, const ZZ& b) {
   403    assert(a.is_initialized);
   404    assert(b.is_initialized);
   405  
   406    assert(a.is_scalar && b.is_scalar);
   407  
   408    return ZZ(a.get() % b.get());
   409  }
   410  
   411  // randomness
   412  
   413  // number: only ever called on ord (and for permutation)
   414  ZZ RandomBnd(const ZZ& n) { // CMPME(n);
   415    assert(n.is_initialized);
   416  
   417    assert(n.is_scalar);
   418  
   419    ZZ ret = ZZ(NTL::RandomBnd(n.get()));
   420    return ret;
   421  }
   422  
   423  // number: only called to get permutation
   424  long RandomBnd(long n) {
   425    return NTL::RandomBnd(n);
   426  }
   427  
   428  #endif