5 * Point multiplication for elliptic curves
7 * (c) 2004 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 ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @ec_imul@, @ec_mul@ --- *
39 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
40 * @ec *d@ = pointer to the destination point
41 * @const ec *p@ = pointer to the generator point
42 * @mp *n@ = integer multiplier
44 * Returns: The destination @d@.
46 * Use: Multiplies a point by a scalar, returning %$n p$%. The
47 * @imul@ variant uses internal representations for argument
51 ec
*ec_imul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
56 if (t
.x
&& (n
->f
& MP_BURN
))
65 if (MP_LEN(n
) < EXP_THRESH
)
74 ec
*ec_mul(ec_curve
*c
, ec
*d
, const ec
*p
, mp
*n
)
78 return (EC_OUT(c
, d
, d
));
81 /* --- @ec_mmul@, @ec_immul@ --- *
83 * Arguments: @ec_curve *c@ = pointer to an elliptic curve
84 * @ec *d@ = pointer to the destination point
85 * @const ec_mulfactor *f@ = pointer to vector of factors
86 * @size_t n@ = number of factors
88 * Returns: The destination @d@.
90 * Use: Does simultaneous point multiplication. The @immul@ variant
91 * uses internal representations for arguments and result.
97 static ec
*immul(ec_curve
*c
, ec
*d
, ec_mulfactor
*f
, size_t n
)
101 for (i
= 0; i
< n
; i
++) {
103 if (MP_NEGP(f
[i
].exp
))
104 EC_NEG(c
, &f
[i
].base
, &f
[i
].base
);
105 if (f
[i
].base
.x
&& f
[i
].exp
->f
& MP_BURN
)
106 f
[i
].base
.x
->f
|= MP_BURN
;
110 for (i
= 0; i
< n
; i
++)
111 EC_DESTROY(&f
[i
].base
);
116 ec
*ec_immul(ec_curve
*c
, ec
*d
, const ec_mulfactor
*f
, size_t n
)
118 ec_mulfactor
*ff
= xmalloc(n
* sizeof(ec_mulfactor
));
121 for (i
= 0; i
< n
; i
++) {
122 EC_CREATE(&ff
[i
].base
);
123 EC_COPY(&ff
[i
].base
, &f
[i
].base
);
124 ff
[i
].exp
= f
[i
].exp
;
126 return (immul(c
, d
, ff
, n
));
129 ec
*ec_mmul(ec_curve
*c
, ec
*d
, const ec_mulfactor
*f
, size_t n
)
131 ec_mulfactor
*ff
= xmalloc(n
* sizeof(ec_mulfactor
));
134 for (i
= 0; i
< n
; i
++) {
135 EC_CREATE(&ff
[i
].base
);
136 EC_IN(c
, &ff
[i
].base
, &f
[i
].base
);
137 ff
[i
].exp
= f
[i
].exp
;
140 return (EC_OUT(c
, d
, d
));
143 /*----- That's all, folks -------------------------------------------------*/