Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
cleanup coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Apr 13, 2015
1 parent 6ec585f commit 21d0bb4
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 132 deletions.
9 changes: 3 additions & 6 deletions base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <sys/types.h>
#include "base58.h"
#include "sha2.h"
#include "macro_utils.h"
#include "macros.h"

static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
Expand Down Expand Up @@ -184,8 +184,6 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
return true;
}

#include <stdio.h>

int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize)
{
if (datalen > 128) {
Expand All @@ -197,10 +195,9 @@ int base58_encode_check(const uint8_t *data, int datalen, char *str, int strsize
sha256_Raw(data, datalen, hash);
sha256_Raw(hash, 32, hash);
size_t res = strsize;
bool fSuccess = b58enc(str, &res, buf, datalen + 4);

bool success = b58enc(str, &res, buf, datalen + 4);
MEMSET_BZERO(buf, sizeof(buf));
return fSuccess ? res : 0;
return success ? res : 0;
}

int base58_decode_check(const char *str, uint8_t *data, int datalen)
Expand Down
25 changes: 11 additions & 14 deletions bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <assert.h>
#include "bignum.h"
#include "secp256k1.h"
#include "macro_utils.h"
#include "macros.h"

inline uint32_t read_be(const uint8_t *data)
{
Expand Down Expand Up @@ -226,7 +226,7 @@ void bn_multiply(const bignum256 *k, bignum256 *x, const bignum256 *prime)
// 0 <= res < 2^(30k + 256) * (2^30 + 1)
// estimate (res / prime)
coef = (res[i] >> 16) + (res[i + 1] << 14);

// coef = res / 2^(30k + 256) rounded down
// 0 <= coef <= 2^30
// subtract (coef * 2^(30k) * prime) from res
Expand All @@ -239,7 +239,7 @@ void bn_multiply(const bignum256 *k, bignum256 *x, const bignum256 *prime)
res[i - 8 + j] = temp & 0x3FFFFFFF;
}
// we don't clear res[i+1] but we never read it again.

// we rely on the fact that prime > 2^256 - 2^196
// res = oldres - coef*2^(30k) * prime;
// and
Expand All @@ -253,8 +253,7 @@ void bn_multiply(const bignum256 *k, bignum256 *x, const bignum256 *prime)
for (i = 0; i < 9; i++) {
x->val[i] = res[i];
}

MEMSET_BZERO(res,sizeof(res));
MEMSET_BZERO(res, sizeof(res));
}

// input x can be any normalized number that fits (0 <= x < 2^270).
Expand Down Expand Up @@ -312,10 +311,8 @@ void bn_sqrt(bignum256 *x, const bignum256 *prime)
}
bn_mod(&res, prime);
memcpy(x, &res, sizeof(bignum256));

MEMSET_BZERO(&res, sizeof(res));
MEMSET_BZERO(&p, sizeof(p));

}

#if ! USE_INVERSE_FAST
Expand Down Expand Up @@ -408,14 +405,14 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
odd = &us;
even = &vr;

// u = prime, v = x
// u = prime, v = x
// r = 0 , s = 1
// k = 0
for (;;) {
// invariants:
// let u = limbs us.a[0..u.len1-1] in little endian,
// let u = limbs us.a[0..u.len1-1] in little endian,
// let s = limbs us.a[u.len..8] in big endian,
// let v = limbs vr.a[0..u.len1-1] in little endian,
// let v = limbs vr.a[0..u.len1-1] in little endian,
// let r = limbs vr.a[u.len..8] in big endian,
// r,s >= 0 ; u,v >= 1
// x*-r = u*2^k mod prime
Expand All @@ -425,7 +422,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
// max(u,v) <= 2^k (*) see comment at end of loop
// gcd(u,v) = 1
// {odd,even} = {&us, &vr}
// odd->a[0] and odd->a[8] are odd
// odd->a[0] and odd->a[8] are odd
// even->a[0] or even->a[8] is even
//
// first u/v are large and r/s small
Expand Down Expand Up @@ -486,7 +483,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
assert(even->a[0] & 1);
assert((even->a[8] & 1) == 0);

// cmp > 0 if us.a[0..len1-1] > vr.a[0..len1-1],
// cmp > 0 if us.a[0..len1-1] > vr.a[0..len1-1],
// cmp = 0 if equal, < 0 if less.
cmp = us.len1 - vr.len1;
if (cmp == 0) {
Expand Down Expand Up @@ -567,7 +564,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
// We use the Explicit Quadratic Modular inverse algorithm.
// http://arxiv.org/pdf/1209.6626.pdf
// a^-1 = (2-a) * PROD_i (1 + (a - 1)^(2^i)) mod 2^32
// the product will converge quickly, because (a-1)^(2^i) will be
// the product will converge quickly, because (a-1)^(2^i) will be
// zero mod 2^32 after at most five iterations.
// We want to compute -prime^-1 so we start with (pp[0]-2).
assert(pp[0] & 1);
Expand Down Expand Up @@ -622,7 +619,7 @@ void bn_inverse(bignum256 *x, const bignum256 *prime)
}
x->val[i] = temp32;

// Let's wipe all temp buffers.
// let's wipe all temp buffers
MEMSET_BZERO(pp, sizeof(pp));
MEMSET_BZERO(&us, sizeof(us));
MEMSET_BZERO(&vr, sizeof(vr));
Expand Down
76 changes: 31 additions & 45 deletions bip32.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "sha2.h"
#include "ripemd160.h"
#include "base58.h"
#include "macro_utils.h"
#include "macros.h"

int hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, HDNode *out)
{
Expand All @@ -53,15 +53,16 @@ int hdnode_from_xprv(uint32_t depth, uint32_t fingerprint, uint32_t child_num, c
bn_read_be(private_key, &a);

bool failed = false;
if (bn_is_zero(&a)) {
failed = true;
}
else if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
MEMSET_BZERO(&a,sizeof(a));
if (bn_is_zero(&a)) { // == 0
failed = true;
} else {
if (!bn_is_less(&a, &order256k1)) { // >= order
failed = true;
}
MEMSET_BZERO(&a, sizeof(a));
}

if(failed) {
if (failed) {
return 0;
}

Expand All @@ -87,25 +88,21 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, HDNode *out)
bn_read_be(out->private_key, &a);

bool failed = false;
if (bn_is_zero(&a)) {
if (bn_is_zero(&a)) { // == 0
failed = true;
}
else
{
if( !bn_is_less(&a, &order256k1)) { // == 0 or >= order
} else {
if (!bn_is_less(&a, &order256k1)) { // >= order
failed = true;
}

// Making sure a is wiped.
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&a, sizeof(a));
}

if(!failed) {
if (!failed) {
memcpy(out->chain_code, I + 32, 32);
hdnode_fill_public_key(out);
}

MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(I, sizeof(I));
return failed ? 0 : 1;
}

Expand Down Expand Up @@ -141,30 +138,25 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
if (!bn_is_less(&b, &order256k1)) { // >= order
failed = true;
}
if(!failed) {

if (!failed) {
bn_addmod(&a, &b, &order256k1);

if (bn_is_zero(&a)) {
failed = true;
}
}

if(!failed)
{
if (!failed) {
inout->depth++;
inout->child_num = i;
bn_write_be(&a, inout->private_key);

hdnode_fill_public_key(inout);
}

// Making sure to wipe our memory!
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&b,sizeof(b));
MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
MEMSET_BZERO(data,sizeof(data));
// making sure to wipe our memory
MEMSET_BZERO(&a, sizeof(a));
MEMSET_BZERO(&b, sizeof(b));
MEMSET_BZERO(I, sizeof(I));
MEMSET_BZERO(fingerprint, sizeof(fingerprint));
MEMSET_BZERO(data, sizeof(data));
return failed ? 0 : 1;
}

Expand Down Expand Up @@ -194,43 +186,37 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
failed = true;
}

if(!failed)
{
if (!failed) {
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
memcpy(inout->chain_code, I + 32, 32);
bn_read_be(I, &c);

if (!bn_is_less(&c, &order256k1)) { // >= order
failed = true;
}
}

if(!failed)
{
if (!failed) {
scalar_multiply(&c, &b); // b = c * G
point_add(&a, &b); // b = a + b

if (!ecdsa_validate_pubkey(&b)) {
failed = true;
}
}

if(!failed)
{
if (!failed) {
inout->public_key[0] = 0x02 | (b.y.val[0] & 0x01);
bn_write_be(&b.x, inout->public_key + 1);

inout->depth++;
inout->child_num = i;
}

// Wipe all stack data.
MEMSET_BZERO(data,sizeof(data));
MEMSET_BZERO(I,sizeof(I));
MEMSET_BZERO(fingerprint,sizeof(fingerprint));
MEMSET_BZERO(&a,sizeof(a));
MEMSET_BZERO(&b,sizeof(b));
MEMSET_BZERO(&c,sizeof(c));
MEMSET_BZERO(data, sizeof(data));
MEMSET_BZERO(I, sizeof(I));
MEMSET_BZERO(fingerprint, sizeof(fingerprint));
MEMSET_BZERO(&a, sizeof(a));
MEMSET_BZERO(&b, sizeof(b));
MEMSET_BZERO(&c, sizeof(c));

return failed ? 0 : 1;
}
Expand Down
Loading

0 comments on commit 21d0bb4

Please sign in to comment.