Skip to content
Draft
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
12 changes: 12 additions & 0 deletions scripts/jshell/ecc-basics.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ecc-basics.jsh

IO.println("ECC Basics Demo")

import module org.bitcoinj.secp

var P = Secp256k1.P.toString(16)
var G = Secp256k1.G

var secp = Secp256k1.get()
var priv = secp.ecPrivKeyCreate()
var pub = secp.ecPubKeyCreate(priv)
12 changes: 12 additions & 0 deletions scripts/jshell/ecdh.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// schnorr.jsh

IO.println("Elliptic Curve Diffie-Hellman Demo")

import module org.bitcoinj.secp

var secp = Secp256k1.get()
var alice = secp.ecKeyPairCreate()
var bob = secp.ecKeyPairCreate()
var secretA = secp.ecdh(bob.publicKey(), alice).get()
var secretB = secp.ecdh(alice.publicKey(), bob).get()
secretA.equals(secretB)
17 changes: 17 additions & 0 deletions scripts/jshell/ecdsa.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ecdsa.jsh

IO.println("ECDSA Sign/Verify Demo")

import module org.bitcoinj.secp

byte[] sha256(String messageString) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(messageString.getBytes());
return digest.digest();
}

var secp = Secp256k1.get()
var alice = secp.ecKeyPairCreate()
var message = sha256("Hello Stanford!")
var signature = secp.ecdsaSign(message, alice).get()
var isValid = secp.ecdsaVerify(signature, message, alice.publicKey()).get()
12 changes: 12 additions & 0 deletions scripts/jshell/providers.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// providers.jsh

IO.println("Providers Demo")

import module org.bitcoinj.secp

var providers = Secp256k1.all().toList()
var ids = Secp256k1.all().map(Secp256k1.Provider::id).toList()

var libsecp = Secp256k1.get()
var bouncy = Secp256k1.getById("bouncy-castle")

11 changes: 11 additions & 0 deletions scripts/jshell/schnorr.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// schnorr.jsh

IO.println("Schnorr Sign/Verify Demo")

import module org.bitcoinj.secp

var secp = Secp256k1.get()
var alice = secp.ecKeyPairCreate()
var taggedMessage = secp.taggedSha256("BitDevs Protocol", "Hello Stanford!")
var schnorrSig = secp.schnorrSigSign32(taggedMessage, alice)
var isValidSchnorr = secp.schnorrSigVerify(schnorrSig, taggedMessage, alice.publicKey()).get()
83 changes: 83 additions & 0 deletions secp-examples-jshell/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import org.codehaus.groovy.runtime.GStringImpl

plugins {
id 'java'
id 'application'
id 'org.beryx.jlink' version '3.1.4-rc'
}

ext.moduleName = 'org.bitcoinj.secp.jshell'
ext.libSecpDir = System.getenv("LIBSECP_DIR")
ext.javaHome = System.getenv("JAVA_HOME")

dependencies {
implementation project(':secp-api')
implementation project(':secp-bouncy')
implementation project(':secp-ffm')
}

tasks.withType(JavaCompile).configureEach {
// Override Default release version
options.release = 25
}

jar {
inputs.property("moduleName", moduleName)
manifest {
attributes 'Implementation-Title': 'Placeholder App for secp256k1-jdk JShell Example',
'Main-Class': application.mainClass,
'Implementation-Version': archiveVersion.get()
}
}

application {
mainModule = 'org.bitcoinj.secp.jshell'
mainClass = 'org.bitcoinj.secp.jshell.Hello'
applicationName = 'hello'
}

run {
jvmArgs += '--enable-native-access=org.bitcoinj.secp.ffm'
}

jlink {
options = ['--add-modules', 'org.bitcoinj.secp.ffm',
'--add-modules', 'org.bitcoinj.secp.bouncy', '--ignore-signing-information',
'--add-modules', 'jdk.jshell']
launcher {
name = application.applicationName
}
}

tasks.register('copyNativeLibraries', Copy) {
dependsOn tasks.jlink
from(libSecpDir) { include 'libsecp256k1.*' }
into("${tasks.jlink.imageDir}/lib")
}

tasks.register('copyJdkSource', Copy) {
dependsOn tasks.jlink
from {
def javaHome = System.getenv('JAVA_HOME') ?: System.getProperty('java.home')
file("$javaHome/lib/src.zip")
}
into("${tasks.jlink.imageDir}/lib")
finalizedBy(tasks.named('addJshellWrapper'))
}

tasks.register('addJshellWrapper') {
dependsOn tasks.copyJdkSource
doLast {
def jshellScript = file("${tasks.jlink.imageDir}/bin/secp-shell")
jshellScript.text = """#!/bin/bash
DIR=\$(dirname "\$0")
"\$DIR/jshell" "\$@"
"""
jshellScript.setExecutable(true)
}
}

tasks.jlink {
finalizedBy(tasks.copyNativeLibraries, tasks.copyJdkSource) // tasks.copyProjectJavadoc
}

21 changes: 21 additions & 0 deletions secp-examples-jshell/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023-2025 secp256k1-jdk Developers.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* JShell Example.
*/
module org.bitcoinj.secp.jshell {
requires org.bitcoinj.secp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023-2025 secp256k1-jdk Developers.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.secp.jshell;

public class Hello {
void main() {
IO.println("Hello! Try running the `secp-shell` executable for JShell with secp256k1-jdk built-in.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2023-2025 secp256k1-jdk Developers.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// Contains a placeholder application for a custom JShell build.
package org.bitcoinj.secp.jshell;
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ include 'secp-graalvm' // GraalVM Foreign Function Registration suppor
include 'secp-bitcoinj' // bitcoinj integration utilities and tests (P2TR address generation, etc.)
include 'secp-integration-test' // Integration tests of API implementations (ffm and bouncy)
include 'secp-examples-java' // Java examples
include 'secp-examples-jshell' // jshell example
include 'secp-examples-kotlin' // Kotlin examples