forked from contrun/libecc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathec_shortw.c
65 lines (54 loc) · 1.68 KB
/
ec_shortw.c
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
/*
* 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.
*/
#include "ec_shortw.h"
#define EC_SHORTW_CRV_MAGIC ((word_t)(0x9c7c46a1a04c6720ULL))
/*
* Check pointed short Weierstrass curve structure as already been
* initialized.
*/
void ec_shortw_crv_check_initialized(ec_shortw_crv_src_t crv)
{
MUST_HAVE((crv != NULL) && (crv->magic == EC_SHORTW_CRV_MAGIC));
}
/*
* Initialize pointed short Weierstrass curve structure using given a and b
* Fp elements representing curve equation (y^2 = x^3 + ax + b) parameters.
*/
void ec_shortw_crv_init(ec_shortw_crv_t crv, fp_src_t a, fp_src_t b, nn_src_t order)
{
MUST_HAVE(crv != NULL);
fp_check_initialized(a);
fp_check_initialized(b);
MUST_HAVE(a->ctx == b->ctx);
nn_check_initialized(order);
fp_init(&(crv->a), a->ctx);
fp_init(&(crv->b), b->ctx);
fp_init(&(crv->a_monty), a->ctx);
fp_copy(&(crv->a), a);
fp_copy(&(crv->b), b);
fp_redcify(&(crv->a_monty), a);
nn_copy(&(crv->order), order);
#ifndef NO_USE_COMPLETE_FORMULAS
fp_init(&(crv->b3), b->ctx);
fp_init(&(crv->b_monty), b->ctx);
fp_init(&(crv->b3_monty), b->ctx);
fp_add(&(crv->b3), b, b);
fp_add(&(crv->b3), &(crv->b3), b);
fp_redcify(&(crv->b_monty), b);
fp_redcify(&(crv->b3_monty), &(crv->b3));
#endif
crv->magic = EC_SHORTW_CRV_MAGIC;
}