3 * $Id: f-niceprime.c,v 1.5 2004/04/02 01:03:49 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.5 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.4 2004/04/01 21:28:41 mdw
37 * Normal basis support (translates to poly basis internally). Rewrite
38 * EC and prime group table generators in awk, so that they can reuse data
39 * for repeated constants.
41 * Revision 1.3 2004/04/01 12:50:09 mdw
42 * Add cyclic group abstraction, with test code. Separate off exponentation
43 * functions for better static linking. Fix a buttload of bugs on the way.
44 * Generally ensure that negative exponents do inversion correctly. Add
45 * table of standard prime-field subgroups. (Binary field subgroups are
46 * currently unimplemented but easy to add if anyone ever finds a good one.)
48 * Revision 1.2 2004/03/27 17:54:11 mdw
49 * Standard curves and curve checking.
51 * Revision 1.1 2004/03/27 00:04:46 mdw
52 * Implement efficient reduction for pleasant-looking primes.
56 /*----- Header files ------------------------------------------------------*/
64 /*----- Main code ---------------------------------------------------------*/
71 /* --- Field operations --- */
73 static void fdestroy(field
*ff
)
74 { fctx
*f
= (fctx
*)ff
; mpreduce_destroy(&f
->r
); DESTROY(f
); }
76 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
77 { fctx
*f
= (fctx
*)ff
; return (mprand_range(d
, f
->r
.p
, r
, 0)); }
79 static int fzerop(field
*ff
, mp
*x
) { return (!MP_LEN(x
)); }
81 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
82 { fctx
*f
= (fctx
*)ff
; return (mp_sub(d
, f
->r
.p
, x
)); }
84 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
85 fctx
*f
= (fctx
*)ff
; d
= mp_add(d
, x
, y
);
86 if (d
->f
& MP_NEG
) d
= mp_add(d
, d
, f
->r
.p
);
87 else if (MP_CMP(d
, >, f
->r
.p
)) d
= mp_sub(d
, d
, f
->r
.p
);
91 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
92 fctx
*f
= (fctx
*)ff
; d
= mp_sub(d
, x
, y
);
93 if (d
->f
& MP_NEG
) d
= mp_add(d
, d
, f
->r
.p
);
94 else if (MP_CMP(d
, >, f
->r
.p
)) d
= mp_sub(d
, d
, f
->r
.p
);
98 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
99 fctx
*f
= (fctx
*)ff
; d
= mp_mul(d
, x
, y
);
100 return (mpreduce_do(&f
->r
, d
, d
));
103 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
) {
104 fctx
*f
= (fctx
*)ff
; d
= mp_sqr(d
, x
);
105 return (mpreduce_do(&f
->r
, d
, d
));
108 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
109 { fctx
*f
= (fctx
*)ff
; mp_gcd(0, 0, &d
, f
->r
.p
, x
); return (d
); }
111 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
112 { fctx
*f
= (fctx
*)ff
; return (mpreduce_do(&f
->r
, d
, x
)); }
114 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
115 { fctx
*f
= (fctx
*)ff
; return (mp_modsqrt(d
, x
, f
->r
.p
)); }
117 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
) {
118 fctx
*f
= (fctx
*)ff
; d
= mp_lsl(d
, x
, 1);
119 if (MP_CMP(d
, >, f
->r
.p
)) d
= mp_sub(d
, d
, f
->r
.p
);
123 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
) {
124 fctx
*f
= (fctx
*)ff
; MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
125 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
126 while (MP_CMP(d
, >, f
->r
.p
)) d
= mp_sub(d
, d
, f
->r
.p
);
130 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
) {
131 fctx
*f
= (fctx
*)ff
; d
= mp_lsl(d
, x
, 2);
132 while (MP_CMP(d
, >, f
->r
.p
)) d
= mp_sub(d
, d
, f
->r
.p
);
136 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
) {
137 fctx
*f
= (fctx
*)ff
;
138 if (!MP_LEN(x
)) { MP_COPY(x
); MP_DROP(d
); return (x
); }
139 if (x
->v
[0] & 1) { d
= mp_add(d
, x
, f
->r
.p
); x
= d
; }
140 return (mp_lsr(d
, x
, 1));
143 /* --- Field operations table --- */
145 static const field_ops fops
= {
146 FTY_PRIME
, "niceprime",
147 fdestroy
, frand
, field_stdsamep
,
149 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
151 fdbl
, ftpl
, fqdl
, fhlv
154 /* --- @field_niceprime@ --- *
156 * Arguments: @mp *p@ = the characteristic of the field
158 * Returns: A pointer to the field.
160 * Use: Creates a field structure for a prime field of size %$p$%,
161 * using efficient reduction for nice primes.
164 field
*field_niceprime(mp
*p
)
166 fctx
*f
= CREATE(fctx
);
170 f
->f
.nbits
= mp_bits(p
);
171 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
172 mpreduce_create(&f
->r
, p
);
177 /*----- That's all, folks -------------------------------------------------*/