Skip to content

Commit

Permalink
web3swift is now faster!
Browse files Browse the repository at this point in the history
debug mode:
BIP32Keystore.init: 9300% faster
EthereumKeystoreV3.init: 14100% faster
scrypt: 28100% faster
keccak256: 21000% faster

release mode:
BIP32Keystore.init: 18% faster
EthereumKeystoreV3.init: 30% faster
scrypt: 40% faster
keccak256: 3800% faster

Removed CryptoSwift dependency
Removed secp256k1.swift dependency

Added keccak target
Added scrypt target
Added secp256k1 target
  • Loading branch information
v57 committed Dec 5, 2018
1 parent d8baca1 commit af5a316
Show file tree
Hide file tree
Showing 196 changed files with 16,209 additions and 97 deletions.
2 changes: 0 additions & 2 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github "mxcl/PromiseKit" ~> 6.0
github "attaswift/BigInt" ~> 3.1
github "krzyzanowskim/CryptoSwift"
github "Boilertalk/secp256k1.swift"
18 changes: 9 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"version": "3.1.0"
}
},
{
"package": "Cryptor",
"repositoryURL": "https://github.com/v57/BlueCryptor.git",
"state": {
"branch": null,
"revision": "62470ce23835f8920ce58e39d59548406af85dea",
"version": "1.0.23"
}
},
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
Expand All @@ -28,15 +37,6 @@
"version": "6.5.2"
}
},
{
"package": "secp256k1",
"repositoryURL": "https://github.com/Boilertalk/secp256k1.swift.git",
"state": {
"branch": null,
"revision": "823281fe9def21b384099b72a9a53ca988317b20",
"version": "0.1.4"
}
},
{
"package": "SipHash",
"repositoryURL": "https://github.com/attaswift/SipHash",
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ let package = Package(
name: "web3swift",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(name: "web3swift", targets: ["web3swift"])
.library(name: "web3swift", targets: ["web3swift"]),
],
dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", from: "3.1.0"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "0.12.0"),
.package(url: "https://github.com/Boilertalk/secp256k1.swift.git", from: "0.1.1"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.4.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(name: "secp256k1"),
.target(name: "keccak"),
.target(name: "scrypt"),
.target(
name: "web3swift",
dependencies: ["BigInt", "CryptoSwift", "secp256k1", "PromiseKit"],
path: "Sources",
dependencies: ["BigInt", "secp256k1", "keccak", "scrypt", "PromiseKit"],
exclude: [
"ObjectiveC",
"Utils/EIP67Code.swift",
Expand Down
24 changes: 24 additions & 0 deletions Sources/keccak/include/keccak.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef KECCAK_FIPS202_H
#define KECCAK_FIPS202_H
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdint.h>
#include <stdlib.h>

#define decshake(bits) \
int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);

#define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);

#define deckeccak(bits) \
int keccak_##bits(uint8_t*, size_t, const uint8_t*, size_t);


decshake(128)
decshake(256)
decsha3(224)
decsha3(256)
decsha3(384)
decsha3(512)
deckeccak(256)
#endif
175 changes: 175 additions & 0 deletions Sources/keccak/keccak.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/** libkeccak-tiny
*
* A single-file implementation of SHA-3 and SHAKE.
*
* Implementor: David Leon Gil
* License: CC0, attribution kindly requested. Blame taken too,
* but not liability.
*/
#include "keccak.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/******** The Keccak-f[1600] permutation ********/

/*** Constants. ***/
static const uint8_t rho[24] = \
{ 1, 3, 6, 10, 15, 21,
28, 36, 45, 55, 2, 14,
27, 41, 56, 8, 25, 43,
62, 18, 39, 61, 20, 44};
static const uint8_t pi[24] = \
{10, 7, 11, 17, 18, 3,
5, 16, 8, 21, 24, 4,
15, 23, 19, 13, 12, 2,
20, 14, 22, 9, 6, 1};
static const uint64_t RC[24] = \
{1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};

/*** Helper macros to unroll the permutation. ***/
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
#define REPEAT6(e) e e e e e e
#define REPEAT24(e) REPEAT6(e e e e)
#define REPEAT5(e) e e e e e
#define FOR5(v, s, e) \
v = 0; \
REPEAT5(e; v += s;)

/*** Keccak-f[1600] ***/
static inline void keccakf(void* state) {
uint64_t* a = (uint64_t*)state;
uint64_t b[5] = {0};
uint64_t t = 0;
uint8_t x, y;

for (int i = 0; i < 24; i++) {
// Theta
FOR5(x, 1,
b[x] = 0;
FOR5(y, 5,
b[x] ^= a[x + y]; ))
FOR5(x, 1,
FOR5(y, 5,
a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
// Rho and pi
t = a[1];
x = 0;
REPEAT24(b[0] = a[pi[x]];
a[pi[x]] = rol(t, rho[x]);
t = b[0];
x++; )
// Chi
FOR5(y,
5,
FOR5(x, 1,
b[x] = a[y + x];)
FOR5(x, 1,
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
// Iota
a[0] ^= RC[i];
}
}

/******** The FIPS202-defined functions. ********/

/*** Some helper macros. ***/

#define _(S) do { S } while (0)
#define FOR(i, ST, L, S) \
_(for (size_t i = 0; i < L; i += ST) { S; })
#define mkapply_ds(NAME, S) \
static inline void NAME(uint8_t* dst, \
const uint8_t* src, \
size_t len) { \
FOR(i, 1, len, S); \
}
#define mkapply_sd(NAME, S) \
static inline void NAME(const uint8_t* src, \
uint8_t* dst, \
size_t len) { \
FOR(i, 1, len, S); \
}

mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
mkapply_sd(setout, dst[i] = src[i]) // setout

#define P keccakf
#define Plen 200

// Fold P*F over the full blocks of an input.
#define foldP(I, L, F) \
while (L >= rate) { \
F(a, I, rate); \
P(a); \
I += rate; \
L -= rate; \
}

/** The sponge-based hash construction. **/
static inline int hash(uint8_t* out, size_t outlen,
const uint8_t* in, size_t inlen,
size_t rate, uint8_t delim) {
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
return -1;
}
uint8_t a[Plen] = {0};
// Absorb input.
foldP(in, inlen, xorin);
// Xor in the DS and pad frame.
a[inlen] ^= delim;
a[rate - 1] ^= 0x80;
// Xor in the last block.
xorin(a, in, inlen);
// Apply P
P(a);
// Squeeze output.
foldP(out, outlen, setout);
setout(a, out, outlen);
memset_s(a, 200, 0, 200);
return 0;
}

/*** Helper macros to define SHA3 and SHAKE instances. ***/
#define defshake(bits) \
int shake##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
}
#define defsha3(bits) \
int sha3_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
}

#define defkeccak(bits) \
int keccak_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
}

/*** FIPS202 SHAKE VOFs ***/
defshake(128)
defshake(256)

/*** FIPS202 SHA3 FOFs ***/
defsha3(224)
defsha3(256)
defsha3(384)
defsha3(512)

/*** pre-FIPS202 Keccak standard ***/
defkeccak(256)
49 changes: 49 additions & 0 deletions Sources/scrypt/asprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include "asprintf.h"

/**
* asprintf(ret, format, ...):
* Do asprintf(3) like GNU and BSD do.
*/
int
asprintf(char ** ret, const char * format, ...)
{
va_list ap;
int len;
size_t buflen;

/* Figure out how long the string needs to be. */
va_start(ap, format);
len = vsnprintf(NULL, 0, format, ap);
va_end(ap);

/* Did we fail? */
if (len < 0)
goto err0;
buflen = (size_t)(len) + 1;

/* Allocate memory. */
if ((*ret = malloc(buflen)) == NULL)
goto err0;

/* Actually generate the string. */
va_start(ap, format);
len = vsnprintf(*ret, buflen, format, ap);
va_end(ap);

/* Did we fail? */
if (len < 0)
goto err1;

/* Success! */
return (len);

err1:
free(*ret);
err0:
/* Failure! */
return (-1);
}
16 changes: 16 additions & 0 deletions Sources/scrypt/asprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _ASPRINTF_H_
#define _ASPRINTF_H_

/* Avoid namespace collisions with BSD/GNU asprintf. */
#ifdef asprintf
#undef asprintf
#endif
#define asprintf libcperciva_asprintf

/**
* asprintf(ret, format, ...):
* Do asprintf(3) like GNU and BSD do.
*/
int asprintf(char **, const char *, ...);

#endif /* !_ASPRINTF_H_ */
Loading

0 comments on commit af5a316

Please sign in to comment.