From 9b8b6877809f0626e8e3eff1f731b1daa796fecb Mon Sep 17 00:00:00 2001 From: mdw Date: Tue, 23 Mar 2004 12:08:26 +0000 Subject: [PATCH] Random field-element selection. --- f-binpoly.c | 14 ++++++++++++-- f-prime.c | 14 ++++++++++++-- field.h | 10 +++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/f-binpoly.c b/f-binpoly.c index 02e683d..463dbb2 100644 --- a/f-binpoly.c +++ b/f-binpoly.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-binpoly.c,v 1.2 2004/03/21 22:52:06 mdw Exp $ + * $Id: f-binpoly.c,v 1.3 2004/03/23 12:08:26 mdw Exp $ * * Binary fields with polynomial basis representation * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: f-binpoly.c,v $ + * Revision 1.3 2004/03/23 12:08:26 mdw + * Random field-element selection. + * * Revision 1.2 2004/03/21 22:52:06 mdw * Merge and close elliptic curve branch. * @@ -45,6 +48,7 @@ #include "field.h" #include "gf.h" #include "gfreduce.h" +#include "mprand.h" /*----- Data structures ---------------------------------------------------*/ @@ -64,6 +68,12 @@ static void fdestroy(field *ff) DESTROY(f); } +static mp *frand(field *ff, grand *r, mp *d) +{ + fctx *f = (fctx *)ff; + return (mprand(d, mp_octets(f->r.p) - 1, r, 0)); +} + static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); @@ -116,7 +126,7 @@ static mp *fquadsolve(field *ff, mp *d, mp *x) /* --- Field operations table --- */ static field_ops fops = { - fdestroy, + fdestroy, frand, freduce, field_id, fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt, fquadsolve, diff --git a/f-prime.c b/f-prime.c index 8454902..5ab4204 100644 --- a/f-prime.c +++ b/f-prime.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: f-prime.c,v 1.4 2004/03/21 22:52:06 mdw Exp $ + * $Id: f-prime.c,v 1.5 2004/03/23 12:08:26 mdw Exp $ * * Prime fields with Montgomery arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: f-prime.c,v $ + * Revision 1.5 2004/03/23 12:08:26 mdw + * Random field-element selection. + * * Revision 1.4 2004/03/21 22:52:06 mdw * Merge and close elliptic curve branch. * @@ -59,6 +62,7 @@ #include "field.h" #include "mpmont.h" +#include "mprand.h" /*----- Data structures ---------------------------------------------------*/ @@ -78,6 +82,12 @@ static void fdestroy(field *ff) DESTROY(f); } +static mp *frand(field *ff, mp *d, grand *r) +{ + fctx *f = (fctx *)ff; + return (mprand_range(d, f->mm.m, r, 0)); +} + static mp *fin(field *ff, mp *d, mp *x) { fctx *f = (fctx *)ff; @@ -208,7 +218,7 @@ static mp *fhlv(field *ff, mp *d, mp *x) /* --- Field operations table --- */ static field_ops fops = { - fdestroy, + fdestroy, frand, fin, fout, fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt, 0, diff --git a/field.h b/field.h index dd674c9..909332f 100644 --- a/field.h +++ b/field.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: field.h,v 1.4 2004/03/21 22:52:06 mdw Exp $ + * $Id: field.h,v 1.5 2004/03/23 12:08:26 mdw Exp $ * * Definitions for field arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: field.h,v $ + * Revision 1.5 2004/03/23 12:08:26 mdw + * Random field-element selection. + * * Revision 1.4 2004/03/21 22:52:06 mdw * Merge and close elliptic curve branch. * @@ -59,6 +62,10 @@ /*----- Header files ------------------------------------------------------*/ +#ifndef CATACOMB_GRAND_H +# include "grand.h" +#endif + #ifndef CATACOMB_MP_H # include "mp.h" #endif @@ -75,6 +82,7 @@ typedef struct field_ops { /* --- Universal operations --- */ void (*destroy)(field */*f*/); + mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/); mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/); mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/); -- 2.11.0