Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Mar 10, 2020
1 parent 589743f commit 2b29ed4
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 45 deletions.
10 changes: 7 additions & 3 deletions src/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ export function generateKeyPair(): KeyPair {
return { privateKey, publicKey };
}

export async function sign(privateKey: Buffer, msg: Buffer): Promise<Buffer> {
export async function sign(
privateKey: Buffer,
msg: Buffer,
noDER?: boolean
): Promise<Buffer> {
checkPrivateKey(privateKey);
checkMessage(msg);
return isNode()
? secp256k1Sign(msg, privateKey)
: ellipticSign(msg, privateKey);
? secp256k1Sign(msg, privateKey, noDER)
: ellipticSign(msg, privateKey, noDER);
}

export async function verify(
Expand Down
1 change: 1 addition & 0 deletions test/common/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export async function getTestMessageToSign(

export async function testSign(
privateKey: Buffer,
noDER?: boolean,
lib: eccryptoJS.IEccrypto = eccryptoJS
) {
const { str, msg } = await getTestMessageToSign(undefined, lib);
Expand Down
16 changes: 14 additions & 2 deletions test/ecdsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ describe('ECDSA', () => {
expect(keyPair).toBeTruthy();
});

it('should sign successfully', async () => {
it('should sign successfully with DER signatures', async () => {
const { sig } = await testSign(keyPair.privateKey);
console.log('ecdsa', 'DER', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should verify signature', async () => {
it('should verify DER signatures successfully', async () => {
const { sig, msg } = await testSign(keyPair.privateKey);
await eccryptoJS.verify(keyPair.publicKey, msg, sig);
});

it('should sign successfully with non-DER signatures', async () => {
const { sig } = await testSign(keyPair.privateKey, true);
console.log('ecdsa', 'RSV', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should verify non-DER signatures successfully', async () => {
const { sig, msg } = await testSign(keyPair.privateKey, true);
await eccryptoJS.verify(keyPair.publicKey, msg, sig);
});
});
96 changes: 96 additions & 0 deletions test/elliptic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as eccryptoJS from '../src';
import {
TEST_PRIVATE_KEY,
TEST_PUBLIC_KEY,
TEST_SHARED_KEY,
compare,
TEST_PUBLIC_KEY_COMPRESSED,
getTestMessageToSign,
} from './common';

const privateKey = Buffer.from(TEST_PRIVATE_KEY, 'hex');
const expectedPublicKey = Buffer.from(TEST_PUBLIC_KEY, 'hex');
const expectedPublicKeyLength = 65;
const expectedPublicKeyCompressed = Buffer.from(
TEST_PUBLIC_KEY_COMPRESSED,
'hex'
);
const expectedPublicKeyCompressedLength = 33;
const expectedSharedKey = Buffer.from(TEST_SHARED_KEY, 'hex');
const expectedSharedKeyLength = 32;

describe('Elliptic', () => {
it('should get public key successfully', () => {
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
expect(publicKey).toBeTruthy();
expect(compare(publicKey, expectedPublicKey)).toBeTruthy();
expect(publicKey.length === expectedPublicKeyLength).toBeTruthy();
});

it('should get public key compressed successfully', () => {
const publicKeyCompressed = eccryptoJS.ellipticGetPublicCompressed(
privateKey
);
expect(publicKeyCompressed).toBeTruthy();
expect(
compare(publicKeyCompressed, expectedPublicKeyCompressed)
).toBeTruthy();
expect(
publicKeyCompressed.length === expectedPublicKeyCompressedLength
).toBeTruthy();
});

it('should compress public key successfully', () => {
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
const publicKeyCompressed = eccryptoJS.ellipticCompress(publicKey);
expect(
compare(publicKeyCompressed, expectedPublicKeyCompressed)
).toBeTruthy();
});

it('should decompress public key successfully', () => {
const publicKeyCompressed = eccryptoJS.ellipticGetPublicCompressed(
privateKey
);
const publicKey = eccryptoJS.ellipticDecompress(publicKeyCompressed);
expect(compare(publicKey, expectedPublicKey)).toBeTruthy();
});

it('should derive shared key successfully', () => {
const sharedKey = eccryptoJS.ellipticDerive(
eccryptoJS.ellipticGetPublic(privateKey),
privateKey
);
expect(sharedKey).toBeTruthy();
expect(compare(sharedKey, expectedSharedKey)).toBeTruthy();
expect(sharedKey.length === expectedSharedKeyLength).toBeTruthy();
});

it('should sign successfully with DER signatures', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.ellipticSign(msg, privateKey);
console.log('elliptic', 'DER', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should verify DER signatures successfully', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.ellipticSign(msg, privateKey);
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
await eccryptoJS.ellipticVerify(publicKey, msg, sig);
});

it('should sign successfully with non-DER signatures', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.ellipticSign(msg, privateKey, true);
console.log('elliptic', 'RSV', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should verify non-DER signatures successfully', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.ellipticSign(msg, privateKey);
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
await eccryptoJS.ellipticVerify(publicKey, msg, sig);
});
});
61 changes: 21 additions & 40 deletions test/secp256k1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TEST_SHARED_KEY,
compare,
TEST_PUBLIC_KEY_COMPRESSED,
getTestMessageToSign,
} from './common';

const privateKey = Buffer.from(TEST_PRIVATE_KEY, 'hex');
Expand Down Expand Up @@ -64,52 +65,32 @@ describe('SECP256K1', () => {
expect(compare(sharedKey, expectedSharedKey)).toBeTruthy();
expect(sharedKey.length === expectedSharedKeyLength).toBeTruthy();
});
});

describe('Elliptic', () => {
it('should get public key successfully', () => {
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
expect(publicKey).toBeTruthy();
expect(compare(publicKey, expectedPublicKey)).toBeTruthy();
expect(publicKey.length === expectedPublicKeyLength).toBeTruthy();
});

it('should get public key compressed successfully', () => {
const publicKeyCompressed = eccryptoJS.ellipticGetPublicCompressed(
privateKey
);
expect(publicKeyCompressed).toBeTruthy();
expect(
compare(publicKeyCompressed, expectedPublicKeyCompressed)
).toBeTruthy();
expect(
publicKeyCompressed.length === expectedPublicKeyCompressedLength
).toBeTruthy();
it('should sign successfully with DER signatures', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.secp256k1Sign(msg, privateKey);
console.log('secp256k1', 'DER', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should compress public key successfully', () => {
const publicKey = eccryptoJS.ellipticGetPublic(privateKey);
const publicKeyCompressed = eccryptoJS.ellipticCompress(publicKey);
expect(
compare(publicKeyCompressed, expectedPublicKeyCompressed)
).toBeTruthy();
it('should verify DER signatures successfully', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.secp256k1Sign(msg, privateKey);
const publicKey = eccryptoJS.secp256k1GetPublic(privateKey);
await eccryptoJS.secp256k1Verify(publicKey, msg, sig);
});

it('should decompress public key successfully', () => {
const publicKeyCompressed = eccryptoJS.ellipticGetPublicCompressed(
privateKey
);
const publicKey = eccryptoJS.ellipticDecompress(publicKeyCompressed);
expect(compare(publicKey, expectedPublicKey)).toBeTruthy();
it('should sign successfully with non-DER signatures', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.secp256k1Sign(msg, privateKey, true);
console.log('secp256k1', 'RSV', sig.toString('hex'));
expect(sig).toBeTruthy();
});

it('should derive shared key successfully', () => {
const sharedKey = eccryptoJS.ellipticDerive(
eccryptoJS.ellipticGetPublic(privateKey),
privateKey
);
expect(sharedKey).toBeTruthy();
expect(compare(sharedKey, expectedSharedKey)).toBeTruthy();
expect(sharedKey.length === expectedSharedKeyLength).toBeTruthy();
it('should verify non-DER signatures successfully', async () => {
const { msg } = await getTestMessageToSign();
const sig = eccryptoJS.secp256k1Sign(msg, privateKey);
const publicKey = eccryptoJS.secp256k1GetPublic(privateKey);
await eccryptoJS.secp256k1Verify(publicKey, msg, sig);
});
});

0 comments on commit 2b29ed4

Please sign in to comment.