Skip to content

Commit

Permalink
Tests for the 50% Secp problem when creating a public short key
Browse files Browse the repository at this point in the history
  • Loading branch information
stormeye2000 committed Jun 28, 2024
1 parent a95d9a9 commit e5098f0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/com/syntifi/crypto/key/Secp256k1PublicKey.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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;

}

}

0 comments on commit e5098f0

Please sign in to comment.