Skip to content

Commit

Permalink
Test all fns in ed25519Bip32
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBenc committed Mar 6, 2024
1 parent f2233e5 commit 68f5ade
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/curves/ed25519Bip32.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { bytesToHex, hexToBytes } from '@metamask/utils';

import { ed25519Bip32 } from '.';
import fixtures from '../../test/fixtures';
import {
bytesToNumberLE,
compressPublicKey,
decompressPublicKey,
isValidPrivateKey,
multiplyWithBase,
} from './ed25519Bip32';

describe('getPublicKey', () => {
fixtures.cip3.forEach((fixture) => {
Expand All @@ -28,3 +35,56 @@ describe('publicAdd', () => {
);
});
});

describe('isValidPrivateKey', () => {
it('returns true for bigint input', () => {
const { privateKey } = fixtures.cip3[0].nodes.bip39Node;
expect(isValidPrivateKey(privateKey)).toBe(true);
});
});

describe('compressPublicKey', () => {
it('returns the same Uint8Array that was input', () => {
const publicKey = Uint8Array.from(
Buffer.from(fixtures.cip3[0].nodes.bip39Node.publicKey, 'hex'),
);
expect(compressPublicKey(publicKey)).toStrictEqual(publicKey);
});
});

describe('decompressPublicKey', () => {
it('returns the same Uint8Array that was input', () => {
const publicKey = Uint8Array.from(
Buffer.from(fixtures.cip3[0].nodes.bip39Node.publicKey, 'hex'),
);
expect(decompressPublicKey(publicKey)).toStrictEqual(publicKey);
});
});

describe('bytesToNumberLE', () => {
it('converts bytes to little endian bignumber', () => {
const bytes = Uint8Array.from([
240, 230, 228, 13, 229, 184, 174, 13, 156, 72, 248, 206, 127, 130, 146,
49, 175, 244, 32, 215, 146, 255, 153, 93, 197, 96, 64, 249, 123, 140, 119,
72,
]);
expect(bytesToNumberLE(bytes)).toBe(
32777749485515042639882960539696351427945957558989008047469858024981459691248n,
);
});
});

describe('multiplyWithBase', () => {
it('multiplies bytes with the curve base', () => {
const bytes = Uint8Array.from([
240, 230, 228, 13, 229, 184, 174, 13, 156, 72, 248, 206, 127, 130, 146,
49, 175, 244, 32, 215, 146, 255, 153, 93, 197, 96, 64, 249, 123, 140, 119,
72,
]);
const expectedResult = Uint8Array.from([
64, 197, 223, 88, 143, 127, 45, 60, 205, 81, 148, 125, 195, 249, 173, 214,
27, 176, 227, 21, 216, 243, 146, 168, 189, 206, 85, 135, 89, 11, 210, 27,
]);
expect(multiplyWithBase(bytes)).toStrictEqual(expectedResult);
});
});
4 changes: 2 additions & 2 deletions src/curves/ed25519Bip32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const publicKeyLength = 32;
* @param bytes - The Uint8Array of bytes to convert.
* @returns The converted bigint value.
*/
const bytesToNumberLE = (bytes: Uint8Array): bigint => {
export const bytesToNumberLE = (bytes: Uint8Array): bigint => {
return hexToBigInt(bytesToHex(Uint8Array.from(bytes).reverse()));
};

Expand All @@ -53,7 +53,7 @@ const bytesToNumberLE = (bytes: Uint8Array): bigint => {
* @param key - The key to multiply with the base point.
* @returns The resulting point on the Edwards curve.
*/
const multiplyWithBase = (key: Uint8Array): Uint8Array => {
export const multiplyWithBase = (key: Uint8Array): Uint8Array => {
// Little-endian SHA512 with modulo n
const scalar = mod(bytesToNumberLE(key), curve.n); // The actual scalar
const point = ed25519.ExtendedPoint.BASE.multiply(scalar); // Point on Edwards curve aka public key
Expand Down

0 comments on commit 68f5ade

Please sign in to comment.