Skip to content

Commit

Permalink
Add missing blst files
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Sep 24, 2024
1 parent fde0d01 commit 496e620
Show file tree
Hide file tree
Showing 123 changed files with 123,862 additions and 0 deletions.
1,202 changes: 1,202 additions & 0 deletions src/blst/bindings/emscripten/build.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/blst/bindings/emscripten/run.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh -e

cd `dirname $0`

python3 build.py "$@"

NODE=`which ${NODE:-${EMSDK_NODE:-node}} 2>&1` || NODE=`which nodejs`
env NODE_PATH=.: $NODE runnable.js

if [ "$OLDPWD" = "$PWD" ]; then
echo "To test from browser start 'python3 -m http.server -b localhost'"
echo "and browse to runnable.html..."
fi
54 changes: 54 additions & 0 deletions src/blst/bindings/emscripten/runnable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>runnable.html</title></head>
<body>
<script type="text/javascript" src="blst.js"></script>
<div id="output"></div>
<script type="text/javascript">
var output = {
div: document.getElementById("output"),
log: function(str) {
this.div.appendChild(document.createTextNode(str));
this.div.appendChild(document.createElement("br"));
}
}

output.log("testing...");

blst['onRuntimeInitialized'] = function() {
var msg = "assertion"; // this what we're signing
var DST = "MY-DST"; // domain separation tag

var SK = new blst.SecretKey();
SK.keygen("*".repeat(32));

////////////////////////////////////////////////////////////////////////
// generate public key and signature

var pk = new blst.P1(SK);
var pk_for_wire = pk.serialize();

var sig = new blst.P2();
var sig_for_wire = sig.hash_to(msg, DST, pk_for_wire)
.sign_with(SK)
.serialize();

////////////////////////////////////////////////////////////////////////
// at this point 'pk_for_wire', 'sig_for_wire' and 'msg' are
// "sent over network," so now on "receiver" side

sig = new blst.P2_Affine(sig_for_wire);
pk = new blst.P1_Affine(pk_for_wire);

if (!pk.in_group()) throw "disaster"; // vet the public key

var ctx = new blst.Pairing(true, DST);
ctx.aggregate(pk, sig, msg, pk_for_wire);
ctx.commit();
if (!ctx.finalverify()) throw "disaster";

output.log("OK");
}
</script>
</body>
</html>
40 changes: 40 additions & 0 deletions src/blst/bindings/emscripten/runnable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

console.log("testing...");

const blst = require("blst");

blst['onRuntimeInitialized'] = function() {
var msg = "assertion"; // this what we're signing
var DST = "MY-DST"; // domain separation tag

var SK = new blst.SecretKey();
SK.keygen("*".repeat(32));

////////////////////////////////////////////////////////////////////////
// generate public key and signature

var pk = new blst.P1(SK);
var pk_for_wire = pk.serialize();

var sig = new blst.P2();
var sig_for_wire = sig.hash_to(msg, DST, pk_for_wire)
.sign_with(SK)
.serialize();

////////////////////////////////////////////////////////////////////////
// at this point 'pk_for_wire', 'sig_for_wire' and 'msg' are
// "sent over network," so now on "receiver" side

sig = new blst.P2_Affine(sig_for_wire);
pk = new blst.P1_Affine(pk_for_wire);

if (!pk.in_group()) throw "disaster"; // vet the public key

var ctx = new blst.Pairing(true, DST);
ctx.aggregate(pk, sig, msg, pk_for_wire);
ctx.commit();
if (!ctx.finalverify()) throw "disaster";

console.log("OK");
}
37 changes: 37 additions & 0 deletions src/blst/bindings/java/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
########################################################################
# Download/copy[/append?] this file to 'jni[/Android.mk]' directory of
# the *target* application. If https://github.com/supranational/blst is
# not configured as a submodule, the repository will be cloned into the
# 'jni' directory. Either way, it would be appropriate to add following
# lines to your .gitignore:
#
# [app/]jni/blst*
# [app/]src/supranational/blst

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := blst

LOCAL_SRC_FILES := blst/src/server.c blst/build/assembly.S blst_wrap.cpp

$(LOCAL_PATH)/blst:
@(cd `dirname $@` && \
mod=`git submodule 2>/dev/null | \
awk '{ if(match($$2,"/blst$$")) print $$2 }'` && \
[ -n "$$mod" ] && ln -s "$$mod" blst \
) || git clone https://github.com/supranational/blst.git $@

$(LOCAL_PATH)/blst/src/server.c $(LOCAL_PATH)/blst/build/assembly.S: $(LOCAL_PATH)/blst

$(LOCAL_PATH)/blst_wrap.cpp: $(LOCAL_PATH)/blst/bindings/blst.swg
blst_classes=`dirname $@`/../src/supranational/blst && \
mkdir -p $$blst_classes && \
swig -c++ -java -package supranational.blst -outdir $$blst_classes -o $@ $<

LOCAL_CFLAGS := -fno-builtin-memcpy -fvisibility=hidden
LOCAL_CPPFLAGS := -fexceptions -I$(LOCAL_PATH)/blst/bindings
LOCAL_LDFLAGS := -Wl,-Bsymbolic

include $(BUILD_SHARED_LIBRARY)
86 changes: 86 additions & 0 deletions src/blst/bindings/java/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/sh -e

JAVA_PACKAGE=${JAVA_PACKAGE:-supranational.blst}

# figure out if this script was source-d
if [ `dirname "$0"` -ef "$PWD" ]; then
TOP="$PWD"
elif [ -n "$OLDPWD" -a "$OLDPWD/$0" -ef `basename "$0"` ]; then
TOP="$PWD"
else
TOP=`dirname "$0"`
fi

TOP=`(cd "$TOP"/.. && echo $PWD)`

# figure out JAVA_HOME if not set by caller
if [ "x$JAVA_HOME" = "x" -a -x /usr/libexec/java_home ]; then
JAVA_HOME=`/usr/libexec/java_home 2>/dev/null || true`
fi
if [ "x$JAVA_HOME" = "x" ]; then
JAVA_HOME=`java -XshowSettings:properties -version 2>&1 | \
awk 'BEGIN { FS="=[ \t]*" } /java\.home/ { print $2 }'`
fi
if [ ! -d "$JAVA_HOME" ]; then
echo "JAVA_HOME='$JAVA_HOME' is not a directory" 1>&2
exit 1
fi
if [ ! -d "$JAVA_HOME"/include ]; then
if [ -d "$JAVA_HOME"/../include ]; then
JAVA_HOME=`dirname "$JAVA_HOME"`
else
echo "JAVA_HOME='$JAVA_HOME' is not a JDK" 1>&2
exit 1
fi
fi
# spot jni_md.h, which is included from jni.h
JNI_MD=`ls "$JAVA_HOME"/include/*/jni_md.h`
JNI_MD=`dirname "$JNI_MD"`

PKG=`echo $JAVA_PACKAGE | tr . /`
mkdir -p $PKG

if [ ! -f blst_wrap.cpp -o "$TOP"/blst.swg -nt blst_wrap.cpp \
-o "$TOP"/blst.hpp -nt blst_wrap.cpp \
-o "$TOP"/blst.h -nt blst_wrap.cpp \
-o ! -f $PKG/blst.java ]; then
(set -x; swig -c++ -java -package $JAVA_PACKAGE -outdir $PKG \
-o blst_wrap.cpp "$TOP"/blst.swg)
fi

if [ ! -f $PKG/blst.class -o $PKG/blst.java -nt $PKG/blst.class ]; then
(set -x; "$JAVA_HOME"/bin/javac $PKG/*.java)
fi

# ask blstJNI how does it name the shared library
SO_NAME=$PKG/`"$JAVA_HOME"/bin/java $PKG/blstJNI 2>/dev/null`

LIBBLST_A=libblst.a
if [ -f "$TOP"/libblst.a -a "$TOP"/libblst.a -nt "$TOP"/blst.h ]; then
LIBBLST_A="$TOP"/libblst.a
elif [ ! -f libblst.a -o "$TOP"/blst.h -nt libblst.a ]; then
$TOP/../build.sh -fvisibility=hidden "$@"
fi

if [ ! -f $SO_NAME -o blst_wrap.cpp -nt $SO_NAME \
-o $LIBBLST_A -nt $SO_NAME ]; then
mkdir -p `dirname $SO_NAME`
case $SO_NAME in
*.so) LDFLAGS=${LDFLAGS:--Wl,-Bsymbolic};;
*.dll) LDFLAGS=${LDFLAGS:--static-libstdc++};;
esac
if [ "x$CXX" = "x" ]; then
CXX=g++
which c++ >/dev/null 2>&1 && CXX=c++
fi
STD=`${CXX} -dM -E -x c++ /dev/null | \
awk '{ if($2=="__cplusplus" && $3<"2011") print "-std=c++11"; }'`
(set -x; ${CXX} ${STD} -shared -o $SO_NAME -fPIC -fvisibility=hidden \
-I"$JAVA_HOME"/include -I"$JNI_MD" -I"$TOP" \
-O -Wall -Wno-unused-function blst_wrap.cpp \
$LIBBLST_A ${LDFLAGS})
fi

if [ ! -f $JAVA_PACKAGE.jar -o $SO_NAME -nt $JAVA_PACKAGE.jar ]; then
(set -x; "$JAVA_HOME"/bin/jar cf $JAVA_PACKAGE.jar $PKG/*.class $PKG/*/*/*)
fi
21 changes: 21 additions & 0 deletions src/blst/bindings/java/run.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh -e

JAVA_PACKAGE=supranational.blst

cd `dirname $0`

#unset _JAVA_OPTIONS

. ./build.sh "$@"

if [ ! -f runnable.class -o runnable.java -nt runnable.class ]; then
(set -x; "$JAVA_HOME"/bin/javac runnable.java)
fi

if [ -f $JAVA_PACKAGE.jar ]; then
PSEP=`"$JAVA_HOME"/bin/java -XshowSettings:properties -version 2>&1 | \
awk 'BEGIN { FS="=[ \t]*" } /path\.separator/ { print $2 }'`
(set -x; "$JAVA_HOME"/bin/java -classpath $JAVA_PACKAGE.jar$PSEP. runnable)
else
(set -x; "$JAVA_HOME"/bin/java -Djava.library.path=. runnable)
fi
67 changes: 67 additions & 0 deletions src/blst/bindings/java/runnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import supranational.blst.*;

public class runnable {
public static void main(String argv[]) {
final var msg = "assertion".getBytes();
final var DST = "MY-DST";

var SK = new SecretKey();
SK.keygen("*".repeat(32).getBytes());

// generate public key and serialize it...
var pk_ = new P1(SK);
var pk_for_wire = pk_.serialize();

// sign |msg| and serialize the signature...
var sig_ = new P2();
var sig_for_wire = sig_.hash_to(msg, DST, pk_for_wire)
.sign_with(SK)
.serialize();

// now on "receiving" side, start with deserialization...
var _sig = new P2_Affine(sig_for_wire);
var _pk = new P1_Affine(pk_for_wire);
if (!_pk.in_group())
throw new java.lang.RuntimeException("disaster");
var ctx = new Pairing(true, DST);
ctx.aggregate(_pk, _sig, msg, pk_for_wire);
ctx.commit();
if (!ctx.finalverify())
throw new java.lang.RuntimeException("disaster");
System.out.println("OK");
}

// helpers...
final protected static char[] HEXARRAY = "0123456789abcdef".toCharArray();

protected static String toHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length<<1];

for (int j = 0, k = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[k++] = HEXARRAY[v >>> 4];
hexChars[k++] = HEXARRAY[v & 0x0F];
}

return new String(hexChars);
}

protected static int fromHexChar(char c) {
if (c>='0' && c<='9') return c - '0';
else if (c>='a' && c<='f') return c - 'a' + 10;
else if (c>='A' && c<='F') return c - 'A' + 10;
throw new IndexOutOfBoundsException("non-hex character");
}

protected static byte[] fromHexString(String str) {
byte[] bytes = new byte[str.length() >>> 1];

for (int j = 0, k = 0; j < bytes.length; j++) {
int hi = fromHexChar(str.charAt(k++));
int lo = fromHexChar(str.charAt(k++));
bytes[j] = (byte)((hi << 4) | lo);
}

return bytes;
}
}
42 changes: 42 additions & 0 deletions src/blst/bindings/node.js/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# All pathnames (except for INTERMEDIATE_DIR) are relative to position
# of *this* file. If it's moved, the pathnames would have to be adjusted
# accordingly. It's also advised to move and customize blst_wrap.py, the
# script responsible for generating or copying pre-generated
# blst_wrap.cpp...
{
'targets': [
{
'target_name': 'blst',
'sources': [
'<(INTERMEDIATE_DIR)/blst_wrap.cpp',
'../../src/server.c',
],
'include_dirs': [ '..' ],
'cflags': [ '-fno-builtin-memcpy', '-fvisibility=hidden' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES' },
'msvs_settings': { 'VCCLCompilerTool': { 'ExceptionHandling': '1' } },
'conditions': [
[ 'OS=="win"', {
'sources': [ '../../build/win64/*-x86_64.asm' ],
}, {
'sources': [ '../../build/assembly.S' ],
}
],
[ 'OS=="linux"', {
'ldflags': [ '-Wl,-Bsymbolic' ],
}
],
],
'actions' : [
{
'action_name': 'blst_wrap',
'variables': { 'cmd' : [ 'blst_wrap.py', '../blst.swg' ] },
'inputs': [ '<@(cmd)' ],
'outputs': [ '<(INTERMEDIATE_DIR)/blst_wrap.cpp' ],
'action': [ 'python', '<@(cmd)', '<@(_outputs)' ],
},
],
},
]
}
Loading

0 comments on commit 496e620

Please sign in to comment.