Skip to content

Commit f813464

Browse files
fix(point): handle point-at-infinity encoding per SEC1 (TOB-19)
1 parent 2285482 commit f813464

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/primitives/Point.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ export default class Point extends BasePoint {
445445
* const encodedPointHex = aPoint.encode(true, 'hex');
446446
*/
447447
encode (compact: boolean = true, enc?: 'hex'): number[] | string {
448+
if (this.inf === true) {
449+
if (enc === 'hex') return '00'
450+
return [0x00]
451+
}
448452
const len = this.curve.p.byteLength()
449453
const x = this.getX().toArray('be', len)
450454
let res: number[]

src/primitives/__tests/utils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
toBase58Check,
1212
verifyNotNull
1313
} from '../../primitives/utils'
14+
import Point from '../../primitives/Point'
1415

1516
describe('utils', () => {
1617
it('should convert to array', () => {
@@ -359,3 +360,19 @@ describe('toUTF8 strict UTF-8 decoding (TOB-21)', () => {
359360

360361
})
361362

363+
describe('Point.encode infinity handling', () => {
364+
it('encodes infinity as 00 (array)', () => {
365+
const p = new Point(null, null)
366+
expect(p.encode()).toEqual([0x00])
367+
})
368+
369+
it('encodes infinity as 00 (hex)', () => {
370+
const p = new Point(null, null)
371+
expect(p.encode(true, 'hex')).toBe('00')
372+
})
373+
374+
it('does not throw for infinity', () => {
375+
const p = new Point(null, null)
376+
expect(() => p.encode()).not.toThrow()
377+
})
378+
})

0 commit comments

Comments
 (0)