mpmul.[ch]: Move internal `HWM' and `LWM' constants to implementation.
[u/mdw/catacomb] / f-niceprime.c
CommitLineData
f46efa79 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
f46efa79 4 *
5 * Prime fields with efficient reduction for special-form primes
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
f46efa79 11 *
12 * This file is part of Catacomb.
13 *
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.
45c0fd36 18 *
f46efa79 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.
45c0fd36 23 *
f46efa79 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,
27 * MA 02111-1307, USA.
28 */
29
f46efa79 30/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/sub.h>
33
34#include "field.h"
f94b972d 35#include "field-guts.h"
f46efa79 36#include "mprand.h"
37
4edc47b8 38/*----- Main code ---------------------------------------------------------*/
f46efa79 39
f46efa79 40/* --- Field operations --- */
41
f94b972d 42static void fdestroy(field *ff) {
43 fctx_niceprime *f = (fctx_niceprime *)ff;
44 mpreduce_destroy(&f->r);
45 DESTROY(f);
46}
f46efa79 47
f94b972d 48static mp *frand(field *ff, mp *d, grand *r) {
49 fctx_niceprime *f = (fctx_niceprime *)ff;
50 return (mprand_range(d, f->r.p, r, 0));
51}
f46efa79 52
a69a3efd 53static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
f46efa79 54
f94b972d 55static mp *fneg(field *ff, mp *d, mp *x) {
56 fctx_niceprime *f = (fctx_niceprime *)ff;
57 return (mp_sub(d, f->r.p, x));
58}
f46efa79 59
4edc47b8 60static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
f94b972d 61 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_add(d, x, y);
a69a3efd 62 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
4edc47b8 63 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 64 return (d);
65}
66
4edc47b8 67static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
f94b972d 68 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sub(d, x, y);
a69a3efd 69 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
4edc47b8 70 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 71 return (d);
72}
73
4edc47b8 74static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
f94b972d 75 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_mul(d, x, y);
f46efa79 76 return (mpreduce_do(&f->r, d, d));
77}
78
4edc47b8 79static mp *fsqr(field *ff, mp *d, mp *x) {
f94b972d 80 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_sqr(d, x);
f46efa79 81 return (mpreduce_do(&f->r, d, d));
82}
83
f94b972d 84static mp *finv(field *ff, mp *d, mp *x) {
85 fctx_niceprime *f = (fctx_niceprime *)ff;
86 d = mp_modinv(d, x, f->r.p);
87 return (d);
88}
f46efa79 89
f94b972d 90static mp *freduce(field *ff, mp *d, mp *x) {
91 fctx_niceprime *f = (fctx_niceprime *)ff;
92 return (mpreduce_do(&f->r, d, x));
93}
f46efa79 94
f94b972d 95static mp *fsqrt(field *ff, mp *d, mp *x) {
96 fctx_niceprime *f = (fctx_niceprime *)ff;
97 return (mp_modsqrt(d, x, f->r.p));
98}
f46efa79 99
4edc47b8 100static mp *fdbl(field *ff, mp *d, mp *x) {
f94b972d 101 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 1);
d2f1ffa0 102 if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 103 return (d);
104}
105
4edc47b8 106static mp *ftpl(field *ff, mp *d, mp *x) {
f94b972d 107 fctx_niceprime *f = (fctx_niceprime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
d2f1ffa0 108 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); d->f &= ~MP_UNDEF;
109 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 110 return (d);
111}
112
4edc47b8 113static mp *fqdl(field *ff, mp *d, mp *x) {
f94b972d 114 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 2);
d2f1ffa0 115 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 116 return (d);
117}
118
4edc47b8 119static mp *fhlv(field *ff, mp *d, mp *x) {
f94b972d 120 fctx_niceprime *f = (fctx_niceprime *)ff;
a69a3efd 121 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
4edc47b8 122 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
f46efa79 123 return (mp_lsr(d, x, 1));
124}
125
126/* --- Field operations table --- */
127
4e66da02 128static const field_ops fops = {
f46efa79 129 FTY_PRIME, "niceprime",
34e4f738 130 fdestroy, frand, field_stdsamep,
f46efa79 131 freduce, field_id,
132 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
133 0,
134 fdbl, ftpl, fqdl, fhlv
135};
136
137/* --- @field_niceprime@ --- *
138 *
139 * Arguments: @mp *p@ = the characteristic of the field
140 *
f4535c64 141 * Returns: A pointer to the field, or null.
f46efa79 142 *
143 * Use: Creates a field structure for a prime field of size %$p$%,
144 * using efficient reduction for nice primes.
145 */
146
147field *field_niceprime(mp *p)
148{
f94b972d 149 fctx_niceprime *f = CREATE(fctx_niceprime);
f46efa79 150 f->f.ops = &fops;
151 f->f.zero = MP_ZERO;
152 f->f.one = MP_ONE;
432c4e18 153 f->f.nbits = mp_bits(p);
154 f->f.noctets = (f->f.nbits + 7) >> 3;
f4535c64 155 if (mpreduce_create(&f->r, p)) {
156 DESTROY(f);
157 return (0);
158 }
432c4e18 159 f->f.m = f->r.p;
d2f1ffa0 160 f->f.q = f->r.p;
f46efa79 161 return (&f->f);
162}
163
164/*----- That's all, folks -------------------------------------------------*/