3 * $Id: f-prime.c,v 1.2 2002/01/13 13:48:44 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.2 2002/01/13 13:48:44 mdw
36 * Revision 1.1 2001/04/29 18:12:33 mdw
41 /*----- Header files ------------------------------------------------------*/
48 /*----- Data structures ---------------------------------------------------*/
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- Field operations --- */
59 static void fdestroy(field
*ff
)
62 mpmont_destroy(&f
->mm
);
66 static mp
*fin(field
*ff
, mp
*d
, mp
*x
)
69 return (mpmont_mul(&f
->mm
, d
, x
, f
->mm
.r2
));
72 static mp
*fout(field
*ff
, mp
*d
, mp
*x
)
75 return (mpmont_reduce(&f
->mm
, d
, x
));
78 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
81 return (mp_sub(d
, f
->mm
.m
, x
));
84 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
86 return (mp_add(d
, x
, y
));
89 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
91 return (mp_sub(d
, x
, y
));
94 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
97 return (mpmont_mul(&f
->mm
, d
, x
, y
));
100 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
)
102 fctx
*f
= (fctx
*)ff
;
104 return (mpmont_reduce(&f
->mm
, d
, d
));
107 static mp
*finv(field
*ff
, mp
*d
, mp
*x
)
109 fctx
*f
= (fctx
*)ff
;
110 d
= mpmont_reduce(&f
->mm
, x
);
111 mp_gcd(0, 0, &d
, f
->mm
.m
, d
);
112 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
115 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
117 fctx
*f
= (fctx
*)ff
;
118 mp_div(0, &d
, x
, f
->mm
.m
);
122 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
)
124 fctx
*f
= (fctx
*)ff
;
125 return (mp_lsl(d
, x
, 1));
128 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
)
130 fctx
*f
= (fctx
*)ff
;
131 MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
132 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
136 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
)
138 fctx
*f
= (fctx
*)ff
;
139 d
= mpmont_reduce(&f
->mm
, x
);
140 d
= mp_modsqrt(d
, d
, f
->mm
.m
);
141 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
144 /* --- Field operations table --- */
146 static field_ops fops
= {
149 fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
,
153 /* --- @field_prime@ --- *
155 * Arguments: @mp *p@ = the characteristic of the field
157 * Returns: A pointer to the field.
159 * Use: Creates a field structure for a prime field of size %$p$%,
160 * using Montgomery reduction for arithmetic.
163 field
*field_prime(mp
*p
)
165 ftcx
*f
= CREATE(fctx
);
167 mpmont_create(&f
->mm
, p
);
173 /*----- That's all, folks -------------------------------------------------*/