-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
292 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _SECP256K1_ECDH_ | ||
# define _SECP256K1_ECDH_ | ||
|
||
# include "secp256k1.h" | ||
|
||
# ifdef __cplusplus | ||
extern "C" { | ||
# endif | ||
|
||
/** Compute an EC Diffie-Hellman secret in constant time | ||
* Returns: 1: exponentiation was successful | ||
* 0: scalar was invalid (zero or overflow) | ||
* In: ctx: pointer to a context object (cannot be NULL) | ||
* point: pointer to a public point | ||
* scalar: a 32-byte scalar with which to multiply the point | ||
* Out: result: a 32-byte array which will be populated by an ECDH | ||
* secret computed from the point and scalar | ||
*/ | ||
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh( | ||
const secp256k1_context_t* ctx, | ||
unsigned char *result, | ||
const secp256k1_pubkey_t *point, | ||
const unsigned char *scalar | ||
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); | ||
|
||
# ifdef __cplusplus | ||
} | ||
# endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2015 Pieter Wuille, Andrew Poelstra * | ||
* Copyright (c) 2015 Andrew Poelstra * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||
**********************************************************************/ | ||
|
||
#ifndef _SECP256K1_ECDH_ | ||
#define _SECP256K1_ECDH_ | ||
#ifndef _SECP256K1_ECMULT_CONST_ | ||
#define _SECP256K1_ECMULT_CONST_ | ||
|
||
#include "scalar.h" | ||
#include "group.h" | ||
|
||
static void secp256k1_point_multiply(secp256k1_gej_t *r, const secp256k1_ge_t *a, const secp256k1_scalar_t *q); | ||
static void secp256k1_ecmult_const(secp256k1_gej_t *r, const secp256k1_ge_t *a, const secp256k1_scalar_t *q); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
include_HEADERS += include/secp256k1_ecdh.h | ||
noinst_HEADERS += src/modules/ecdh/main_impl.h | ||
noinst_HEADERS += src/modules/ecdh/tests_impl.h | ||
if USE_BENCHMARK | ||
noinst_PROGRAMS += bench_ecdh | ||
bench_ecdh_SOURCES = src/bench_ecdh.c | ||
bench_ecdh_LDADD = libsecp256k1.la $(SECP_LIBS) | ||
bench_ecdh_LDFLAGS = -static | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2015 Andrew Poelstra * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||
**********************************************************************/ | ||
|
||
#ifndef _SECP256K1_MODULE_ECDH_MAIN_ | ||
#define _SECP256K1_MODULE_ECDH_MAIN_ | ||
|
||
#include "ecmult_const_impl.h" | ||
|
||
int secp256k1_ecdh(const secp256k1_context_t* ctx, unsigned char *result, const secp256k1_pubkey_t *point, const unsigned char *scalar) { | ||
int ret = 0; | ||
int overflow = 0; | ||
secp256k1_gej_t res; | ||
secp256k1_ge_t pt; | ||
secp256k1_scalar_t s; | ||
ARG_CHECK(result != NULL); | ||
ARG_CHECK(point != NULL); | ||
ARG_CHECK(scalar != NULL); | ||
(void)ctx; | ||
|
||
secp256k1_pubkey_load(ctx, &pt, point); | ||
secp256k1_scalar_set_b32(&s, scalar, &overflow); | ||
if (overflow || secp256k1_scalar_is_zero(&s)) { | ||
ret = 0; | ||
} else { | ||
unsigned char x[32]; | ||
unsigned char y[1]; | ||
secp256k1_sha256_t sha; | ||
|
||
secp256k1_ecmult_const(&res, &pt, &s); | ||
secp256k1_ge_set_gej(&pt, &res); | ||
/* Compute a hash of the point in compressed form | ||
* Note we cannot use secp256k1_eckey_pubkey_serialize here since it does not | ||
* expect its output to be secret and has a timing sidechannel. */ | ||
secp256k1_fe_normalize(&pt.x); | ||
secp256k1_fe_normalize(&pt.y); | ||
secp256k1_fe_get_b32(x, &pt.x); | ||
y[0] = 0x02 | secp256k1_fe_is_odd(&pt.y); | ||
|
||
secp256k1_sha256_initialize(&sha); | ||
secp256k1_sha256_write(&sha, y, sizeof(y)); | ||
secp256k1_sha256_write(&sha, x, sizeof(x)); | ||
secp256k1_sha256_finalize(&sha, result); | ||
ret = 1; | ||
} | ||
|
||
secp256k1_scalar_clear(&s); | ||
return ret; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2015 Andrew Poelstra * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||
**********************************************************************/ | ||
|
||
#ifndef _SECP256K1_MODULE_ECDH_TESTS_ | ||
#define _SECP256K1_MODULE_ECDH_TESTS_ | ||
|
||
void test_ecdh_generator_basepoint(void) { | ||
unsigned char s_one[32] = { 0 }; | ||
secp256k1_pubkey_t point[2]; | ||
int i; | ||
|
||
s_one[31] = 1; | ||
/* Check against pubkey creation when the basepoint is the generator */ | ||
for (i = 0; i < 100; ++i) { | ||
secp256k1_sha256_t sha; | ||
unsigned char s_b32[32]; | ||
unsigned char output_ecdh[32]; | ||
unsigned char output_ser[32]; | ||
unsigned char point_ser[33]; | ||
int point_ser_len = sizeof(point_ser); | ||
secp256k1_scalar_t s; | ||
|
||
random_scalar_order(&s); | ||
secp256k1_scalar_get_b32(s_b32, &s); | ||
|
||
/* compute using ECDH function */ | ||
CHECK(secp256k1_ec_pubkey_create(ctx, &point[0], s_one) == 1); | ||
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32) == 1); | ||
/* compute "explicitly" */ | ||
CHECK(secp256k1_ec_pubkey_create(ctx, &point[1], s_b32) == 1); | ||
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], 1) == 1); | ||
CHECK(point_ser_len == sizeof(point_ser)); | ||
secp256k1_sha256_initialize(&sha); | ||
secp256k1_sha256_write(&sha, point_ser, point_ser_len); | ||
secp256k1_sha256_finalize(&sha, output_ser); | ||
/* compare */ | ||
CHECK(memcmp(output_ecdh, output_ser, sizeof(output_ser)) == 0); | ||
} | ||
} | ||
|
||
void run_ecdh_tests(void) { | ||
test_ecdh_generator_basepoint(); | ||
} | ||
|
||
#endif |
Oops, something went wrong.