Add littleEndian conversions

This commit is contained in:
chimp1984 2022-05-26 12:36:59 +02:00
parent ed4eaafcdf
commit 7fe93d9be7
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
2 changed files with 10 additions and 1 deletions

View file

@ -42,7 +42,11 @@ public class CryptoUtil {
public static BigInteger l = BigInteger.valueOf(2).pow(252).add(new BigInteger("27742317777372353535851937790883648493"));
public static String toCanonicalTxKey(String txKey) {
return HexEncoder.getString(new BigInteger(HexEncoder.getBytes(txKey)).mod(l).toByteArray());
byte[] bytes = HexEncoder.getBytes(txKey);
byte[] asLittleEndianBytes = ensure32BytesAndConvertToLittleEndian(bytes);
byte[] nonMalleable = new BigInteger(asLittleEndianBytes).mod(l).toByteArray();
byte[] nonMalleableAsLittleEndian = ensure32BytesAndConvertToLittleEndian(nonMalleable);
return HexEncoder.getString(nonMalleableAsLittleEndian);
}
public static byte[] scReduce32(byte[] a) {

View file

@ -20,6 +20,7 @@ package knaccc.monero.address;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -31,5 +32,9 @@ public class CryptoUtilTest {
String txKey = "6c336e52ed537676968ee319af6983c80b869ca6a732b5962c02748b486f8f0f";
assertEquals(txKey, CryptoUtil.toCanonicalTxKey(txKey));
assertEquals(txKey, CryptoUtil.toCanonicalTxKey(txKey.toUpperCase()));
// key with 1 above l value (created with HexEncoder.getString(ensure32BytesAndConvertToLittleEndian(l.add(BigInteger.ONE).toByteArray())))
txKey = "eed3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010";
assertFalse(txKey.equals(CryptoUtil.toCanonicalTxKey(txKey)));
}
}