-
Notifications
You must be signed in to change notification settings - Fork 121
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
feat(schnorr): add support for schnorr signatures #189
Open
kewde
wants to merge
2
commits into
cryptocoinjs:master
Choose a base branch
from
kewde:kewde/schnorr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const util = require('./util') | ||
|
||
const testVectors = [{ | ||
pk: [ | ||
0xD6, 0x9C, 0x35, 0x09, 0xBB, 0x99, 0xE4, 0x12, | ||
0xE6, 0x8B, 0x0F, 0xE8, 0x54, 0x4E, 0x72, 0x83, | ||
0x7D, 0xFA, 0x30, 0x74, 0x6D, 0x8B, 0xE2, 0xAA, | ||
0x65, 0x97, 0x5F, 0x29, 0xD2, 0x2D, 0xC7, 0xB9 | ||
], | ||
msg: [ | ||
0x4D, 0xF3, 0xC3, 0xF6, 0x8F, 0xCC, 0x83, 0xB2, | ||
0x7E, 0x9D, 0x42, 0xC9, 0x04, 0x31, 0xA7, 0x24, | ||
0x99, 0xF1, 0x78, 0x75, 0xC8, 0x1A, 0x59, 0x9B, | ||
0x56, 0x6C, 0x98, 0x89, 0xB9, 0x69, 0x67, 0x03 | ||
], | ||
sig: [ | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x3B, 0x78, 0xCE, 0x56, 0x3F, | ||
0x89, 0xA0, 0xED, 0x94, 0x14, 0xF5, 0xAA, 0x28, | ||
0xAD, 0x0D, 0x96, 0xD6, 0x79, 0x5F, 0x9C, 0x63, | ||
0x76, 0xAF, 0xB1, 0x54, 0x8A, 0xF6, 0x03, 0xB3, | ||
0xEB, 0x45, 0xC9, 0xF8, 0x20, 0x7D, 0xEE, 0x10, | ||
0x60, 0xCB, 0x71, 0xC0, 0x4E, 0x80, 0xF5, 0x93, | ||
0x06, 0x0B, 0x07, 0xD2, 0x83, 0x08, 0xD7, 0xF4 | ||
] | ||
} | ||
] | ||
|
||
module.exports = (t, secp256k1) => { | ||
t.test('schnorrSign', (t) => { | ||
t.test('arg: invalid message', (t) => { | ||
t.throws(() => { | ||
secp256k1.schnorrSign(null) | ||
}, /^Error: Expected message to be an Uint8Array$/, 'should be be an Uint8Array') | ||
|
||
t.throws(() => { | ||
const message = util.getMessage().slice(1) | ||
secp256k1.schnorrSign(message) | ||
}, /^Error: Expected message to be an Uint8Array with length 32$/, 'should have length 32') | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('arg: invalid private key', (t) => { | ||
t.throws(() => { | ||
const message = util.getMessage() | ||
secp256k1.schnorrSign(message, null) | ||
}, /^Error: Expected private key to be an Uint8Array$/, 'should be be an Uint8Array') | ||
|
||
t.throws(() => { | ||
const message = util.getMessage() | ||
const privateKey = util.getPrivateKey().slice(1) | ||
secp256k1.schnorrSign(message, privateKey) | ||
}, /^Error: Expected private key to be an Uint8Array with length 32$/, 'should have length 32') | ||
|
||
t.throws(() => { | ||
const message = util.getMessage() | ||
const privateKey = new Uint8Array(32) | ||
secp256k1.schnorrSign(message, privateKey) | ||
}, /^Error: The nonce generation function failed, or the private key was invalid$/, 'should throw on zero private key') | ||
|
||
t.throws(() => { | ||
const message = util.getMessage() | ||
const privateKey = util.ec.n.toArrayLike(Buffer, 'be', 32) | ||
secp256k1.schnorrSign(message, privateKey) | ||
}, /^Error: The nonce generation function failed, or the private key was invalid$/, 'should throw on overflowed private key: equal to N') | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('arg: invalid output', (t) => { | ||
const message = util.getMessage() | ||
const privateKey = util.getPrivateKey() | ||
|
||
t.throws(() => { | ||
secp256k1.schnorrSign(message, privateKey, null) | ||
}, /^Error: Expected output to be an Uint8Array$/, 'should be an Uint8Array') | ||
|
||
t.throws(() => { | ||
secp256k1.schnorrSign(message, privateKey, new Uint8Array(42)) | ||
}, /^Error: Expected output to be an Uint8Array with length 64$/, 'should have length 64') | ||
|
||
secp256k1.schnorrSign(message, privateKey, (len) => { | ||
t.same(len, 64, 'should ask Uint8Array with length 64') | ||
return new Uint8Array(len) | ||
}) | ||
|
||
t.plan(3) | ||
t.end() | ||
}) | ||
|
||
t.test('should sign and verify', (t) => { | ||
const message = util.getMessage() | ||
const privateKey = util.getPrivateKey() | ||
const publicKey = util.getPublicKey(privateKey).compressed | ||
|
||
const { signature } = secp256k1.schnorrSign(message, privateKey, (len) => { | ||
return new Uint8Array(len) | ||
}) | ||
|
||
const verified = secp256k1.schnorrVerify(signature, message, publicKey) | ||
t.same(verified, true, 'verify own signature') | ||
t.end() | ||
}) | ||
|
||
t.test('should verify testvectors', (t) => { | ||
testVectors.forEach((tv) => { | ||
const publicKey = Buffer.from(tv.pk) | ||
const message = Buffer.from(tv.msg) | ||
const signature = Buffer.from(tv.sig) | ||
const verified = secp256k1.schnorrVerify(signature, message, publicKey) | ||
t.same(verified, true, 'verify own signature') | ||
}) | ||
t.end() | ||
}) | ||
|
||
t.end() | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This will allow an odd-Y DER pubkey to verify true against a schnorr signature when technically xonly pubkeys are always assumed to be even. Currently this is achieved by throwing away the parity bit in the code.
While this is pretty understandable for signing (the C library will automatically negate the private key if Y is odd) verification should be stricter IMO.
I am asking the library "Does this pubkey verify the signature X" and if the Y is odd, we're flipping the pubkey to a different pubkey before returning true... we would need to widen the definition of what "pubkey" means in the context of this function alone to mean "Either the given pubkey or its inverse"
To reduce confusion on this point, I think only the xonly (32 length) pubkey should be accepted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could check the DER pubkeys for evenness and return false/throw. (check first byte is 0x02 for 33 length, check that pubkey[64] & 1 === 0 for 65 length)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense - test vector 3 requires negation of the seckey therefore I assumed it was fine.
https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.py#L73-L85
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That outputs a 32 byte xonly pubkey hex to the csv.
Like I said, automatically flipping to even when signing is fine. It's during verification that you need to be more strict.