atproto/packages/crypto/tests/key-compression.test.ts
Daniel Holmgren 2e94c43010
Cleanup @atproto/crypto (#1218)
* remove webcrypto, upgrade @noble/curves, normalize p256 interface

* cleanup

* Test vectors for secp and p256 signature verification (#737)

Add test vectors for secp and p256 signature verification

* fix up test vectors

* add explicit test vectors for high-s signatures

* tidy json to pass verify check

---------

Co-authored-by: devin ivy <devinivy@gmail.com>
2023-06-26 18:47:28 -05:00

70 lines
2.4 KiB
TypeScript

import P256Keypair from '../src/p256/keypair'
import Secp256k1Keypair from '../src/secp256k1/keypair'
import * as secpEncoding from '../src/secp256k1/encoding'
import * as p256Encoding from '../src/p256/encoding'
import * as did from '../src/did'
describe('public key compression', () => {
describe('secp256k1', () => {
let keyBytes: Uint8Array
let compressed: Uint8Array
it('compresses a key to the correct length', async () => {
const keypair = await Secp256k1Keypair.create()
const parsed = did.parseDidKey(keypair.did())
keyBytes = parsed.keyBytes
compressed = secpEncoding.compressPubkey(keyBytes)
expect(compressed.length).toBe(33)
})
it('decompresses a key to the original', async () => {
const decompressed = secpEncoding.decompressPubkey(compressed)
expect(decompressed.length).toBe(65)
expect(decompressed).toEqual(keyBytes)
})
it('works consistently', async () => {
const pubkeys: Uint8Array[] = []
for (let i = 0; i < 100; i++) {
const key = await Secp256k1Keypair.create()
const parsed = did.parseDidKey(key.did())
pubkeys.push(parsed.keyBytes)
}
const compressed = pubkeys.map(secpEncoding.compressPubkey)
const decompressed = compressed.map(secpEncoding.decompressPubkey)
expect(pubkeys).toEqual(decompressed)
})
})
describe('P-256', () => {
let keyBytes: Uint8Array
let compressed: Uint8Array
it('compresses a key to the correct length', async () => {
const keypair = await P256Keypair.create()
const parsed = did.parseDidKey(keypair.did())
keyBytes = parsed.keyBytes
compressed = p256Encoding.compressPubkey(keyBytes)
expect(compressed.length).toBe(33)
})
it('decompresses a key to the original', async () => {
const decompressed = p256Encoding.decompressPubkey(compressed)
expect(decompressed.length).toBe(65)
expect(decompressed).toEqual(keyBytes)
})
it('works consistently', async () => {
const pubkeys: Uint8Array[] = []
for (let i = 0; i < 100; i++) {
const key = await P256Keypair.create()
const parsed = did.parseDidKey(key.did())
pubkeys.push(parsed.keyBytes)
}
const compressed = pubkeys.map(p256Encoding.compressPubkey)
const decompressed = compressed.map(p256Encoding.decompressPubkey)
expect(pubkeys).toEqual(decompressed)
})
})
})