Skip to content

Repository files navigation

fourQ-lib-java

A library that implements the high-security, high-performance elliptic curve fourQ. Useful cryptographic functions are provided which include:

  • fourqj.api.SchnorrQ message signing
  • fourqj.api.SchnorrQ signature verification
  • Public key generation from a private key
  • Public-private key pair generation

Note on Endianness

This Java implementation of FourQ maintains compatibility with the original C reference implementation, which uses little-endian byte ordering. However, Java's BigInteger class and most Java operations naturally work with big-endian byte ordering.

To bridge this compatibility gap, the library performs byte array reversals at specific points in the cryptographic operations. This ensures that:

  • Input/Output Compatibility: Keys and signatures are compatible with other FourQ implementations
  • Internal Consistency: Java's big-endian arithmetic works correctly with the curve parameters
  • Cross-Platform Interoperability: Data can be exchanged with C/C++ FourQ implementations

Example: Byte Array Reversal

The library includes utility functions for handling endianness conversions:

importfourqj.utils.ByteArrayUtils;
importfourqj.utils.ByteArrayReverseMode;
importjava.util.Optional;
// Example: Converting between little-endian and big-endian representationspublicstaticbyte[] reverseForCompatibility(byte[] data) {
returnByteArrayUtils.reverseByteArray(data, Optional.empty());
}
// Usage examplebyte[] littleEndianData = {0x01, 0x02, 0x03, 0x04};
byte[] bigEndianData = reverseForCompatibility(littleEndianData);
// Result: {0x04, 0x03, 0x02, 0x01}

Note: Users of the library typically don't need to handle these conversions manually, as they are performed automatically within the cryptographic operations. Secondary Note: Reversals are often compounded with padding fixes that can be seen in some util classes. These padding arise due to BigInteger's natural zero appending patterns to the front of byte arrays, that can cause errors when reversed.

Dependency

Gradle

Add the following dependency to your build.gradle file:

implementation("com.namanmalhotra:fourQ:1.0.2")

Maven

Add the following dependency to your pom.xml file:

<dependency>
<groupId>com.namanmalhotra</groupId>
<artifactId>fourQ</artifactId>
<version>1.0.2</version>
</dependency>

Import

After adding the dependency, import the main class in your Java code:

importfourqj.api.SchnorrQ;

Examples

Public Key Generation

finalfourqj.api.SchnorrQschnorrQ = newfourqj.api.SchnorrQ();
finalintHEX_RADIX = 16;
finalBigIntegerprivateKey = newBigInteger("F510847AAB323", HEX_RADIX);
try {
finalBigIntegerpublicKey = schnorrQ.schnorrQKeyGeneration(privateKey);
} catch (EncryptionExceptione) {
System.err.println("Error generating public key: " + e.getMessage());
}

Public-Private Key Pair Generation

finalfourqj.api.SchnorrQschnorrQ = newfourqj.api.SchnorrQ();
try {
finalPair<BigInteger, BigInteger> keyPair = schnorrQ.schnorrQFullKeyGeneration();
finalBigIntegerprivateKey = keyPair.first;
finalBigIntegerpublicKey = keyPair.second;
} catch (EncryptionExceptione) {
System.err.println("Error generating key pair: " + e.getMessage());
}

fourqj.api.SchnorrQ Message Signing

finalfourqj.api.SchnorrQschnorrQ = newfourqj.api.SchnorrQ();
finalStringmessage = "The quick brown fox jumps over the lazy dog";
finalbyte[] messageBytes = message.getBytes();
try {
finalBigIntegersignature = schnorrQ.schnorrQSign(privateKey, publicKey, messageBytes);
} catch (EncryptionExceptione) {
System.err.println("Error signing message: " + e.getMessage());
}

fourqj.api.SchnorrQ Signature Verification

finalfourqj.api.SchnorrQschnorrQ = newfourqj.api.SchnorrQ();
try {
if (schnorrQ.schnorrQVerify(publicKey, signature, messageBytes)) {
System.out.println("Signature is valid");
} else {
System.out.println("Signature is invalid");
}
} catch (EncryptionExceptione) {
System.err.println("Error verifying signature: " + e.getMessage());
}

Test Project

For more reference on implementation and use cases refer to the following project. Noting however that this assumes version 1.0.1, but subsequent versions maintain parity.

Support

If you find this library useful, consider sponsoring the project:

Sponsor

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages