5ab4204fdb1b9fa4bf38a230eb127b5037bbc725
3 * $Id: f-prime.c,v 1.5 2004/03/23 12:08:26 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.5 2004/03/23 12:08:26 mdw
34 * Random field-element selection.
36 * Revision 1.4 2004/03/21 22:52:06 mdw
37 * Merge and close elliptic curve branch.
39 * Revision 1.3.4.3 2004/03/21 22:39:46 mdw
40 * Elliptic curves on binary fields work.
42 * Revision 1.3.4.2 2004/03/20 00:13:31 mdw
43 * Projective coordinates for prime curves
45 * Revision 1.3.4.1 2003/06/10 13:43:53 mdw
46 * Simple (non-projective) curves over prime fields now seem to work.
48 * Revision 1.3 2003/05/15 23:25:59 mdw
49 * Make elliptic curve stuff build.
51 * Revision 1.2 2002/01/13 13:48:44 mdw
54 * Revision 1.1 2001/04/29 18:12:33 mdw
59 /*----- Header files ------------------------------------------------------*/
67 /*----- Data structures ---------------------------------------------------*/
74 /*----- Main code ---------------------------------------------------------*/
76 /* --- Field operations --- */
78 static void fdestroy(field
*ff
)
81 mpmont_destroy(&f
->mm
);
85 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
88 return (mprand_range(d
, f
->mm
.m
, r
, 0));
91 static mp
*fin(field
*ff
, mp
*d
, mp
*x
)
94 mp_div(0, &d
, x
, f
->mm
.m
);
95 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
98 static mp
*fout(field
*ff
, mp
*d
, mp
*x
)
100 fctx
*f
= (fctx
*)ff
;
101 return (mpmont_reduce(&f
->mm
, d
, x
));
104 static int fzerop(field
*ff
, mp
*x
)
109 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
111 fctx
*f
= (fctx
*)ff
;
112 return (mp_sub(d
, f
->mm
.m
, x
));
115 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
117 fctx
*f
= (fctx
*)ff
;
120 d
= mp_add(d
, d
, f
->mm
.m
);
121 else if (MP_CMP(d
, >, f
->mm
.m
))
122 d
= mp_sub(d
, d
, f
->mm
.m
);
126 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
128 fctx
*f
= (fctx
*)ff
;
131 d
= mp_add(d
, d
, f
->mm
.m
);
132 else if (MP_CMP(d
, >, f
->mm
.m
))
133 d
= mp_sub(d
, d
, f
->mm
.m
);
137 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
139 fctx
*f
= (fctx
*)ff
;
140 return (mpmont_mul(&f
->mm
, d
, x
, y
));
143 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
145 fctx
*f
= (fctx
*)ff
;
147 return (mpmont_reduce(&f
->mm
, d
, d
));
150 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
152 fctx
*f
= (fctx
*)ff
;
153 d
= mpmont_reduce(&f
->mm
, d
, x
);
154 mp_gcd(0, 0, &d
, f
->mm
.m
, d
);
155 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
158 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
160 fctx
*f
= (fctx
*)ff
;
161 mp_div(0, &d
, x
, f
->mm
.m
);
165 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
167 fctx
*f
= (fctx
*)ff
;
168 d
= mpmont_reduce(&f
->mm
, d
, x
);
169 d
= mp_modsqrt(d
, d
, f
->mm
.m
);
172 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
175 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
)
177 fctx
*f
= (fctx
*)ff
;
179 if (MP_CMP(d
, >, f
->mm
.m
))
180 d
= mp_sub(d
, d
, f
->mm
.m
);
184 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
)
186 fctx
*f
= (fctx
*)ff
;
187 MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
188 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
189 while (MP_CMP(d
, >, f
->mm
.m
))
190 d
= mp_sub(d
, d
, f
->mm
.m
);
194 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
)
196 fctx
*f
= (fctx
*)ff
;
198 while (MP_CMP(d
, >, f
->mm
.m
))
199 d
= mp_sub(d
, d
, f
->mm
.m
);
203 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
)
205 fctx
*f
= (fctx
*)ff
;
212 d
= mp_add(d
, x
, f
->mm
.m
);
215 return (mp_lsr(d
, x
, 1));
218 /* --- Field operations table --- */
220 static field_ops fops
= {
223 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
225 fdbl
, ftpl
, fqdl
, fhlv
228 /* --- @field_prime@ --- *
230 * Arguments: @mp *p@ = the characteristic of the field
232 * Returns: A pointer to the field.
234 * Use: Creates a field structure for a prime field of size %$p$%,
235 * using Montgomery reduction for arithmetic.
238 field
*field_prime(mp
*p
)
240 fctx
*f
= CREATE(fctx
);
242 mpmont_create(&f
->mm
, p
);
248 /*----- That's all, folks -------------------------------------------------*/