4ffc028e71bf6aeee969faf23055b734c4c9e450
3 * $Id: f-niceprime.c,v 1.3 2004/04/01 12:50:09 mdw Exp $
5 * Prime fields with efficient reduction for special-form primes
7 * (c) 2004 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: f-niceprime.c,v $
33 * Revision 1.3 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.2 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
43 * Revision 1.1 2004/03/27 00:04:46 mdw
44 * Implement efficient reduction for pleasant-looking primes.
48 /*----- Header files ------------------------------------------------------*/
56 /*----- Data structures ---------------------------------------------------*/
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- Field operations --- */
67 static void fdestroy(field
*ff
)
70 mpreduce_destroy(&f
->r
);
74 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
77 return (mprand_range(d
, f
->r
.p
, r
, 0));
80 static int fzerop(field
*ff
, mp
*x
)
85 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
88 return (mp_sub(d
, f
->r
.p
, x
));
91 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
96 d
= mp_add(d
, d
, f
->r
.p
);
97 else if (MP_CMP(d
, >, f
->r
.p
))
98 d
= mp_sub(d
, d
, f
->r
.p
);
102 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
104 fctx
*f
= (fctx
*)ff
;
107 d
= mp_add(d
, d
, f
->r
.p
);
108 else if (MP_CMP(d
, >, f
->r
.p
))
109 d
= mp_sub(d
, d
, f
->r
.p
);
113 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
115 fctx
*f
= (fctx
*)ff
;
117 return (mpreduce_do(&f
->r
, d
, d
));
120 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
122 fctx
*f
= (fctx
*)ff
;
124 return (mpreduce_do(&f
->r
, d
, d
));
127 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
129 fctx
*f
= (fctx
*)ff
;
130 mp_gcd(0, 0, &d
, f
->r
.p
, x
);
134 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
136 fctx
*f
= (fctx
*)ff
;
137 return (mpreduce_do(&f
->r
, d
, x
));
140 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
142 fctx
*f
= (fctx
*)ff
;
143 return (mp_modsqrt(d
, x
, f
->r
.p
));
146 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
)
148 fctx
*f
= (fctx
*)ff
;
150 if (MP_CMP(d
, >, f
->r
.p
))
151 d
= mp_sub(d
, d
, f
->r
.p
);
155 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
)
157 fctx
*f
= (fctx
*)ff
;
158 MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
159 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
160 while (MP_CMP(d
, >, f
->r
.p
))
161 d
= mp_sub(d
, d
, f
->r
.p
);
165 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
)
167 fctx
*f
= (fctx
*)ff
;
169 while (MP_CMP(d
, >, f
->r
.p
))
170 d
= mp_sub(d
, d
, f
->r
.p
);
174 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
)
176 fctx
*f
= (fctx
*)ff
;
183 d
= mp_add(d
, x
, f
->r
.p
);
186 return (mp_lsr(d
, x
, 1));
189 /* --- Field operations table --- */
191 static field_ops fops
= {
192 FTY_PRIME
, "niceprime",
193 fdestroy
, frand
, field_stdsamep
,
195 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
197 fdbl
, ftpl
, fqdl
, fhlv
200 /* --- @field_niceprime@ --- *
202 * Arguments: @mp *p@ = the characteristic of the field
204 * Returns: A pointer to the field.
206 * Use: Creates a field structure for a prime field of size %$p$%,
207 * using efficient reduction for nice primes.
210 field
*field_niceprime(mp
*p
)
212 fctx
*f
= CREATE(fctx
);
216 f
->f
.nbits
= mp_bits(p
);
217 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
218 mpreduce_create(&f
->r
, p
);
223 /*----- That's all, folks -------------------------------------------------*/