Home | History | Annotate | Download | only in math
      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 /// Definition of Large Integer math
     17 /*! \file */
     18 
     19 #ifndef EPID_MEMBER_TINY_MATH_VLI_H_
     20 #define EPID_MEMBER_TINY_MATH_VLI_H_
     21 
     22 #include <stdint.h>
     23 #include "epid/common/bitsupplier.h"
     24 /// \cond
     25 typedef struct VeryLargeInt VeryLargeInt;
     26 typedef struct VeryLargeIntProduct VeryLargeIntProduct;
     27 /// \endcond
     28 
     29 /// Add two large integers.
     30 /*!
     31 \param[out] result target.
     32 \param[in] left The first operand to be added.
     33 \param[in] right The second operand to be added.
     34 
     35 \returns the carry portion of the addition.
     36 */
     37 uint32_t VliAdd(VeryLargeInt* result, VeryLargeInt const* left,
     38                 VeryLargeInt const* right);
     39 
     40 /// Multiply two large integers.
     41 /*!
     42 \param[out] result of multiplying left and right.
     43 \param[in] left The first operand to be multiplied.
     44 \param[in] right The second operand to be multiplied.
     45 */
     46 void VliMul(VeryLargeIntProduct* result, VeryLargeInt const* left,
     47             VeryLargeInt const* right);
     48 
     49 /// Right shift a large integers.
     50 /*!
     51 \param[out] result target.
     52 \param[in] in The value to be shifted.
     53 \param[in] shift The number of bits to shift.
     54 */
     55 void VliRShift(VeryLargeInt* result, VeryLargeInt const* in, uint32_t shift);
     56 
     57 /// Subtract two large integers.
     58 /*!
     59 \param[out] result target.
     60 \param[in] left The operand to be subtracted from.
     61 \param[in] right The operand to subtract.
     62 \returns 1 on success, 0 on failure
     63 */
     64 uint32_t VliSub(VeryLargeInt* result, VeryLargeInt const* left,
     65                 VeryLargeInt const* right);
     66 
     67 /// Set a large integer's value.
     68 /*!
     69 \param[out] result target.
     70 \param[in] in value to set.
     71 */
     72 void VliSet(VeryLargeInt* result, VeryLargeInt const* in);
     73 
     74 /// Clear a large integer's value.
     75 /*!
     76 \param[out] result value to clear.
     77 */
     78 void VliClear(VeryLargeInt* result);
     79 
     80 /// Test if a large integer is zero.
     81 /*!
     82 \param[in] in the value to test.
     83 \returns A value different from zero (i.e., true) if indeed
     84          the value is zero. Zero (i.e., false) otherwise.
     85 */
     86 int VliIsZero(VeryLargeInt const* in);
     87 
     88 /// Conditionally Set a large inter's value to one of two values.
     89 /*!
     90 \param[out] result target.
     91 \param[in] true_val value to set if condition is true.
     92 \param[in] false_val value to set if condition is false.
     93 \param[in] truth_val value of condition.
     94 */
     95 void VliCondSet(VeryLargeInt* result, VeryLargeInt const* true_val,
     96                 VeryLargeInt const* false_val, int truth_val);
     97 
     98 /// Test the value of a bit in a large integer.
     99 /*!
    100 \param[in] in the value to test.
    101 \param[in] bit the bit index.
    102 
    103 \returns value of the bit (1 or 0).
    104 */
    105 uint32_t VliTestBit(VeryLargeInt const* in, uint32_t bit);
    106 
    107 /// Generate a random large integer.
    108 /*!
    109 \param[in] result the random value.
    110 \param[in] rnd_func Random number generator.
    111 \param[in] rnd_param Pass through context data for rnd_func.
    112 \returns A value different from zero (i.e., true) if on success.
    113          Zero (i.e., false) otherwise.
    114 */
    115 int VliRand(VeryLargeInt* result, BitSupplier rnd_func, void* rnd_param);
    116 
    117 /// compare two large integers.
    118 /*!
    119 \param[in] left the left hand value.
    120 \param[in] right the right hand value.
    121 
    122 \returns the sign of left - right
    123 */
    124 int VliCmp(VeryLargeInt const* left, VeryLargeInt const* right);
    125 
    126 /// Add two large integers modulo a value.
    127 /*!
    128 \param[out] result target.
    129 \param[in] left The first operand to be added.
    130 \param[in] right The second operand to be added.
    131 \param[in] mod The modulo.
    132 */
    133 void VliModAdd(VeryLargeInt* result, VeryLargeInt const* left,
    134                VeryLargeInt const* right, VeryLargeInt const* mod);
    135 
    136 /// Subtract two large integers modulo a value.
    137 /*!
    138 \param[out] result target.
    139 \param[in] left The operand to be subtracted from.
    140 \param[in] right The operand to subtract.
    141 \param[in] mod The modulo.
    142 */
    143 void VliModSub(VeryLargeInt* result, VeryLargeInt const* left,
    144                VeryLargeInt const* right, VeryLargeInt const* mod);
    145 
    146 /// Multiply two large integers modulo a value.
    147 /*!
    148 \param[out] result target.
    149 \param[in] left The first operand to be multiplied.
    150 \param[in] right The second operand to be multiplied.
    151 \param[in] mod The modulo.
    152 */
    153 void VliModMul(VeryLargeInt* result, VeryLargeInt const* left,
    154                VeryLargeInt const* right, VeryLargeInt const* mod);
    155 
    156 /// Exponentiate a large integer modulo a value.
    157 /*!
    158 \param[out] result target.
    159 \param[in] base the base.
    160 \param[in] exp the exponent.
    161 \param[in] mod The modulo.
    162 */
    163 void VliModExp(VeryLargeInt* result, VeryLargeInt const* base,
    164                VeryLargeInt const* exp, VeryLargeInt const* mod);
    165 
    166 /// Invert  a large integer modulo a value.
    167 /*!
    168 \param[out] result target.
    169 \param[in] input the value to invert.
    170 \param[in] mod The modulo.
    171 */
    172 void VliModInv(VeryLargeInt* result, VeryLargeInt const* input,
    173                VeryLargeInt const* mod);
    174 
    175 /// Square a large integer modulo a value.
    176 /*!
    177 \param[out] result target.
    178 \param[in] input the base.
    179 \param[in] mod The modulo.
    180 */
    181 void VliModSquare(VeryLargeInt* result, VeryLargeInt const* input,
    182                   VeryLargeInt const* mod);
    183 
    184 /// Reduce a value to a modulo.
    185 /*!
    186 \param[out] result target.
    187 \param[in] input the base.
    188 \param[in] mod The modulo.
    189 
    190 \warning This function makes significant assumptions about
    191 the range of values input
    192 */
    193 void VliModBarrett(VeryLargeInt* result, VeryLargeIntProduct const* input,
    194                    VeryLargeInt const* mod);
    195 
    196 #endif  // EPID_MEMBER_TINY_MATH_VLI_H_
    197