Skip to content

Commit

Permalink
fix: add final to some constant fields
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed May 3, 2024
1 parent 83a76ab commit 8ded155
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public abstract class BLSAbstractLazyObject extends Message {
boolean initialized;
boolean legacy;

public BLSAbstractLazyObject(NetworkParameters params) {
protected BLSAbstractLazyObject(NetworkParameters params) {
super(params);
}

public BLSAbstractLazyObject(BLSAbstractLazyObject lazyObject) {
protected BLSAbstractLazyObject(BLSAbstractLazyObject lazyObject) {
super(lazyObject.params);
this.buffer = lazyObject.buffer;
this.initialized = lazyObject.initialized;
this.legacy = lazyObject.legacy;
}

public BLSAbstractLazyObject(NetworkParameters params, byte [] payload, int offset, boolean legacy) {
protected BLSAbstractLazyObject(NetworkParameters params, byte [] payload, int offset, boolean legacy) {
super(params, payload, offset, params.getProtocolVersionNum(legacy ? NetworkParameters.ProtocolVersion.BLS_LEGACY : NetworkParameters.ProtocolVersion.BLS_BASIC));
}

Expand All @@ -49,7 +50,7 @@ protected void parse() throws ProtocolException {
initialized = false;
}

abstract protected void bitcoinSerializeToStream(OutputStream stream, boolean legacy) throws IOException;
protected abstract void bitcoinSerializeToStream(OutputStream stream, boolean legacy) throws IOException;

public void bitcoinSerialize(OutputStream stream, boolean legacy) throws IOException {
bitcoinSerializeToStream(stream, legacy);
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/bitcoinj/crypto/BLSId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import org.bitcoinj.core.Sha256Hash;

public class BLSId extends BLSAbstractObject {
public static int BLS_CURVE_ID_SIZE = 32;
Sha256Hash hash;
public static final int BLS_CURVE_ID_SIZE = 32;

BLSId() {
super(BLS_CURVE_ID_SIZE);
}

BLSId(Sha256Hash hash) {
public BLSId(Sha256Hash hash) {
super(BLS_CURVE_ID_SIZE);
valid = true;
this.hash = Sha256Hash.wrap(hash.getBytes());
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/bitcoinj/crypto/BLSLazyPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ protected void bitcoinSerializeToStream(OutputStream stream, boolean legacy) thr
}
}

public static BLSPublicKey invalidSignature = new BLSPublicKey();
public static final BLSPublicKey invalidKey = new BLSPublicKey();

public BLSPublicKey getPublicKey() {
if(buffer == null && !initialized)
return invalidSignature;
return invalidKey;
if(!initialized) {
publicKey = new BLSPublicKey(params, buffer, 0, legacy);
buffer = null; //save memory
Expand All @@ -117,7 +117,7 @@ public BLSPublicKey getPublicKey() {

@Override
public String toString() {
return initialized ? publicKey.toString() : (buffer == null ? invalidSignature.toString() : Utils.HEX.encode(buffer));
return initialized ? publicKey.toString() : (buffer == null ? invalidKey.toString() : Utils.HEX.encode(buffer));
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public BLSLazySignature assign(BLSLazySignature blsLazySignature) {
return this;
}

public static BLSSignature invalidSignature = new BLSSignature();
public static final BLSSignature invalidSignature = new BLSSignature();

public void setSignature(BLSSignature signature) {
lock.lock();
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/bitcoinj/crypto/BLSPublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/

public class BLSPublicKey extends BLSAbstractObject {
public static int BLS_CURVE_PUBKEY_SIZE = 48;
public static final int BLS_CURVE_PUBKEY_SIZE = 48;
G1Element publicKeyImpl;

public BLSPublicKey() {
Expand Down Expand Up @@ -216,6 +216,7 @@ public long getFingerprint(boolean legacy) {
return publicKeyImpl.getFingerprint(legacy);
}

@Override
public boolean isLegacy() {
return legacy;
}
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/org/bitcoinj/crypto/BLSSecretKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import org.dashj.bls.PrivateKey;
import org.dashj.bls.PrivateKeyVector;

import java.util.ArrayList;
import java.util.List;

/**
* This class wraps a PrivateKey in the BLS library
*/

public class BLSSecretKey extends BLSAbstractObject
{
public static int BLS_CURVE_SECKEY_SIZE = 32;
public static final int BLS_CURVE_SECKEY_SIZE = 32;
PrivateKey privateKey;

BLSSecretKey() {
Expand Down Expand Up @@ -110,7 +110,7 @@ public void aggregateInsecure(BLSSecretKey sk) {
updateHash();
}

public static BLSSecretKey aggregateInsecure(ArrayList<BLSSecretKey> sks) {
public static BLSSecretKey aggregateInsecure(List<BLSSecretKey> sks) {
if(sks.isEmpty()) {
return new BLSSecretKey();
}
Expand All @@ -125,8 +125,7 @@ public static BLSSecretKey aggregateInsecure(ArrayList<BLSSecretKey> sks) {
return new BLSSecretKey(agg);
}

static public BLSSecretKey makeNewKey()
{
public static BLSSecretKey makeNewKey() {
byte [] buf = new byte[32];
while (true) {
new LinuxSecureRandom().engineNextBytes(buf);
Expand All @@ -138,8 +137,7 @@ static public BLSSecretKey makeNewKey()
}
}

boolean secretKeyShare(ArrayList<BLSSecretKey> msk, BLSId id)
{
public boolean secretKeyShare(List<BLSSecretKey> msk, BLSId id) {
valid = false;
updateHash();

Expand Down
11 changes: 4 additions & 7 deletions core/src/main/java/org/bitcoinj/crypto/BLSSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/

public class BLSSignature extends BLSAbstractObject {

public static int BLS_CURVE_SIG_SIZE = 96;
static byte [] emptySignatureBytes = new byte[BLS_CURVE_SIG_SIZE];
public static final int BLS_CURVE_SIG_SIZE = 96;
static final byte [] emptySignatureBytes = new byte[BLS_CURVE_SIG_SIZE];
G2Element signatureImpl;

BLSSignature() {
Expand Down Expand Up @@ -220,8 +219,7 @@ public boolean verifySecureAggregated(ArrayList<BLSPublicKey> pks, Sha256Hash ha
return BLSScheme.get(legacy).verifySecure(vecPublicKeys, signatureImpl, hash.getBytes());
}

public boolean recover(ArrayList<BLSSignature> sigs, ArrayList<BLSId> ids)
{
public boolean recover(List<BLSSignature> sigs, List<BLSId> ids) {
valid = false;
updateHash();

Expand Down Expand Up @@ -253,8 +251,7 @@ public boolean recover(ArrayList<BLSSignature> sigs, ArrayList<BLSId> ids)
return true;
}

public boolean checkMalleable(byte [] buf, int size)
{
public boolean checkMalleable(byte [] buf, int size) {
byte [] buf2 = getBuffer(serializedSize, legacy);
if (!Arrays.equals(buf, buf2)) {
// TODO not sure if this is actually possible with the BLS libs. I'm assuming here that somewhere deep inside
Expand Down
7 changes: 1 addition & 6 deletions core/src/main/java/org/bitcoinj/crypto/bls/BLSKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,6 @@ private String toString(boolean includePrivate, @Nullable KeyParameter aesKey, @
BLSKey decryptedKey = isEncrypted() ? decrypt(checkNotNull(aesKey)) : this;
try {
helper.add("priv HEX", decryptedKey.getPrivateKeyAsHex());
//helper.add("priv WIF", decryptedKey.getPrivateKeyAsWiF(params));
} catch (IllegalStateException e) {
// TODO: Make hasPrivKey() work for deterministic keys and fix this.
} catch (Exception e) {
Expand All @@ -834,11 +833,7 @@ private String toString(boolean includePrivate, @Nullable KeyParameter aesKey, @
public void formatKeyWithAddress(boolean includePrivateKeys, @Nullable KeyParameter aesKey, StringBuilder builder,
NetworkParameters params, Script.ScriptType outputScriptType, @Nullable String comment) {
builder.append(" addr:");
if (outputScriptType != null) {
builder.append(Address.fromPubKeyHash(params, getPubKeyHash()));
} else {
builder.append(Address.fromPubKeyHash(params, getPubKeyHash()));
}
builder.append(Address.fromPubKeyHash(params, getPubKeyHash()));

builder.append(" hash160:");
builder.append(Utils.HEX.encode(getPubKeyHash()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@

public class BLSVerifySignature {

private static final Random rand = new Random();
static {
BLS.Init();
}

private static byte [] getRandomSeed(int size) {
Random rand = new Random();
BigInteger result = new BigInteger((size) *8 - 1, rand); // (2^4-1) = 15 is the maximum value
byte [] bytes = new byte [32];
System.arraycopy(result.toByteArray(), 0, bytes, 0, result.toByteArray().length);
Expand Down

0 comments on commit 8ded155

Please sign in to comment.