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

The secp256k1 algorithm signature result is one byte less. #1269

Open
sujiulong opened this issue Mar 29, 2019 · 2 comments
Open

The secp256k1 algorithm signature result is one byte less. #1269

sujiulong opened this issue Mar 29, 2019 · 2 comments

Comments

@sujiulong
Copy link

@test
public void verifyTest() {
String pri = "87385e10d018f971f66cf2c065663d4aa427286f259f85bb8b2438130f4f1ee7";
String sourceHash = "7d50e029128286afba20cb62cf1b874d76a527a896f3d39167289a9b0446c518";
ECKey ecKey = ECKey.fromPrivate(Hex.decode(pri));
byte[] sign = ecKey.sign(Hex.decode(sourceHash)).toByteArray();
System.out.println(sign.length);
}

The result is always 64 not 65 bytes.

@mkalinin
Copy link
Contributor

65 on my side for this particular test.

@sujiulong
Copy link
Author

Thank you very much for your reply. My code is too old.
This is my local code.
public byte[] toByteArray() {
final byte fixedV = this.v >= 27
? (byte) (this.v - 27)
: this.v;

        return ByteUtil.merge(
                ByteUtil.bigIntegerToBytes(this.r),
                ByteUtil.bigIntegerToBytes(this.s),
                new byte[]{fixedV});
    }

This is latest code.
public byte[] toByteArray() {
final byte fixedV = this.v >= 27
? (byte) (this.v - 27)
: this.v;

        return ByteUtil.merge(
                ByteUtil.bigIntegerToBytes(this.r, 32),
                ByteUtil.bigIntegerToBytes(this.s, 32),
                new byte[]{fixedV});
    }

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

3 participants
@mkalinin @sujiulong and others