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 /*----- Header files ------------------------------------------------------*/
36 #include "field-guts.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- Field operations --- */
42 static void fdestroy(field
*ff
) {
43 fctx_prime
*f
= (fctx_prime
*)ff
;
44 mpmont_destroy(&f
->mm
);
48 static mp
*frand(field
*ff
, mp
*d
, grand
*r
) {
49 fctx_prime
*f
= (fctx_prime
*)ff
;
50 return (mprand_range(d
, f
->mm
.m
, r
, 0));
53 static mp
*fin(field
*ff
, mp
*d
, mp
*x
) {
54 fctx_prime
*f
= (fctx_prime
*)ff
;
55 mp_div(0, &d
, x
, f
->mm
.m
);
56 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
59 static mp
*fout(field
*ff
, mp
*d
, mp
*x
) {
60 fctx_prime
*f
= (fctx_prime
*)ff
;
61 return (mpmont_reduce(&f
->mm
, d
, x
));
64 static int fzerop(field
*ff
, mp
*x
) { return (MP_ZEROP(x
)); }
66 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
) {
67 fctx_prime
*f
= (fctx_prime
*)ff
;
68 return (mp_sub(d
, f
->mm
.m
, x
));
71 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
72 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mp_add(d
, x
, y
);
73 if (MP_NEGP(d
)) d
= mp_add(d
, d
, f
->mm
.m
);
74 else if (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
78 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
79 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mp_sub(d
, x
, y
);
80 if (MP_NEGP(d
)) d
= mp_add(d
, d
, f
->mm
.m
);
81 else if (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
85 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
86 fctx_prime
*f
= (fctx_prime
*)ff
;
87 return (mpmont_mul(&f
->mm
, d
, x
, y
));
90 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
) {
91 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mp_sqr(d
, x
);
92 return (mpmont_reduce(&f
->mm
, d
, d
));
95 static mp
*finv(field
*ff
, mp
*d
, mp
*x
) {
96 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mpmont_reduce(&f
->mm
, d
, x
);
97 d
= mp_modinv(d
, d
, f
->mm
.m
); return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
100 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
) {
101 fctx_prime
*f
= (fctx_prime
*)ff
;
102 mp_div(0, &d
, x
, f
->mm
.m
);
106 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
) {
107 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mpmont_reduce(&f
->mm
, d
, x
);
108 d
= mp_modsqrt(d
, d
, f
->mm
.m
); if (!d
) return (d
);
109 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
112 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
) {
113 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mp_lsl(d
, x
, 1);
114 if (MP_CMP(d
, >=, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
118 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
) {
119 fctx_prime
*f
= (fctx_prime
*)ff
; MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
120 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3); d
->f
&= ~MP_UNDEF
;
121 while (MP_CMP(d
, >=, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
125 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
) {
126 fctx_prime
*f
= (fctx_prime
*)ff
; d
= mp_lsl(d
, x
, 2);
127 while (MP_CMP(d
, >=, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
131 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
) {
132 fctx_prime
*f
= (fctx_prime
*)ff
;
133 if (MP_ZEROP(x
)) { MP_COPY(x
); MP_DROP(d
); return (x
); }
134 if (x
->v
[0] & 1) { d
= mp_add(d
, x
, f
->mm
.m
); x
= d
; }
135 return (mp_lsr(d
, x
, 1));
138 /* --- Field operations table --- */
140 static const field_ops fops
= {
142 fdestroy
, frand
, field_stdsamep
,
144 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
146 fdbl
, ftpl
, fqdl
, fhlv
149 /* --- @field_prime@ --- *
151 * Arguments: @mp *p@ = the characteristic of the field
153 * Returns: A pointer to the field or null.
155 * Use: Creates a field structure for a prime field of size %$p$%,
156 * using Montgomery reduction for arithmetic.
159 field
*field_prime(mp
*p
)
163 f
= CREATE(fctx_prime
);
165 if (mpmont_create(&f
->mm
, p
)) {
172 f
->f
.nbits
= mp_bits(p
);
173 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
178 /*----- That's all, folks -------------------------------------------------*/