Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dmitry Baryshkov
nettle
Commits
0b511c91
Commit
0b511c91
authored
Sep 22, 2014
by
Niels Möller
Browse files
Use struct ecc_modulo for all mod and redc functions.
parent
a78c9459
Changes
21
Hide whitespace changes
Inline
Side-by-side
ChangeLog
View file @
0b511c91
2014-09-22 Niels Möller <nisse@lysator.liu.se>
* ecc-generic-modp.c (ecc_generic_modp): Deleted file and
function. We no longer need a wrapper around ecc_mod.
* ecc-generic-modq.c (ecc_generic_modq): Likewise deleted.
* Makefile.in (hogweed_SOURCES): Removed ecc-generic-modp.c and
ecc-generic-modq.c.
* ecc-internal.h (typedef ecc_mod_func): Take a const struct
ecc_modulo * argument, not const struct ecc_curve *. Updated all
implementations and all callers.
* ecc-mod.c (ecc_mod): Use struct ecc_modulo to specify the
modulo. Drop input size argument, always reduce from 2*size to
size.
* ecc-internal.h (struct ecc_modulo): New struct, collecting
constants needed for modulo arithmetic.
(struct ecc_curve): Use struct ecc_modulo for p and q arithmetic.
...
...
Makefile.in
View file @
0b511c91
...
...
@@ -162,7 +162,7 @@ hogweed_SOURCES = sexp.c sexp-format.c \
der-iterator.c der2rsa.c der2dsa.c
\
sec-add-1.c sec-sub-1.c sec-modinv.c sec-tabselect.c
\
gmp-glue.c cnd-copy.c
\
ecc-mod.c
ecc-generic-modp.c ecc-generic-modq.c
\
ecc-mod.c
\
ecc-modp.c ecc-modq.c ecc-pp1-redc.c ecc-pm1-redc.c
\
ecc-192.c ecc-224.c ecc-256.c ecc-384.c ecc-521.c
\
ecc-25519.c
\
...
...
ecc-192.c
View file @
0b511c91
...
...
@@ -52,14 +52,14 @@
#define ecc_192_modp nettle_ecc_192_modp
void
ecc_192_modp
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
);
ecc_192_modp
(
const
struct
ecc_
modulo
*
m
,
mp_limb_t
*
rp
);
/* Use that p = 2^{192} - 2^64 - 1, to eliminate 128 bits at a time. */
#elif GMP_NUMB_BITS == 32
/* p is 6 limbs, p = B^6 - B^2 - 1 */
static
void
ecc_192_modp
(
const
struct
ecc_
curve
*
ecc
UNUSED
,
mp_limb_t
*
rp
)
ecc_192_modp
(
const
struct
ecc_
modulo
*
m
UNUSED
,
mp_limb_t
*
rp
)
{
mp_limb_t
cy
;
...
...
@@ -84,7 +84,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
#elif GMP_NUMB_BITS == 64
/* p is 3 limbs, p = B^3 - B - 1 */
static
void
ecc_192_modp
(
const
struct
ecc_
curve
*
ecc
UNUSED
,
mp_limb_t
*
rp
)
ecc_192_modp
(
const
struct
ecc_
modulo
*
m
UNUSED
,
mp_limb_t
*
rp
)
{
mp_limb_t
cy
;
...
...
@@ -107,7 +107,7 @@ ecc_192_modp (const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
}
#else
#define ecc_192_modp ecc_
generic_
mod
p
#define ecc_192_modp ecc_mod
#endif
const
struct
ecc_curve
nettle_secp_192r1
=
...
...
@@ -145,7 +145,7 @@ const struct ecc_curve nettle_secp_192r1 =
ecc_192_modp
,
ECC_REDC_SIZE
>=
1
?
ecc_pp1_redc
:
NULL
,
ecc_192_modp
,
ecc_
generic_
mod
q
,
ecc_mod
,
ecc_add_jjj
,
ecc_mul_a
,
...
...
ecc-224.c
View file @
0b511c91
...
...
@@ -45,11 +45,11 @@
#define USE_REDC 0
#define ecc_224_modp nettle_ecc_224_modp
void
ecc_224_modp
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
);
ecc_224_modp
(
const
struct
ecc_
modulo
*
m
,
mp_limb_t
*
rp
);
#else
#define USE_REDC (ECC_REDC_SIZE != 0)
#define ecc_224_modp ecc_
generic_
mod
p
#define ecc_224_modp ecc_mod
#endif
#include
"ecc-224.h"
...
...
@@ -97,7 +97,7 @@ const struct ecc_curve nettle_secp_224r1 =
ecc_224_modp
,
ecc_224_redc
,
USE_REDC
?
ecc_224_redc
:
ecc_224_modp
,
ecc_
generic_
mod
q
,
ecc_mod
,
ecc_add_jjj
,
ecc_mul_a
,
...
...
ecc-25519.c
View file @
0b511c91
...
...
@@ -58,7 +58,7 @@ ecc_25519_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
#endif
static
void
ecc_25519_modp
(
const
struct
ecc_
curve
*
ecc
UNUSED
,
mp_limb_t
*
rp
)
ecc_25519_modp
(
const
struct
ecc_
modulo
*
m
UNUSED
,
mp_limb_t
*
rp
)
{
mp_limb_t
hi
,
cy
;
...
...
@@ -78,7 +78,7 @@ ecc_25519_modp(const struct ecc_curve *ecc UNUSED, mp_limb_t *rp)
#endif
static
void
ecc_25519_modq
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_25519_modq
(
const
struct
ecc_
modulo
*
q
,
mp_limb_t
*
rp
)
{
mp_size_t
n
;
mp_limb_t
cy
;
...
...
@@ -87,17 +87,17 @@ ecc_25519_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
for
(
n
=
ECC_LIMB_SIZE
;
n
--
>
0
;)
{
cy
=
mpn_submul_1
(
rp
+
n
,
ecc
->
q
.
B_shifted
,
ECC_LIMB_SIZE
,
q
->
B_shifted
,
ECC_LIMB_SIZE
,
rp
[
n
+
ECC_LIMB_SIZE
]);
/* Top limb of mBmodq_shifted is zero, so we get cy == 0 or 1 */
assert
(
cy
<
2
);
cnd_add_n
(
cy
,
rp
+
n
,
ecc
->
q
.
m
,
ECC_LIMB_SIZE
);
cnd_add_n
(
cy
,
rp
+
n
,
q
->
m
,
ECC_LIMB_SIZE
);
}
cy
=
mpn_submul_1
(
rp
,
ecc
->
q
.
m
,
ECC_LIMB_SIZE
,
cy
=
mpn_submul_1
(
rp
,
q
->
m
,
ECC_LIMB_SIZE
,
rp
[
ECC_LIMB_SIZE
-
1
]
>>
(
GMP_NUMB_BITS
-
QHIGH_BITS
));
assert
(
cy
<
2
);
cnd_add_n
(
cy
,
rp
,
ecc
->
q
.
m
,
ECC_LIMB_SIZE
);
cnd_add_n
(
cy
,
rp
,
q
->
m
,
ECC_LIMB_SIZE
);
}
/* Needs 2*ecc->size limbs at rp, and 2*ecc->size additional limbs of
...
...
ecc-256.c
View file @
0b511c91
...
...
@@ -65,22 +65,22 @@ ecc_256_redc (const struct ecc_curve *ecc, mp_limb_t *rp);
#endif
/* !HAVE_NATIVE_ecc_256_redc */
#if ECC_BMODP_SIZE < ECC_LIMB_SIZE
#define ecc_256_modp ecc_
generic_
mod
p
#define ecc_256_modq ecc_
generic_
mod
q
#define ecc_256_modp ecc_mod
#define ecc_256_modq ecc_mod
#elif GMP_NUMB_BITS == 64
static
void
ecc_256_modp
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_256_modp
(
const
struct
ecc_
modulo
*
p
,
mp_limb_t
*
rp
)
{
mp_limb_t
u1
,
u0
;
mp_size_t
n
;
n
=
2
*
ecc
->
p
.
size
;
n
=
2
*
p
->
size
;
u1
=
rp
[
--
n
];
u0
=
rp
[
n
-
1
];
/* This is not particularly fast, but should work well with assembly implementation. */
for
(;
n
>=
ecc
->
p
.
size
;
n
--
)
for
(;
n
>=
p
->
size
;
n
--
)
{
mp_limb_t
q2
,
q1
,
q0
,
t
,
cy
;
...
...
@@ -115,8 +115,8 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
/* We multiply by two low limbs of p, 2^96 - 1, so we could use
shifts rather than mul. */
t
=
mpn_submul_1
(
rp
+
n
-
4
,
ecc
->
p
.
m
,
2
,
q1
);
t
+=
cnd_sub_n
(
q2
,
rp
+
n
-
3
,
ecc
->
p
.
m
,
1
);
t
=
mpn_submul_1
(
rp
+
n
-
4
,
p
->
m
,
2
,
q1
);
t
+=
cnd_sub_n
(
q2
,
rp
+
n
-
3
,
p
->
m
,
1
);
t
+=
(
-
q2
)
&
0xffffffff
;
u0
=
rp
[
n
-
2
];
...
...
@@ -124,7 +124,7 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
u0
-=
t
;
t
=
(
u1
<
cy
);
u1
-=
cy
;
u1
+=
cnd_add_n
(
t
,
rp
+
n
-
4
,
ecc
->
p
.
m
,
3
);
u1
+=
cnd_add_n
(
t
,
rp
+
n
-
4
,
p
->
m
,
3
);
u1
-=
(
-
t
)
&
0xffffffff
;
}
rp
[
2
]
=
u0
;
...
...
@@ -132,17 +132,17 @@ ecc_256_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
}
static
void
ecc_256_modq
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_256_modq
(
const
struct
ecc_
modulo
*
q
,
mp_limb_t
*
rp
)
{
mp_limb_t
u2
,
u1
,
u0
;
mp_size_t
n
;
n
=
2
*
ecc
->
q
.
size
;
n
=
2
*
q
->
size
;
u2
=
rp
[
--
n
];
u1
=
rp
[
n
-
1
];
/* This is not particularly fast, but should work well with assembly implementation. */
for
(;
n
>=
ecc
->
q
.
size
;
n
--
)
for
(;
n
>=
q
->
size
;
n
--
)
{
mp_limb_t
q2
,
q1
,
q0
,
t
,
c1
,
c0
;
...
...
@@ -196,9 +196,9 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
assert
(
q2
<
2
);
c0
=
cnd_sub_n
(
q2
,
rp
+
n
-
3
,
ecc
->
q
.
m
,
1
);
c0
+=
(
-
q2
)
&
ecc
->
q
.
m
[
1
];
t
=
mpn_submul_1
(
rp
+
n
-
4
,
ecc
->
q
.
m
,
2
,
q1
);
c0
=
cnd_sub_n
(
q2
,
rp
+
n
-
3
,
q
->
m
,
1
);
c0
+=
(
-
q2
)
&
q
->
m
[
1
];
t
=
mpn_submul_1
(
rp
+
n
-
4
,
q
->
m
,
2
,
q1
);
c0
+=
t
;
c1
=
c0
<
t
;
...
...
@@ -213,7 +213,7 @@ ecc_256_modq (const struct ecc_curve *ecc, mp_limb_t *rp)
u1
+=
t
;
u2
+=
(
t
<<
32
)
+
(
u0
<
t
);
t
=
cnd_add_n
(
t
,
rp
+
n
-
4
,
ecc
->
q
.
m
,
2
);
t
=
cnd_add_n
(
t
,
rp
+
n
-
4
,
q
->
m
,
2
);
u1
+=
t
;
u2
+=
(
u1
<
t
);
}
...
...
ecc-384.c
View file @
0b511c91
...
...
@@ -62,7 +62,7 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
almost 8 at a time. Do only 7, to avoid additional carry
propagation, followed by 5. */
static
void
ecc_384_modp
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_384_modp
(
const
struct
ecc_
modulo
*
p
,
mp_limb_t
*
rp
)
{
mp_limb_t
cy
,
bw
;
...
...
@@ -99,14 +99,14 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
assert
(
cy
>=
bw
);
cy
-=
bw
;
assert
(
cy
<=
1
);
cy
=
cnd_add_n
(
cy
,
rp
,
ecc
->
Bmodp
,
ECC_LIMB_SIZE
);
cy
=
cnd_add_n
(
cy
,
rp
,
p
->
B
,
ECC_LIMB_SIZE
);
assert
(
cy
==
0
);
}
#elif GMP_NUMB_BITS == 64
/* p is 6 limbs, and B^6 - p = B^2 + 2^32 (B - 1) + 1. Eliminate 3
(almost 4) limbs at a time. */
static
void
ecc_384_modp
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_384_modp
(
const
struct
ecc_
modulo
*
p
,
mp_limb_t
*
rp
)
{
mp_limb_t
tp
[
6
];
mp_limb_t
cy
;
...
...
@@ -140,11 +140,11 @@ ecc_384_modp (const struct ecc_curve *ecc, mp_limb_t *rp)
cy
=
sec_add_1
(
rp
+
5
,
rp
+
5
,
1
,
cy
);
assert
(
cy
<=
1
);
cy
=
cnd_add_n
(
cy
,
rp
,
ecc
->
p
.
B
,
ECC_LIMB_SIZE
);
cy
=
cnd_add_n
(
cy
,
rp
,
p
->
B
,
ECC_LIMB_SIZE
);
assert
(
cy
==
0
);
}
#else
#define ecc_384_modp ecc_
generic_
mod
p
#define ecc_384_modp ecc_mod
#endif
const
struct
ecc_curve
nettle_secp_384r1
=
...
...
@@ -182,7 +182,7 @@ const struct ecc_curve nettle_secp_384r1 =
ecc_384_modp
,
ECC_REDC_SIZE
>
0
?
ecc_pp1_redc
:
NULL
,
ecc_384_modp
,
ecc_
generic_
mod
q
,
ecc_mod
,
ecc_add_jjj
,
ecc_mul_a
,
...
...
ecc-521.c
View file @
0b511c91
...
...
@@ -57,7 +57,7 @@ ecc_521_modp (const struct ecc_curve *ecc, mp_limb_t *rp);
/* Result may be *slightly* larger than 2^521 */
static
void
ecc_521_modp
(
const
struct
ecc_
curve
*
ecc
UNUSED
,
mp_limb_t
*
rp
)
ecc_521_modp
(
const
struct
ecc_
modulo
*
m
UNUSED
,
mp_limb_t
*
rp
)
{
/* FIXME: Should use mpn_addlsh_n_ip1 */
mp_limb_t
hi
;
...
...
@@ -110,7 +110,7 @@ const struct ecc_curve nettle_secp_521r1 =
ecc_521_modp
,
ECC_REDC_SIZE
>
0
?
ecc_pp1_redc
:
NULL
,
ecc_521_modp
,
ecc_
generic_
mod
q
,
ecc_mod
,
ecc_add_jjj
,
ecc_mul_a
,
...
...
ecc-a-to-j.c
View file @
0b511c91
...
...
@@ -47,10 +47,10 @@ ecc_a_to_j (const struct ecc_curve *ecc,
mpn_copyd
(
r
+
ecc
->
p
.
size
,
p
,
2
*
ecc
->
p
.
size
);
mpn_zero
(
r
,
ecc
->
p
.
size
);
ecc
->
modp
(
ecc
,
r
);
ecc
->
modp
(
&
ecc
->
p
,
r
);
mpn_zero
(
r
+
ecc
->
p
.
size
,
ecc
->
p
.
size
);
ecc
->
modp
(
ecc
,
r
+
ecc
->
p
.
size
);
ecc
->
modp
(
&
ecc
->
p
,
r
+
ecc
->
p
.
size
);
}
else
if
(
r
!=
p
)
mpn_copyi
(
r
,
p
,
2
*
ecc
->
p
.
size
);
...
...
ecc-generic-modp.c
deleted
100644 → 0
View file @
a78c9459
/* ecc-generic-modp.c
Copyright (C) 2013 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include
<assert.h>
#include
"ecc-internal.h"
void
ecc_generic_modp
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
rp
)
{
assert
(
ecc
->
p
.
B_size
<
ecc
->
p
.
size
);
ecc_mod
(
&
ecc
->
p
,
rp
,
2
*
ecc
->
p
.
size
);
}
ecc-generic-modq.c
deleted
100644 → 0
View file @
a78c9459
/* ecc-generic-modq.c
Copyright (C) 2013 Niels Möller
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include
<assert.h>
#include
"ecc-internal.h"
void
ecc_generic_modq
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
rp
)
{
assert
(
ecc
->
q
.
B_size
<
ecc
->
q
.
size
);
ecc_mod
(
&
ecc
->
q
,
rp
,
2
*
ecc
->
q
.
size
);
}
ecc-internal.h
View file @
0b511c91
...
...
@@ -40,10 +40,8 @@
#include
"gmp-glue.h"
/* Name mangling */
#define ecc_generic_modp _nettle_ecc_generic_modp
#define ecc_pp1_redc _nettle_ecc_pp1_redc
#define ecc_pm1_redc _nettle_ecc_pm1_redc
#define ecc_generic_modq _nettle_ecc_generic_modq
#define ecc_modp_add _nettle_ecc_modp_add
#define ecc_modp_sub _nettle_ecc_modp_sub
#define ecc_modp_mul_1 _nettle_ecc_modp_mul_1
...
...
@@ -97,7 +95,7 @@ struct ecc_modulo
/* Reduces from 2*ecc->size to ecc->size. */
/* Required to return a result < 2q. This property is inherited by
modp_mul and modp_sqr. */
typedef
void
ecc_mod_func
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
);
typedef
void
ecc_mod_func
(
const
struct
ecc_
modulo
*
m
,
mp_limb_t
*
rp
);
typedef
void
ecc_add_func
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
r
,
...
...
@@ -180,11 +178,9 @@ struct ecc_curve
};
/* In-place reduction. */
ecc_mod_func
ecc_
generic_
mod
p
;
ecc_mod_func
ecc_mod
;
ecc_mod_func
ecc_pp1_redc
;
ecc_mod_func
ecc_pm1_redc
;
ecc_mod_func
ecc_generic_modq
;
void
ecc_modp_add
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
rp
,
...
...
@@ -233,9 +229,6 @@ void
ecc_modq_random
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
xp
,
void
*
ctx
,
nettle_random_func
*
random
,
mp_limb_t
*
scratch
);
void
ecc_mod
(
const
struct
ecc_modulo
*
m
,
mp_limb_t
*
rp
,
mp_size_t
rn
);
void
ecc_hash
(
const
struct
ecc_curve
*
ecc
,
mp_limb_t
*
hp
,
...
...
ecc-j-to-a.c
View file @
0b511c91
...
...
@@ -73,16 +73,16 @@ ecc_j_to_a (const struct ecc_curve *ecc,
mpn_copyi
(
up
,
p
+
2
*
ecc
->
p
.
size
,
ecc
->
p
.
size
);
mpn_zero
(
up
+
ecc
->
p
.
size
,
ecc
->
p
.
size
);
ecc
->
redc
(
ecc
,
up
);
ecc
->
redc
(
&
ecc
->
p
,
up
);
mpn_zero
(
up
+
ecc
->
p
.
size
,
ecc
->
p
.
size
);
ecc
->
redc
(
ecc
,
up
);
ecc
->
redc
(
&
ecc
->
p
,
up
);
ecc_modp_inv
(
ecc
,
izp
,
up
,
up
+
ecc
->
p
.
size
);
/* Divide this common factor by B */
mpn_copyi
(
izBp
,
izp
,
ecc
->
p
.
size
);
mpn_zero
(
izBp
+
ecc
->
p
.
size
,
ecc
->
p
.
size
);
ecc
->
redc
(
ecc
,
izBp
);
ecc
->
redc
(
&
ecc
->
p
,
izBp
);
ecc_modp_mul
(
ecc
,
iz2p
,
izp
,
izBp
);
}
...
...
ecc-mod.c
View file @
0b511c91
...
...
@@ -39,14 +39,15 @@
#include
"ecc-internal.h"
/* Computes r mod m. */
/* Computes r mod m
, input 2*m->size, output m->size
. */
void
ecc_mod
(
const
struct
ecc_modulo
*
m
,
mp_limb_t
*
rp
,
mp_size_t
rn
)
ecc_mod
(
const
struct
ecc_modulo
*
m
,
mp_limb_t
*
rp
)
{
mp_limb_t
hi
;
mp_size_t
mn
=
m
->
size
;
mp_size_t
bn
=
m
->
B_size
;
mp_size_t
sn
=
mn
-
bn
;
mp_size_t
rn
=
2
*
mn
;
mp_size_t
i
;
unsigned
shift
;
...
...
ecc-modp.c
View file @
0b511c91
...
...
@@ -115,7 +115,7 @@ ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const
mp_limb_t
*
ap
,
const
mp_limb_t
*
bp
)
{
mpn_mul_n
(
rp
,
ap
,
bp
,
ecc
->
p
.
size
);
ecc
->
reduce
(
ecc
,
rp
);
ecc
->
reduce
(
&
ecc
->
p
,
rp
);
}
void
...
...
@@ -123,7 +123,7 @@ ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp,
const
mp_limb_t
*
ap
)
{
mpn_sqr
(
rp
,
ap
,
ecc
->
p
.
size
);
ecc
->
reduce
(
ecc
,
rp
);
ecc
->
reduce
(
&
ecc
->
p
,
rp
);
}
void
...
...
ecc-modq.c
View file @
0b511c91
...
...
@@ -57,7 +57,7 @@ ecc_modq_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
const
mp_limb_t
*
ap
,
const
mp_limb_t
*
bp
)
{
mpn_mul_n
(
rp
,
ap
,
bp
,
ecc
->
q
.
size
);
ecc
->
modq
(
ecc
,
rp
);
ecc
->
modq
(
&
ecc
->
q
,
rp
);
}
void
...
...
ecc-pm1-redc.c
View file @
0b511c91
...
...
@@ -42,27 +42,27 @@
/* Use that 1 = - (p - 1) (mod p), and that at least one low limb of p
- 1 is zero. */
void
ecc_pm1_redc
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_pm1_redc
(
const
struct
ecc_
modulo
*
m
,
mp_limb_t
*
rp
)
{
unsigned
i
;
mp_limb_t
hi
,
cy
;
unsigned
shift
=
ecc
->
p
.
size
*
GMP_NUMB_BITS
-
ecc
->
p
.
bit_size
;
mp_size_t
k
=
ecc
->
p
.
redc_size
;
unsigned
shift
=
m
->
size
*
GMP_NUMB_BITS
-
m
->
bit_size
;
mp_size_t
k
=
m
->
redc_size
;
for
(
i
=
0
;
i
<
ecc
->
p
.
size
;
i
++
)
for
(
i
=
0
;
i
<
m
->
size
;
i
++
)
rp
[
i
]
=
mpn_submul_1
(
rp
+
i
+
k
,
ecc
->
p
.
redc_mpm1
,
ecc
->
p
.
size
-
k
,
rp
[
i
]);
hi
=
mpn_sub_n
(
rp
,
rp
+
ecc
->
p
.
size
,
rp
,
ecc
->
p
.
size
);
cy
=
cnd_add_n
(
hi
,
rp
,
ecc
->
p
.
m
,
ecc
->
p
.
size
);
m
->
redc_mpm1
,
m
->
size
-
k
,
rp
[
i
]);
hi
=
mpn_sub_n
(
rp
,
rp
+
m
->
size
,
rp
,
m
->
size
);
cy
=
cnd_add_n
(
hi
,
rp
,
m
->
m
,
m
->
size
);
assert
(
cy
==
hi
);
if
(
shift
>
0
)
{
/* Result is always < 2p, provided that
2^shift * Bmodp_shifted <= p */
hi
=
(
rp
[
ecc
->
p
.
size
-
1
]
>>
(
GMP_NUMB_BITS
-
shift
));
rp
[
ecc
->
p
.
size
-
1
]
=
(
rp
[
ecc
->
p
.
size
-
1
]
hi
=
(
rp
[
m
->
size
-
1
]
>>
(
GMP_NUMB_BITS
-
shift
));
rp
[
m
->
size
-
1
]
=
(
rp
[
m
->
size
-
1
]
&
(((
mp_limb_t
)
1
<<
(
GMP_NUMB_BITS
-
shift
))
-
1
))
+
mpn_addmul_1
(
rp
,
ecc
->
p
.
B_shifted
,
ecc
->
p
.
size
-
1
,
hi
);
+
mpn_addmul_1
(
rp
,
m
->
B_shifted
,
m
->
size
-
1
,
hi
);
}
}
ecc-pp1-redc.c
View file @
0b511c91
...
...
@@ -42,28 +42,28 @@
/* Use that 1 = p + 1 (mod p), and that at least one low limb of p + 1
is zero. */
void
ecc_pp1_redc
(
const
struct
ecc_
curve
*
ecc
,
mp_limb_t
*
rp
)
ecc_pp1_redc
(
const
struct
ecc_
modulo
*
m
,
mp_limb_t
*
rp
)
{
unsigned
i
;
mp_limb_t
hi
,
cy
;
unsigned
shift
=
ecc
->
p
.
size
*
GMP_NUMB_BITS
-
ecc
->
p
.
bit_size
;
mp_size_t
k
=
ecc
->
p
.
redc_size
;
unsigned
shift
=
m
->
size
*
GMP_NUMB_BITS
-
m
->
bit_size
;
mp_size_t
k
=
m
->
redc_size
;
for
(
i
=
0
;
i
<
ecc
->
p
.
size
;
i
++
)
for
(
i
=
0
;
i
<
m
->
size
;
i
++
)
rp
[
i
]
=
mpn_addmul_1
(
rp
+
i
+
k
,
ecc
->
p
.
redc_mpm1
,
ecc
->
p
.
size
-
k
,
rp
[
i
]);
hi
=
mpn_add_n
(
rp
,
rp
,
rp
+
ecc
->
p
.
size
,
ecc
->
p
.
size
);
m
->
redc_mpm1
,
m
->
size
-
k
,
rp
[
i
]);
hi
=
mpn_add_n
(
rp
,
rp
,
rp
+
m
->
size
,
m
->
size
);
if
(
shift
>
0
)
{
hi
=
(
hi
<<
shift
)
|
(
rp
[
ecc
->
p
.
size
-
1
]
>>
(
GMP_NUMB_BITS
-
shift
));
rp
[
ecc
->
p
.
size
-
1
]
=
(
rp
[
ecc
->
p
.
size
-
1
]
hi
=
(
hi
<<
shift
)
|
(
rp
[
m
->
size
-
1
]
>>
(
GMP_NUMB_BITS
-
shift
));
rp
[
m
->
size
-
1
]
=
(
rp
[
m
->
size
-
1
]
&
(((
mp_limb_t
)
1
<<
(
GMP_NUMB_BITS
-
shift
))
-
1
))
+
mpn_addmul_1
(
rp
,
ecc
->
p
.
B_shifted
,
ecc
->
p
.
size
-
1
,
hi
);
+
mpn_addmul_1
(
rp
,
m
->
B_shifted
,
m
->
size
-
1
,
hi
);
}
else
{
cy
=
cnd_sub_n
(
hi
,
rp
,
ecc
->
p
.
m
,
ecc
->
p
.
size
);
cy
=
cnd_sub_n
(
hi
,
rp
,
m
->
m
,
m
->
size
);
assert
(
cy
==
hi
);
}
}
examples/ecc-benchmark.c
View file @
0b511c91
...
...
@@ -150,7 +150,7 @@ bench_modp (void *p)
{
struct
ecc_ctx
*
ctx
=
(
struct
ecc_ctx
*
)
p
;
mpn_copyi
(
ctx
->
rp
,
ctx
->
ap
,
2
*
ctx
->
ecc
->
p
.
size
);
ctx
->
ecc
->
modp
(
ctx
->
ecc
,
ctx
->
rp
);
ctx
->
ecc
->
modp
(
&
ctx
->
ecc
->
p
,
ctx
->
rp
);
}
static
void
...
...
@@ -158,7 +158,7 @@ bench_redc (void *p)
{
struct
ecc_ctx
*
ctx
=
(
struct
ecc_ctx
*
)
p
;
mpn_copyi
(
ctx
->
rp
,
ctx
->
ap
,
2
*
ctx
->
ecc
->
p
.
size
);
ctx
->
ecc
->
redc
(
ctx
->
ecc
,
ctx
->
rp
);
ctx
->
ecc
->
redc
(
&
ctx
->
ecc
->
p
,
ctx
->
rp
);
}
static
void
...
...
@@ -166,7 +166,7 @@ bench_modq (void *p)
{
struct
ecc_ctx
*
ctx
=
(
struct
ecc_ctx
*
)
p
;
mpn_copyi
(
ctx
->
rp
,
ctx
->
ap
,
2
*
ctx
->
ecc
->
p
.
size
);
ctx
->
ecc
->
modq
(
ctx
->
ecc