Git Product home page Git Product logo

schnorrkel-java's Introduction

schnorrkel-java

Java implementation of the sr25519 signature algorithm (schnorr over ristretto25519). The existing rust implementation is here.

This library is currently able to create sr25519 keys, import sr25519 keys, and sign and verify messages.

usage

Example: signing and verification

import com.debuggor.schnorrkel.utils.HexUtils;

public class SignTest {

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = KeyPair.fromSecretSeed(HexUtils.hexToBytes("579d7aa286b37b800b95fe41adabbf0c2a577caf2854baeca98f8fb242ff43ae"), ExpansionMode.Ed25519);

        byte[] message = "test message".getBytes();
        SigningContext ctx = SigningContext.createSigningContext("good".getBytes());
        SigningTranscript t = ctx.bytes(message);
        Signature signature = keyPair.sign(t);
        byte[] sign = signature.to_bytes();
        System.out.println(HexUtils.bytesToHex(sign));


        SigningContext ctx2 = SigningContext.createSigningContext("good".getBytes());
        SigningTranscript t2 = ctx2.bytes(message);
        KeyPair fromPublicKey = KeyPair.fromPublicKey(keyPair.getPublicKey().toPublicKey());
        boolean verify = fromPublicKey.verify(t2, sign);
        System.out.println(verify);
    }
}

other

Go implementation

Thanks to @str4d and @isislovecruft for the open source Curve25519 library

schnorrkel-java's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

schnorrkel-java's Issues

The generated address is incorrect

i want generated polkadot address.
privareKey : 0xd3d8015b804bcf9114a62038e203fc87c8d7658568dc2116b936a81a0eef99fd
right should be:14SWSpQQjSafs3zniZrDrbPx89iu1BbAi3vNA3gSuuiiC1FU
but result:44c8b1681419a6c32c1728c30eb9d976d62051d9a3ae443270acff468dad3275

what should i do?

Below was my code:

public static KeyPair generateKeyPairSR(String secret) {
    byte[] seeds = HexUtils.hexToBytes(secret);
    Sr25519 sr25519 = Sr25519.fromSecretSeed(seeds);
    PrivateKey privateKey = sr25519.getPrivateKey();
    PublicKey publicKey = sr25519.getPublicKey();
    return new KeyPair(secret, HexUtils.bytesToHex(publicKey.toPublicKey()));
}

private static final RistrettoGeneratorTable ristrettoTable = Constants.RISTRETTO_GENERATOR_TABLE;

public static Sr25519 fromSecretSeed(byte[] seed) {
    PrivateKey privateKey = new PrivateKey(seed, ExpansionMode.Ed25519);
    byte[] key = privateKey.getKey();
    RistrettoElement ristretto = ristrettoTable.multiply(Scalar.fromBits(key));
    PublicKey publicKey = new PublicKey(ristretto);
    return new Sr25519(publicKey, privateKey);
}

System randomness

There is seemingly no system randomness used in

public Scalar witness_scalar(byte[] label, byte[] nonce_seeds) throws Exception {

It's secure without system randomness here because you only do simple signatures, but stuff like multi-sigs are not secure without system randomness. We always use system randomness in schnorrkel because we do these other protocols that require it.

It's not immediately critical, but just a warning if you implement more.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.