1 /* 2 * Copyright (C) 2017 - This file is part of libecc project 3 * 4 * Authors: 5 * Ryad BENADJILA <ryadbenadjila@gmail.com> 6 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 7 * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr> 8 * 9 * Contributors: 10 * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr> 11 * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr> 12 * 13 * This software is licensed under a dual BSD and GPL v2 license. 14 * See LICENSE file at the root folder of the project. 15 */ 16 #ifndef __FP_H__ 17 #define __FP_H__ 18 19 #include <libecc/nn/nn.h> 20 #include <libecc/nn/nn_div_public.h> 21 #include <libecc/nn/nn_modinv.h> 22 #include <libecc/nn/nn_mul_public.h> 23 #include <libecc/nn/nn_mul_redc1.h> 24 #include <libecc/fp/fp_config.h> 25 26 /* 27 * First, definition of our Fp context, containing all the elements 28 * needed to efficiently implement Fp operations. 29 */ 30 31 typedef struct { 32 /* 33 * Value of p (extended by one word to handle 34 * overflows in Fp). p_bitlen provides its 35 * length in bit. 36 */ 37 nn p; 38 bitcnt_t p_bitlen; 39 40 /* -p^-1 mod 2^(bitsizeof(word_t)) */ 41 word_t mpinv; 42 43 /* 2^bitsizeof(p) mod p */ 44 nn r; 45 46 /* 2^(2*bitsizeof(p)) mod p */ 47 nn r_square; 48 49 /* clz(p) */ 50 bitcnt_t p_shift; 51 /* p << p_shift */ 52 nn p_normalized; 53 /* floor(B^3/(DMSW(p_normalized) + 1)) - B */ 54 word_t p_reciprocal; 55 56 word_t magic; 57 } fp_ctx; 58 59 typedef fp_ctx *fp_ctx_t; 60 typedef const fp_ctx *fp_ctx_src_t; 61 62 ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_check_initialized(fp_ctx_src_t ctx); 63 ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_init(fp_ctx_t ctx, nn_src_t p, bitcnt_t p_bitlen, 64 nn_src_t r, nn_src_t r_square, 65 word_t mpinv, 66 bitcnt_t p_shift, nn_src_t p_normalized, word_t p_reciprocal); 67 ATTRIBUTE_WARN_UNUSED_RET int fp_ctx_init_from_p(fp_ctx_t ctx, nn_src_t p); 68 69 /* 70 * Then the definition of our Fp elements 71 */ 72 73 typedef struct { 74 nn fp_val; 75 fp_ctx_src_t ctx; 76 word_t magic; 77 } fp; 78 79 typedef fp *fp_t; 80 typedef const fp *fp_src_t; 81 82 ATTRIBUTE_WARN_UNUSED_RET int fp_check_initialized(fp_src_t in); 83 ATTRIBUTE_WARN_UNUSED_RET int fp_init(fp_t A, fp_ctx_src_t fpctx); 84 ATTRIBUTE_WARN_UNUSED_RET int fp_init_from_buf(fp_t A, fp_ctx_src_t fpctx, const u8 *buf, u16 buflen); 85 void fp_uninit(fp_t A); 86 ATTRIBUTE_WARN_UNUSED_RET int fp_set_nn(fp_t out, nn_src_t in); 87 ATTRIBUTE_WARN_UNUSED_RET int fp_zero(fp_t out); 88 ATTRIBUTE_WARN_UNUSED_RET int fp_one(fp_t out); 89 ATTRIBUTE_WARN_UNUSED_RET int fp_set_word_value(fp_t out, word_t val); 90 ATTRIBUTE_WARN_UNUSED_RET int fp_cmp(fp_src_t in1, fp_src_t in2, int *cmp); 91 ATTRIBUTE_WARN_UNUSED_RET int fp_iszero(fp_src_t in, int *iszero); 92 ATTRIBUTE_WARN_UNUSED_RET int fp_copy(fp_t out, fp_src_t in); 93 ATTRIBUTE_WARN_UNUSED_RET int fp_tabselect(fp_t out, u8 idx, fp_src_t *tab, u8 tabsize); 94 ATTRIBUTE_WARN_UNUSED_RET int fp_eq_or_opp(fp_src_t in1, fp_src_t in2, int *eq_or_opp); 95 ATTRIBUTE_WARN_UNUSED_RET int fp_import_from_buf(fp_t out_fp, const u8 *buf, u16 buflen); 96 ATTRIBUTE_WARN_UNUSED_RET int fp_export_to_buf(u8 *buf, u16 buflen, fp_src_t in_fp); 97 98 #endif /* __FP_H__ */ 99