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