Skip to content

Commit 56f8699

Browse files
committed
Merge pull request ethereum#1940 from wildfjre/lightkdfflag
cmd/utils, crypto: add --lightkdf flag for lighter KDF
2 parents 6b5a42a + 05ea892 commit 56f8699

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

cmd/utils/flags.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ var (
157157
Name: "fast",
158158
Usage: "Enables fast syncing through state downloads",
159159
}
160+
LightKDFFlag = cli.BoolFlag{
161+
Name: "lightkdf",
162+
Usage: "Reduce KDF memory & CPU usage at some expense of KDF strength",
163+
}
160164

161165
// miner settings
162166
// TODO: refactor CPU vs GPU mining flags
@@ -579,7 +583,13 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
579583
if ctx.GlobalBool(TestNetFlag.Name) {
580584
dataDir += "/testnet"
581585
}
582-
ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"))
586+
scryptN := crypto.StandardScryptN
587+
scryptP := crypto.StandardScryptP
588+
if ctx.GlobalBool(LightKDFFlag.Name) {
589+
scryptN = crypto.LightScryptN
590+
scryptP = crypto.LightScryptP
591+
}
592+
ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"), scryptN, scryptP)
583593
return accounts.NewManager(ks)
584594
}
585595

common/natspec/natspec_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) {
128128
db, _ := ethdb.NewMemDatabase()
129129
addr := common.HexToAddress(testAddress)
130130
core.WriteGenesisBlockForTesting(db, core.GenesisAccount{addr, common.String2Big(testBalance)})
131-
ks := crypto.NewKeyStorePassphrase(filepath.Join(tmp, "keystore"))
131+
ks := crypto.NewKeyStorePassphrase(filepath.Join(tmp, "keystore"), crypto.LightScryptN, crypto.LightScryptP)
132132
am := accounts.NewManager(ks)
133133
keyb, err := crypto.HexToECDSA(testKey)
134134
if err != nil {

common/natspec/natspec_e2e_test.go.orig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) {
106106
}
107107

108108
// create a testAddress
109-
ks := crypto.NewKeyStorePassphrase("/tmp/eth-natspec/keystore")
109+
ks := crypto.NewKeyStorePassphrase("/tmp/eth-natspec/keystore", crypto.LightScryptN, crypto.LightScryptP)
110110
am := accounts.NewManager(ks)
111111
testAccount, err := am.NewAccount("password")
112112
if err != nil {

crypto/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) {
215215

216216
// Used only by block tests.
217217
func ImportBlockTestKey(privKeyBytes []byte) error {
218-
ks := NewKeyStorePassphrase(common.DefaultDataDir() + "/keystore")
218+
ks := NewKeyStorePassphrase(common.DefaultDataDir()+"/keystore", LightScryptN, LightScryptP)
219219
ecKey := ToECDSA(privKeyBytes)
220220
key := &Key{
221221
Id: uuid.NewRandom(),

crypto/key_store_passphrase.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,29 @@ import (
4545

4646
const (
4747
keyHeaderKDF = "scrypt"
48-
// 2^18 / 8 / 1 uses 256MB memory and approx 1s CPU time on a modern CPU.
49-
scryptN = 1 << 18
50-
scryptr = 8
51-
scryptp = 1
52-
scryptdkLen = 32
48+
49+
// n,r,p = 2^18, 8, 1 uses 256MB memory and approx 1s CPU time on a modern CPU.
50+
StandardScryptN = 1 << 18
51+
StandardScryptP = 1
52+
53+
// n,r,p = 2^12, 8, 6 uses 4MB memory and approx 100ms CPU time on a modern CPU.
54+
LightScryptN = 1 << 12
55+
LightScryptP = 6
56+
57+
scryptR = 8
58+
scryptDKLen = 32
5359
)
5460

5561
type keyStorePassphrase struct {
5662
keysDirPath string
63+
scryptN int
64+
scryptP int
65+
scryptR int
66+
scryptDKLen int
5767
}
5868

59-
func NewKeyStorePassphrase(path string) KeyStore {
60-
return &keyStorePassphrase{path}
69+
func NewKeyStorePassphrase(path string, scryptN int, scryptP int) KeyStore {
70+
return &keyStorePassphrase{path, scryptN, scryptP, scryptR, scryptDKLen}
6171
}
6272

6373
func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
@@ -87,11 +97,10 @@ func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err
8797
func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
8898
authArray := []byte(auth)
8999
salt := randentropy.GetEntropyCSPRNG(32)
90-
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptr, scryptp, scryptdkLen)
100+
derivedKey, err := scrypt.Key(authArray, salt, ks.scryptN, ks.scryptR, ks.scryptP, ks.scryptDKLen)
91101
if err != nil {
92102
return err
93103
}
94-
95104
encryptKey := derivedKey[:16]
96105
keyBytes := FromECDSA(key.PrivateKey)
97106

@@ -104,10 +113,10 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
104113
mac := Sha3(derivedKey[16:32], cipherText)
105114

106115
scryptParamsJSON := make(map[string]interface{}, 5)
107-
scryptParamsJSON["n"] = scryptN
108-
scryptParamsJSON["r"] = scryptr
109-
scryptParamsJSON["p"] = scryptp
110-
scryptParamsJSON["dklen"] = scryptdkLen
116+
scryptParamsJSON["n"] = ks.scryptN
117+
scryptParamsJSON["r"] = ks.scryptR
118+
scryptParamsJSON["p"] = ks.scryptP
119+
scryptParamsJSON["dklen"] = ks.scryptDKLen
111120
scryptParamsJSON["salt"] = hex.EncodeToString(salt)
112121

113122
cipherParamsJSON := cipherparamsJSON{

crypto/key_store_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestKeyStorePlain(t *testing.T) {
5656
}
5757

5858
func TestKeyStorePassphrase(t *testing.T) {
59-
ks := NewKeyStorePassphrase(common.DefaultDataDir())
59+
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
6060
pass := "foo"
6161
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
6262
if err != nil {
@@ -82,7 +82,7 @@ func TestKeyStorePassphrase(t *testing.T) {
8282
}
8383

8484
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
85-
ks := NewKeyStorePassphrase(common.DefaultDataDir())
85+
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
8686
pass := "foo"
8787
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
8888
if err != nil {
@@ -110,7 +110,7 @@ func TestImportPreSaleKey(t *testing.T) {
110110
// python pyethsaletool.py genwallet
111111
// with password "foo"
112112
fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"[email protected]\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
113-
ks := NewKeyStorePassphrase(common.DefaultDataDir())
113+
ks := NewKeyStorePassphrase(common.DefaultDataDir(), LightScryptN, LightScryptP)
114114
pass := "foo"
115115
_, err := ImportPreSaleKey(ks, []byte(fileContent), pass)
116116
if err != nil {
@@ -168,7 +168,7 @@ func TestV1_1(t *testing.T) {
168168
}
169169

170170
func TestV1_2(t *testing.T) {
171-
ks := NewKeyStorePassphrase("tests/v1")
171+
ks := NewKeyStorePassphrase("tests/v1", LightScryptN, LightScryptP)
172172
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
173173
k, err := ks.GetKey(addr, "g")
174174
if err != nil {

tests/block_test_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func runBlockTests(bt map[string]*BlockTest, skipTests []string) error {
162162

163163
}
164164
func runBlockTest(test *BlockTest) error {
165-
ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"))
165+
ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"), crypto.StandardScryptN, crypto.StandardScryptP)
166166
am := accounts.NewManager(ks)
167167
db, _ := ethdb.NewMemDatabase()
168168
cfg := &eth.Config{

0 commit comments

Comments
 (0)