Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arch): support x86 on jdk17 #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion chainbase/src/main/java/org/tron/common/math/Maths.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,88 @@
package org.tron.common.math;

import com.google.common.primitives.Bytes;
import java.nio.ByteBuffer;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.context.GlobalContext;
import org.tron.core.store.MathStore;
import org.tron.core.store.StrictMathStore;

/**
* This class is deprecated and should not be used in new code,
* for cross-platform consistency, please use {@link StrictMathWrapper} instead,
* especially for floating-point calculations.
*/
@Deprecated
@Component
@Slf4j(topic = "math")
public class Maths {

private static Optional<MathStore> mathStore = Optional.empty();
private static Optional<StrictMathStore> strictMathStore = Optional.empty();

@Autowired
public Maths(@Autowired MathStore mathStore, @Autowired StrictMathStore strictMathStore) {
Maths.mathStore = Optional.ofNullable(mathStore);
Maths.strictMathStore = Optional.ofNullable(strictMathStore);
}

private enum Op {

POW((byte) 0x01);

private final byte code;

Op(byte code) {
this.code = code;
}
}

/**
* Returns the value of the first argument raised to the power of the second argument.
* @param a the base.
* @param b the exponent.
* @return the value {@code a}<sup>{@code b}</sup>.
*/
public static double pow(double a, double b, boolean useStrictMath) {
return useStrictMath ? StrictMathWrapper.pow(a, b) : MathWrapper.pow(a, b);
double result = MathWrapper.pow(a, b);
double strictResult = StrictMathWrapper.pow(a, b);
if (useStrictMath) {
return strictResult;
}
final boolean isNoStrict = Double.compare(result, strictResult) != 0;
Optional<Long> header = GlobalContext.getHeader();
header.ifPresent(h -> {
byte[] key = Bytes.concat(longToBytes(h), new byte[]{Op.POW.code},
doubleToBytes(a), doubleToBytes(b));
if (isNoStrict) {
logger.info("{}\t{}\t{}\t{}\t{}\t{}", h, Op.POW.code, doubleToHex(a), doubleToHex(b),
doubleToHex(result), doubleToHex(strictResult));
}
mathStore.ifPresent(s -> s.put(key, doubleToBytes(result)));
strictMathStore.ifPresent(s -> s.put(key, doubleToBytes(strictResult)));
});
return result;
}

static String doubleToHex(double input) {
// Convert the starting value to the equivalent value in a long
long doubleAsLong = Double.doubleToRawLongBits(input);
// and then convert the long to a hex string
return Long.toHexString(doubleAsLong);
}

private static byte[] doubleToBytes(double value) {
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
buffer.putDouble(value);
return buffer.array();
}

private static byte[] longToBytes(long value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(value);
return buffer.array();
}
}
111 changes: 94 additions & 17 deletions chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.tron.common.zksnark;

import java.nio.ByteBuffer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.context.GlobalContext;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteUtil;
import org.tron.common.zksnark.LibrustzcashParam.BindingSigParams;
import org.tron.common.zksnark.LibrustzcashParam.CheckOutputNewParams;
Expand All @@ -26,7 +31,7 @@
import org.tron.common.zksnark.LibrustzcashParam.Zip32XskMasterParams;
import org.tron.core.exception.ZksnarkException;

@Slf4j
@Slf4j(topic = "zcash")
public class JLibrustzcash {

private static Librustzcash INSTANCE;
Expand Down Expand Up @@ -66,27 +71,36 @@ public static void librustzcashCrhIvk(CrhIvkParams params) {
return;
}
INSTANCE.librustzcashCrhIvk(params.getAk(), params.getNk(), params.getIvk());
log(4, params.getAk(), params.getNk(), params.getIvk());
}

public static boolean librustzcashKaAgree(KaAgreeParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingKaAgree(params.getP(), params.getSk(), params.getResult());
boolean res = INSTANCE.librustzcashSaplingKaAgree(params.getP(),
params.getSk(), params.getResult());
log(5, params.getP(), params.getSk(), params.getResult(), b2B(res));
return res;
}

public static boolean librustzcashComputeCm(ComputeCmParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingComputeCm(params.getD(), params.getPkD(),
boolean res = INSTANCE.librustzcashSaplingComputeCm(params.getD(), params.getPkD(),
params.getValue(), params.getR(), params.getCm());
log(8, params.getD(), params.getPkD(), l2B(params.getValue()), params.getR(), params.getCm(),
b2B(res));
return res;
}

public static boolean librustzcashComputeNf(ComputeNfParams params) {
if (isOpenZen()) {
INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(),
params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult());
log(1, params.getD(), params.getPkD(), l2B(params.getValue()), params.getR(),
params.getAk(), params.getNk(), l2B(params.getPosition()), params.getResult());
}
return true;
}
Expand All @@ -102,6 +116,7 @@ public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException {
LibrustzcashParam.valid32Params(ask);
byte[] ak = new byte[32];
INSTANCE.librustzcashAskToAk(ask, ak);
log(0, ask, ak);
return ak;
}

Expand All @@ -116,6 +131,7 @@ public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException {
LibrustzcashParam.valid32Params(nsk);
byte[] nk = new byte[32];
INSTANCE.librustzcashNskToNk(nsk, nk);
log(2, nsk, nk);
return nk;
}

Expand All @@ -137,8 +153,10 @@ public static boolean librustzcashSaplingKaDerivepublic(KaDerivepublicParams par
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingKaDerivepublic(params.getDiversifier(), params.getEsk(),
params.getResult());
boolean res = INSTANCE.librustzcashSaplingKaDerivepublic(params.getDiversifier(),
params.getEsk(), params.getResult());
log(3, params.getDiversifier(), params.getEsk(), params.getResult(), b2B(res));
return res;
}

public static long librustzcashSaplingProvingCtxInit() {
Expand All @@ -158,42 +176,55 @@ public static boolean librustzcashCheckDiversifier(byte[] d) throws ZksnarkExcep
return true;
}
LibrustzcashParam.valid11Params(d);
return INSTANCE.librustzcashCheckDiversifier(d);
boolean res = INSTANCE.librustzcashCheckDiversifier(d);
log(6, d, b2B(res));
return res;
}

public static boolean librustzcashSaplingSpendProof(SpendProofParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingSpendProof(params.getCtx(), params.getAk(),
boolean res = INSTANCE.librustzcashSaplingSpendProof(params.getCtx(), params.getAk(),
params.getNsk(), params.getD(), params.getR(), params.getAlpha(), params.getValue(),
params.getAnchor(), params.getVoucherPath(), params.getCv(), params.getRk(),
params.getZkproof());
log(9, params.getAk(), params.getNsk(), params.getD(), params.getR(),
params.getAlpha(), l2B(params.getValue()), params.getAnchor(), params.getVoucherPath(),
params.getCv(), params.getRk(), params.getZkproof(), b2B(res));
return res;
}

public static boolean librustzcashSaplingOutputProof(OutputProofParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingOutputProof(params.getCtx(), params.getEsk(),
boolean res = INSTANCE.librustzcashSaplingOutputProof(params.getCtx(), params.getEsk(),
params.getD(), params.getPkD(), params.getR(), params.getValue(), params.getCv(),
params.getZkproof());
log(10, params.getEsk(), params.getD(), params.getPkD(), params.getR(),
l2B(params.getValue()), params.getCv(), params.getZkproof(), b2B(res));
return res;
}

public static boolean librustzcashSaplingSpendSig(SpendSigParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingSpendSig(params.getAsk(), params.getAlpha(),
boolean res = INSTANCE.librustzcashSaplingSpendSig(params.getAsk(), params.getAlpha(),
params.getSigHash(), params.getResult());
log(11, params.getAsk(), params.getAlpha(), params.getSigHash(), params.getResult(), b2B(res));
return res;
}

public static boolean librustzcashSaplingBindingSig(BindingSigParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingBindingSig(params.getCtx(),
boolean res = INSTANCE.librustzcashSaplingBindingSig(params.getCtx(),
params.getValueBalance(), params.getSighash(), params.getResult());
log(12, l2B(params.getValueBalance()), params.getSighash(), params.getResult(), b2B(res));
return res;
}

/**
Expand All @@ -209,6 +240,7 @@ public static void librustzcashToScalar(byte[] value, byte[] data) throws Zksnar
LibrustzcashParam.validParamLength(value, 64);
LibrustzcashParam.valid32Params(data);
INSTANCE.librustzcashToScalar(value, data);
log(18, value, data);
}

public static void librustzcashSaplingProvingCtxFree(long ctx) {
Expand All @@ -229,52 +261,70 @@ public static boolean librustzcashSaplingCheckSpend(CheckSpendParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckSpend(params.getCtx(), params.getCv(),
boolean res = INSTANCE.librustzcashSaplingCheckSpend(params.getCtx(), params.getCv(),
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
params.getSpendAuthSig(), params.getSighashValue());
log(13, params.getCv(), params.getAnchor(), params.getNullifier(), params.getRk(),
params.getZkproof(), params.getSpendAuthSig(), params.getSighashValue(), b2B(res));
return res;
}

public static boolean librustzcashSaplingCheckOutput(CheckOutputParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckOutput(params.getCtx(), params.getCv(),
boolean res = INSTANCE.librustzcashSaplingCheckOutput(params.getCtx(), params.getCv(),
params.getCm(), params.getEphemeralKey(), params.getZkproof());
log(14, params.getCv(), params.getCm(), params.getEphemeralKey(), params.getZkproof(),
b2B(res));
return res;
}

public static boolean librustzcashSaplingFinalCheck(FinalCheckParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingFinalCheck(params.getCtx(),
boolean res = INSTANCE.librustzcashSaplingFinalCheck(params.getCtx(),
params.getValueBalance(), params.getBindingSig(), params.getSighashValue());
log(15, l2B(params.getValueBalance()), params.getBindingSig(), params.getSighashValue(),
b2B(res));
return res;
}

public static boolean librustzcashSaplingCheckSpendNew(CheckSpendNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckSpendNew(params.getCv(),
boolean res = INSTANCE.librustzcashSaplingCheckSpendNew(params.getCv(),
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
params.getSpendAuthSig(), params.getSighashValue());
log(19, params.getCv(), params.getAnchor(), params.getNullifier(), params.getRk(),
params.getZkproof(), params.getSpendAuthSig(), params.getSighashValue(), b2B(res));
return res;
}

public static boolean librustzcashSaplingCheckOutputNew(CheckOutputNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckOutputNew(params.getCv(), params.getCm(),
boolean res = INSTANCE.librustzcashSaplingCheckOutputNew(params.getCv(), params.getCm(),
params.getEphemeralKey(), params.getZkproof());
log(20, params.getCv(), params.getCm(), params.getEphemeralKey(), params.getZkproof(), b2B(res));
return res;
}

public static boolean librustzcashSaplingFinalCheckNew(FinalCheckNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE
boolean res = INSTANCE
.librustzcashSaplingFinalCheckNew(params.getValueBalance(), params.getBindingSig(),
params.getSighashValue(), params.getSpendCv(), params.getSpendCvLen(),
params.getOutputCv(), params.getOutputCvLen());
log(21, l2B(params.getValueBalance()), params.getBindingSig(), params.getSighashValue(),
params.getSpendCv(), i2B(params.getSpendCvLen()),
params.getOutputCv(), i2B(params.getOutputCvLen()), b2B(res));
return res;
}

public static void librustzcashSaplingVerificationCtxFree(long ctx) {
Expand All @@ -288,7 +338,9 @@ public static boolean librustzcashIvkToPkd(IvkToPkdParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashIvkToPkd(params.getIvk(), params.getD(), params.getPkD());
boolean res = INSTANCE.librustzcashIvkToPkd(params.getIvk(), params.getD(), params.getPkD());
log(7, params.getIvk(), params.getD(), params.getPkD(), b2B(res));
return res;
}

public static void librustzcashMerkleHash(MerkleHashParams params) {
Expand All @@ -297,6 +349,7 @@ public static void librustzcashMerkleHash(MerkleHashParams params) {
}
INSTANCE.librustzcashMerkleHash(params.getDepth(), params.getA(), params.getB(),
params.getResult());
log(16, l2B(params.getDepth()), params.getA(), params.getB(), params.getResult());
}

/**
Expand All @@ -308,6 +361,7 @@ public static void librustzcashTreeUncommitted(byte[] result) throws ZksnarkExce
}
LibrustzcashParam.valid32Params(result);
INSTANCE.librustzcashTreeUncommitted(result);
log(17, result);
}

public static boolean isOpenZen() {
Expand All @@ -318,4 +372,27 @@ public static boolean isOpenZen() {
return res;
}

private static void log(int c, byte[] ... params) {
if (GlobalContext.isLog()) {
GlobalContext.getHeader().ifPresent(header -> logger.info("{}:{}:{}", header, c,
Stream.of(params).map(ByteArray::toHexString).collect(Collectors.joining(","))));
}
}

private static byte[] l2B(long value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(value);
return buffer.array();
}

private static byte[] i2B(int value) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.putInt(value);
return buffer.array();
}

private static byte[] b2B(boolean b) {
return b ? new byte[]{1} : new byte[]{0};
}

}
Loading
Loading