-
Notifications
You must be signed in to change notification settings - Fork 305
feat(account-lib): add support for secp256k1 curve TSS #2256
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
Merged
tylerlevine
merged 1 commit into
master
from
BG-47402-add-support-for-secp-256-k-1-curve
May 20, 2022
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import curve from './baseCurve'; | ||
| import { Ed25519Curve } from './ed25519'; | ||
| import { Secp256k1Curve } from './secp256k1'; | ||
|
|
||
| export default curve; | ||
| export { Ed25519Curve }; | ||
| export { Ed25519Curve, Secp256k1Curve }; |
This file contains hidden or 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,55 @@ | ||
| import { bigIntFromU8ABE, bigIntToBufferBE } from '../util'; | ||
| import BaseCurve from './baseCurve'; | ||
| import * as secp from '@noble/secp256k1'; | ||
|
|
||
| const order = secp.CURVE.n; | ||
|
|
||
| export class Secp256k1Curve implements BaseCurve { | ||
| scalarRandom(): bigint { | ||
| return bigIntFromU8ABE(secp.utils.randomPrivateKey()); | ||
| } | ||
|
|
||
| scalarAdd(x: bigint, y: bigint): bigint { | ||
| return bigIntFromU8ABE(secp.utils.privateAdd(x, bigIntToBufferBE(y))); | ||
| } | ||
|
|
||
| scalarSub(x: bigint, y: bigint): bigint { | ||
|
sachushajiabraham marked this conversation as resolved.
Outdated
|
||
| const negatedY = secp.utils.privateNegate(y); | ||
| return bigIntFromU8ABE(secp.utils.privateAdd(x, negatedY)); | ||
| } | ||
|
|
||
| scalarMult(x: bigint, y: bigint): bigint { | ||
| return secp.utils.mod(x * y, order); | ||
| } | ||
|
|
||
| scalarReduce(s: bigint): bigint { | ||
| return secp.utils.mod(s, order); | ||
| } | ||
|
|
||
| scalarNegate(s: bigint): bigint { | ||
| return bigIntFromU8ABE(secp.utils.privateNegate(s)); | ||
| } | ||
|
|
||
| scalarInvert(s: bigint): bigint { | ||
| return secp.utils.invert(s, order); | ||
| } | ||
|
|
||
| pointAdd(a: bigint, b: bigint): bigint { | ||
| const pointA = secp.Point.fromHex(bigIntToBufferBE(a)); | ||
| const pointB = secp.Point.fromHex(bigIntToBufferBE(b)); | ||
| return bigIntFromU8ABE(pointA.add(pointB).toRawBytes(true)); | ||
| } | ||
|
|
||
| basePointMult(n: bigint): bigint { | ||
| const point = bigIntToBufferBE(n); | ||
| return bigIntFromU8ABE(secp.getPublicKey(point, true)); | ||
| } | ||
|
|
||
| verify(message: Buffer, signature: Buffer, publicKey: bigint): boolean { | ||
| return secp.verify(signature, message, bigIntToBufferBE(publicKey)); | ||
| } | ||
|
|
||
| order(): bigint { | ||
| return order; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| /** | ||
| * @prettier | ||
| */ | ||
| import { Secp256k1Curve } from '../../../../src/mpc/curves'; | ||
| import { bigIntToBufferBE } from '../../../../src/mpc/util'; | ||
|
|
||
| describe('secp256k1 curve implementation', function () { | ||
| const sec256k1 = new Secp256k1Curve(); | ||
| const testValue = BigInt(12000); | ||
|
|
||
| it('should properly generate a random number in the specified range', function () { | ||
| const start = BigInt(1); | ||
| const stop = sec256k1.order(); | ||
| const test = sec256k1.scalarRandom(); | ||
| const isOkay = test >= start && test <= stop; | ||
| isOkay.should.equal(true); | ||
| }); | ||
|
|
||
| it('should correctly perform scalar add', function () { | ||
| const valueOne = sec256k1.scalarAdd(sec256k1.order() - BigInt(1), testValue); | ||
| valueOne.should.equal(testValue - BigInt(1)); | ||
| const valueTwo = sec256k1.scalarAdd(BigInt(5), testValue); | ||
| valueTwo.should.equal(BigInt(5) + testValue); | ||
| }); | ||
|
|
||
| it('should correctly perform scalar sub', function () { | ||
| const valueOne = sec256k1.scalarSub(BigInt(2) * testValue, testValue); | ||
| valueOne.should.equal(testValue); | ||
| const valueTwo = sec256k1.scalarSub(BigInt(5), testValue); | ||
| valueTwo.should.equal(sec256k1.order() + (BigInt(5) - testValue)); | ||
| }); | ||
|
|
||
| it('should correctly perform scalar multiplication', function () { | ||
| const valueOne = sec256k1.scalarMult(sec256k1.order(), testValue); | ||
| valueOne.should.equal(BigInt(0)); | ||
| const valueTwo = sec256k1.scalarMult(BigInt(5), testValue); | ||
| valueTwo.should.equal(BigInt(5) * testValue); | ||
| }); | ||
|
|
||
| it('should correctly perform scalar reduce', function () { | ||
| const valueOne = sec256k1.scalarReduce(sec256k1.order() + testValue); | ||
| valueOne.should.equal(testValue); | ||
| const valueTwo = sec256k1.scalarReduce(sec256k1.order() - testValue); | ||
| valueTwo.should.equal(sec256k1.order() - testValue); | ||
| }); | ||
|
|
||
| it('should correctly perform scalar negate', function () { | ||
| const valueOne = sec256k1.scalarNegate(testValue); | ||
| valueOne.should.equal(sec256k1.order() - testValue); | ||
| const valueTwo = sec256k1.scalarNegate(sec256k1.order() - testValue); | ||
| valueTwo.should.equal(testValue); | ||
| }); | ||
|
|
||
| it('should correctly perform inverts', function () { | ||
| // Had to convert bigint to string as comparison was not properly working in mocha | ||
| const testOne = sec256k1.scalarInvert(BigInt(3)).toString(); | ||
| testOne.should.equal('77194726158210796949047323339125271901891709519383269588403442094345440996225'); | ||
|
|
||
| const testTwo = sec256k1.scalarInvert(sec256k1.order() + BigInt(1)).toString(); | ||
| testTwo.should.equal('1'); | ||
|
|
||
| const testThree = sec256k1 | ||
| .scalarInvert(BigInt('57657865876576467547584635432343132435146576543543134312435443514313234131324551034534')) | ||
| .toString(); | ||
| testThree.should.equal('45199239020920791752653273479365580486664737121914751844510223387235717825519'); | ||
|
|
||
| const testFour = sec256k1.scalarInvert(BigInt('-23232424342224')).toString(); | ||
| testFour.should.equal('109007816100548644677753129517258866095932985532097924870123596204155595187610'); | ||
|
|
||
| const testFive = sec256k1.scalarInvert(testValue); | ||
| testFive.toString().should.equal('16664411509403755791375590925833668071820872792496863322396593062116822075060'); | ||
| // Checking invert(invert(x)) === x | ||
| sec256k1.scalarInvert(testFive).should.equal(testValue); | ||
| }); | ||
|
|
||
| it('should correctly perform point add', function () { | ||
| const A = BigInt('0x0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'); | ||
| const B = BigInt('0x0252972572d465d016d4c501887b8df303eee3ed602c056b1eb09260dfa0da0ab2'); | ||
| const publicKey = '0x0' + sec256k1.pointAdd(A, B).toString(16); | ||
| publicKey.should.equal('0x03e2d3ec45106a89f82e7d46400faefc191ed72344caa781bfa97677a999d0828e'); | ||
| }); | ||
|
|
||
| it('should correctly perform base point multiplication', function () { | ||
| const privKey = BigInt('0x79FE45D61339181238E49424E905446A35497A8ADEA8B7D5241A1E7F2C95A04D'); | ||
| const publicKey = '0x0' + sec256k1.basePointMult(privKey).toString(16); | ||
| publicKey.should.equal('0x032a574ea59cae80b09d6ba415746e9b031abfbe83f149b43b37be035b87164872'); | ||
| }); | ||
|
|
||
| it('should correctly verify signature', function () { | ||
| const publicKey = BigInt('0x33ccfc7998b1ce2fad8b3f287d873f07ca8b3037897c2136c11100c02213c028e'); | ||
| const signature = bigIntToBufferBE( | ||
| BigInt( | ||
| '0x6a43d5da66ec4b4c6eb307d18791b9744cc59dd2402ad395bb9efb513898c07f767139118b76e387564bf572b2cc481dbf3068b7ac27620f83a2819d735b7e5f', | ||
| ), | ||
| ); | ||
| const message = bigIntToBufferBE(BigInt('0xbe5548911159dca31c02102c9df8c842adb50ac6def360c343ee5586f2749c0c')); | ||
| let verify = sec256k1.verify(message, signature, publicKey); | ||
| verify.should.equal(true); | ||
| const messageWrong = bigIntToBufferBE(BigInt('0xd3b91b70417e766b738288cb1c5f6ef13a607011134544d4bce2ebf6ed5944ef')); | ||
| verify = sec256k1.verify(messageWrong, signature, publicKey); | ||
| verify.should.equal(false); | ||
| }); | ||
|
|
||
| it('should have the correct curve order', function () { | ||
| const curveOrder = sec256k1.order().toString(16).toUpperCase(); | ||
| curveOrder.should.equal('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); | ||
| }); | ||
| }); |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.