Gather up another utility.
[u/mdw/catacomb] / f-niceprime.c
CommitLineData
f46efa79 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: f-niceprime.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
f46efa79 4 *
5 * Prime fields with efficient reduction for special-form primes
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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"
35#include "mpreduce.h"
36#include "mprand.h"
37
4edc47b8 38/*----- Main code ---------------------------------------------------------*/
f46efa79 39
40typedef struct fctx {
41 field f;
42 mpreduce r;
43} fctx;
44
f46efa79 45/* --- Field operations --- */
46
47static void fdestroy(field *ff)
4edc47b8 48 { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
f46efa79 49
50static mp *frand(field *ff, mp *d, grand *r)
4edc47b8 51 { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
f46efa79 52
4edc47b8 53static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
f46efa79 54
55static mp *fneg(field *ff, mp *d, mp *x)
4edc47b8 56 { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
f46efa79 57
4edc47b8 58static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
59 fctx *f = (fctx *)ff; d = mp_add(d, x, y);
60 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
61 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 62 return (d);
63}
64
4edc47b8 65static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
66 fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
67 if (d->f & MP_NEG) d = mp_add(d, d, f->r.p);
68 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 69 return (d);
70}
71
4edc47b8 72static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
73 fctx *f = (fctx *)ff; d = mp_mul(d, x, y);
f46efa79 74 return (mpreduce_do(&f->r, d, d));
75}
76
4edc47b8 77static mp *fsqr(field *ff, mp *d, mp *x) {
78 fctx *f = (fctx *)ff; d = mp_sqr(d, x);
f46efa79 79 return (mpreduce_do(&f->r, d, d));
80}
81
82static mp *finv(field *ff, mp *d, mp *x)
b817bfc6 83 { fctx *f = (fctx *)ff; d = mp_modinv(d, x, f->r.p); return (d); }
f46efa79 84
85static mp *freduce(field *ff, mp *d, mp *x)
4edc47b8 86 { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
f46efa79 87
88static mp *fsqrt(field *ff, mp *d, mp *x)
4edc47b8 89 { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
f46efa79 90
4edc47b8 91static mp *fdbl(field *ff, mp *d, mp *x) {
92 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
93 if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 94 return (d);
95}
96
4edc47b8 97static mp *ftpl(field *ff, mp *d, mp *x) {
98 fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
f46efa79 99 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
4edc47b8 100 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 101 return (d);
102}
103
4edc47b8 104static mp *fqdl(field *ff, mp *d, mp *x) {
105 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
106 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
f46efa79 107 return (d);
108}
109
4edc47b8 110static mp *fhlv(field *ff, mp *d, mp *x) {
f46efa79 111 fctx *f = (fctx *)ff;
4edc47b8 112 if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); }
113 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
f46efa79 114 return (mp_lsr(d, x, 1));
115}
116
117/* --- Field operations table --- */
118
4e66da02 119static const field_ops fops = {
f46efa79 120 FTY_PRIME, "niceprime",
34e4f738 121 fdestroy, frand, field_stdsamep,
f46efa79 122 freduce, field_id,
123 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
124 0,
125 fdbl, ftpl, fqdl, fhlv
126};
127
128/* --- @field_niceprime@ --- *
129 *
130 * Arguments: @mp *p@ = the characteristic of the field
131 *
132 * Returns: A pointer to the field.
133 *
134 * Use: Creates a field structure for a prime field of size %$p$%,
135 * using efficient reduction for nice primes.
136 */
137
138field *field_niceprime(mp *p)
139{
140 fctx *f = CREATE(fctx);
141 f->f.ops = &fops;
142 f->f.zero = MP_ZERO;
143 f->f.one = MP_ONE;
432c4e18 144 f->f.nbits = mp_bits(p);
145 f->f.noctets = (f->f.nbits + 7) >> 3;
f46efa79 146 mpreduce_create(&f->r, p);
432c4e18 147 f->f.m = f->r.p;
f46efa79 148 return (&f->f);
149}
150
151/*----- That's all, folks -------------------------------------------------*/