3 * $Id: ec-prime.c,v 1.3.4.1 2003/06/10 13:43:53 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.3.4.1 2003/06/10 13:43:53 mdw
34 * Simple (non-projective) curves over prime fields now seem to work.
36 * Revision 1.3 2003/05/15 23:25:59 mdw
37 * Make elliptic curve stuff build.
39 * Revision 1.2 2002/01/13 13:48:44 mdw
42 * Revision 1.1 2001/04/29 18:12:33 mdw
47 /*----- Header files ------------------------------------------------------*/
53 /*----- Data structures ---------------------------------------------------*/
55 typedef struct ecctx
{
60 /*----- Simple prime curves -----------------------------------------------*/
62 static const ec_ops ec_primeops
;
64 static ec
*ecneg(ec_curve
*c
, ec
*d
, const ec
*p
)
67 d
->y
= F_NEG(c
->f
, d
->y
, d
->y
);
71 static ec
*ecdbl(ec_curve
*c
, ec
*d
, const ec
*a
)
75 else if (!MP_LEN(a
->y
))
79 ecctx
*cc
= (ecctx
*)c
;
83 dx
= F_SQR(f
, MP_NEW
, a
->x
);
84 dy
= F_DBL(f
, MP_NEW
, a
->y
);
85 dx
= F_TPL(f
, dx
, dx
);
86 dx
= F_ADD(f
, dx
, dx
, cc
->a
);
87 dy
= F_INV(f
, dy
, dy
);
88 lambda
= F_MUL(f
, MP_NEW
, dx
, dy
);
90 dx
= F_SQR(f
, dx
, lambda
);
91 dy
= F_DBL(f
, dy
, a
->x
);
92 dx
= F_SUB(f
, dx
, dx
, dy
);
93 dy
= F_SUB(f
, dy
, a
->x
, dx
);
94 dy
= F_MUL(f
, dy
, lambda
, dy
);
95 dy
= F_SUB(f
, dy
, dy
, a
->y
);
106 static ec
*ecadd(ec_curve
*c
, ec
*d
, const ec
*a
, const ec
*b
)
110 else if (EC_ATINF(a
))
112 else if (EC_ATINF(b
))
119 if (!MP_EQ(a
->x
, b
->x
)) {
120 dy
= F_SUB(f
, MP_NEW
, a
->y
, b
->y
);
121 dx
= F_SUB(f
, MP_NEW
, a
->x
, b
->x
);
122 dx
= F_INV(f
, dx
, dx
);
123 lambda
= F_MUL(f
, MP_NEW
, dy
, dx
);
124 } else if (!MP_LEN(a
->y
) || !MP_EQ(a
->y
, b
->y
)) {
128 ecctx
*cc
= (ecctx
*)c
;
129 dx
= F_SQR(f
, MP_NEW
, a
->x
);
130 dx
= F_TPL(f
, dx
, dx
);
131 dx
= F_ADD(f
, dx
, dx
, cc
->a
);
132 dy
= F_DBL(f
, MP_NEW
, a
->y
);
133 dy
= F_INV(f
, dy
, dy
);
134 lambda
= F_MUL(f
, MP_NEW
, dx
, dy
);
137 dx
= F_SQR(f
, dx
, lambda
);
138 dx
= F_SUB(f
, dx
, dx
, a
->x
);
139 dx
= F_SUB(f
, dx
, dx
, b
->x
);
140 dy
= F_SUB(f
, dy
, b
->x
, dx
);
141 dy
= F_MUL(f
, dy
, lambda
, dy
);
142 dy
= F_SUB(f
, dy
, dy
, b
->y
);
153 static void ecdestroy(ec_curve
*c
)
155 ecctx
*cc
= (ecctx
*)c
;
161 /* --- @ec_prime@, @ec_primeproj@ --- *
163 * Arguments: @field *f@ = the underlying field for this elliptic curve
164 * @mp *a, *b@ = the coefficients for this curve
166 * Returns: A pointer to the curve.
168 * Use: Creates a curve structure for an elliptic curve defined over
169 * a prime field. The @primeproj@ variant uses projective
170 * coordinates, which can be a win.
173 extern ec_curve
*ec_prime(field
*f
, mp
*a
, mp
*b
)
175 ecctx
*cc
= CREATE(ecctx
);
176 cc
->c
.ops
= &ec_primeops
;
178 cc
->a
= F_IN(f
, MP_NEW
, a
);
179 cc
->b
= F_IN(f
, MP_NEW
, b
);
183 static const ec_ops ec_primeops
= {
184 ecdestroy
, ec_idin
, ec_idout
, 0, ecneg
, ecadd
, ec_stdsub
, ecdbl
187 /*----- Test rig ----------------------------------------------------------*/
191 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
197 ec g
= EC_INIT
, d
= EC_INIT
;
200 printf("ec-prime: ");
203 b
= MP(0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1);
204 p
= MP(6277101735386680763835789423207666416083908700390324961279);
205 r
= MP(6277101735386680763835789423176059013767194773182842284080);
208 c
= ec_prime(f
, a
, b
);
210 g
.x
= MP(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012);
211 g
.y
= MP(0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811);
213 ec_mul(c
, &d
, &g
, r
);
215 fprintf(stderr
, "zero too early\n");
218 ec_add(c
, &d
, &d
, &g
);
220 fprintf(stderr
, "didn't reach zero\n");
221 MP_EPRINT("d.x", d
.x
);
222 MP_EPRINT("d.y", d
.y
);
230 MP_DROP(p
); MP_DROP(a
); MP_DROP(b
); MP_DROP(r
);
231 assert(!mparena_count(&mparena_global
));
238 /*----- That's all, folks -------------------------------------------------*/