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

     1  /* Use limited-size elliptic curve point scalars? */
     2  #define USE_NTL 1
     3  
     4  #if USE_NTL
     5  
     6  # include <NTL/ZZ.h>
     7  
     8  #else
     9  
    10  # ifndef FAKE_ZZ_H
    11  # define FAKE_ZZ_H
    12  
    13  // independent of ZZ
    14  
    15  # include <iostream>
    16  # include <math.h>
    17  # include <stdexcept>
    18  
    19  using namespace std;
    20  
    21  long bit(long a, long k);
    22  
    23  # include "edgamal.h"
    24  
    25  // dependent on ZZ
    26  
    27  # include <NTL/ZZ.h>
    28  # ifdef NTL_CLIENT
    29  #  undef NTL_CLIENT
    30  # endif
    31  # define NTL_CLIENT
    32  
    33  // class stuff
    34  
    35  // either
    36  class ZZ { // TODO need to fix public NTL members either via pointer or via clever include
    37   public:
    38    ZZ();
    39    ~ZZ();
    40  
    41    bool operator !=(const ZZ& b) const;
    42    bool operator !=(const long b) const;
    43  
    44    bool operator ==(const ZZ& b) const;
    45    bool operator ==(const long b) const;
    46  
    47    void operator =(const long c);
    48    void operator =(const ZZ& c);
    49  
    50    friend ostream& operator <<(ostream& os, const ZZ a);
    51    friend istream& operator >>(istream& is, ZZ& x);
    52  
    53  
    54    // TODO self-implemented; should never be publicly called
    55    ZZ(NTL::ZZ zz);
    56    NTL::ZZ get() const;
    57    void set(NTL::ZZ zz);
    58  
    59    bool is_scalar;
    60    bool is_initialized;
    61  
    62    NTL::ZZ zz;
    63    curve_point P;
    64   //   private:
    65   //    NTL::ZZ zz;
    66  };
    67  
    68  // data format and representation
    69  
    70  ZZ to_ZZ(long val);
    71  ZZ to_ZZ(const ZZ& a); // TODO this is just the identity function
    72  
    73  long NumBits(const ZZ& a);
    74  long NumBits(long a);
    75  
    76  ZZ ZZFromBytes(const unsigned char *p, long n);
    77  
    78  long bit(const ZZ& a, long k); // TODO this is used for multi_expo -- need to know if replaceable
    79  
    80  // arithmetic
    81  
    82  void AddMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a+b)%n
    83  void SubMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a-b)%n
    84  void NegateMod(ZZ& x, const ZZ& a, const ZZ& n); // x = -a % n
    85  
    86  void MulMod(ZZ& x, const ZZ& a, const ZZ& b, const ZZ& n); // x = (a*b)%n
    87  void MulMod(ZZ& x, const ZZ& a, long b, const ZZ& n); // TODO beware of long to point conversion
    88  ZZ MulMod(const ZZ& a, const ZZ& b, const ZZ& n);
    89  
    90  void SqrMod(ZZ& x, const ZZ& a, const ZZ& n); // x = a^2 % n
    91  ZZ sqr(const ZZ& a);
    92  
    93  void PowerMod(ZZ& x, const ZZ& a, const ZZ& e, const ZZ& n);
    94  void PowerMod(ZZ& x, const ZZ& a, long e, const ZZ& n); // TODO beware of long to point conversion
    95  ZZ PowerMod(const ZZ& a, const ZZ& e, const ZZ& n);
    96  
    97  void InvMod(ZZ& x, const ZZ& a, const ZZ& n);
    98  ZZ InvMod(const ZZ& a, const ZZ& n);
    99  
   100  ZZ operator%(const ZZ& a, const ZZ& b);
   101  
   102  // randomness
   103  
   104  ZZ RandomBnd(const ZZ& n);
   105  
   106  long RandomBnd(long n);
   107  
   108  # endif
   109  
   110  #endif