3 * Point multiplication for elliptic curves
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 /*----- Main code ---------------------------------------------------------*/
35 /* --- @ec_imul@, @ec_mul@ --- *
37 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
38 * @ec *d@ = pointer to the destination point
39 * @const ec *p@ = pointer to the generator point
40 * @mp *n@ = integer multiplier
42 * Returns: The destination @d@.
44 * Use: Multiplies a point by a scalar, returning %$n p$%. The
45 * @imul@ variant uses internal representations for argument
49 ec *ec_imul(ec_curve *c, ec *d, const ec *p, mp *n)
54 if (t.x && (n->f & MP_BURN))
63 if (MP_LEN(n) < EXP_THRESH)
72 ec *ec_mul(ec_curve *c, ec *d, const ec *p, mp *n)
76 return (EC_OUT(c, d, d));
79 /* --- @ec_mmul@, @ec_immul@ --- *
81 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
82 * @ec *d@ = pointer to the destination point
83 * @const ec_mulfactor *f@ = pointer to vector of factors
84 * @size_t n@ = number of factors
86 * Returns: The destination @d@.
88 * Use: Does simultaneous point multiplication. The @immul@ variant
89 * uses internal representations for arguments and result.
95 static ec *immul(ec_curve *c, ec *d, ec_mulfactor *f, size_t n)
99 for (i = 0; i < n; i++) {
101 if (MP_NEGP(f[i].exp))
102 EC_NEG(c, &f[i].base, &f[i].base);
103 if (f[i].base.x && f[i].exp->f & MP_BURN)
104 f[i].base.x->f |= MP_BURN;
108 for (i = 0; i < n; i++)
109 EC_DESTROY(&f[i].base);
114 ec *ec_immul(ec_curve *c, ec *d, const ec_mulfactor *f, size_t n)
116 ec_mulfactor *ff = xmalloc(n * sizeof(ec_mulfactor));
119 for (i = 0; i < n; i++) {
120 EC_CREATE(&ff[i].base);
121 EC_COPY(&ff[i].base, &f[i].base);
122 ff[i].exp = f[i].exp;
124 return (immul(c, d, ff, n));
127 ec *ec_mmul(ec_curve *c, ec *d, const ec_mulfactor *f, size_t n)
129 ec_mulfactor *ff = xmalloc(n * sizeof(ec_mulfactor));
132 for (i = 0; i < n; i++) {
133 EC_CREATE(&ff[i].base);
134 EC_IN(c, &ff[i].base, &f[i].base);
135 ff[i].exp = f[i].exp;
138 return (EC_OUT(c, d, d));
141 /*----- That's all, folks -------------------------------------------------*/