3 * $Id: f-prime.c,v 1.6 2004/03/23 15:19:32 mdw Exp $
5 * Prime fields with Montgomery arithmetic
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 --------------------------------------------------*
33 * Revision 1.6 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
36 * Revision 1.5 2004/03/23 12:08:26 mdw
37 * Random field-element selection.
39 * Revision 1.4 2004/03/21 22:52:06 mdw
40 * Merge and close elliptic curve branch.
42 * Revision 1.3.4.3 2004/03/21 22:39:46 mdw
43 * Elliptic curves on binary fields work.
45 * Revision 1.3.4.2 2004/03/20 00:13:31 mdw
46 * Projective coordinates for prime curves
48 * Revision 1.3.4.1 2003/06/10 13:43:53 mdw
49 * Simple (non-projective) curves over prime fields now seem to work.
51 * Revision 1.3 2003/05/15 23:25:59 mdw
52 * Make elliptic curve stuff build.
54 * Revision 1.2 2002/01/13 13:48:44 mdw
57 * Revision 1.1 2001/04/29 18:12:33 mdw
62 /*----- Header files ------------------------------------------------------*/
70 /*----- Data structures ---------------------------------------------------*/
77 /*----- Main code ---------------------------------------------------------*/
79 /* --- Field operations --- */
81 static void fdestroy(field
*ff
)
84 mpmont_destroy(&f
->mm
);
88 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
91 return (mprand_range(d
, f
->mm
.m
, r
, 0));
94 static mp
*fin(field
*ff
, mp
*d
, mp
*x
)
97 mp_div(0, &d
, x
, f
->mm
.m
);
98 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
101 static mp
*fout(field
*ff
, mp
*d
, mp
*x
)
103 fctx
*f
= (fctx
*)ff
;
104 return (mpmont_reduce(&f
->mm
, d
, x
));
107 static int fzerop(field
*ff
, mp
*x
)
112 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
114 fctx
*f
= (fctx
*)ff
;
115 return (mp_sub(d
, f
->mm
.m
, x
));
118 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
120 fctx
*f
= (fctx
*)ff
;
123 d
= mp_add(d
, d
, f
->mm
.m
);
124 else if (MP_CMP(d
, >, f
->mm
.m
))
125 d
= mp_sub(d
, d
, f
->mm
.m
);
129 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
131 fctx
*f
= (fctx
*)ff
;
134 d
= mp_add(d
, d
, f
->mm
.m
);
135 else if (MP_CMP(d
, >, f
->mm
.m
))
136 d
= mp_sub(d
, d
, f
->mm
.m
);
140 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
142 fctx
*f
= (fctx
*)ff
;
143 return (mpmont_mul(&f
->mm
, d
, x
, y
));
146 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
148 fctx
*f
= (fctx
*)ff
;
150 return (mpmont_reduce(&f
->mm
, d
, d
));
153 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
155 fctx
*f
= (fctx
*)ff
;
156 d
= mpmont_reduce(&f
->mm
, d
, x
);
157 mp_gcd(0, 0, &d
, f
->mm
.m
, d
);
158 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
161 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
163 fctx
*f
= (fctx
*)ff
;
164 mp_div(0, &d
, x
, f
->mm
.m
);
168 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
170 fctx
*f
= (fctx
*)ff
;
171 d
= mpmont_reduce(&f
->mm
, d
, x
);
172 d
= mp_modsqrt(d
, d
, f
->mm
.m
);
175 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
178 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
)
180 fctx
*f
= (fctx
*)ff
;
182 if (MP_CMP(d
, >, f
->mm
.m
))
183 d
= mp_sub(d
, d
, f
->mm
.m
);
187 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
)
189 fctx
*f
= (fctx
*)ff
;
190 MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
191 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
192 while (MP_CMP(d
, >, f
->mm
.m
))
193 d
= mp_sub(d
, d
, f
->mm
.m
);
197 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
)
199 fctx
*f
= (fctx
*)ff
;
201 while (MP_CMP(d
, >, f
->mm
.m
))
202 d
= mp_sub(d
, d
, f
->mm
.m
);
206 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
)
208 fctx
*f
= (fctx
*)ff
;
215 d
= mp_add(d
, x
, f
->mm
.m
);
218 return (mp_lsr(d
, x
, 1));
221 /* --- Field operations table --- */
223 static field_ops fops
= {
227 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
229 fdbl
, ftpl
, fqdl
, fhlv
232 /* --- @field_prime@ --- *
234 * Arguments: @mp *p@ = the characteristic of the field
236 * Returns: A pointer to the field.
238 * Use: Creates a field structure for a prime field of size %$p$%,
239 * using Montgomery reduction for arithmetic.
242 field
*field_prime(mp
*p
)
244 fctx
*f
= CREATE(fctx
);
246 mpmont_create(&f
->mm
, p
);
252 /*----- That's all, folks -------------------------------------------------*/