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

     1  #include "CurvePoint.h"
     2  
     3  #include "edgamal.h"
     4  #include <string.h>
     5  
     6  #include "assert.h"
     7  
     8  #include "FakeZZ.h"
     9  NTL_CLIENT
    10  
    11  const ZZ ord = ZZ(NTL::conv<NTL::ZZ>("7237005577332262213973186563042994240857116359379907606001950938285454250989"));
    12  
    13  // TODO: ensure memory management is OK with switch to pointers
    14  
    15  // helpers
    16  
    17  void ScalarFromZZ(edgamal_curve_scalar s, const ZZ& x) {
    18    // normalize first
    19    ZZ n = x;
    20    BytesFromZZ(s, n, EDGAMAL_CURVE_SCALAR_SIZE);
    21   // std::cout << "scalar from zz, n =" << n << " ,s = " << s << std::endl;
    22  }
    23  
    24  // interface fns
    25  
    26  CurvePoint::CurvePoint() {
    27  #if USE_REAL_POINTS
    28    // TODO what should we do? leave uninitialized?
    29    // memset(P, 0, EDGAMAL_CURVE_POINT_SIZE);
    30  #else
    31    // pass
    32  #endif
    33  }
    34  
    35  CurvePoint::CurvePoint(const CurvePoint &c)
    36  #if !USE_REAL_POINTS
    37    : zz(c.zz) {}
    38  #else
    39    {edgamal_copy_point(&P, &c.P);}
    40  #endif
    41  
    42  CurvePoint::~CurvePoint() {}
    43  
    44  // both: Cipher_elg constructor uses point, Mod_p * and != uses number, Verifier_toom.check_challenge uses number
    45  bool CurvePoint::operator !=(const CurvePoint& b) const {
    46  #if USE_REAL_POINTS
    47    return edgamal_compare_points(&P, &b.P) != 0;
    48  #else
    49    return zz != b.zz;
    50  #endif
    51  }
    52  
    53  bool CurvePoint::operator ==(const CurvePoint& b) const {
    54  #if USE_REAL_POINTS
    55    return edgamal_compare_points(&P, &b.P) == 0;
    56  #else
    57    return zz == b.zz;
    58  #endif
    59  }
    60  
    61  void CurvePoint::operator =(const CurvePoint& c) {
    62  #if USE_REAL_POINTS
    63    edgamal_copy_point(&P, &c.P);
    64  #else
    65    zz = c.zz;
    66  #endif
    67  }
    68  
    69  // these tend to be used in the NIZK proof,
    70  // while (de)serialize tend to be used in encryption/decryption
    71  ostream& operator <<(ostream& os, const CurvePoint point) {
    72  #if USE_REAL_POINTS
    73    ZZ x;
    74    uint8_t t[128];
    75    edgamal_serialize_point(t, &point.P);
    76    ZZFromBytes(x, t, 128);
    77    os << x;
    78  #else
    79    os << point.zz;
    80  #endif
    81    return os;
    82  }
    83  istream& operator >>(istream& is, CurvePoint& point) {
    84  #if USE_REAL_POINTS
    85    ZZ x;
    86    uint8_t t[128];
    87    is >> x;
    88    BytesFromZZ(t, x, 128);
    89    edgamal_deserialize_point(&point.P, t);
    90  #else
    91    is >> point.zz;
    92  #endif
    93    return is;
    94  }
    95  
    96  void CurvePoint::serialize_canonical(char* p) {
    97  #if USE_REAL_POINTS
    98    edgamal_compress_point((uint8_t*) p, &P);
    99  #else
   100    assert(false);
   101  #endif
   102  }
   103  void CurvePoint::serialize(char *str) {
   104  #if USE_REAL_POINTS
   105  # if (CURVE_POINT_BYTESIZE == 128)
   106    edgamal_serialize_point((uint8_t*) str, &P);
   107  # elif (CURVE_POINT_BYTESIZE == 32)
   108    edgamal_compress_point((uint8_t*) str, &P);
   109  # else
   110    assert(false);
   111  # endif
   112  #else
   113    assert(false);
   114  #endif
   115  }
   116  void CurvePoint::deserialize(const char *str) {
   117  #if USE_REAL_POINTS
   118  # if (CURVE_POINT_BYTESIZE == 128)
   119    edgamal_deserialize_point(&P, (uint8_t*) str);
   120  # elif (CURVE_POINT_BYTESIZE == 32)
   121    edgamal_decompress_point(&P, (uint8_t*) str);
   122  # else
   123    assert(false);
   124  # endif
   125  #else
   126    assert(false);
   127  #endif
   128  }
   129  
   130  void MulMod(CurvePoint& x, const CurvePoint& a, const CurvePoint& b, const ZZ& n) {
   131  #if USE_REAL_POINTS
   132    edgamal_add_points(&x.P, &a.P, &b.P);
   133  #else
   134  # if USE_NTL
   135    NTL::MulMod(x.zz, a.zz, b.zz, n);
   136  # else
   137    NTL::MulMod(x.zz, a.zz, b.zz, n.zz);
   138  # endif
   139  #endif
   140  }
   141  CurvePoint MulMod(const CurvePoint& a, const CurvePoint& b, const ZZ& n) {
   142    CurvePoint x;
   143  #if USE_REAL_POINTS
   144    edgamal_add_points(&x.P, &a.P, &b.P);
   145  #else
   146  # if USE_NTL
   147    NTL::MulMod(x.zz, a.zz, b.zz, n);
   148  # else
   149    NTL::MulMod(x.zz, a.zz, b.zz, n.zz);
   150  # endif
   151  #endif
   152    return x;
   153  }
   154  
   155  void SqrMod(CurvePoint& x, const CurvePoint& a, const ZZ& n) {
   156  #if USE_REAL_POINTS
   157    edgamal_add_points(&x.P, &a.P, &a.P);
   158  #else
   159  # if USE_NTL
   160    NTL::SqrMod(x.zz, a.zz, n);
   161  # else
   162    NTL::SqrMod(x.zz, a.zz, n.zz);
   163  # endif
   164  #endif
   165  }
   166  CurvePoint sqr(const CurvePoint& a) {
   167    CurvePoint x;
   168  #if USE_REAL_POINTS
   169    edgamal_add_points(&x.P, &a.P, &a.P);
   170  #else
   171    NTL::sqr(a.zz);
   172  #endif
   173    return x;
   174  }
   175  
   176  void PowerMod(CurvePoint& x, const CurvePoint& a, const ZZ& e, const ZZ& n) {
   177  #if USE_REAL_POINTS
   178    edgamal_curve_scalar s = {0};
   179    ScalarFromZZ(s, e);
   180    edgamal_scalar_multiply_point(&x.P, &a.P, s);
   181  #else
   182  # if USE_NTL
   183    NTL::PowerMod(x.zz, a.zz, e, n);
   184  # else
   185    NTL::PowerMod(x.zz, a.zz, e.zz, n.zz);
   186  # endif
   187  #endif
   188  }
   189  void PowerMod(CurvePoint& x, const CurvePoint& a, long e, const ZZ& n) {
   190  #if USE_REAL_POINTS
   191    edgamal_curve_scalar s = {0};
   192    ScalarFromZZ(s, to_ZZ(e));
   193    edgamal_scalar_multiply_point(&x.P, &a.P, s);
   194  #else
   195  # if USE_NTL
   196    NTL::PowerMod(x.zz, a.zz, e, n);
   197  # else
   198    NTL::PowerMod(x.zz, a.zz, e, n.zz);
   199  # endif
   200  #endif
   201  }
   202  CurvePoint PowerMod(const CurvePoint& a, const ZZ& e, const ZZ& n) {
   203    CurvePoint x;
   204  #if USE_REAL_POINTS
   205    edgamal_curve_scalar s = {0};
   206    ScalarFromZZ(s, e);
   207    edgamal_scalar_multiply_point(&x.P, &a.P, s);
   208  #else
   209  # if USE_NTL
   210    NTL::PowerMod(x.zz, a.zz, e, n);
   211  # else
   212    NTL::PowerMod(x.zz, a.zz, e.zz, n.zz);
   213  # endif
   214  #endif
   215    return x;
   216  }
   217  
   218  void basepoint_scalarmult(CurvePoint& x, const ZZ& e) {
   219  #if USE_REAL_POINTS
   220    edgamal_curve_scalar s = {0};
   221    ScalarFromZZ(s, e);
   222    edgamal_scalar_multiply_basepoint(&x.P, s);
   223  #else
   224    assert(false);
   225  #endif
   226  }
   227  
   228  void InvMod(CurvePoint& x, const CurvePoint& a, const ZZ& n) {
   229  #if USE_REAL_POINTS
   230    edgamal_negate_point(&x.P, &a.P);
   231  #else
   232  # if USE_NTL
   233    NTL::InvMod(x.zz, a.zz, n);
   234  # else
   235    NTL::InvMod(x.zz, a.zz, n.zz);
   236  # endif
   237  #endif
   238  }
   239  CurvePoint InvMod(const CurvePoint& a, const ZZ& n) {
   240    CurvePoint x;
   241  #if USE_REAL_POINTS
   242    edgamal_negate_point(&x.P, &a.P);
   243  #else
   244  # if USE_NTL
   245    NTL::InvMod(x.zz, a.zz, n);
   246  # else
   247    NTL::InvMod(x.zz, a.zz, n.zz);
   248  # endif
   249  #endif
   250    return x;
   251  }
   252  
   253  // fixes
   254  CurvePoint zz_to_curve_pt(NTL::ZZ a) {
   255    CurvePoint x;
   256  #if USE_REAL_POINTS
   257    assert(false);
   258  #else
   259    x.zz = NTL::ZZ(a);
   260  #endif
   261    return x;
   262  }
   263  
   264  CurvePoint curve_zeropoint() {
   265    CurvePoint x;
   266  #if USE_REAL_POINTS
   267    edgamal_copy_point(&x.P, &edgamal_zeropoint);
   268  #else
   269    x.zz = NTL::ZZ(1);
   270  #endif
   271    return x;
   272  }
   273  CurvePoint curve_basepoint() {
   274    CurvePoint x;
   275  #if USE_REAL_POINTS
   276    edgamal_copy_point(&x.P, &edgamal_basepoint);
   277  #else
   278    assert(false);
   279  #endif
   280    return x;
   281  }
   282  CurvePoint raw_curve_pt(const uint8_t p[32]) {
   283    CurvePoint x;
   284  #if USE_REAL_POINTS
   285    edgamal_decompress_point(&x.P, p);
   286  #else
   287    assert(false);
   288  #endif
   289    return x;
   290  }