Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signature is not canonical #22

Closed
t3ran13 opened this issue Jan 25, 2020 · 3 comments
Closed

signature is not canonical #22

t3ran13 opened this issue Jan 25, 2020 · 3 comments

Comments

@t3ran13
Copy link

t3ran13 commented Jan 25, 2020

is it woking, did you check? blockchain tells it is not canonical

@ldudzsim
Copy link
Member

If you talking about this and forcing to use smaller s value in signature, we do not have it, because our lib is strict elliptic math, without implementation focused on bitcoin/blockchain or something. But if you need that, you can check this

<?php

require_once("vendor/autoload.php");

use Elliptic\EC;
use Elliptic\EC\Signature;

function getNegativeSignature($ec, $sig) {
    $res = new Signature($sig);
    $res->s = $res->s->neg()->add($ec->n);
    return $res;
}

$ec = new EC('secp256k1');
$key = $ec->genKeyPair();
$msg = 'ab4c3451';
$signature = $key->sign($msg);
$negativeSignature = getNegativeSignature($ec, $signature);

echo "Normal signature:   " . $signature->toDER('hex') . "\n";
echo "Negative signature: " . $negativeSignature->toDER('hex') . "\n";

echo "Verify normal:   " . (($key->verify($msg, $signature) == TRUE) ? "true" : "false") . "\n";
echo "Verify negative: " . (($key->verify($msg, $negativeSignature) == TRUE) ? "true" : "false") . "\n";

if ($signature->s->cmp($negativeSignature->s) < 0) {
    echo "Normal is canonical\n";
}
else {
    echo "Negative is canonical\n";
}

@t3ran13
Copy link
Author

t3ran13 commented Jan 26, 2020

sorry, it is work correct, i forgot make hash of message #21

canonical sign can be checked with DER

    public static function isSignatureCanonical($der)
    {
        $buffer = new ByteBuffer();
        $buffer->write($der);
        $lenR = $buffer->readInt8(3);
        $lenS = $buffer->readInt8(5 + $lenR);

        return $lenR === 32 && $lenS === 32;
    }

run sign one more time in loop if it is not canonical

@ldudzsim
Copy link
Member

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants