Home | History | Annotate | Download | only in src
      1 /*############################################################################
      2 # Copyright 2017 Intel Corporation
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 ############################################################################*/
     16 /// Implementation of Fq2 math
     17 /*! \file */
     18 
     19 #include "epid/member/tiny/math/fq2.h"
     20 
     21 #include "epid/member/tiny/math/fq.h"
     22 #include "epid/member/tiny/math/mathtypes.h"
     23 
     24 void Fq2Cp(Fq2Elem* result, Fq2Elem const* in) {
     25   FqCp(&(result->x0), &(in->x0));
     26   FqCp(&(result->x1), &(in->x1));
     27 }
     28 
     29 void Fq2Set(Fq2Elem* result, uint32_t in) {
     30   FqSet(&(result->x0), in);
     31   FqClear(&(result->x1));
     32 }
     33 
     34 void Fq2Clear(Fq2Elem* result) {
     35   FqClear(&result->x0);
     36   FqClear(&result->x1);
     37 }
     38 
     39 void Fq2Add(Fq2Elem* result, Fq2Elem const* left, Fq2Elem const* right) {
     40   FqAdd(&(result->x0), &(left->x0), &(right->x0));
     41   FqAdd(&(result->x1), &(left->x1), &(right->x1));
     42 }
     43 
     44 void Fq2Exp(Fq2Elem* result, Fq2Elem const* base, VeryLargeInt const* exp) {
     45   int i, j;
     46   Fq2Elem tmp;
     47   Fq2Elem tmp2;
     48   Fq2Elem* temp = &tmp;
     49   Fq2Elem* temp2 = &tmp2;
     50   FqSet(&(temp->x0), 1);
     51   FqClear(&(temp->x1));
     52   for (i = NUM_ECC_DIGITS - 1; i >= 0; i--) {
     53     for (j = 31; j >= 0; j--) {
     54       Fq2Square(temp, temp);
     55       Fq2Mul(temp2, temp, base);
     56 
     57       Fq2CondSet(temp, temp2, temp, (int)((exp->word[i] >> j) & (0x1)));
     58     }
     59   }
     60   Fq2Cp(result, temp);
     61 }
     62 
     63 void Fq2Sub(Fq2Elem* result, Fq2Elem const* left, Fq2Elem const* right) {
     64   FqSub(&(result->x0), &(left->x0), &(right->x0));
     65   FqSub(&(result->x1), &(left->x1), &(right->x1));
     66 }
     67 
     68 void Fq2Mul(Fq2Elem* result, Fq2Elem const* left, Fq2Elem const* right) {
     69   FqElem A;
     70   FqElem B;
     71   FqElem* a = &A;
     72   FqElem* b = &B;
     73 
     74   FqAdd(a, &left->x0, &left->x1);
     75   FqAdd(b, &right->x0, &right->x1);
     76   FqMul(a, a, b);
     77   FqMul(&result->x0, &left->x0, &right->x0);
     78   FqSub(a, a, &result->x0);
     79   FqMul(b, &left->x1, &right->x1);
     80   FqSub(&result->x1, a, b);
     81   FqNeg(b, b);  // b = b*beta
     82   FqAdd(&result->x0, &result->x0, b);
     83 }
     84 
     85 void Fq2Inv(Fq2Elem* result, Fq2Elem const* in) {
     86   FqElem tmp;
     87   FqElem tmp2;
     88   FqElem* temp = &tmp;
     89   FqElem* temp2 = &tmp2;
     90   FqSquare(temp, &in->x1);
     91   FqSquare(temp2, &in->x0);
     92   FqAdd(temp, temp, temp2);
     93   FqInv(temp, temp);
     94   FqMul(&result->x0, temp, &in->x0);
     95   FqNeg(temp, temp);
     96   FqMul(&result->x1, temp, &in->x1);
     97 }
     98 
     99 void Fq2Neg(Fq2Elem* result, Fq2Elem const* in) {
    100   FqNeg(&(result->x0), &(in->x0));
    101   FqNeg(&(result->x1), &(in->x1));
    102 }
    103 
    104 void Fq2Conj(Fq2Elem* result, Fq2Elem const* in) {
    105   FqCp(&result->x0, &in->x0);
    106   FqNeg(&result->x1, &in->x1);
    107 }
    108 
    109 void Fq2Square(Fq2Elem* result, Fq2Elem const* in) {
    110   FqElem tmpa;
    111   FqElem* temp_a = &tmpa;
    112   FqElem tmpb;
    113   FqElem* temp_b = &tmpb;
    114   FqAdd(temp_a, &in->x0, &in->x1);
    115   FqMul(temp_b, &in->x0, &in->x1);
    116   FqSub(&result->x0, &in->x0, &in->x1);
    117   FqMul(&result->x0, temp_a, &result->x0);
    118   FqAdd(&result->x1, temp_b, temp_b);
    119 }
    120 
    121 void Fq2MulScalar(Fq2Elem* result, Fq2Elem const* left, FqElem const* right) {
    122   FqMul(&(result->x0), &(left->x0), right);
    123   FqMul(&(result->x1), &(left->x1), right);
    124 }
    125 
    126 void Fq2CondSet(Fq2Elem* result, Fq2Elem const* true_val,
    127                 Fq2Elem const* false_val, int truth_val) {
    128   FqCondSet(&(result->x0), &(true_val->x0), &(false_val->x0), truth_val);
    129   FqCondSet(&(result->x1), &(true_val->x1), &(false_val->x1), truth_val);
    130 }
    131 
    132 int Fq2Eq(Fq2Elem const* left, Fq2Elem const* right) {
    133   return FqEq(&(left->x0), &(right->x0)) && FqEq(&(left->x1), &(right->x1));
    134 }
    135 
    136 void Fq2MulXi(Fq2Elem* result, Fq2Elem const* in) {
    137   // has the same effect as Fq2Mul(result, in, &Fq2xi) with better speed, low
    138   // space;
    139   FqElem tmp;
    140   FqElem* temp = &tmp;
    141   FqAdd(temp, &in->x0, &in->x0);
    142   FqSub(temp, temp, &in->x1);
    143   FqAdd(&result->x1, &in->x1, &in->x1);
    144   FqAdd(&result->x1, &result->x1, &in->x0);
    145   FqCp(&result->x0, temp);
    146 }
    147 
    148 int Fq2IsZero(Fq2Elem const* value) {
    149   return FqIsZero(&value->x0) && FqIsZero(&value->x1);
    150 }
    151