296 lines
8.8 KiB
C
296 lines
8.8 KiB
C
|
/*
|
||
|
* Copyright (c) 2012-2020 MIRACL UK Ltd.
|
||
|
*
|
||
|
* This file is part of MIRACL Core
|
||
|
* (see https://github.com/miracl/core).
|
||
|
*
|
||
|
* Licensed 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.
|
||
|
*/
|
||
|
|
||
|
#ifndef FF_WWW_H
|
||
|
#define FF_WWW_H
|
||
|
|
||
|
#include "big_XXX.h"
|
||
|
#include "config_ff_WWW.h"
|
||
|
|
||
|
using namespace core;
|
||
|
|
||
|
#define HFLEN_WWW (FFLEN_WWW/2) /**< Useful for half-size RSA private key operations */
|
||
|
#define P_MBITS_WWW (MODBYTES_XXX*8)
|
||
|
#define P_TBITS_WWW (P_MBITS_WWW%BASEBITS_XXX)
|
||
|
#define P_EXCESS_WWW(a) (((a[NLEN_XXX-1])>>(P_TBITS_WWW))+1)
|
||
|
#define P_FEXCESS_WWW ((chunk)1<<(BASEBITS_XXX*NLEN_XXX-P_MBITS_WWW-1))
|
||
|
|
||
|
|
||
|
namespace WWW {
|
||
|
|
||
|
/* Finite Field Prototypes */
|
||
|
/** @brief Copy one FF element of given length to another
|
||
|
*
|
||
|
@param x FF instance to be copied to, on exit = y
|
||
|
@param y FF instance to be copied from
|
||
|
@param n size of FF in BIGs
|
||
|
|
||
|
*/
|
||
|
extern void FF_copy(XXX::BIG *x, XXX::BIG *y, int n);
|
||
|
/** @brief Initialize an FF element of given length from a 32-bit integer m
|
||
|
*
|
||
|
@param x FF instance to be copied to, on exit = m
|
||
|
@param m integer
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_init(XXX::BIG *x, sign32 m, int n);
|
||
|
/** @brief Set FF element of given size to zero
|
||
|
*
|
||
|
@param x FF instance to be set to zero
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_zero(XXX::BIG *x, int n);
|
||
|
/** @brief Tests for FF element equal to zero
|
||
|
*
|
||
|
@param x FF number to be tested
|
||
|
@param n size of FF in BIGs
|
||
|
@return 1 if zero, else returns 0
|
||
|
*/
|
||
|
extern int FF_iszilch(XXX::BIG *x, int n);
|
||
|
/** @brief return parity of an FF, that is the least significant bit
|
||
|
*
|
||
|
@param x FF number
|
||
|
@return 0 or 1
|
||
|
*/
|
||
|
extern int FF_parity(XXX::BIG *x);
|
||
|
/** @brief return least significant m bits of an FF
|
||
|
*
|
||
|
@param x FF number
|
||
|
@param m number of bits to return. Assumed to be less than BASEBITS.
|
||
|
@return least significant n bits as an integer
|
||
|
*/
|
||
|
extern int FF_lastbits(XXX::BIG *x, int m);
|
||
|
/** @brief Set FF element of given size to unity
|
||
|
*
|
||
|
@param x FF instance to be set to unity
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_one(XXX::BIG *x, int n);
|
||
|
/** @brief Compares two FF numbers. Inputs must be normalised externally
|
||
|
*
|
||
|
@param x first FF number to be compared
|
||
|
@param y second FF number to be compared
|
||
|
@param n size of FF in BIGs
|
||
|
@return -1 is x<y, 0 if x=y, 1 if x>y
|
||
|
*/
|
||
|
extern int FF_comp(XXX::BIG *x, XXX::BIG *y, int n);
|
||
|
/** @brief addition of two FFs
|
||
|
*
|
||
|
@param x FF instance, on exit = y+z
|
||
|
@param y FF instance
|
||
|
@param z FF instance
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_add(XXX::BIG *x, XXX::BIG *y, XXX::BIG *z, int n);
|
||
|
/** @brief subtraction of two FFs
|
||
|
*
|
||
|
@param x FF instance, on exit = y-z
|
||
|
@param y FF instance
|
||
|
@param z FF instance
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_sub(XXX::BIG *x, XXX::BIG *y, XXX::BIG *z, int n);
|
||
|
/** @brief increment an FF by an integer,and normalise
|
||
|
*
|
||
|
@param x FF instance, on exit = x+m
|
||
|
@param m an integer to be added to x
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_inc(XXX::BIG *x, int m, int n);
|
||
|
/** @brief Decrement an FF by an integer,and normalise
|
||
|
*
|
||
|
@param x FF instance, on exit = x-m
|
||
|
@param m an integer to be subtracted from x
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_dec(XXX::BIG *x, int m, int n);
|
||
|
/** @brief Normalises the components of an FF
|
||
|
*
|
||
|
@param x FF instance to be normalised
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_norm(XXX::BIG *x, int n);
|
||
|
/** @brief Shift left an FF by 1 bit
|
||
|
*
|
||
|
@param x FF instance to be shifted left
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_shl(XXX::BIG *x, int n);
|
||
|
/** @brief Shift right an FF by 1 bit
|
||
|
*
|
||
|
@param x FF instance to be shifted right
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_shr(XXX::BIG *x, int n);
|
||
|
/** @brief Formats and outputs an FF to the console
|
||
|
*
|
||
|
@param x FF instance to be printed
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_output(XXX::BIG *x, int n);
|
||
|
/** @brief Formats and outputs an FF to the console, in raw form
|
||
|
*
|
||
|
@param x FF instance to be printed
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_rawoutput(XXX::BIG *x, int n);
|
||
|
/** @brief Formats and outputs an FF instance to an octet string
|
||
|
*
|
||
|
Converts an FF to big-endian base 256 form.
|
||
|
@param S output octet string
|
||
|
@param x FF instance to be converted to an octet string
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_toOctet(octet *S, XXX::BIG *x, int n);
|
||
|
/** @brief Populates an FF instance from an octet string
|
||
|
*
|
||
|
Creates FF from big-endian base 256 form.
|
||
|
@param x FF instance to be created from an octet string
|
||
|
@param S input octet string
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_fromOctet(XXX::BIG *x, octet *S, int n);
|
||
|
/** @brief Multiplication of two FFs
|
||
|
*
|
||
|
Uses Karatsuba method internally
|
||
|
@param x FF instance, on exit = y*z
|
||
|
@param y FF instance
|
||
|
@param z FF instance
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_mul(XXX::BIG *x, XXX::BIG *y, XXX::BIG *z, int n);
|
||
|
/** @brief Reduce FF mod a modulus
|
||
|
*
|
||
|
This is slow
|
||
|
@param x FF instance to be reduced mod m - on exit = x mod m
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_mod(XXX::BIG *x, XXX::BIG *m, int n);
|
||
|
/** @brief Square an FF
|
||
|
*
|
||
|
Uses Karatsuba method internally
|
||
|
@param x FF instance, on exit = y^2
|
||
|
@param y FF instance to be squared
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_sqr(XXX::BIG *x, XXX::BIG *y, int n);
|
||
|
/** @brief Reduces a double-length FF with respect to a given modulus
|
||
|
*
|
||
|
This is slow
|
||
|
@param x FF instance, on exit = y mod z
|
||
|
@param y FF instance, of double length 2*n
|
||
|
@param z FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_dmod(XXX::BIG *x, XXX::BIG *y, XXX::BIG *z, int n);
|
||
|
/** @brief Invert an FF mod a prime modulus
|
||
|
*
|
||
|
@param x FF instance, on exit = 1/y mod z
|
||
|
@param y FF instance
|
||
|
@param z FF prime modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_invmodp(XXX::BIG *x, XXX::BIG *y, XXX::BIG *z, int n);
|
||
|
/** @brief Create an FF from a random number generator
|
||
|
*
|
||
|
@param x FF instance, on exit x is a random number of length n BIGs with most significant bit a 1
|
||
|
@param R an instance of a Cryptographically Secure Random Number Generator
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_random(XXX::BIG *x, csprng *R, int n);
|
||
|
/** @brief Create a random FF less than a given modulus from a random number generator
|
||
|
*
|
||
|
@param x FF instance, on exit x is a random number < y
|
||
|
@param y FF instance, the modulus
|
||
|
@param R an instance of a Cryptographically Secure Random Number Generator
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_randomnum(XXX::BIG *x, XXX::BIG *y, csprng *R, int n);
|
||
|
/** @brief Calculate r=x^e mod m, side channel resistant
|
||
|
*
|
||
|
@param r FF instance, on exit = x^e mod p
|
||
|
@param x FF instance
|
||
|
@param e FF exponent
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_skpow(XXX::BIG *r, XXX::BIG *x, XXX::BIG * e, XXX::BIG *m, int n);
|
||
|
/** @brief Calculate r=x^e mod m, side channel resistant
|
||
|
*
|
||
|
For short BIG exponent
|
||
|
@param r FF instance, on exit = x^e mod p
|
||
|
@param x FF instance
|
||
|
@param e BIG exponent
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_skspow(XXX::BIG *r, XXX::BIG *x, XXX::BIG e, XXX::BIG *m, int n);
|
||
|
/** @brief Calculate r=x^e mod m
|
||
|
*
|
||
|
For very short integer exponent
|
||
|
@param r FF instance, on exit = x^e mod p
|
||
|
@param x FF instance
|
||
|
@param e integer exponent
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_power(XXX::BIG *r, XXX::BIG *x, int e, XXX::BIG *m, int n);
|
||
|
/** @brief Calculate r=x^e mod m
|
||
|
*
|
||
|
@param r FF instance, on exit = x^e mod p
|
||
|
@param x FF instance
|
||
|
@param e FF exponent
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_pow(XXX::BIG *r, XXX::BIG *x, XXX::BIG *e, XXX::BIG *m, int n);
|
||
|
/** @brief Test if an FF has factor in common with integer s
|
||
|
*
|
||
|
@param x FF instance to be tested
|
||
|
@param s the supplied integer
|
||
|
@param n size of FF in BIGs
|
||
|
@return 1 if gcd(x,s)!=1, else return 0
|
||
|
*/
|
||
|
extern int FF_cfactor(XXX::BIG *x, sign32 s, int n);
|
||
|
/** @brief Test if an FF is prime
|
||
|
*
|
||
|
Uses Miller-Rabin Method
|
||
|
@param x FF instance to be tested
|
||
|
@param R an instance of a Cryptographically Secure Random Number Generator
|
||
|
@param n size of FF in BIGs
|
||
|
@return 1 if x is (almost certainly) prime, else return 0
|
||
|
*/
|
||
|
extern int FF_prime(XXX::BIG *x, csprng *R, int n);
|
||
|
/** @brief Calculate r=x^e.y^f mod m
|
||
|
*
|
||
|
@param r FF instance, on exit = x^e.y^f mod p
|
||
|
@param x FF instance
|
||
|
@param e BIG exponent
|
||
|
@param y FF instance
|
||
|
@param f BIG exponent
|
||
|
@param m FF modulus
|
||
|
@param n size of FF in BIGs
|
||
|
*/
|
||
|
extern void FF_pow2(XXX::BIG *r, XXX::BIG *x, XXX::BIG e, XXX::BIG *y, XXX::BIG f, XXX::BIG *m, int n);
|
||
|
|
||
|
}
|
||
|
|
||
|
#endif
|