From e5098f08e06658c78efcc25993ad839de90cff90 Mon Sep 17 00:00:00 2001 From: Carl Norburn Date: Fri, 28 Jun 2024 16:13:21 +0100 Subject: [PATCH] Tests for the 50% Secp problem when creating a public short key --- .../com/syntifi/crypto/key/Secp256k1PublicKey.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/syntifi/crypto/key/Secp256k1PublicKey.java b/src/main/java/com/syntifi/crypto/key/Secp256k1PublicKey.java index 9496ebe5..f17b0fe7 100644 --- a/src/main/java/com/syntifi/crypto/key/Secp256k1PublicKey.java +++ b/src/main/java/com/syntifi/crypto/key/Secp256k1PublicKey.java @@ -1,7 +1,6 @@ package com.syntifi.crypto.key; import com.casper.sdk.model.key.AlgorithmTag; -import com.syntifi.crypto.key.encdec.Hex; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import org.bouncycastle.asn1.*; @@ -123,11 +122,16 @@ public Boolean verify(byte[] message, byte[] signature) { * @return short key as byte array */ public static byte[] getShortKey(final byte[] key) { - final BigInteger pubKey = new BigInteger(key); - final String pubKeyPrefix = pubKey.testBit(0) ? "03" : "02"; + final int startBit = key[0] == (byte) 0 ? 1 : 0; - final byte[] pubKeyBytes = Arrays.copyOfRange(key, startBit, (AlgorithmTag.SECP256K1.getLength() - 1) + startBit); - return Hex.decode(pubKeyPrefix + Hex.encode(pubKeyBytes)); + + final byte[] shortKey = new byte[AlgorithmTag.SECP256K1.getLength()]; + shortKey[0] = (byte) (new BigInteger(key).testBit(0) ? 3 : 2); + + System.arraycopy(key, startBit, shortKey, 1, AlgorithmTag.SECP256K1.getLength() - 1); + + return shortKey; + } }