/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/fp/fp_mul.h>16#include <libecc/fp/fp_pow.h>17#include <libecc/nn/nn_add.h>18#include <libecc/nn/nn_mul_public.h>19#include <libecc/nn/nn_modinv.h>20/* Include the "internal" header as we use non public API here */21#include "../nn/nn_div.h"2223/*24* Compute out = in1 * in2 mod p. 'out' parameter must have been initialized25* by the caller. Returns 0 on success, -1 on error.26*27* Aliasing is supported.28*/29int fp_mul(fp_t out, fp_src_t in1, fp_src_t in2)30{31int ret;3233ret = fp_check_initialized(in1); EG(ret, err);34ret = fp_check_initialized(in2); EG(ret, err);35ret = fp_check_initialized(out); EG(ret, err);3637MUST_HAVE(out->ctx == in1->ctx, ret, err);38MUST_HAVE(out->ctx == in2->ctx, ret, err);3940ret = nn_mul(&(out->fp_val), &(in1->fp_val), &(in2->fp_val)); EG(ret, err);41ret = nn_mod_unshifted(&(out->fp_val), &(out->fp_val), &(in1->ctx->p_normalized),42in1->ctx->p_reciprocal, in1->ctx->p_shift);4344err:45return ret;46}4748/*49* Compute out = in * in mod p. 'out' parameter must have been initialized50* by the caller. Returns 0 on success, -1 on error.51*52* Aliasing is supported.53*/54int fp_sqr(fp_t out, fp_src_t in)55{56return fp_mul(out, in, in);57}5859/* We use Fermat's little theorem for our inversion in Fp:60* x^(p-1) = 1 mod (p) means that x^(p-2) mod(p) is the modular61* inverse of x mod (p)62*63* Aliasing is supported.64*/65int fp_inv(fp_t out, fp_src_t in)66{67/* Use our lower layer Fermat modular inversion with precomputed68* Montgomery coefficients.69*/70int ret;7172ret = fp_check_initialized(in); EG(ret, err);73ret = fp_check_initialized(out); EG(ret, err);7475MUST_HAVE(out->ctx == in->ctx, ret, err);7677/* We can use the Fermat inversion as p is surely prime here */78ret = nn_modinv_fermat_redc(&(out->fp_val), &(in->fp_val), &(in->ctx->p), &(in->ctx->r), &(in->ctx->r_square), in->ctx->mpinv);7980err:81return ret;82}8384/*85* Compute out = w^-1 mod p. 'out' parameter must have been initialized86* by the caller. Returns 0 on success, -1 on error.87*/88int fp_inv_word(fp_t out, word_t w)89{90int ret;9192ret = fp_check_initialized(out); EG(ret, err);9394ret = nn_modinv_word(&(out->fp_val), w, &(out->ctx->p));9596err:97return ret;98}99100/*101* Compute out such that num = out * den mod p. 'out' parameter must have been initialized102* by the caller. Returns 0 on success, -1 on error.103*104* Aliasing is supported.105*/106int fp_div(fp_t out, fp_src_t num, fp_src_t den)107{108int ret;109110ret = fp_check_initialized(num); EG(ret, err);111ret = fp_check_initialized(den); EG(ret, err);112ret = fp_check_initialized(out); EG(ret, err);113114MUST_HAVE(out->ctx == num->ctx, ret, err);115MUST_HAVE(out->ctx == den->ctx, ret, err);116117if(out == num){118/* Handle aliasing of out and num */119fp _num;120_num.magic = WORD(0);121122ret = fp_copy(&_num, num); EG(ret, err1);123ret = fp_inv(out, den); EG(ret, err1);124ret = fp_mul(out, &_num, out);125126err1:127fp_uninit(&_num);128EG(ret, err);129}130else{131ret = fp_inv(out, den); EG(ret, err);132ret = fp_mul(out, num, out);133}134135err:136return ret;137}138139140