-
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.
Add native num.h implementation with 32- and 64-bit variants
This num.h implementation works using fixed-size arrays large enough to hold a 256-bit number (plus one word for slop). It includes a modular inversion. Typical perf numbers on my 64-bit system are: scalar_inverse: constant time: min 13.4us / avg 13.5us / max 13.8us native num.h: min 5.18us / avg 4.55us / max 5.43us gmp num.h: min 2.65us / avg 2.68us / max 2.70us field_inverse: constant time: min 6.02us / avg 6.09us / max 6.15us native num.h: min 5.48us / avg 4.94us / max 5.68us gmp num.h: min 2.96us / avg 3.02us / max 3.09us
- Loading branch information
Showing
14 changed files
with
805 additions
and
36 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
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,28 @@ | ||
/********************************************************************** | ||
* 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_NUM_5X64_ | ||
#define _SECP256K1_NUM_5X64_ | ||
|
||
#include "util.h" | ||
|
||
#define NUM_N_WORDS 5 | ||
#define NUM_WORD_WIDTH 64 | ||
#define NUM_WORD_CTLZ __builtin_clzl | ||
typedef uint64_t secp256k1_num_word; | ||
typedef int64_t secp256k1_num_sword; | ||
typedef uint128_t secp256k1_num_dword; | ||
|
||
typedef struct { | ||
/* we need an extra word for auxiallary stuff during algorithms, | ||
* so we have an extra word beyond what we need for 256-bit | ||
* numbers. Import/export (by set_bin and get_bin) expects to | ||
* work with 32-byte buffers, so the top word is not directly | ||
* accessible to users of the API. */ | ||
secp256k1_num_word data[NUM_N_WORDS]; | ||
} secp256k1_num; | ||
|
||
#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,49 @@ | ||
/********************************************************************** | ||
* 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_NUM_5X64_IMPL_ | ||
#define _SECP256K1_NUM_5X64_IMPL_ | ||
|
||
#include <string.h> | ||
|
||
#include "num.h" | ||
#include "num_5x64.h" | ||
#include "util.h" | ||
|
||
#include "num_native_impl.h" | ||
|
||
static void secp256k1_num_debug_print(const char *name, const secp256k1_num *a) { | ||
int i; | ||
printf ("%s: 0x", name); | ||
for (i = 4; i >= 0; --i) | ||
printf("%016lx", a->data[i]); | ||
puts(""); | ||
} | ||
|
||
static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num *a) { | ||
uint64_t v; | ||
(void) rlen; | ||
VERIFY_CHECK(rlen >= 32); | ||
|
||
v = BE64(a->data[3]); memcpy(&r[0], &v, sizeof(v)); | ||
v = BE64(a->data[2]); memcpy(&r[8], &v, sizeof(v)); | ||
v = BE64(a->data[1]); memcpy(&r[16], &v, sizeof(v)); | ||
v = BE64(a->data[0]); memcpy(&r[24], &v, sizeof(v)); | ||
} | ||
|
||
static void secp256k1_num_set_bin(secp256k1_num *r, const unsigned char *a, unsigned int alen) { | ||
uint64_t v; | ||
(void) alen; | ||
VERIFY_CHECK(alen >= 32); | ||
|
||
r->data[4] = 0; | ||
memcpy(&v, &a[0], sizeof(v)); r->data[3] = BE64(v); | ||
memcpy(&v, &a[8], sizeof(v)); r->data[2] = BE64(v); | ||
memcpy(&v, &a[16], sizeof(v)); r->data[1] = BE64(v); | ||
memcpy(&v, &a[24], sizeof(v)); r->data[0] = BE64(v); | ||
} | ||
|
||
#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,28 @@ | ||
/********************************************************************** | ||
* 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_NUM_9X32_ | ||
#define _SECP256K1_NUM_9X32_ | ||
|
||
#include "util.h" | ||
|
||
#define NUM_N_WORDS 9 | ||
#define NUM_WORD_WIDTH 32 | ||
#define NUM_WORD_CTLZ __builtin_clz | ||
typedef uint32_t secp256k1_num_word; | ||
typedef int32_t secp256k1_num_sword; | ||
typedef uint64_t secp256k1_num_dword; | ||
|
||
typedef struct { | ||
/* we need an extra word for auxiallary stuff during algorithms, | ||
* so we have an extra word beyond what we need for 256-bit | ||
* numbers. Import/export (by set_bin and get_bin) expects to | ||
* work with 32-byte buffers, so the top word is not directly | ||
* accessible to users of the API. */ | ||
secp256k1_num_word data[NUM_N_WORDS]; | ||
} secp256k1_num; | ||
|
||
#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,57 @@ | ||
/********************************************************************** | ||
* 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_NUM_9X32_IMPL_ | ||
#define _SECP256K1_NUM_9X32_IMPL_ | ||
|
||
#include <string.h> | ||
|
||
#include "num.h" | ||
#include "num_9x32.h" | ||
#include "util.h" | ||
|
||
#include "num_native_impl.h" | ||
|
||
static void secp256k1_num_debug_print(const char *name, const secp256k1_num *a) { | ||
int i; | ||
printf ("%s: 0x", name); | ||
for (i = 8; i >= 0; --i) | ||
printf("%08x", a->data[i]); | ||
puts(""); | ||
} | ||
|
||
static void secp256k1_num_get_bin(unsigned char *r, unsigned int rlen, const secp256k1_num *a) { | ||
uint32_t v; | ||
(void) rlen; | ||
VERIFY_CHECK(rlen >= 32); | ||
|
||
v = BE32(a->data[7]); memcpy(&r[0], &v, sizeof(v)); | ||
v = BE32(a->data[6]); memcpy(&r[4], &v, sizeof(v)); | ||
v = BE32(a->data[5]); memcpy(&r[8], &v, sizeof(v)); | ||
v = BE32(a->data[4]); memcpy(&r[12], &v, sizeof(v)); | ||
v = BE32(a->data[3]); memcpy(&r[16], &v, sizeof(v)); | ||
v = BE32(a->data[2]); memcpy(&r[20], &v, sizeof(v)); | ||
v = BE32(a->data[1]); memcpy(&r[24], &v, sizeof(v)); | ||
v = BE32(a->data[0]); memcpy(&r[28], &v, sizeof(v)); | ||
} | ||
|
||
static void secp256k1_num_set_bin(secp256k1_num *r, const unsigned char *a, unsigned int alen) { | ||
uint32_t v; | ||
(void) alen; | ||
VERIFY_CHECK(alen >= 32); | ||
|
||
r->data[8] = 0; | ||
memcpy(&v, &a[0], sizeof(v)); r->data[7] = BE32(v); | ||
memcpy(&v, &a[4], sizeof(v)); r->data[6] = BE32(v); | ||
memcpy(&v, &a[8], sizeof(v)); r->data[5] = BE32(v); | ||
memcpy(&v, &a[12], sizeof(v)); r->data[4] = BE32(v); | ||
memcpy(&v, &a[16], sizeof(v)); r->data[3] = BE32(v); | ||
memcpy(&v, &a[20], sizeof(v)); r->data[2] = BE32(v); | ||
memcpy(&v, &a[24], sizeof(v)); r->data[1] = BE32(v); | ||
memcpy(&v, &a[28], sizeof(v)); r->data[0] = BE32(v); | ||
} | ||
|
||
#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
Oops, something went wrong.