/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * @file big_XXX.h * @author Mike Scott * @brief BIG Header File * */ #ifndef BIG_XXX_H #define BIG_XXX_H #include #include #include #include "arch.h" #include "amcl.h" #include "config_big_XXX.h" //#define UNWOUND #define BIGBITS_XXX (8*MODBYTES_XXX) /**< Length in bits */ #define NLEN_XXX (1+((8*MODBYTES_XXX-1)/BASEBITS_XXX)) /**< length in bytes */ #define DNLEN_XXX 2*NLEN_XXX /**< Double length in bytes */ #define BMASK_XXX (((chunk)1<y */ extern int BIG_XXX_comp(const BIG_XXX x,const BIG_XXX y); /** @brief Compares two DBIG numbers. Inputs must be normalised externally * @param x first DBIG number to be compared @param y second DBIG number to be compared @return -1 is xy */ extern int BIG_XXX_dcomp(const DBIG_XXX x,const DBIG_XXX y); /** @brief Calculate number of bits in a BIG - output normalised - leaks log2(x) * @param x BIG number @return Number of bits in x */ extern int BIG_XXX_nbits(const BIG_XXX x); /** @brief Calculate number of bits in a DBIG - output normalised - leaks log2(x) * @param x DBIG number @return Number of bits in x */ extern int BIG_XXX_dnbits(const DBIG_XXX x); /** @brief Reduce x mod n - input and output normalised - leaks log2(x)-log2(n) * Slow but rarely used @param x BIG number to be reduced mod n @param n The modulus */ extern void BIG_XXX_mod(BIG_XXX x,const BIG_XXX n); /** @brief Divide x by n - output normalised - leaks log2(x)-log2(n) * Slow but rarely used @param x BIG number to be divided by n @param n The Divisor */ extern void BIG_XXX_sdiv(BIG_XXX x,const BIG_XXX n); /** @brief x=y mod n - output normalised - leaks log2(y)-log2(n) * Slow but rarely used. y is destroyed. @param x BIG number, on exit = y mod n @param y DBIG number @param n Modulus */ extern void BIG_XXX_dmod(BIG_XXX x,DBIG_XXX y,const BIG_XXX n); /** @brief x=y/n - output normalised - leaks log2(y)-log2(n) * Slow but rarely used. y is destroyed. @param x BIG number, on exit = y/n @param y DBIG number @param n Modulus */ extern void BIG_XXX_ddiv(BIG_XXX x,DBIG_XXX y,const BIG_XXX n); /** @brief return parity of BIG, that is the least significant bit * @param x BIG number @return 0 or 1 */ extern int BIG_XXX_parity(const BIG_XXX x); /** @brief return i-th of BIG * @param x BIG number @param i the bit of x to be returned @return 0 or 1 */ extern int BIG_XXX_bit(const BIG_XXX x,int i); /** @brief return i-th of BIG * @param x BIG number @param i the first bit of x to be returned @param w the width of the window to return @return bits in the window as an integer */ int BIG_XXX_bits(const BIG_XXX x, int i, int w); /** @brief return least significant bits of a BIG * @param x BIG number @param n number of bits to return. Assumed to be less than BASEBITS. @return least significant n bits as an integer */ extern int BIG_XXX_lastbits(BIG_XXX x,int n); /** @brief Create a random BIG from a random number generator * Assumes that the random number generator has been suitably initialised @param x BIG number, on exit a random number @param r A pointer to a Cryptographically Secure Random Number Generator */ extern void BIG_XXX_random(BIG_XXX x,csprng *r); /** @brief Create an unbiased random BIG from a random number generator, reduced with respect to a modulus - leaks log2(n) * Assumes that the random number generator has been suitably initialised @param x BIG number, on exit a random number @param n The modulus @param r A pointer to a Cryptographically Secure Random Number Generator */ extern void BIG_XXX_randomnum(BIG_XXX x,const BIG_XXX n,csprng *r); /** brief return NAF (Non-Adjacent-Form) value as +/- 1, 3 or 5, inputs must be normalised * Given x and 3*x extracts NAF value from given bit position, and returns number of bits processed, and number of trailing zeros detected if any param x BIG number param x3 BIG number, three times x param i bit position param nbs pointer to integer returning number of bits processed param nzs pointer to integer returning number of trailing 0s return + or - 1, 3 or 5 */ /** @brief Calculate x=y*z mod n - leaks log2(x)-log2(n) * Slow method for modular multiplication @param x BIG number, on exit = y*z mod n @param y BIG number @param z BIG number @param n The BIG Modulus */ extern void BIG_XXX_modmul(BIG_XXX x,const BIG_XXX y,const BIG_XXX z,const BIG_XXX n); /** @brief Calculate x=y/z mod n - leaks log2(x)-log2(n) * Slow method for modular division @param x BIG number, on exit = y/z mod n @param y BIG number @param z BIG number @param n The BIG Modulus */ extern void BIG_XXX_moddiv(BIG_XXX x,const BIG_XXX y,const BIG_XXX z,const BIG_XXX n); /** @brief Calculate x=y^2 mod n - leaks log2(x)-log2(n) * Slow method for modular squaring @param x BIG number, on exit = y^2 mod n @param y BIG number @param n The BIG Modulus */ extern void BIG_XXX_modsqr(BIG_XXX x,const BIG_XXX y,const BIG_XXX n); /** @brief Calculate x=-y mod n - leaks log2(x)-log2(n) * Modular negation @param x BIG number, on exit = -y mod n @param y BIG number @param n The BIG Modulus */ extern void BIG_XXX_modneg(BIG_XXX x,const BIG_XXX y,const BIG_XXX n); /** @brief Calculate jacobi Symbol (x/y) - not constant time * @param x BIG number @param y BIG number @return Jacobi symbol, -1,0 or 1 */ extern int BIG_XXX_jacobi(BIG_XXX x,const BIG_XXX y); /** @brief Calculate x=1/y mod n * Modular Inversion - This is slow. Uses binary method. @param x BIG number, on exit = 1/y mod n @param y BIG number @param n The BIG Modulus */ extern void BIG_XXX_invmodp(BIG_XXX x,const BIG_XXX y,const BIG_XXX n); /** @brief Calculate x=x mod 2^m * Truncation @param x BIG number, on reduced mod 2^m @param m new truncated size */ extern void BIG_XXX_mod2m(BIG_XXX x,int m); /** @brief Calculate x=x mod 2^m * Truncation @param x DBIG number, on reduced mod 2^m @param m new truncated size */ extern void BIG_XXX_dmod2m(DBIG_XXX x,int m); /** @brief Calculates a*b+c+*d * Calculate partial product of a.b, add in carry c, and add total to d @param x multiplier @param y multiplicand @param c carry @param r pointer to accumulated bottom half of result @return top half of result */ #ifdef dchunk /* Method required to calculate x*y+c+r, bottom half in r, top half returned */ static inline chunk muladd_XXX(chunk x,chunk y,chunk c,chunk *r) { dchunk prod=(dchunk)x*y+c+*r; *r=(chunk)prod&BMASK_XXX; return (chunk)(prod>>BASEBITS_XXX); } #else /* No integer type available that can store double the wordlength */ /* accumulate partial products */ static inline chunk muladd_XXX(chunk x,chunk y,chunk c,chunk *r) { chunk x0; chunk x1; chunk y0; chunk y1; chunk bot; chunk top; chunk mid; chunk carry; x0=x&HMASK_XXX; x1=(x>>HBITS_XXX); y0=y&HMASK_XXX; y1=(y>>HBITS_XXX); bot=x0*y0; top=x1*y1; mid=x0*y1+x1*y0; x0=mid&HMASK_XXX; x1=(mid>>HBITS_XXX); bot+=x0<>BASEBITS_XXX; bot&=BMASK_XXX; top+=carry; *r=bot; return top; } #endif #endif