From c9a8fa639e7d200fbad31c24eb031c7e77ddc7ec Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 28 Feb 2020 19:32:35 +0000 Subject: [PATCH] New query function ecc_montgomery_is_identity. To begin with, this allows me to add a regression test for the change in the previous commit. --- ecc.c | 5 +++++ ecc.h | 5 +++++ test/cryptsuite.py | 6 ++++++ testcrypt.h | 1 + 4 files changed, 17 insertions(+) diff --git a/ecc.c b/ecc.c index 857fc024..72f9bfe5 100644 --- a/ecc.c +++ b/ecc.c @@ -833,6 +833,11 @@ void ecc_montgomery_get_affine(MontgomeryPoint *mp, mp_int **x) *x = monty_export(mc->mc, mp->X); } +unsigned ecc_montgomery_is_identity(MontgomeryPoint *mp) +{ + return mp_eq_integer(mp->Z, 0); +} + /* ---------------------------------------------------------------------- * Twisted Edwards curves. */ diff --git a/ecc.h b/ecc.h index 13118b29..96eebdf0 100644 --- a/ecc.h +++ b/ecc.h @@ -170,6 +170,11 @@ MontgomeryPoint *ecc_montgomery_multiply(MontgomeryPoint *, mp_int *); */ void ecc_montgomery_get_affine(MontgomeryPoint *mp, mp_int **x); +/* + * Test whether a point is the curve identity. + */ +unsigned ecc_montgomery_is_identity(MontgomeryPoint *mp); + /* ---------------------------------------------------------------------- * Twisted Edwards curves. * diff --git a/test/cryptsuite.py b/test/cryptsuite.py index 1df54117..18a2a330 100755 --- a/test/cryptsuite.py +++ b/test/cryptsuite.py @@ -769,6 +769,12 @@ class ecc(MyTestBase): check_point(ecc_montgomery_double(mP), rP + rP) check_point(ecc_montgomery_double(mQ), rQ + rQ) + zero = ecc_montgomery_point_new(mc, 0) + self.assertEqual(ecc_montgomery_is_identity(zero), False) + identity = ecc_montgomery_double(zero) + ecc_montgomery_get_affine(identity) + self.assertEqual(ecc_montgomery_is_identity(identity), True) + def testEdwardsSimple(self): p, d, a = 3141592661, 2688750488, 367934288 diff --git a/testcrypt.h b/testcrypt.h index 9efbb87d..bc9dcea9 100644 --- a/testcrypt.h +++ b/testcrypt.h @@ -110,6 +110,7 @@ FUNC3(val_mpoint, ecc_montgomery_diff_add, val_mpoint, val_mpoint, val_mpoint) FUNC1(val_mpoint, ecc_montgomery_double, val_mpoint) FUNC2(val_mpoint, ecc_montgomery_multiply, val_mpoint, val_mpint) FUNC2(void, ecc_montgomery_get_affine, val_mpoint, out_val_mpint) +FUNC1(boolean, ecc_montgomery_is_identity, val_mpoint) FUNC4(val_ecurve, ecc_edwards_curve, val_mpint, val_mpint, val_mpint, opt_val_mpint) FUNC3(val_epoint, ecc_edwards_point_new, val_ecurve, val_mpint, val_mpint) FUNC3(val_epoint, ecc_edwards_point_new_from_y, val_ecurve, val_mpint, uint)