Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'jacoco'

allprojects {
group = 'io.emeraldpay.polkaj'
version = "0.5.1-SNAPSHOT"
version = "0.5.2-SNAPSHOT"

repositories {
mavenLocal()
Expand Down
17 changes: 1 addition & 16 deletions polkaj-schnorrkel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,4 @@ task compileRust(type:Exec) {
commandLine 'cargo', 'build', '--release', '--target-dir=../../build/rust'
}

compileJava.dependsOn(compileRust)

jar {
from("${buildDir}/rust/release") {
into "native/macos"
include '*.dylib'
}
from("${buildDir}/rust/release") {
into "native/linux"
include '*.so'
}
from("${buildDir}/rust/release") {
into "native/windows"
include '*.dll'
}
}
compileJava.dependsOn(compileRust)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.emeraldpay.polkaj.merlin;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
* A container class to simply hold all the necessary input data (label + messages) to construct the actual
* transcript on Rust's side. Think of this as "the bag of currently necessary arguments".
* The main idea of this class is to be easily portable to Rust using JNI.
* It has no usage on its own on the Java side alone.
*/
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "FieldCanBeLocal"})
public class TranscriptData {
// INTENTIONALITY: Those fields being of type ArrayList is essential for the JNI mappings on the Rust side

// HACK:
// Semantically, the internal representation of the domainSeparationLabel should be a single byte[]
// Due to an unresolved issue with robusta (the rust lib) though,
// the field mapping from byte[] doesn't work as expected, so this is the current workaround.
private final ArrayList<byte[]> domainSeparationLabel;

private final ArrayList<byte[]> labels;
private final ArrayList<byte[]> messages;

public TranscriptData(byte[] domainSeparationLabel) {
this.domainSeparationLabel = new ArrayList<>();
this.domainSeparationLabel.add(domainSeparationLabel);
this.labels = new ArrayList<>();
this.messages = new ArrayList<>();
}

/**
* Appends an ASCII encoded string message to the transcript with an ASCII encoded string label.
* @param label the ASCII encoded label
* @param message the ASCII encoded message
*/
public void appendMessage(String label, String message) {
appendMessage(label, message.getBytes(StandardCharsets.US_ASCII));
}

/**
* Appends a message to the transcript with an ASCII encoded string label.
* @param label the ASCII encoded label
* @param message the actual message (content)
*/
public void appendMessage(String label, byte[] message) {
appendMessage(label.getBytes(StandardCharsets.US_ASCII), message);
}

/**
* Appends a message to the transcript.
* @param label the label of the message
* @param message the actual message (content)
*/
public void appendMessage(byte[] label, byte[] message) {
labels.add(label);
messages.add(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package io.emeraldpay.polkaj.schnorrkel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import io.emeraldpay.polkaj.merlin.TranscriptData;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.logging.MemoryHandler;

/**
* Schnorrkel implements Schnorr signature on Ristretto compressed Ed25519 points, as well as related protocols like
Expand Down Expand Up @@ -141,6 +132,10 @@ public static Schnorrkel getInstance() {
*/
public abstract Schnorrkel.PublicKey derivePublicKeySoft(Schnorrkel.PublicKey base, Schnorrkel.ChainCode chainCode) throws SchnorrkelException;

public abstract boolean vrfVerify(PublicKey sk, TranscriptData transcript, VrfOutputAndProof vrfOutputAndProof);

public abstract VrfOutputAndProof vrfSign(KeyPair keyPair, TranscriptData transcript);

// ====================== Supporting Classes ======================

/**
Expand Down Expand Up @@ -248,5 +243,4 @@ public int hashCode() {
return Arrays.hashCode(value);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.emeraldpay.polkaj.schnorrkel;

import io.emeraldpay.polkaj.merlin.TranscriptData;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -55,6 +57,8 @@ private static byte[] encodeKeyPair(Schnorrkel.KeyPair keyPair) {
return result;
}

// ====================== Mapping to the Native Library ======================

private static native byte[] sign(byte[] publicKey, byte[] secretKey, byte[] message);

private static native byte[] keypairFromSeed(byte[] seed);
Expand All @@ -67,7 +71,9 @@ private static byte[] encodeKeyPair(Schnorrkel.KeyPair keyPair) {

private static native byte[] derivePublicKeySoft(byte[] publicKey, byte[] cc);

// ====================== Mapping to the Native Library ======================
private static native boolean vrfVerify(byte[] publicKey, TranscriptData transcript, byte[] vrfOutput, byte[] vrfProof);

private static native byte[] vrfSign(byte[] secretKey, TranscriptData transcript);

private static boolean extractAndLoadJNI() throws IOException {
// define which of files bundled with Jar to extract
Expand Down Expand Up @@ -99,7 +105,6 @@ private static boolean extractAndLoadJNI() throws IOException {

// extract native lib to the filesystem
InputStream lib = Schnorrkel.class.getResourceAsStream(classpathFile);
System.out.println(classpathFile);
if (lib == null) {
System.err.println("Library " + classpathFile + " is not found in the classpath");
return false;
Expand All @@ -109,7 +114,7 @@ private static boolean extractAndLoadJNI() throws IOException {

Files.copy(lib, target);
System.load(target.toFile().getAbsolutePath());
System.out.println("library " + classpathFile + " is loaded");
System.out.println("Library " + classpathFile + " is loaded");

// setup JVM to delete files on exit, when possible
target.toFile().deleteOnExit();
Expand Down Expand Up @@ -170,4 +175,28 @@ public PublicKey derivePublicKeySoft(PublicKey base, ChainCode chainCode) throws
return new Schnorrkel.PublicKey(key);
}

@Override
public boolean vrfVerify(PublicKey pk, TranscriptData transcript, VrfOutputAndProof vrfOutputAndProof) {
return vrfVerify(
pk.getPublicKey(),
transcript,
vrfOutputAndProof.getOutput(),
vrfOutputAndProof.getProof()
);
}

@Override
public VrfOutputAndProof vrfSign(KeyPair keyPair, TranscriptData transcript) {
byte[] vrfOutputAndProofBytes = vrfSign(keyPair.getSecretKey(), transcript);
final int OUTPUT_LEN = VrfOutputAndProof.OUTPUT_BYTE_LEN;
final int PROOF_LEN = VrfOutputAndProof.PROOF_BYTE_LEN;

// effectively, split the array
byte[] vrfOutput = new byte[OUTPUT_LEN];
byte[] vrfProof = new byte[PROOF_LEN];
System.arraycopy(vrfOutputAndProofBytes, 0, vrfOutput, 0, OUTPUT_LEN);
System.arraycopy(vrfOutputAndProofBytes, OUTPUT_LEN, vrfProof, 0, PROOF_LEN);

return VrfOutputAndProof.wrap(vrfOutput, vrfProof);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.emeraldpay.polkaj.schnorrkel;

/**
* Essentially a record to hold the VRF output together with its proof.
*/
public class VrfOutputAndProof {
public static final int OUTPUT_BYTE_LEN = 32;
public static final int PROOF_BYTE_LEN = 64;

private final byte[] output;

private final byte[] proof;

public static VrfOutputAndProof wrap(byte[] output, byte[] proof) {
if (output.length != OUTPUT_BYTE_LEN) {
throw new IllegalArgumentException(
String.format("VRF output must be %d bytes (compressed ristretto point).", OUTPUT_BYTE_LEN));
}

if (proof.length != PROOF_BYTE_LEN) {
throw new IllegalArgumentException(
String.format("VRF proof must be %d bytes (compressed ristretto point).", PROOF_BYTE_LEN));
}

return new VrfOutputAndProof(output, proof);
}

private VrfOutputAndProof(byte[] output, byte[] proof) {
this.output = output;
this.proof = proof;
}

public byte[] getOutput() {
return output;
}

public byte[] getProof() {
return proof;
}
Comment thread
David-Petrov marked this conversation as resolved.
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion polkaj-schnorrkel/src/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Cargo.lock
target/
5 changes: 5 additions & 0 deletions polkaj-schnorrkel/src/rust/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}
Loading