Skip to content

Commit

Permalink
Removed TYPES in hashing and signature algorithms and used Suppliers …
Browse files Browse the repository at this point in the history
…at their place
  • Loading branch information
spoto committed Sep 27, 2023
1 parent a2e8dd2 commit 1903276
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.hotmoka.crypto.api;

import java.security.NoSuchAlgorithmException;
import java.util.function.Function;

/**
* An algorithm that hashes values into bytes.
*
Expand Down Expand Up @@ -57,6 +60,13 @@ public interface HashingAlgorithm<T> extends Cloneable {
*/
int length();

/**
* Yields the supplier of this hashing algorithm.
*
* @return the supplier
*/
Supplier<T> getSupplier();

/**
* Yields the name of the algorithm.
*
Expand All @@ -72,4 +82,22 @@ public interface HashingAlgorithm<T> extends Cloneable {
* @return the clone of this algorithm
*/
HashingAlgorithm<T> clone();

/**
* A supplier of a hashing algorithm.
*
* @param <T> the type of the objects that will get hashed
*/
interface Supplier<T> {

/**
* Supplies a hashing algorithm with this supplier.
*
* @param toBytes a function applied to the objects to hash, to transform them into bytes that
* will then be hashed
* @return the hashing algorithm
* @throws NoSuchAlgorithmException if the hashing algorithm is not available
*/
HashingAlgorithm<T> get(Function<? super T, byte[]> toBytes) throws NoSuchAlgorithmException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.function.Function;

/**
* An algorithm that signs values and verifies such signatures back.
Expand Down Expand Up @@ -100,13 +102,25 @@ public interface SignatureAlgorithm<T> {
*/
byte[] encodingOf(PrivateKey privateKey) throws InvalidKeyException;

/**
* Yields the type of this signature algotihm.
*
* @return the type
*/
/**
* Yields the name of the algorithm.
*
* @return the name of the algorithm
*/
String getName();

/**
* Yields the supplier of this signature algorithm.
*
* @return the supplier
*/
Supplier<T> getSupplier();

/**
* Creates a key pair from the given entropy and password.
*
Expand All @@ -125,4 +139,22 @@ public interface SignatureAlgorithm<T> {
* @return the key pair derived from entropy and password
*/
KeyPair getKeyPair(byte[] entropy, String password);

/**
* A supplier of a signature algorithm.
*
* @param <T> the type of the objects that will get signed
*/
interface Supplier<T> {

/**
* Supplies a signature algorithm with this supplier.
*
* @param toBytes a function applied to the objects to sign, to transform them into bytes that
* will then be signed
* @return the signature algorithm
* @throws NoSuchAlgorithmException if the signature algorithm is not available
*/
SignatureAlgorithm<T> get(Function<? super T, byte[]> toBytes) throws NoSuchAlgorithmException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,4 @@ public static <T> HashingAlgorithm<T> of(String name, Function<? super T, byte[]
throw new NoSuchAlgorithmException("Unknown hashing algorithm named " + name, e);
}
}

/**
* Yields the hashing algorithm for the given type of values.
*
* @param <T> the type of the values that get hashed
* @param type the type of the algorithm
* @param supplier how values get transformed into bytes, before being hashed
* @return the algorithm
* @throws NoSuchAlgorithmException if the installation does not include the given algorithm
*/
public static <T> HashingAlgorithm<T> of(TYPES type, Function<? super T, byte[]> supplier) throws NoSuchAlgorithmException {
return of(type.name(), supplier);
}

/**
* The alternatives of hashing algorithms currently implemented.
*/
public enum TYPES {

/**
* The Sha256 hashing algorithm.
*/
SHA256,

/**
* The Shabal256 hashing algorithm.
*/
SHABAL256
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,54 +135,4 @@ public static <T> SignatureAlgorithm<T> of(String name, Function<? super T, byte
throw new NoSuchAlgorithmException("Unknown signature algorithm named " + name, e);
}
}

/**
* Yields the signature algorithm for the given type of values.
*
* @param <T> the type of the values that get signed
* @param type the type of the algorithm
* @param supplier how values get transformed into bytes, before being hashed and then signed
* @return the algorithm
* @throws NoSuchAlgorithmException if the installation does not include the given algorithm
*/
public static <T> SignatureAlgorithm<T> of(TYPES type, Function<? super T, byte[]> supplier) throws NoSuchAlgorithmException {
return of(type.name(), supplier);
}

/**
* The alternatives of signature algorithms currently implemented.
*/
public enum TYPES {

/**
* The ed25519 signature algorithm.
*/
ED25519,

/**
* The ed25519 signature algorithm, in a deterministic fashion: it generates
* keys in a deterministic way (therefore, only use for testing).
*/
ED25519DET,

/**
* The empty signature algorithm, that signs everything with an empty array of bytes.
*/
EMPTY,

/**
* The qTESLA-p-I signature algorithm.
*/
QTESLA1,

/**
* The qTESLA-p-III signature algorithm.
*/
QTESLA3,

/**
* A signature algorithm that uses the SHA256 hashing algorithm and then the DSA algorithm.
*/
SHA256DSA
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,9 @@ private static void ensureProvider() {
public String getName() {
return "ed25519";
}

@Override
public Supplier<T> getSupplier() {
return ED25519::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,9 @@ private static void ensureProvider() {
public String getName() {
return "ed25519det";
}

@Override
public Supplier<T> getSupplier() {
return ED25519DET::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public PrivateKey privateKeyFromEncoding(byte[] encoded) throws InvalidKeySpecEx
public String getName() {
return "empty";
}

@Override
public Supplier<T> getSupplier() {
return _toBytes -> new EMPTY<T>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ private static void ensureProvider() {
public String getName() {
return "qtesla1";
}

@Override
public Supplier<T> getSupplier() {
return QTESLA1::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ private static void ensureProvider() {
public String getName() {
return "qtesla3";
}

@Override
public Supplier<T> getSupplier() {
return QTESLA3::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ public SHA256<T> clone() {
throw new IllegalStateException("cannot clone SHA256 since the provider is not available");
}
}

@Override
public Supplier<T> getSupplier() {
return SHA256::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ public PrivateKey privateKeyFromEncoding(byte[] encoded) throws InvalidKeySpecEx
public String getName() {
return "sha256dsa";
}

@Override
public Supplier<T> getSupplier() {
return SHA256DSA::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public String getName() {
return "shabal256";
}

@Override
public Supplier<T> getSupplier() {
return SHABAL256::new;
}

@Override
public SHABAL256<T> clone() {
return new SHABAL256<T>(supplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import io.hotmoka.beans.values.BigIntegerValue;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.SignatureAlgorithms;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.helpers.GasHelpers;
Expand Down Expand Up @@ -98,9 +97,9 @@ public StorageReference paidByFaucet(SignatureAlgorithm<SignedTransactionRequest

String methodName;
ClassType eoaType;
String signature = signatureAlgorithm.getName();
BigInteger gas = gasForCreatingAccountWithSignature(signature);
BigInteger gas = gasForCreatingAccountWithSignature(signatureAlgorithm);

String signature = signatureAlgorithm.getName();
switch (signature) {
case "ed25519":
case "sha256dsa":
Expand Down Expand Up @@ -155,7 +154,7 @@ public StorageReference paidBy(StorageReference payer, KeyPair keysOfPayer,

SignatureAlgorithm<SignedTransactionRequest> signatureForPayer = SignatureHelpers.of(node).signatureAlgorithmFor(payer);

BigInteger gas1 = gasForCreatingAccountWithSignature(signature);
BigInteger gas1 = gasForCreatingAccountWithSignature(signatureAlgorithm);
BigInteger gas2 = gasForTransactionWhosePayerHasSignature(signatureForPayer.getName());
BigInteger totalGas = balanceRed.signum() > 0 ? gas1.add(gas2).add(gas2) : gas1.add(gas2);
if (addToLedger)
Expand Down Expand Up @@ -213,7 +212,7 @@ public StorageReference tendermintValidatorPaidByFaucet(PublicKey publicKey,
StorageReference gamete = (StorageReference) node.runInstanceMethodCallTransaction(new InstanceMethodCallTransactionRequest
(manifest, _100_000, takamakaCode, CodeSignature.GET_GAMETE, manifest));

BigInteger gas = gasForCreatingAccountWithSignature(SignatureAlgorithms.TYPES.ED25519.name());
BigInteger gas = gasForCreatingAccountWithSignature(SignatureAlgorithmForTransactionRequests.ed25519());

// we use an empty signature algorithm and an arbitrary key, since the faucet is unsigned
SignatureAlgorithm<SignedTransactionRequest> signatureForFaucet = SignatureAlgorithmForTransactionRequests.empty();
Expand All @@ -238,7 +237,7 @@ public StorageReference tendermintValidatorPaidBy(StorageReference payer, KeyPai

SignatureAlgorithm<SignedTransactionRequest> signatureForPayer = SignatureHelpers.of(node).signatureAlgorithmFor(payer);

BigInteger gas1 = gasForCreatingAccountWithSignature(SignatureAlgorithms.TYPES.ED25519.name());
BigInteger gas1 = gasForCreatingAccountWithSignature(SignatureAlgorithmForTransactionRequests.ed25519());
BigInteger gas2 = gasForTransactionWhosePayerHasSignature(signatureForPayer.getName());
BigInteger totalGas = balanceRed.signum() > 0 ? gas1.add(gas2).add(gas2) : gas1.add(gas2);

Expand Down Expand Up @@ -267,8 +266,8 @@ public StorageReference tendermintValidatorPaidBy(StorageReference payer, KeyPai
return validator;
}

private static BigInteger gasForCreatingAccountWithSignature(String signature) {
switch (signature) {
private static BigInteger gasForCreatingAccountWithSignature(SignatureAlgorithm<?> signature) {
switch (signature.getName()) {
case "ed25519":
return _100_000;
case "sha256dsa":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import io.hotmoka.beans.requests.SignedTransactionRequest;
import io.hotmoka.crypto.SignatureAlgorithms;
import io.hotmoka.crypto.SignatureAlgorithms.TYPES;
import io.hotmoka.crypto.api.SignatureAlgorithm;

/**
Expand Down Expand Up @@ -113,18 +112,7 @@ public static SignatureAlgorithm<SignedTransactionRequest> of(String name) throw
return (SignatureAlgorithm<SignedTransactionRequest>) method.invoke(null);
}
catch (NoSuchMethodException | SecurityException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
throw new NoSuchAlgorithmException("unknown signature algorithm named " + name, e);
throw new NoSuchAlgorithmException("Unknown signature algorithm named " + name, e);
}
}

/**
* Yields the signature algorithm for transaction requests with the given type.
*
* @param type the type of the algorithm
* @return the algorithm
* @throws NoSuchAlgorithmException if the installation does not include the given algorithm
*/
public static SignatureAlgorithm<SignedTransactionRequest> of(TYPES type) throws NoSuchAlgorithmException {
return of(type.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public int length() {
public String getName() {
return "custom";
}

@Override
public Supplier<TransactionReference> getSupplier() {
return __ -> new HashingForTransactionReference();
}
}
Loading

0 comments on commit 1903276

Please sign in to comment.