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 Fp math 17 /*! \file */ 18 19 #ifndef EPID_MEMBER_TINY_MATH_FP_H_ 20 #define EPID_MEMBER_TINY_MATH_FP_H_ 21 22 #include <stddef.h> 23 #include <stdint.h> 24 #include "epid/common/bitsupplier.h" 25 26 /// \cond 27 typedef struct FpElem FpElem; 28 typedef struct VeryLargeInt VeryLargeInt; 29 /// \endcond 30 31 /// Test if an element is in Fp 32 /*! 33 \param[in] in the element to test 34 \returns A value different from zero (i.e., true) indeed 35 the value is in the field. Zero (i.e., false) otherwise. 36 */ 37 int FpInField(FpElem const* in); 38 39 /// Add two elements of Fp 40 /*! 41 \param[out] result of adding left and right. 42 \param[in] left The first operand to be added. 43 \param[in] right The second operand to be added. 44 */ 45 void FpAdd(FpElem* result, FpElem const* left, FpElem const* right); 46 47 /// Multiply two elements of Fp. 48 /*! 49 \param[out] result of multiplying left and right. 50 \param[in] left The first operand to be multiplied. 51 \param[in] right The second operand to be multiplied. 52 */ 53 void FpMul(FpElem* result, FpElem const* left, FpElem const* right); 54 55 /// Subtract two elements of Fp. 56 /*! 57 \param[out] result of subtracting left from right. 58 \param[in] left The operand to be subtracted from. 59 \param[in] right The operand to subtract. 60 */ 61 void FpSub(FpElem* result, FpElem const* left, FpElem const* right); 62 63 /// Exponentiate an element of Fp by a large integer. 64 /*! 65 \param[out] result target. 66 \param[in] base the base. 67 \param[in] exp the exponent. 68 */ 69 void FpExp(FpElem* result, FpElem const* base, VeryLargeInt const* exp); 70 71 /// Negate an element of Fp. 72 /*! 73 \param[out] result target. 74 \param[in] in the value to negate. 75 */ 76 void FpNeg(FpElem* result, FpElem const* in); 77 78 /// Test if two elements in Fp are equal 79 /*! 80 \param[in] left The first operand to be tested. 81 \param[in] right The second operand to be tested. 82 \returns A value different from zero (i.e., true) if indeed 83 the values are equal. Zero (i.e., false) otherwise. 84 */ 85 int FpEq(FpElem const* left, FpElem const* right); 86 87 /// Invert an element of Fp. 88 /*! 89 \param[out] result target. 90 \param[in] in the value to invert. 91 */ 92 void FpInv(FpElem* result, FpElem const* in); 93 94 /// Generate a random element of Fp. 95 /*! 96 \param[in] result the random value. 97 \param[in] rnd_func Random number generator. 98 \param[in] rnd_param Pass through context data for rnd_func. 99 \returns A value different from zero (i.e., true) if on success. 100 Zero (i.e., false) otherwise. 101 */ 102 int FpRand(FpElem* result, BitSupplier rnd_func, void* rnd_param); 103 104 /// Generate a non-zero random element of Fp. 105 /*! 106 \param[in] result the random value. 107 \param[in] rnd_func Random number generator. 108 \param[in] rnd_param Pass through context data for rnd_func. 109 \returns A value different from zero (i.e., true) if on success. 110 Zero (i.e., false) otherwise. 111 */ 112 int FpRandNonzero(FpElem* result, BitSupplier rnd_func, void* rnd_param); 113 114 /// Clear an element of Fp. 115 /*! 116 \param[out] result value to clear. 117 */ 118 void FpClear(FpElem* result); 119 120 /// Set a element of Fp's value. 121 /*! 122 \param[out] result target. 123 \param[in] in value to set. 124 */ 125 void FpSet(FpElem* result, uint32_t in); 126 127 /// Reinterpret a buffer as an element of Fp 128 /*! 129 \param[out] result target. 130 \param[in] hash buffer to reinterpret. 131 \param[in] len length of hash in bytes. 132 */ 133 void FpFromHash(FpElem* result, unsigned char const* hash, size_t len); 134 135 #endif // EPID_MEMBER_TINY_MATH_FP_H_ 136