mirror of
https://github.com/tcsenpai/falconTS.git
synced 2025-06-03 02:00:08 +00:00
141 lines
4.9 KiB
TypeScript
141 lines
4.9 KiB
TypeScript
import Falcon from '../src/falcon';
|
|
|
|
describe('Falcon', () => {
|
|
let falcon: Falcon;
|
|
|
|
beforeEach(async () => {
|
|
falcon = new Falcon();
|
|
await falcon.init();
|
|
});
|
|
|
|
describe('Key Generation and Management', () => {
|
|
it('should initialize with default algorithm', async () => {
|
|
const algid = await falcon.getAlgid();
|
|
expect(algid).toBe('falcon512_n3_v1');
|
|
});
|
|
|
|
it('should generate a new keypair', async () => {
|
|
await falcon.genkey();
|
|
const publicKey = await falcon.getPublicKey();
|
|
const privateKey = await falcon.getPrivateKey();
|
|
|
|
expect(publicKey).toBeDefined();
|
|
expect(privateKey).toBeDefined();
|
|
expect(publicKey.length).toBeGreaterThan(0);
|
|
expect(privateKey.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should convert keys to and from hex format', async () => {
|
|
await falcon.genkey();
|
|
const publicKeyHex = await falcon.getPublicKeyHex();
|
|
const privateKeyHex = await falcon.getPrivateKeyHex();
|
|
|
|
expect(publicKeyHex).toMatch(/^[0-9a-f]+$/i);
|
|
expect(privateKeyHex).toMatch(/^[0-9a-f]+$/i);
|
|
});
|
|
|
|
it('should convert keys to and from base64 format', async () => {
|
|
await falcon.genkey();
|
|
const publicKeyBase64 = await falcon.getPublicKeyBase64();
|
|
const privateKeyBase64 = await falcon.getPrivateKeyBase64();
|
|
|
|
expect(publicKeyBase64).toMatch(/^[A-Za-z0-9+/=]+$/);
|
|
expect(privateKeyBase64).toMatch(/^[A-Za-z0-9+/=]+$/);
|
|
|
|
// Verify that base64 is shorter than hex
|
|
const publicKeyHex = await falcon.getPublicKeyHex();
|
|
const privateKeyHex = await falcon.getPrivateKeyHex();
|
|
|
|
expect(publicKeyBase64.length).toBeLessThan(publicKeyHex.length);
|
|
expect(privateKeyBase64.length).toBeLessThan(privateKeyHex.length);
|
|
});
|
|
});
|
|
|
|
describe('Signing and Verification', () => {
|
|
it('should sign and verify a message', async () => {
|
|
await falcon.genkey();
|
|
const message = 'Hello, Falcon!';
|
|
const signature = await falcon.sign(message);
|
|
|
|
expect(signature).toBeDefined();
|
|
expect(signature.length).toBeGreaterThan(0);
|
|
|
|
const isValid = await falcon.verify(message, signature);
|
|
expect(isValid).toBe(true);
|
|
});
|
|
|
|
it('should sign and verify using hex format', async () => {
|
|
await falcon.genkey();
|
|
const message = 'Hello, Falcon!';
|
|
const signatureHex = await falcon.signHex(message);
|
|
|
|
expect(signatureHex).toMatch(/^[0-9a-f]+$/i);
|
|
|
|
const isValid = await falcon.verifyHex(message, signatureHex);
|
|
expect(isValid).toBe(true);
|
|
});
|
|
|
|
it('should sign and verify using base64 format', async () => {
|
|
await falcon.genkey();
|
|
const message = 'Hello, Falcon!';
|
|
const signatureBase64 = await falcon.signBase64(message);
|
|
|
|
expect(signatureBase64).toMatch(/^[A-Za-z0-9+/=]+$/);
|
|
|
|
const isValid = await falcon.verifyBase64(message, signatureBase64);
|
|
expect(isValid).toBe(true);
|
|
|
|
// Verify that base64 is shorter than hex
|
|
const signatureHex = await falcon.signHex(message);
|
|
expect(signatureBase64.length).toBeLessThan(signatureHex.length);
|
|
});
|
|
|
|
it('should reject invalid signatures', async () => {
|
|
await falcon.genkey();
|
|
const message = 'Hello, Falcon!';
|
|
const signature = await falcon.sign(message);
|
|
|
|
// Modify the signature
|
|
const modifiedSignature = new Uint8Array(signature);
|
|
modifiedSignature[0] = modifiedSignature[0] ^ 1;
|
|
|
|
const isValid = await falcon.verify(message, modifiedSignature);
|
|
expect(isValid).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('Key Import and Export', () => {
|
|
it('should create public key from private key', async () => {
|
|
await falcon.genkey();
|
|
const privateKey = await falcon.getPrivateKey();
|
|
const publicKey = await falcon.publicKeyCreate(privateKey);
|
|
|
|
expect(publicKey).toBeDefined();
|
|
expect(publicKey.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should set private key from hex and derive public key', async () => {
|
|
await falcon.genkey();
|
|
const originalPrivateKeyHex = await falcon.getPrivateKeyHex();
|
|
const originalPublicKeyHex = await falcon.getPublicKeyHex();
|
|
|
|
const falcon2 = new Falcon();
|
|
await falcon2.init();
|
|
const derivedPublicKeyHex = await falcon2.setPrivateKeyHex(originalPrivateKeyHex);
|
|
|
|
expect(derivedPublicKeyHex).toBe(originalPublicKeyHex);
|
|
});
|
|
|
|
it('should set private key from base64 and derive public key', async () => {
|
|
await falcon.genkey();
|
|
const originalPrivateKeyBase64 = await falcon.getPrivateKeyBase64();
|
|
const originalPublicKeyBase64 = await falcon.getPublicKeyBase64();
|
|
|
|
const falcon2 = new Falcon();
|
|
await falcon2.init();
|
|
const derivedPublicKeyBase64 = await falcon2.setPrivateKeyBase64(originalPrivateKeyBase64);
|
|
|
|
expect(derivedPublicKeyBase64).toBe(originalPublicKeyBase64);
|
|
});
|
|
});
|
|
});
|