Fix javadocs to allow build to pass in JDK 17 (#4834)

- Added missing javadocs so that javadoc doclint passes against JDK 17 (invoke by Besu gradle build).
- Exclude following packages from javadoc lint:
org.hyperledger.besu.privacy.contracts.generated
org.hyperledger.besu.tests.acceptance.*
- Temporarily exclude ethereum and evm submodule for doc lint checks.
- Run the javadoc task using GitHub actions (use Java 17) to report any javadoc errors during the PR builds
- Updating plugin-api build.gradle with new hash as javadoc comments caused it to change

Signed-off-by: Usman Saleem <usman@usmans.info>
This commit is contained in:
Usman Saleem
2023-01-18 22:51:00 +10:00
committed by GitHub
parent aef335cfcf
commit 9eb32836b7
763 changed files with 15266 additions and 74 deletions

View File

@@ -41,22 +41,38 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.math.ec.ECPoint;
/** The Abstract secp256. */
public abstract class AbstractSECP256 implements SignatureAlgorithm {
/** The constant PRIVATE_KEY_BYTE_LENGTH. */
protected static final int PRIVATE_KEY_BYTE_LENGTH = 32;
/** The constant PUBLIC_KEY_BYTE_LENGTH. */
protected static final int PUBLIC_KEY_BYTE_LENGTH = 64;
/** The constant SIGNATURE_BYTE_LENGTH. */
protected static final int SIGNATURE_BYTE_LENGTH = 65;
/** The constant PROVIDER. */
public static final String PROVIDER = "BC";
/** The Curve. */
protected final ECDomainParameters curve;
/** The Half curve order. */
protected final BigInteger halfCurveOrder;
/** The Key pair generator. */
protected final KeyPairGenerator keyPairGenerator;
/** The Curve order. */
protected final BigInteger curveOrder;
/** The Prime. */
final BigInteger prime;
/**
* Instantiates a new Abstract secp 256.
*
* @param curveName the curve name
* @param prime the prime
*/
protected AbstractSECP256(final String curveName, final BigInteger prime) {
this.prime = prime;
Security.addProvider(new BouncyCastleProvider());
@@ -215,8 +231,20 @@ public abstract class AbstractSECP256 implements SignatureAlgorithm {
return PROVIDER;
}
/**
* Gets K calculator.
*
* @return the K Calculator
*/
public abstract DSAKCalculator getKCalculator();
/**
* Decompress key ec point.
*
* @param xBN the x bn
* @param yBit the y bit
* @return the ec point
*/
// Decompress a compressed public key (x co-ord and low-bit of y-coord).
protected ECPoint decompressKey(final BigInteger xBN, final boolean yBit) {
final X9IntegerConverter x9 = new X9IntegerConverter();

View File

@@ -18,12 +18,15 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Provider;
/** The Besu Security provider. */
public final class BesuProvider extends Provider {
private static final String info = "Besu Security Provider v1.0";
/** The constant PROVIDER_NAME. */
public static final String PROVIDER_NAME = "Besu";
/** Instantiates a new Besu provider. */
@SuppressWarnings({"unchecked", "removal"})
public BesuProvider() {
super(PROVIDER_NAME, "1.0", info);

View File

@@ -25,9 +25,11 @@ import org.bouncycastle.util.Pack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** The type Blake2bf message digest. */
public class Blake2bfMessageDigest extends BCMessageDigest implements Cloneable {
private static final Logger LOG = LoggerFactory.getLogger(Blake2bfMessageDigest.class);
/** Instantiates a new Blake2bf message digest. */
public Blake2bfMessageDigest() {
super(new Blake2bfDigest());
}
@@ -42,6 +44,7 @@ public class Blake2bfMessageDigest extends BCMessageDigest implements Cloneable
* <p>Optimized for 64-bit platforms
*/
public static class Blake2bfDigest implements Digest {
/** The constant MESSAGE_LENGTH_BYTES. */
public static final int MESSAGE_LENGTH_BYTES = 213;
private static final long[] IV = {
@@ -91,6 +94,7 @@ public class Blake2bfMessageDigest extends BCMessageDigest implements Cloneable
}
}
/** Instantiates a new Blake2bf digest. */
Blake2bfDigest() {
if (!useNative) {
LOG.info("Native blake2bf not available");
@@ -108,10 +112,16 @@ public class Blake2bfMessageDigest extends BCMessageDigest implements Cloneable
v = new long[16];
}
/** Disable native. */
public static void disableNative() {
useNative = false;
}
/**
* Is native.
*
* @return the boolean
*/
public static boolean isNative() {
return useNative;
}

View File

@@ -22,7 +22,14 @@ import org.apache.tuweni.bytes.Bytes32;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.util.BigIntegers;
/** The Ec point util class. */
public class ECPointUtil {
/**
* From bouncy castle ec point to ECPoint.
*
* @param bouncyCastleECPoint the bouncy castle ec point
* @return the ECPoint
*/
public static ECPoint fromBouncyCastleECPoint(
final org.bouncycastle.math.ec.ECPoint bouncyCastleECPoint) {
final ECFieldElement xCoord = bouncyCastleECPoint.getAffineXCoord();
@@ -37,6 +44,12 @@ public class ECPointUtil {
return new ECPoint(x, y);
}
/**
* Gets encoded bytes.
*
* @param ecPoint the ec point
* @return the encoded bytes
*/
public static Bytes getEncodedBytes(final ECPoint ecPoint) {
final Bytes xBytes = Bytes32.wrap(BigIntegers.asUnsignedByteArray(32, ecPoint.getAffineX()));
final Bytes yBytes = Bytes32.wrap(BigIntegers.asUnsignedByteArray(32, ecPoint.getAffineY()));

View File

@@ -26,7 +26,9 @@ import org.apache.tuweni.bytes.Bytes32;
public abstract class Hash {
private Hash() {}
/** The constant KECCAK256_ALG. */
public static final String KECCAK256_ALG = "KECCAK-256";
private static final String SHA256_ALG = "SHA-256";
private static final String RIPEMD160_ALG = "RIPEMD160";
private static final String BLAKE2BF_ALG = "BLAKE2BF";

View File

@@ -25,11 +25,18 @@ import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
/** The Key pair class. */
public class KeyPair {
private final SECPPrivateKey privateKey;
private final SECPPublicKey publicKey;
/**
* Instantiates a new Key pair.
*
* @param privateKey the private key
* @param publicKey the public key
*/
public KeyPair(final SECPPrivateKey privateKey, final SECPPublicKey publicKey) {
checkNotNull(privateKey);
checkNotNull(publicKey);
@@ -37,11 +44,26 @@ public class KeyPair {
this.publicKey = publicKey;
}
/**
* Create key pair.
*
* @param privateKey the private key
* @param curve the curve
* @param algorithm the algorithm
* @return the key pair
*/
public static KeyPair create(
final SECPPrivateKey privateKey, final ECDomainParameters curve, final String algorithm) {
return new KeyPair(privateKey, SECPPublicKey.create(privateKey, curve, algorithm));
}
/**
* Generate key pair.
*
* @param keyPairGenerator the key pair generator
* @param algorithm the algorithm
* @return the key pair
*/
public static KeyPair generate(final KeyPairGenerator keyPairGenerator, final String algorithm) {
final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair();
final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
@@ -85,10 +107,20 @@ public class KeyPair {
+ "]");
}
/**
* Gets private key.
*
* @return the private key
*/
public SECPPrivateKey getPrivateKey() {
return privateKey;
}
/**
* Gets public key.
*
* @return the public key
*/
public SECPPublicKey getPublicKey() {
return publicKey;
}

View File

@@ -36,6 +36,11 @@ public class KeyPairSecurityModule implements SecurityModule {
private final PublicKey publicKey;
private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
/**
* Instantiates a new Key pair security module.
*
* @param keyPair the key pair
*/
public KeyPairSecurityModule(final KeyPair keyPair) {
this.keyPair = keyPair;
this.publicKey = convertPublicKey(keyPair.getPublicKey());
@@ -84,6 +89,11 @@ public class KeyPairSecurityModule implements SecurityModule {
private final SECPSignature signature;
/**
* Instantiates a new Signature.
*
* @param signature the signature
*/
SignatureImpl(final SECPSignature signature) {
this.signature = signature;
}
@@ -102,6 +112,11 @@ public class KeyPairSecurityModule implements SecurityModule {
private static class PublicKeyImpl implements PublicKey {
private final ECPoint w;
/**
* Instantiates a new Public key.
*
* @param w the w ECPoint
*/
PublicKeyImpl(final ECPoint w) {
this.w = w;
}

View File

@@ -32,11 +32,18 @@ import org.apache.tuweni.bytes.Bytes32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** The Key pair util. */
public class KeyPairUtil {
private static final Logger LOG = LoggerFactory.getLogger(KeyPairUtil.class);
private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Suppliers.memoize(SignatureAlgorithmFactory::getInstance);
/**
* Load resource file string.
*
* @param resourcePath the resource path
* @return the string
*/
public static String loadResourceFile(final String resourcePath) {
try {
URL path = KeyPairUtil.class.getClassLoader().getResource(resourcePath);
@@ -46,6 +53,12 @@ public class KeyPairUtil {
}
}
/**
* Load key pair from resource.
*
* @param resourcePath the resource path
* @return the key pair
*/
public static KeyPair loadKeyPairFromResource(final String resourcePath) {
final KeyPair keyPair;
String keyData = loadResourceFile(resourcePath);
@@ -60,6 +73,12 @@ public class KeyPairUtil {
return keyPair;
}
/**
* Load key pair.
*
* @param keyFile the key file
* @return the key pair
*/
public static KeyPair loadKeyPair(final File keyFile) {
final KeyPair key;
@@ -82,10 +101,22 @@ public class KeyPairUtil {
return key;
}
/**
* Load key pair.
*
* @param directory the directory
* @return the key pair
*/
public static KeyPair loadKeyPair(final Path directory) {
return loadKeyPair(getDefaultKeyFile(directory));
}
/**
* Store key file.
*
* @param keyPair the key pair
* @param homeDirectory the home directory
*/
public static void storeKeyFile(final KeyPair keyPair, final Path homeDirectory) {
try {
storeKeyPair(keyPair, getDefaultKeyFile(homeDirectory));
@@ -94,14 +125,32 @@ public class KeyPairUtil {
}
}
/**
* Gets default key file.
*
* @param directory the directory
* @return the default key file
*/
public static File getDefaultKeyFile(final Path directory) {
return directory.resolve("key").toFile();
}
/**
* Load key pair.
*
* @param file the file
* @return the key pair
*/
public static KeyPair load(final File file) {
return SIGNATURE_ALGORITHM.get().createKeyPair(loadPrivateKey(file));
}
/**
* Load secp private key.
*
* @param file the file
* @return the secp private key
*/
static SECPPrivateKey loadPrivateKey(final File file) {
try {
final List<String> info = Files.readAllLines(file.toPath());
@@ -114,6 +163,13 @@ public class KeyPairUtil {
}
}
/**
* Store key pair.
*
* @param keyPair the key pair
* @param file the file
* @throws IOException the io exception
*/
static void storeKeyPair(final KeyPair keyPair, final File file) throws IOException {
final File privateKeyDir = file.getParentFile();
privateKeyDir.mkdirs();

View File

@@ -20,6 +20,7 @@ import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/** The Message digest factory. */
public class MessageDigestFactory {
static {
@@ -27,6 +28,13 @@ public class MessageDigestFactory {
Security.addProvider(new BouncyCastleProvider());
}
/**
* Create message digest.
*
* @param algorithm the algorithm
* @return the message digest
* @throws NoSuchAlgorithmException the no such algorithm exception
*/
@SuppressWarnings("DoNotInvokeMessageDigestDirectly")
public static MessageDigest create(final String algorithm) throws NoSuchAlgorithmException {
return MessageDigest.getInstance(algorithm);

View File

@@ -19,15 +19,27 @@ import org.hyperledger.besu.plugin.services.securitymodule.data.Signature;
import org.apache.tuweni.bytes.Bytes32;
/** The Node key. */
public class NodeKey {
private final SecurityModule securityModule;
private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
/**
* Instantiates a new Node key.
*
* @param securityModule the security module
*/
public NodeKey(final SecurityModule securityModule) {
this.securityModule = securityModule;
}
/**
* Sign hash and get secp signature.
*
* @param dataHash the data hash
* @return the secp signature
*/
public SECPSignature sign(final Bytes32 dataHash) {
final Signature signature = securityModule.sign(dataHash);
@@ -35,11 +47,22 @@ public class NodeKey {
signature.getR(), signature.getS(), getPublicKey(), dataHash);
}
/**
* Gets public key.
*
* @return the public key
*/
public SECPPublicKey getPublicKey() {
return signatureAlgorithm.createPublicKey(
ECPointUtil.getEncodedBytes(securityModule.getPublicKey().getW()));
}
/**
* Calculate ecdh key agreement.
*
* @param partyKey the party key
* @return the bytes32
*/
public Bytes32 calculateECDHKeyAgreement(final SECPPublicKey partyKey) {
return securityModule.calculateECDHKeyAgreement(
() -> ECPointUtil.fromBouncyCastleECPoint(signatureAlgorithm.publicKeyAsEcPoint(partyKey)));

View File

@@ -36,6 +36,7 @@ import org.bouncycastle.math.ec.custom.sec.SecP256K1Curve;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** The SECP256K1 implementation. */
/*
* Adapted from the BitcoinJ ECKey (Apache 2 License) implementation:
* https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/ECKey.java
@@ -50,8 +51,10 @@ public class SECP256K1 extends AbstractSECP256 {
private boolean useNative;
/** The constant CURVE_NAME. */
public static final String CURVE_NAME = "secp256k1";
/** Instantiates a new SECP256K1. */
public SECP256K1() {
super(CURVE_NAME, SecP256K1Curve.q);

View File

@@ -29,14 +29,18 @@ import org.bouncycastle.math.ec.custom.sec.SecP256R1Curve;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** The SECP256R1 implementation. */
public class SECP256R1 extends AbstractSECP256 {
private static final Logger LOG = LoggerFactory.getLogger(SECP256R1.class);
/** The constant CURVE_NAME. */
public static final String CURVE_NAME = "secp256r1";
private boolean useNative;
private final LibSECP256R1 libSECP256R1 = new LibSECP256R1();
/** Instantiates a new SECP256R1. */
public SECP256R1() {
super(CURVE_NAME, SecP256R1Curve.q);
try {

View File

@@ -21,9 +21,12 @@ import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Secp private key. */
public class SECPPrivateKey implements java.security.PrivateKey {
/** Encoded Bytes */
private final Bytes32 encoded;
/** Algorithm */
private final String algorithm;
private SECPPrivateKey(final Bytes32 encoded, final String algorithm) {
@@ -33,11 +36,25 @@ public class SECPPrivateKey implements java.security.PrivateKey {
this.algorithm = algorithm;
}
/**
* Create secp private key.
*
* @param key the key
* @param algorithm the algorithm
* @return the secp private key
*/
public static SECPPrivateKey create(final BigInteger key, final String algorithm) {
checkNotNull(key);
return create(UInt256.valueOf(key), algorithm);
}
/**
* Create secp private key.
*
* @param key the key
* @param algorithm the algorithm
* @return the secp private key
*/
public static SECPPrivateKey create(final Bytes32 key, final String algorithm) {
return new SECPPrivateKey(key, algorithm);
}
@@ -57,10 +74,20 @@ public class SECPPrivateKey implements java.security.PrivateKey {
return encoded.toArrayUnsafe();
}
/**
* Gets encoded bytes.
*
* @return the encoded bytes
*/
public Bytes32 getEncodedBytes() {
return encoded;
}
/**
* Gets d.
*
* @return the d
*/
public BigInteger getD() {
return encoded.toUnsignedBigInteger();
}

View File

@@ -26,22 +26,48 @@ import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
/** The Secp public key. */
public class SECPPublicKey implements java.security.PublicKey {
/** The constant BYTE_LENGTH. */
public static final int BYTE_LENGTH = 64;
/** Encoded Bytes */
private final Bytes encoded;
/** Algorithm */
private final String algorithm;
/**
* Create secp public key.
*
* @param key the key
* @param algorithm the algorithm
* @return the secp public key
*/
public static SECPPublicKey create(final BigInteger key, final String algorithm) {
checkNotNull(key);
return create(toBytes64(key.toByteArray()), algorithm);
}
/**
* Create secp public key.
*
* @param encoded the encoded
* @param algorithm the algorithm
* @return the secp public key
*/
public static SECPPublicKey create(final Bytes encoded, final String algorithm) {
return new SECPPublicKey(encoded, algorithm);
}
/**
* Create secp public key.
*
* @param privateKey the private key
* @param curve the curve
* @param algorithm the algorithm
* @return the secp public key
*/
public static SECPPublicKey create(
final SECPPrivateKey privateKey, final ECDomainParameters curve, final String algorithm) {
BigInteger privKey = privateKey.getEncodedBytes().toUnsignedBigInteger();
@@ -111,6 +137,11 @@ public class SECPPublicKey implements java.security.PublicKey {
return encoded.toArrayUnsafe();
}
/**
* Gets encoded bytes.
*
* @return the encoded bytes
*/
public Bytes getEncodedBytes() {
return encoded;
}

View File

@@ -26,8 +26,10 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.MutableBytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Secp signature. */
public class SECPSignature {
/** The constant BYTES_REQUIRED. */
public static final int BYTES_REQUIRED = 65;
/**
* The recovery id to reconstruct the public key used to create the signature.
@@ -44,6 +46,13 @@ public class SECPSignature {
private final Supplier<Bytes> encoded = Suppliers.memoize(this::_encodedBytes);
/**
* Instantiates a new SECPSignature.
*
* @param r the r
* @param s the s
* @param recId the rec id
*/
SECPSignature(final BigInteger r, final BigInteger s, final byte recId) {
this.r = r;
this.s = s;
@@ -88,6 +97,13 @@ public class SECPSignature {
}
}
/**
* Decode secp signature.
*
* @param bytes the bytes
* @param curveOrder the curve order
* @return the secp signature
*/
public static SECPSignature decode(final Bytes bytes, final BigInteger curveOrder) {
checkArgument(
bytes.size() == BYTES_REQUIRED, "encoded SECP256K1 signature must be 65 bytes long");
@@ -98,6 +114,11 @@ public class SECPSignature {
return SECPSignature.create(r, s, recId, curveOrder);
}
/**
* Encoded bytes.
*
* @return the bytes
*/
public Bytes encodedBytes() {
return encoded.get();
}
@@ -125,14 +146,29 @@ public class SECPSignature {
return Objects.hash(r, s, recId);
}
/**
* Gets rec id.
*
* @return the rec id
*/
public byte getRecId() {
return recId;
}
/**
* Gets r.
*
* @return the r
*/
public BigInteger getR() {
return r;
}
/**
* Gets s.
*
* @return the s
*/
public BigInteger getS() {
return s;
}

View File

@@ -16,14 +16,27 @@ package org.hyperledger.besu.crypto;
import java.security.SecureRandom;
/**
* The Secure random provider. Errorprone checks are in place to enforce only this class is used
* wherever SecureRandom instance is required.
*/
public class SecureRandomProvider {
private static final SecureRandom publicSecureRandom = secureRandom();
// Returns a shared instance of secure random intended to be used where the value is used publicly
/**
* Returns a shared instance of secure random intended to be used where the value is used publicly
*
* @return the secure random
*/
public static SecureRandom publicSecureRandom() {
return publicSecureRandom;
}
/**
* Create secure random.
*
* @return the secure random
*/
public static SecureRandom createSecureRandom() {
return secureRandom();
}

View File

@@ -22,62 +22,205 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.bouncycastle.math.ec.ECPoint;
/** The interface Signature algorithm. */
public interface SignatureAlgorithm {
/** The constant ALGORITHM. */
// needs to be known at compile time otherwise triggers InsecureCryptoUsage error
String ALGORITHM = "ECDSA";
/** Disable native. */
void disableNative();
/**
* Is native enabled.
*
* @return the boolean
*/
boolean isNative();
/**
* Sign secp signature.
*
* @param dataHash the data hash
* @param keyPair the key pair
* @return the secp signature
*/
SECPSignature sign(final Bytes32 dataHash, final KeyPair keyPair);
/**
* Verify given data.
*
* @param data the data
* @param signature the signature
* @param pub the pub
* @return the boolean
*/
boolean verify(final Bytes data, final SECPSignature signature, final SECPPublicKey pub);
/**
* Verify given data.
*
* @param data the data
* @param signature the signature
* @param pub the pub
* @param preprocessor the preprocessor
* @return the boolean
*/
boolean verify(
final Bytes data,
final SECPSignature signature,
final SECPPublicKey pub,
final UnaryOperator<Bytes> preprocessor);
/**
* Normalise secp signature.
*
* @param nativeR the native r
* @param nativeS the native s
* @param publicKey the public key
* @param dataHash the data hash
* @return the secp signature
*/
SECPSignature normaliseSignature(
final BigInteger nativeR,
final BigInteger nativeS,
final SECPPublicKey publicKey,
final Bytes32 dataHash);
/**
* Calculate ecdh key agreement as bytes32.
*
* @param privKey the private key
* @param theirPubKey the public key
* @return the bytes 32
*/
Bytes32 calculateECDHKeyAgreement(final SECPPrivateKey privKey, final SECPPublicKey theirPubKey);
/**
* Gets half curve order.
*
* @return the half curve order
*/
BigInteger getHalfCurveOrder();
/**
* Gets provider.
*
* @return the provider
*/
String getProvider();
/**
* Gets curve name.
*
* @return the curve name
*/
String getCurveName();
/**
* Create secp private key.
*
* @param key the key
* @return the secp private key
*/
SECPPrivateKey createPrivateKey(final BigInteger key);
/**
* Create secp private key.
*
* @param key the key
* @return the secp private key
*/
SECPPrivateKey createPrivateKey(final Bytes32 key);
/**
* Create secp public key.
*
* @param privateKey the private key
* @return the secp public key
*/
SECPPublicKey createPublicKey(final SECPPrivateKey privateKey);
/**
* Create secp public key.
*
* @param key the key
* @return the secp public key
*/
SECPPublicKey createPublicKey(final BigInteger key);
/**
* Createsecp public key.
*
* @param encoded the encoded
* @return the secp public key
*/
SECPPublicKey createPublicKey(final Bytes encoded);
/**
* Recover public key from signature optional.
*
* @param dataHash the data hash
* @param signature the signature
* @return the optional public key
*/
Optional<SECPPublicKey> recoverPublicKeyFromSignature(
final Bytes32 dataHash, final SECPSignature signature);
/**
* Public key as EcPoint.
*
* @param publicKey the public key
* @return the ec point
*/
ECPoint publicKeyAsEcPoint(final SECPPublicKey publicKey);
/**
* Is valid public key.
*
* @param publicKey the public key
* @return the boolean
*/
boolean isValidPublicKey(SECPPublicKey publicKey);
/**
* Create key pair.
*
* @param privateKey the private key
* @return the key pair
*/
KeyPair createKeyPair(final SECPPrivateKey privateKey);
/**
* Generate key pair.
*
* @return the key pair
*/
KeyPair generateKeyPair();
/**
* Create secp signature.
*
* @param r the r
* @param s the s
* @param recId the rec id
* @return the secp signature
*/
SECPSignature createSignature(final BigInteger r, final BigInteger s, final byte recId);
/**
* Decode secp signature.
*
* @param bytes the bytes
* @return the secp signature
*/
SECPSignature decodeSignature(final Bytes bytes);
/**
* Compress public key bytes.
*
* @param uncompressedKey the uncompressed key
* @return the bytes
*/
Bytes compressPublicKey(final SECPPublicKey uncompressedKey);
}

View File

@@ -18,6 +18,7 @@ import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** The Signature algorithm factory. */
public class SignatureAlgorithmFactory {
private static final Logger LOG = LoggerFactory.getLogger(SignatureAlgorithmFactory.class);
@@ -26,10 +27,17 @@ public class SignatureAlgorithmFactory {
private SignatureAlgorithmFactory() {}
/** Sets default instance. */
public static void setDefaultInstance() {
instance = SignatureAlgorithmType.createDefault().getInstance();
}
/**
* Sets instance.
*
* @param signatureAlgorithmType the signature algorithm type
* @throws IllegalStateException the illegal state exception
*/
public static void setInstance(final SignatureAlgorithmType signatureAlgorithmType)
throws IllegalStateException {
if (instance != null) {
@@ -52,7 +60,7 @@ public class SignatureAlgorithmFactory {
* getInstance will always return a valid SignatureAlgorithm and never null. This is necessary in
* the unit tests be able to use the factory without having to call setInstance first.
*
* @return SignatureAlgorithm
* @return SignatureAlgorithm instance
*/
public static SignatureAlgorithm getInstance() {
return instance != null
@@ -60,10 +68,16 @@ public class SignatureAlgorithmFactory {
: SignatureAlgorithmType.DEFAULT_SIGNATURE_ALGORITHM_TYPE.get();
}
/**
* Is instance set boolean.
*
* @return the boolean
*/
public static boolean isInstanceSet() {
return instance != null;
}
/** Reset instance. */
@VisibleForTesting
public static void resetInstance() {
instance = null;

View File

@@ -20,12 +20,16 @@ import java.util.function.Supplier;
import com.google.common.collect.ImmutableMap;
/** The Signature algorithm type. */
public class SignatureAlgorithmType {
/** The constant DEFAULT_EC_CURVE_NAME. */
public static final String DEFAULT_EC_CURVE_NAME = "secp256k1";
private static final ImmutableMap<String, Supplier<SignatureAlgorithm>> SUPPORTED_ALGORITHMS =
ImmutableMap.of(DEFAULT_EC_CURVE_NAME, SECP256K1::new, "secp256r1", SECP256R1::new);
/** The constant DEFAULT_SIGNATURE_ALGORITHM_TYPE. */
public static final Supplier<SignatureAlgorithm> DEFAULT_SIGNATURE_ALGORITHM_TYPE =
SUPPORTED_ALGORITHMS.get(DEFAULT_EC_CURVE_NAME);
@@ -35,6 +39,13 @@ public class SignatureAlgorithmType {
this.instantiator = instantiator;
}
/**
* Create signature algorithm type.
*
* @param ecCurve the ec curve
* @return the signature algorithm type
* @throws IllegalArgumentException the illegal argument exception
*/
public static SignatureAlgorithmType create(final String ecCurve)
throws IllegalArgumentException {
if (!isValidType(ecCurve)) {
@@ -44,18 +55,40 @@ public class SignatureAlgorithmType {
return new SignatureAlgorithmType(SUPPORTED_ALGORITHMS.get(ecCurve));
}
/**
* Create default signature algorithm type.
*
* @return the signature algorithm type
*/
public static SignatureAlgorithmType createDefault() {
return new SignatureAlgorithmType(DEFAULT_SIGNATURE_ALGORITHM_TYPE);
}
/**
* Gets instance.
*
* @return the instance
*/
public SignatureAlgorithm getInstance() {
return instantiator.get();
}
/**
* Is valid type boolean.
*
* @param ecCurve the ec curve
* @return the boolean
*/
public static boolean isValidType(final String ecCurve) {
return SUPPORTED_ALGORITHMS.containsKey(ecCurve);
}
/**
* Is default signature algorithm.
*
* @param signatureAlgorithm the signature algorithm
* @return the boolean
*/
public static boolean isDefault(final SignatureAlgorithm signatureAlgorithm) {
return signatureAlgorithm.getCurveName().equals(DEFAULT_EC_CURVE_NAME);
}

View File

@@ -22,26 +22,48 @@ import com.google.common.base.MoreObjects;
/**
* Adapted from the pc_ecc (Apache 2 License) implementation:
* https://github.com/ethereum/py_ecc/blob/master/py_ecc/bn128/bn128_field_elements.py
*
* @param <U> the type parameter
*/
@SuppressWarnings("rawtypes")
public abstract class AbstractFieldPoint<U extends AbstractFieldPoint> implements FieldPoint<U> {
private static final BigInteger TWO = BigInteger.valueOf(2);
/** The X. */
@SuppressWarnings("rawtypes")
protected final FieldElement x;
/** The Y. */
@SuppressWarnings("rawtypes")
protected final FieldElement y;
/**
* Instantiates a new Abstract field point.
*
* @param x the x
* @param y the y
*/
@SuppressWarnings("rawtypes")
AbstractFieldPoint(final FieldElement x, final FieldElement y) {
this.x = x;
this.y = y;
}
/**
* Infinity u.
*
* @return the u
*/
protected abstract U infinity();
/**
* New instance of generic type U.
*
* @param x the x
* @param y the y
* @return the U
*/
@SuppressWarnings("rawtypes")
protected abstract U newInstance(final FieldElement x, final FieldElement y);

View File

@@ -23,15 +23,27 @@ import com.google.common.base.MoreObjects;
/**
* Adapted from the pc_ecc (Apache 2 License) implementation:
* https://github.com/ethereum/py_ecc/blob/master/py_ecc/bn128/bn128_field_elements.py
*
* @param <T> the type parameter
*/
@SuppressWarnings("rawtypes")
public abstract class AbstractFqp<T extends AbstractFqp> implements FieldElement<T> {
private static final BigInteger BIGINT_2 = BigInteger.valueOf(2);
/** The Degree. */
protected final int degree;
/** The Modulus coefficients. */
protected final Fq[] modulusCoefficients;
/** The Coefficients. */
protected final Fq[] coefficients;
/**
* Instantiates a new Abstract fqp.
*
* @param degree the degree
* @param modulusCoefficients the modulus coefficients
* @param coefficients the coefficients
*/
protected AbstractFqp(final int degree, final Fq[] modulusCoefficients, final Fq[] coefficients) {
if (degree != coefficients.length) {
throw new IllegalArgumentException(
@@ -47,8 +59,19 @@ public abstract class AbstractFqp<T extends AbstractFqp> implements FieldElement
this.coefficients = coefficients;
}
/**
* New instance t.
*
* @param coefficients the coefficients
* @return the t
*/
protected abstract T newInstance(final Fq[] coefficients);
/**
* Get coefficients fq [ ].
*
* @return the fq [ ]
*/
public Fq[] getCoefficients() {
return coefficients;
}
@@ -173,6 +196,11 @@ public abstract class AbstractFqp<T extends AbstractFqp> implements FieldElement
}
}
/**
* Inverse fq [ ].
*
* @return the fq [ ]
*/
protected Fq[] inverse() {
Fq[] lm = lm();
Fq[] hm = hm();

View File

@@ -33,6 +33,13 @@ public class AltBn128Fq12Pairer {
new BigInteger(
"21888242871839275222246405745257275088548364400416034343698204186575808495617");
/**
* Pair fq 12.
*
* @param p the p
* @param q the q
* @return the fq 12
*/
public static Fq12 pair(final AltBn128Point p, final AltBn128Fq2Point q) {
return millerLoop(cast(p), twist(q));
}
@@ -78,6 +85,12 @@ public class AltBn128Fq12Pairer {
return f;
}
/**
* Finalize fq 12.
*
* @param f the f
* @return the fq 12
*/
public static Fq12 finalize(final Fq12 f) {
return f.power(FieldElement.FIELD_MODULUS.pow(12).subtract(BigInteger.ONE).divide(CURVE_ORDER));
}

View File

@@ -22,10 +22,21 @@ import java.util.Arrays;
*/
public class AltBn128Fq12Point extends AbstractFieldPoint<AltBn128Fq12Point> {
/**
* G 12 alt bn 128 fq 12 point.
*
* @return the alt bn 128 fq 12 point
*/
public static AltBn128Fq12Point g12() {
return twist(AltBn128Fq2Point.g2());
}
/**
* Twist alt bn 128 fq 12 point.
*
* @param p the p
* @return the alt bn 128 fq 12 point
*/
public static AltBn128Fq12Point twist(final AltBn128Fq2Point p) {
final Fq2 x = p.getX();
final Fq2 y = p.getY();
@@ -52,14 +63,30 @@ public class AltBn128Fq12Point extends AbstractFieldPoint<AltBn128Fq12Point> {
return Fq12.create(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**
* Instantiates a new Alt bn 128 fq 12 point.
*
* @param x the x
* @param y the y
*/
public AltBn128Fq12Point(final Fq12 x, final Fq12 y) {
super(x, y);
}
/**
* Gets x.
*
* @return the x
*/
public Fq12 getX() {
return (Fq12) x;
}
/**
* Gets y.
*
* @return the y
*/
public Fq12 getY() {
return (Fq12) y;
}

View File

@@ -25,6 +25,11 @@ public class AltBn128Fq2Point extends AbstractFieldPoint<AltBn128Fq2Point> {
new BigInteger(
"21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);
/**
* G 2 alt bn 128 fq 2 point.
*
* @return the alt bn 128 fq 2 point
*/
public static AltBn128Fq2Point g2() {
final Fq2 x =
Fq2.create(
@@ -41,14 +46,30 @@ public class AltBn128Fq2Point extends AbstractFieldPoint<AltBn128Fq2Point> {
return new AltBn128Fq2Point(x, y);
}
/**
* Instantiates a new Alt bn 128 fq 2 point.
*
* @param x the x
* @param y the y
*/
public AltBn128Fq2Point(final Fq2 x, final Fq2 y) {
super(x, y);
}
/**
* Gets x.
*
* @return the x
*/
public Fq2 getX() {
return (Fq2) x;
}
/**
* Gets y.
*
* @return the y
*/
public Fq2 getY() {
return (Fq2) y;
}
@@ -58,6 +79,11 @@ public class AltBn128Fq2Point extends AbstractFieldPoint<AltBn128Fq2Point> {
return new AltBn128Fq2Point(Fq2.zero(), Fq2.zero());
}
/**
* Is on curve boolean.
*
* @return the boolean
*/
public boolean isOnCurve() {
if (!x.isValid() || !y.isValid()) {
return false;
@@ -73,6 +99,11 @@ public class AltBn128Fq2Point extends AbstractFieldPoint<AltBn128Fq2Point> {
return y.power(2).subtract(x.power(3)).equals(Fq2.b2());
}
/**
* Is in group boolean.
*
* @return the boolean
*/
public boolean isInGroup() {
return multiply(CURVE_ORDER).isInfinity();
}

View File

@@ -20,26 +20,54 @@ package org.hyperledger.besu.crypto.altbn128;
*/
public class AltBn128Point extends AbstractFieldPoint<AltBn128Point> {
/** The B. */
static final Fq B = Fq.create(3);
/**
* G 1 alt bn 128 point.
*
* @return the alt bn 128 point
*/
public static final AltBn128Point g1() {
return new AltBn128Point(Fq.create(1), Fq.create(2));
}
/** The Infinity. */
static final AltBn128Point INFINITY = new AltBn128Point(Fq.zero(), Fq.zero());
/**
* Instantiates a new Alt bn 128 point.
*
* @param x the x
* @param y the y
*/
public AltBn128Point(final Fq x, final Fq y) {
super(x, y);
}
/**
* Gets x.
*
* @return the x
*/
public Fq getX() {
return (Fq) x;
}
/**
* Gets y.
*
* @return the y
*/
public Fq getY() {
return (Fq) y;
}
/**
* Is on curve boolean.
*
* @return the boolean
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public boolean isOnCurve() {
if (!x.isValid() || !y.isValid()) {

View File

@@ -19,31 +19,91 @@ import java.math.BigInteger;
/**
* Adapted from the pc_ecc (Apache 2 License) implementation:
* https://github.com/ethereum/py_ecc/blob/master/py_ecc/bn128/bn128_field_elements.py
*
* @param <T> the type parameter
*/
@SuppressWarnings("rawtypes")
public interface FieldElement<T extends FieldElement> {
/** The constant FIELD_MODULUS. */
BigInteger FIELD_MODULUS =
new BigInteger(
"21888242871839275222246405745257275088696311157297823662689037894645226208583");
/**
* Is valid boolean.
*
* @return the boolean
*/
boolean isValid();
/**
* Is zero boolean.
*
* @return the boolean
*/
boolean isZero();
/**
* Add t.
*
* @param other the other
* @return the t
*/
T add(T other);
/**
* Subtract t.
*
* @param other the other
* @return the t
*/
T subtract(T other);
/**
* Multiply t.
*
* @param val the val
* @return the t
*/
T multiply(int val);
/**
* Multiply t.
*
* @param other the other
* @return the t
*/
T multiply(T other);
/**
* Negate t.
*
* @return the t
*/
T negate();
/**
* Divide t.
*
* @param other the other
* @return the t
*/
T divide(T other);
/**
* Power t.
*
* @param n the n
* @return the t
*/
T power(int n);
/**
* Power t.
*
* @param n the n
* @return the t
*/
T power(BigInteger n);
}

View File

@@ -19,19 +19,54 @@ import java.math.BigInteger;
/**
* Adapted from the pc_ecc (Apache 2 License) implementation:
* https://github.com/ethereum/py_ecc/blob/master/py_ecc/bn128/bn128_field_elements.py
*
* @param <T> the type parameter
*/
@SuppressWarnings("rawtypes")
public interface FieldPoint<T extends FieldPoint> {
/**
* Is infinity boolean.
*
* @return the boolean
*/
boolean isInfinity();
/**
* Add t.
*
* @param other the other
* @return the t
*/
T add(T other);
/**
* Multiply t.
*
* @param other the other
* @return the t
*/
T multiply(T other);
/**
* Multiply t.
*
* @param n the n
* @return the t
*/
T multiply(BigInteger n);
/**
* Doub t.
*
* @return the t
*/
T doub();
/**
* Negate t.
*
* @return the t
*/
T negate();
}

View File

@@ -28,20 +28,42 @@ public class Fq implements FieldElement<Fq> {
private static final BigInteger TWO = BigInteger.valueOf(2);
/**
* fq that represents 0.
*
* @return the fq
*/
public static Fq zero() {
return create(0);
}
/**
* fq that represents 1.
*
* @return the fq
*/
public static Fq one() {
return create(1);
}
private final BigInteger n;
/**
* Create fq.
*
* @param n the n
* @return the fq
*/
public static Fq create(final BigInteger n) {
return new Fq(n);
}
/**
* Create fq.
*
* @param n the n
* @return the fq
*/
static Fq create(final long n) {
return create(BigInteger.valueOf(n));
}
@@ -50,6 +72,11 @@ public class Fq implements FieldElement<Fq> {
this.n = n;
}
/**
* To bytes.
*
* @return the bytes
*/
public Bytes toBytes() {
return Bytes.wrap(n.toByteArray()).trimLeadingZeros();
}

View File

@@ -20,12 +20,23 @@ package org.hyperledger.besu.crypto.altbn128;
*/
public class Fq12 extends AbstractFqp<Fq12> {
/** The constant DEGREE. */
public static final int DEGREE = 12;
/**
* Zero fq 12.
*
* @return the fq 12
*/
static final Fq12 zero() {
return create(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**
* One fq 12.
*
* @return the fq 12
*/
public static final Fq12 one() {
return create(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
@@ -46,6 +57,23 @@ public class Fq12 extends AbstractFqp<Fq12> {
Fq.create(0)
};
/**
* Create fq 12.
*
* @param c0 the c 0
* @param c1 the c 1
* @param c2 the c 2
* @param c3 the c 3
* @param c4 the c 4
* @param c5 the c 5
* @param c6 the c 6
* @param c7 the c 7
* @param c8 the c 8
* @param c9 the c 9
* @param c10 the c 10
* @param c11 the c 11
* @return the fq 12
*/
public static Fq12 create(
final long c0,
final long c1,
@@ -74,6 +102,11 @@ public class Fq12 extends AbstractFqp<Fq12> {
Fq.create(c11));
}
/**
* Instantiates a new Fq 12.
*
* @param coefficients the coefficients
*/
protected Fq12(final Fq... coefficients) {
super(DEGREE, MODULUS_COEFFICIENTS, coefficients);
}

View File

@@ -24,20 +24,44 @@ public class Fq2 extends AbstractFqp<Fq2> {
private static final int DEGREE = 2;
/**
* Zero fq2.
*
* @return the fq2
*/
static final Fq2 zero() {
return new Fq2(new Fq[] {Fq.zero(), Fq.zero()});
}
/**
* One fq2.
*
* @return the fq2
*/
static final Fq2 one() {
return new Fq2(new Fq[] {Fq.one(), Fq.zero()});
}
private static final Fq[] MODULUS_COEFFICIENTS = new Fq[] {Fq.create(1), Fq.create(0)};
/**
* Create fq2.
*
* @param c0 the c0
* @param c1 the c1
* @return the fq2
*/
public static final Fq2 create(final long c0, final long c1) {
return create(BigInteger.valueOf(c0), BigInteger.valueOf((c1)));
}
/**
* Create fq2.
*
* @param c0 the c0
* @param c1 the c1
* @return the fq2
*/
public static final Fq2 create(final BigInteger c0, final BigInteger c1) {
return new Fq2(Fq.create(c0), Fq.create(c1));
}
@@ -46,6 +70,11 @@ public class Fq2 extends AbstractFqp<Fq2> {
super(DEGREE, MODULUS_COEFFICIENTS, coefficients);
}
/**
* b2 Fq2.
*
* @return the Fq2
*/
public static Fq2 b2() {
final Fq2 numerator = create(3, 0);
final Fq2 denominator = create(9, 1);