Skip to content

Commit

Permalink
Trivial: replace systemm.out with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
semux committed Jul 2, 2019
1 parent 50bbd1b commit 118e1e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
31 changes: 17 additions & 14 deletions src/main/java/org/semux/crypto/bip32/HdKeyGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.semux.crypto.bip32.util.HashUtil;
import org.semux.crypto.bip32.util.Hmac;
import org.semux.crypto.bip32.util.Secp256k1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.EdDSAPublicKey;
Expand All @@ -31,7 +33,7 @@

public class HdKeyGenerator {

private static boolean trace = true;
private static final Logger logger = LoggerFactory.getLogger(HdKeyGenerator.class);

private static final EdDSAParameterSpec ED25519SPEC = EdDSANamedCurveTable.getByName("ed25519");

Expand Down Expand Up @@ -265,9 +267,9 @@ public HdKeyPair getChildKeyPair(HdKeyPair parent, long child, boolean isHardene
publicKey.setKeyData(BytesUtil.merge(new byte[] { 0 }, pk.getAbyte()));
break;
case BIP32_ED25519:
byte[] kL = parent.getPrivateKey().getKeyData();
byte[] kLP = Arrays.copyOfRange(kL, 0, 32);
byte[] kRP = Arrays.copyOfRange(kL, 32, 64);
byte[] kP = parent.getPrivateKey().getKeyData();
byte[] kLP = Arrays.copyOfRange(kP, 0, 32);
byte[] kRP = Arrays.copyOfRange(kP, 32, 64);
byte[] AP = parent.getPublicKey().getKeyData();
byte[] cP = parent.getPublicKey().getChainCode();

Expand All @@ -287,11 +289,11 @@ public HdKeyPair getChildKeyPair(HdKeyPair parent, long child, boolean isHardene
byte[] ZL = Arrays.copyOfRange(Z, 0, 28);
byte[] ZR = Arrays.copyOfRange(Z, 32, 64);

if (trace) {
System.out.println("parent, kLP = " + Hex.encode(kLP));
System.out.println("parent, kRP = " + Hex.encode(kRP));
System.out.println("parent, AP = " + Hex.encode(AP));
System.out.println("parent, cP = " + Hex.encode(cP));
if (logger.isTraceEnabled()) {
logger.trace("parent, kLP = " + Hex.encode(kLP));
logger.trace("parent, kRP = " + Hex.encode(kRP));
logger.trace("parent, AP = " + Hex.encode(AP));
logger.trace("parent, cP = " + Hex.encode(cP));
}

BigInteger kLiBI = parseUnsignedLE(ZL)
Expand All @@ -318,12 +320,13 @@ public HdKeyPair getChildKeyPair(HdKeyPair parent, long child, boolean isHardene
privateKey.setChainCode(c);
publicKey.setChainCode(c);

if (trace) {
System.out.println("child, IL = " + Hex.encode(IL));
System.out.println("child, IR = " + Hex.encode(IR));
System.out.println("child, A = " + Hex.encode(A));
System.out.println("child, c = " + Hex.encode(c));
if (logger.isTraceEnabled()) {
logger.trace("child, IL = " + Hex.encode(IL));
logger.trace("child, IR = " + Hex.encode(IR));
logger.trace("child, A = " + Hex.encode(A));
logger.trace("child, c = " + Hex.encode(c));
}

break;
}

Expand Down
44 changes: 24 additions & 20 deletions src/test/java/org/semux/crypto/bip32/Bip32Ed25519Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
import org.semux.crypto.bip32.key.KeyVersion;
import org.semux.crypto.bip39.Language;
import org.semux.crypto.bip39.MnemonicGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Bip32Ed25519Test {

private static final Logger logger = LoggerFactory.getLogger(Bip32Ed25519Test.class);

private byte[] SEED = new MnemonicGenerator().getSeedFromWordlist(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
"",
Expand All @@ -26,20 +30,20 @@ public class Bip32Ed25519Test {

@Test
public void testRoot() {
System.out.println("k = " + Hex.encode(root.getPrivateKey().getKeyData()));
System.out.println("A = " + Hex.encode(root.getPublicKey().getKeyData()));
System.out.println("c = " + Hex.encode(root.getPublicKey().getChainCode()));

assertEquals(
"5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4",
Hex.encode(SEED));
assertEquals("402b03cd9c8bed9ba9f9bd6cd9c315ce9fcc59c7c25d37c85a36096617e69d41"
+ "8e35cb4a3b737afd007f0688618f21a8831643c0e6c77fc33c06026d2a0fc938",
Hex.encode(root.getPrivateKey().getKeyData()));
assertEquals("291ea7aa3766cd26a3a8688375aa07b3fed73c13d42543a9f19a48dc8b6bfd07",
Hex.encode(root.getPublicKey().getKeyData()));
assertEquals("32596435e70647d7d98ef102a32ea40319ca8fb6c851d7346d3bd8f9d1492658",
Hex.encode(root.getPublicKey().getChainCode()));
String seed = "5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4";
String kL = "402b03cd9c8bed9ba9f9bd6cd9c315ce9fcc59c7c25d37c85a36096617e69d41";
String kR = "8e35cb4a3b737afd007f0688618f21a8831643c0e6c77fc33c06026d2a0fc938";
String A = "291ea7aa3766cd26a3a8688375aa07b3fed73c13d42543a9f19a48dc8b6bfd07";
String c = "32596435e70647d7d98ef102a32ea40319ca8fb6c851d7346d3bd8f9d1492658";

logger.info("k = " + Hex.encode(root.getPrivateKey().getKeyData()));
logger.info("A = " + Hex.encode(root.getPublicKey().getKeyData()));
logger.info("c = " + Hex.encode(root.getPublicKey().getChainCode()));

assertEquals(seed, Hex.encode(SEED));
assertEquals(kL + kR, Hex.encode(root.getPrivateKey().getKeyData()));
assertEquals(A, Hex.encode(root.getPublicKey().getKeyData()));
assertEquals(c, Hex.encode(root.getPublicKey().getChainCode()));
}

@Test
Expand All @@ -54,9 +58,9 @@ public void testOne() {
HdKeyPair child2 = generator.getChildKeyPair(child1, 1, false);
HdKeyPair child3 = generator.getChildKeyPair(child2, 2, false);

System.out.println("k = " + Hex.encode(child3.getPrivateKey().getKeyData()));
System.out.println("A = " + Hex.encode(child3.getPublicKey().getKeyData()));
System.out.println("c = " + Hex.encode(child3.getPublicKey().getChainCode()));
logger.info("k = " + Hex.encode(child3.getPrivateKey().getKeyData()));
logger.info("A = " + Hex.encode(child3.getPublicKey().getKeyData()));
logger.info("c = " + Hex.encode(child3.getPublicKey().getChainCode()));

assertEquals(kL + kR, Hex.encode(child3.getPrivateKey().getKeyData()));
assertEquals(A, Hex.encode(child3.getPublicKey().getKeyData()));
Expand All @@ -75,9 +79,9 @@ public void testTwo() {
HdKeyPair child2 = generator.getChildKeyPair(child1, 3, true);
HdKeyPair child3 = generator.getChildKeyPair(child2, 5, false);

System.out.println("k = " + Hex.encode(child3.getPrivateKey().getKeyData()));
System.out.println("A = " + Hex.encode(child3.getPublicKey().getKeyData()));
System.out.println("c = " + Hex.encode(child3.getPublicKey().getChainCode()));
logger.info("k = " + Hex.encode(child3.getPrivateKey().getKeyData()));
logger.info("A = " + Hex.encode(child3.getPublicKey().getKeyData()));
logger.info("c = " + Hex.encode(child3.getPublicKey().getChainCode()));

assertEquals(kL + kR, Hex.encode(child3.getPrivateKey().getKeyData()));
assertEquals(A, Hex.encode(child3.getPublicKey().getKeyData()));
Expand Down

0 comments on commit 118e1e4

Please sign in to comment.