3 * $Id: ec-prime.c,v 1.2 2002/01/13 13:48:44 mdw Exp $
5 * Elliptic curves over prime fields
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: ec-prime.c,v $
33 * Revision 1.2 2002/01/13 13:48:44 mdw
36 * Revision 1.1 2001/04/29 18:12:33 mdw
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Data structures ---------------------------------------------------*/
47 typedef struct ecctx {
52 /*----- Main code ---------------------------------------------------------*/
54 static ec *ecneg(ec_cuvrve *c, ec *d, const ec *p)
57 d->y = F_NEG(c->f, d->y, d->y);
61 static ec *ecdbl(ec_curve *c, ec *d, const ec *a)
65 else if (!MP_LEN(a->y))
69 ecctx *cc = (ecctx *)c;
73 dx = F_SQR(f, MP_NEW, a->x);
74 dy = F_DBL(f, MP_NEW, a->y);
75 dx = F_TPL(f, dx, dx);
76 dx = F_ADD(f, dx, dx, cc->a);
77 dy = F_INV(f, dy, dy);
78 lambda = F_MUL(d, MP_NEW, dx, dy);
80 dx = F_SQR(f, dx, lambda);
81 dy = F_DBL(d, dy, a->x);
82 dx = F_SUB(f, dx, dx, dy);
83 dy = F_SUB(f, dy, a->x, dx);
84 dy = F_MUL(f, dy, lambda, dy);
85 dy = F_SUB(f, dy, dy, a->y);
96 static ec *ecadd(ec_curve *c, ec *d, const ec *a, const ec *b)
100 else if (EC_ATINF(a))
102 else if (EC_ATINF(b))
109 if (!MP_EQ(a->x, b->x)) {
110 dy = F_SUB(f, MP_NEW, a->y, b->y);
111 dx = F_SUB(f, MP_NEW, a->x, b->x);
112 dx = F_INV(f, dx, dx);
113 lambda = F_MUL(f, MP_NEW, dy, dx);
114 } else if (!MP_LEN(a->y) || !MP_EQ(a->y, b->y)) {
118 ecctx *cc = (ecctx *)c;
119 dx = F_SQR(f, MP_NEW, a->x);
120 dx = F_TPL(f, dx, dx);
121 dx = F_ADD(f, dx, dx, cc->a);
122 dy = F_DBL(f, MP_NEW, a->y);
123 dy = F_INV(f, dy, dy);
124 lambda = F_MUL(d, MP_NEW, dx, dy);
127 dx = F_SQR(f, dx, lambda);
128 dx = F_SUB(f, dx, dx, a->x);
129 dx = F_SUB(f, dx, dx, b->x);
130 dy = F_SUB(f, dy, b->x, dx);
131 dy = F_MUL(f, dy, lambda, dy);
132 dy = F_SUB(f, dy, dy, b->y);
143 /*----- That's all, folks -------------------------------------------------*/