Skip to content

Commit

Permalink
Generate valid secp256k1 key during key creation (#10051)
Browse files Browse the repository at this point in the history
Valid secp256k1 key values should start with 0x02 or 0x03 indicating the y-coordinate of the elliptic curve being odd or even. The change is fixing 6 integration tests as of PR opening.

Signed-off-by: sdimitrov9 <[email protected]>
  • Loading branch information
sdimitrov9 authored Jan 6, 2025
1 parent a41b950 commit 6819dc4
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import jakarta.persistence.EntityManager;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.LocalDate;
Expand Down Expand Up @@ -1147,14 +1148,23 @@ public byte[] key(KeyCase keyCase) {
var key =
switch (keyCase) {
case ECDSA_SECP256K1 -> Key.newBuilder()
.setECDSASecp256K1(ByteString.copyFrom(bytes(KEY_LENGTH_ECDSA)));
.setECDSASecp256K1(ByteString.copyFrom(generateSecp256k1Key()));
case ED25519 -> Key.newBuilder().setEd25519(ByteString.copyFrom(bytes(KEY_LENGTH_ED25519)));
default -> throw new UnsupportedOperationException("Key type not supported");
};

return key.build().toByteArray();
}

private byte[] generateSecp256k1Key() {
byte[] xCoordinate = bytes(32); // The x-coordinate of the elliptic curve point
byte prefix = (byte) (Math.random() < 0.5 ? 0x02 : 0x03); // Randomly chosen even or odd y-coordinate
ByteBuffer compressedKey = ByteBuffer.allocate(33);
compressedKey.put(prefix);
compressedKey.put(xCoordinate);
return compressedKey.array();
}

public long number() {
return id.incrementAndGet();
}
Expand Down

0 comments on commit 6819dc4

Please sign in to comment.