relic-toolkit / relic

Code
Other
452 stars 179 forks source link

C++ operator overloading with types from relic #233

Open ghoshbishakh opened 2 years ago

ghoshbishakh commented 2 years ago

I am trying to use relic in a C++ project (MPSPDZ). In a class with members of type gt_t, I am trying to overload some operators. For example, to assign gt_t gtpoint, we need to use gt_copy. However gt_copy expects a non constant pointer but for operator overloading we will always get a const.

Two questions:

  1. Will changing the argument type to const in gt_copy affect relic in any unwanted way?
  2. From the macro gt_copy how to determine which function is going to be used, is there any documentation on that?
  3. Is there any possible trick to do this without changes in relic?
GtElement& GtElement::operator =(const GtElement& other)
{
    gt_copy(gtpoint, other.gtpoint); // other.gtpoint is a non constant pointer 
    return *this;
}
ghoshbishakh commented 2 years ago

This is my current hack:

GtElement& GtElement::operator =(const GtElement& other)
{
    gt_t tmp;
    gt_null(tmp);
    gt_new(tmp);
    memcpy(tmp, other.gtpoint, sizeof(other.gtpoint));
    gt_copy(gtpoint, tmp);
    gt_free(tmp);
    return *this;
}
dfaranha commented 2 years ago

Thanks for the notification+details!

If you are using the BLS12-381 preset or the default configuration, then gt_copy is defined to be fp12_copy. I had tried enforcing const correctness for the extension field types a few years ago, but failed somehow.

Would that simple change fix the situation?

ghoshbishakh commented 2 years ago

It should definitely fix the problem. Currently, the way around I mentioned above is working fine it seems. There is another issue #234 which caused a major headache.

Thank you so much for your support and this awsm project!