3 * $Id: f-prime.c,v 1.12 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
45 /* --- Field operations --- */
47 static void fdestroy(field
*ff
)
48 { fctx
*f
= (fctx
*)ff
; mpmont_destroy(&f
->mm
); DESTROY(f
); }
50 static mp
*frand(field
*ff
, mp
*d
, grand
*r
)
51 { fctx
*f
= (fctx
*)ff
; return (mprand_range(d
, f
->mm
.m
, r
, 0)); }
53 static mp
*fin(field
*ff
, mp
*d
, mp
*x
) {
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
*f
= (fctx
*)ff
; return (mpmont_reduce(&f
->mm
, d
, x
)); }
62 static int fzerop(field
*ff
, mp
*x
) { return (!MP_LEN(x
)); }
64 static mp
*fneg(field
*ff
, mp
*d
, mp
*x
)
65 { fctx
*f
= (fctx
*)ff
; return (mp_sub(d
, f
->mm
.m
, x
)); }
67 static mp
*fadd(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
68 fctx
*f
= (fctx
*)ff
; d
= mp_add(d
, x
, y
);
69 if (d
->f
& MP_NEG
) d
= mp_add(d
, d
, f
->mm
.m
);
70 else if (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
74 static mp
*fsub(field
*ff
, mp
*d
, mp
*x
, mp
*y
) {
75 fctx
*f
= (fctx
*)ff
; d
= mp_sub(d
, x
, y
);
76 if (d
->f
& MP_NEG
) d
= mp_add(d
, d
, f
->mm
.m
);
77 else if (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
81 static mp
*fmul(field
*ff
, mp
*d
, mp
*x
, mp
*y
)
82 { fctx
*f
= (fctx
*)ff
; return (mpmont_mul(&f
->mm
, d
, x
, y
)); }
84 static mp
*fsqr(field
*ff
, mp
*d
, mp
*x
) {
85 fctx
*f
= (fctx
*)ff
; d
= mp_sqr(d
, x
);
86 return (mpmont_reduce(&f
->mm
, d
, d
));
89 static mp
*finv(field
*ff
, mp
*d
, mp
*x
) {
90 fctx
*f
= (fctx
*)ff
; d
= mpmont_reduce(&f
->mm
, d
, x
);
91 d
= mp_modinv(d
, d
, f
->mm
.m
); return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
94 static mp
*freduce(field
*ff
, mp
*d
, mp
*x
)
95 { fctx
*f
= (fctx
*)ff
; mp_div(0, &d
, x
, f
->mm
.m
); return (d
); }
97 static mp
*fsqrt(field
*ff
, mp
*d
, mp
*x
) {
98 fctx
*f
= (fctx
*)ff
; d
= mpmont_reduce(&f
->mm
, d
, x
);
99 d
= mp_modsqrt(d
, d
, f
->mm
.m
); if (!d
) return (d
);
100 return (mpmont_mul(&f
->mm
, d
, d
, f
->mm
.r2
));
103 static mp
*fdbl(field
*ff
, mp
*d
, mp
*x
) {
104 fctx
*f
= (fctx
*)ff
; d
= mp_lsl(d
, x
, 1);
105 if (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
109 static mp
*ftpl(field
*ff
, mp
*d
, mp
*x
) {
110 fctx
*f
= (fctx
*)ff
; MP_DEST(d
, MP_LEN(x
) + 1, x
->f
);
111 MPX_UMULN(d
->v
, d
->vl
, x
->v
, x
->vl
, 3);
112 while (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
116 static mp
*fqdl(field
*ff
, mp
*d
, mp
*x
) {
117 fctx
*f
= (fctx
*)ff
; d
= mp_lsl(d
, x
, 2);
118 while (MP_CMP(d
, >, f
->mm
.m
)) d
= mp_sub(d
, d
, f
->mm
.m
);
122 static mp
*fhlv(field
*ff
, mp
*d
, mp
*x
) {
123 fctx
*f
= (fctx
*)ff
;
124 if (!MP_LEN(x
)) { MP_COPY(x
); MP_DROP(d
); return (x
); }
125 if (x
->v
[0] & 1) { d
= mp_add(d
, x
, f
->mm
.m
); x
= d
; }
126 return (mp_lsr(d
, x
, 1));
129 /* --- Field operations table --- */
131 static const field_ops fops
= {
133 fdestroy
, frand
, field_stdsamep
,
135 fzerop
, fneg
, fadd
, fsub
, fmul
, fsqr
, finv
, freduce
, fsqrt
,
137 fdbl
, ftpl
, fqdl
, fhlv
140 /* --- @field_prime@ --- *
142 * Arguments: @mp *p@ = the characteristic of the field
144 * Returns: A pointer to the field or null.
146 * Use: Creates a field structure for a prime field of size %$p$%,
147 * using Montgomery reduction for arithmetic.
150 field
*field_prime(mp
*p
)
154 if (!MP_ISPOS(p
) || !MP_ISODD(p
))
158 mpmont_create(&f
->mm
, p
);
162 f
->f
.nbits
= mp_bits(p
);
163 f
->f
.noctets
= (f
->f
.nbits
+ 7) >> 3;
167 /*----- That's all, folks -------------------------------------------------*/