You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
7.5 KiB
245 lines
7.5 KiB
/*
|
|
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 fp_FP256BN.h
|
|
* @author Mike Scott
|
|
* @brief FP Header File
|
|
*
|
|
*/
|
|
|
|
#ifndef FP_FP256BN_H
|
|
#define FP_FP256BN_H
|
|
|
|
#include "big_256_56.h"
|
|
#include "config_field_FP256BN.h"
|
|
|
|
|
|
/**
|
|
@brief FP Structure - quadratic extension field
|
|
*/
|
|
|
|
typedef struct
|
|
{
|
|
BIG_256_56 g; /**< Big representation of field element */
|
|
sign32 XES; /**< Excess */
|
|
} FP_FP256BN;
|
|
|
|
|
|
/* Field Params - see rom.c */
|
|
extern const BIG_256_56 Modulus_FP256BN; /**< Actual Modulus set in romf_yyy.c */
|
|
extern const BIG_256_56 R2modp_FP256BN; /**< Montgomery constant */
|
|
extern const chunk MConst_FP256BN; /**< Constant associated with Modulus - for Montgomery = 1/p mod 2^BASEBITS */
|
|
|
|
|
|
#define MODBITS_FP256BN MBITS_FP256BN /**< Number of bits in Modulus for selected curve */
|
|
#define TBITS_FP256BN (MBITS_FP256BN%BASEBITS_256_56) /**< Number of active bits in top word */
|
|
#define TMASK_FP256BN (((chunk)1<<TBITS_FP256BN)-1) /**< Mask for active bits in top word */
|
|
#define FEXCESS_FP256BN ((sign32)1<<MAXXES_FP256BN) /**< 2^(BASEBITS*NLEN-MODBITS) - normalised BIG can be multiplied by more than this before reduction */
|
|
#define OMASK_FP256BN (-((chunk)(1)<<TBITS_FP256BN)) /**< for masking out overflow bits */
|
|
|
|
//#define FUSED_MODMUL
|
|
//#define DEBUG_REDUCE
|
|
|
|
/* FP prototypes */
|
|
|
|
/** @brief Tests for FP equal to zero mod Modulus
|
|
*
|
|
@param x BIG number to be tested
|
|
@return 1 if zero, else returns 0
|
|
*/
|
|
extern int FP_FP256BN_iszilch(FP_FP256BN *x);
|
|
|
|
|
|
/** @brief Set FP to zero
|
|
*
|
|
@param x FP number to be set to 0
|
|
*/
|
|
extern void FP_FP256BN_zero(FP_FP256BN *x);
|
|
|
|
/** @brief Copy an FP
|
|
*
|
|
@param y FP number to be copied to
|
|
@param x FP to be copied from
|
|
*/
|
|
extern void FP_FP256BN_copy(FP_FP256BN *y,FP_FP256BN *x);
|
|
|
|
/** @brief Copy from ROM to an FP
|
|
*
|
|
@param y FP number to be copied to
|
|
@param x BIG to be copied from ROM
|
|
*/
|
|
extern void FP_FP256BN_rcopy(FP_FP256BN *y,const BIG_256_56 x);
|
|
|
|
|
|
/** @brief Compares two FPs
|
|
*
|
|
@param x FP number
|
|
@param y FP number
|
|
@return 1 if equal, else returns 0
|
|
*/
|
|
extern int FP_FP256BN_equals(FP_FP256BN *x,FP_FP256BN *y);
|
|
|
|
|
|
/** @brief Conditional constant time swap of two FP numbers
|
|
*
|
|
Conditionally swaps parameters in constant time (without branching)
|
|
@param x an FP number
|
|
@param y another FP number
|
|
@param s swap takes place if not equal to 0
|
|
*/
|
|
extern void FP_FP256BN_cswap(FP_FP256BN *x,FP_FP256BN *y,int s);
|
|
/** @brief Conditional copy of FP number
|
|
*
|
|
Conditionally copies second parameter to the first (without branching)
|
|
@param x an FP number
|
|
@param y another FP number
|
|
@param s copy takes place if not equal to 0
|
|
*/
|
|
extern void FP_FP256BN_cmove(FP_FP256BN *x,FP_FP256BN *y,int s);
|
|
/** @brief Converts from BIG integer to residue form mod Modulus
|
|
*
|
|
@param x BIG number to be converted
|
|
@param y FP result
|
|
*/
|
|
extern void FP_FP256BN_nres(FP_FP256BN *y,BIG_256_56 x);
|
|
/** @brief Converts from residue form back to BIG integer form
|
|
*
|
|
@param y FP number to be converted to BIG
|
|
@param x BIG result
|
|
*/
|
|
extern void FP_FP256BN_redc(BIG_256_56 x,FP_FP256BN *y);
|
|
/** @brief Sets FP to representation of unity in residue form
|
|
*
|
|
@param x FP number to be set equal to unity.
|
|
*/
|
|
extern void FP_FP256BN_one(FP_FP256BN *x);
|
|
/** @brief Reduces DBIG to BIG exploiting special form of the modulus
|
|
*
|
|
This function comes in different flavours depending on the form of Modulus that is currently in use.
|
|
@param r BIG number, on exit = d mod Modulus
|
|
@param d DBIG number to be reduced
|
|
*/
|
|
extern void FP_FP256BN_mod(BIG_256_56 r,DBIG_256_56 d);
|
|
|
|
#ifdef FUSED_MODMUL
|
|
extern void FP_FP256BN_modmul(BIG_256_56,BIG_256_56,BIG_256_56);
|
|
#endif
|
|
|
|
/** @brief Fast Modular multiplication of two FPs, mod Modulus
|
|
*
|
|
Uses appropriate fast modular reduction method
|
|
@param x FP number, on exit the modular product = y*z mod Modulus
|
|
@param y FP number, the multiplicand
|
|
@param z FP number, the multiplier
|
|
*/
|
|
extern void FP_FP256BN_mul(FP_FP256BN *x,FP_FP256BN *y,FP_FP256BN *z);
|
|
/** @brief Fast Modular multiplication of an FP, by a small integer, mod Modulus
|
|
*
|
|
@param x FP number, on exit the modular product = y*i mod Modulus
|
|
@param y FP number, the multiplicand
|
|
@param i a small number, the multiplier
|
|
*/
|
|
extern void FP_FP256BN_imul(FP_FP256BN *x,FP_FP256BN *y,int i);
|
|
/** @brief Fast Modular squaring of an FP, mod Modulus
|
|
*
|
|
Uses appropriate fast modular reduction method
|
|
@param x FP number, on exit the modular product = y^2 mod Modulus
|
|
@param y FP number, the number to be squared
|
|
|
|
*/
|
|
extern void FP_FP256BN_sqr(FP_FP256BN *x,FP_FP256BN *y);
|
|
/** @brief Modular addition of two FPs, mod Modulus
|
|
*
|
|
@param x FP number, on exit the modular sum = y+z mod Modulus
|
|
@param y FP number
|
|
@param z FP number
|
|
*/
|
|
extern void FP_FP256BN_add(FP_FP256BN *x,FP_FP256BN *y,FP_FP256BN *z);
|
|
/** @brief Modular subtraction of two FPs, mod Modulus
|
|
*
|
|
@param x FP number, on exit the modular difference = y-z mod Modulus
|
|
@param y FP number
|
|
@param z FP number
|
|
*/
|
|
extern void FP_FP256BN_sub(FP_FP256BN *x,FP_FP256BN *y,FP_FP256BN *z);
|
|
/** @brief Modular division by 2 of an FP, mod Modulus
|
|
*
|
|
@param x FP number, on exit =y/2 mod Modulus
|
|
@param y FP number
|
|
*/
|
|
extern void FP_FP256BN_div2(FP_FP256BN *x,FP_FP256BN *y);
|
|
/** @brief Fast Modular exponentiation of an FP, to the power of a BIG, mod Modulus
|
|
*
|
|
@param x FP number, on exit = y^z mod Modulus
|
|
@param y FP number
|
|
@param z BIG number exponent
|
|
*/
|
|
extern void FP_FP256BN_pow(FP_FP256BN *x,FP_FP256BN *y,BIG_256_56 z);
|
|
/** @brief Fast Modular square root of a an FP, mod Modulus
|
|
*
|
|
@param x FP number, on exit = sqrt(y) mod Modulus
|
|
@param y FP number, the number whose square root is calculated
|
|
|
|
*/
|
|
extern void FP_FP256BN_sqrt(FP_FP256BN *x,FP_FP256BN *y);
|
|
/** @brief Modular negation of a an FP, mod Modulus
|
|
*
|
|
@param x FP number, on exit = -y mod Modulus
|
|
@param y FP number
|
|
*/
|
|
extern void FP_FP256BN_neg(FP_FP256BN *x,FP_FP256BN *y);
|
|
/** @brief Outputs an FP number to the console
|
|
*
|
|
Converts from residue form before output
|
|
@param x an FP number
|
|
*/
|
|
extern void FP_FP256BN_output(FP_FP256BN *x);
|
|
/** @brief Outputs an FP number to the console, in raw form
|
|
*
|
|
@param x a BIG number
|
|
*/
|
|
extern void FP_FP256BN_rawoutput(FP_FP256BN *x);
|
|
/** @brief Reduces possibly unreduced FP mod Modulus
|
|
*
|
|
@param x FP number, on exit reduced mod Modulus
|
|
*/
|
|
extern void FP_FP256BN_reduce(FP_FP256BN *x);
|
|
/** @brief normalizes FP
|
|
*
|
|
@param x FP number, on exit normalized
|
|
*/
|
|
extern void FP_FP256BN_norm(FP_FP256BN *x);
|
|
/** @brief Tests for FP a quadratic residue mod Modulus
|
|
*
|
|
@param x FP number to be tested
|
|
@return 1 if quadratic residue, else returns 0 if quadratic non-residue
|
|
*/
|
|
extern int FP_FP256BN_qr(FP_FP256BN *x);
|
|
/** @brief Modular inverse of a an FP, mod Modulus
|
|
*
|
|
@param x FP number, on exit = 1/y mod Modulus
|
|
@param y FP number
|
|
*/
|
|
extern void FP_FP256BN_inv(FP_FP256BN *x,FP_FP256BN *y);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|