diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-07-02 19:58:35 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-07-02 19:58:35 +0000 |
commit | fece22eebf8c8d54e79d05f748019e7234823828 (patch) | |
tree | f875095ec8a2918a263f273a71b721654cfba612 /src/util/test_crypto_ecc_dlog.c | |
parent | ed53a24f07a861edf7edd327c04fc7a23111e3c4 (diff) | |
download | gnunet-fece22eebf8c8d54e79d05f748019e7234823828.tar.gz gnunet-fece22eebf8c8d54e79d05f748019e7234823828.zip |
-adding ecc dlog support
Diffstat (limited to 'src/util/test_crypto_ecc_dlog.c')
-rw-r--r-- | src/util/test_crypto_ecc_dlog.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/util/test_crypto_ecc_dlog.c b/src/util/test_crypto_ecc_dlog.c new file mode 100644 index 000000000..a594e5795 --- /dev/null +++ b/src/util/test_crypto_ecc_dlog.c | |||
@@ -0,0 +1,116 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2015 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | |||
20 | */ | ||
21 | /** | ||
22 | * @file util/test_crypto_ecc_dlog.c | ||
23 | * @brief testcase for ECC DLOG calculation | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * TODO: | ||
27 | * - test negative numbers | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include <gcrypt.h> | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Name of the curve we are using. Note that we have hard-coded | ||
36 | * structs that use 256 bits, so using a bigger curve will require | ||
37 | * changes that break stuff badly. The name of the curve given here | ||
38 | * must be agreed by all peers and be supported by libgcrypt. | ||
39 | */ | ||
40 | #define CURVE "Ed25519" | ||
41 | |||
42 | /** | ||
43 | * Maximum value we test dlog for. | ||
44 | */ | ||
45 | #define MAX_FACT 1000000 | ||
46 | |||
47 | /** | ||
48 | * Maximum memory to use, sqrt(MAX_FACT) is a good choice. | ||
49 | */ | ||
50 | #define MAX_MEM 1000 | ||
51 | |||
52 | |||
53 | static void | ||
54 | test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc) | ||
55 | { | ||
56 | gcry_mpi_t fact; | ||
57 | gcry_ctx_t ctx; | ||
58 | gcry_mpi_point_t q; | ||
59 | gcry_mpi_point_t g; | ||
60 | unsigned int i; | ||
61 | unsigned int x; | ||
62 | |||
63 | GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE)); | ||
64 | g = gcry_mpi_ec_get_point ("g", ctx, 0); | ||
65 | GNUNET_assert (NULL != g); | ||
66 | q = gcry_mpi_point_new (0); | ||
67 | fact = gcry_mpi_new (0); | ||
68 | for (i=0;i<10;i++) | ||
69 | { | ||
70 | x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
71 | MAX_FACT); | ||
72 | gcry_mpi_set_ui (fact, x); | ||
73 | gcry_mpi_ec_mul (q, fact, g, ctx); | ||
74 | if (x != | ||
75 | GNUNET_CRYPTO_ecc_dlog (edc, | ||
76 | q)) | ||
77 | { | ||
78 | fprintf (stderr, | ||
79 | "DLOG failed for value %u\n", | ||
80 | x); | ||
81 | GNUNET_assert (0); | ||
82 | } | ||
83 | } | ||
84 | gcry_mpi_release (fact); | ||
85 | gcry_mpi_point_release (g); | ||
86 | gcry_mpi_point_release (q); | ||
87 | gcry_ctx_release (ctx); | ||
88 | } | ||
89 | |||
90 | |||
91 | int | ||
92 | main (int argc, char *argv[]) | ||
93 | { | ||
94 | struct GNUNET_CRYPTO_EccDlogContext *edc; | ||
95 | |||
96 | if (! gcry_check_version ("1.6.0")) | ||
97 | { | ||
98 | FPRINTF (stderr, | ||
99 | _ | ||
100 | ("libgcrypt has not the expected version (version %s is required).\n"), | ||
101 | "1.6.0"); | ||
102 | return 0; | ||
103 | } | ||
104 | if (getenv ("GNUNET_GCRYPT_DEBUG")) | ||
105 | gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0); | ||
106 | GNUNET_log_setup ("test-crypto-ecc-dlog", | ||
107 | "WARNING", | ||
108 | NULL); | ||
109 | edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT, | ||
110 | MAX_MEM); | ||
111 | test_dlog (edc); | ||
112 | GNUNET_CRYPTO_ecc_dlog_release (edc); | ||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | /* end of test_crypto_ecc_dlog.c */ | ||