Skip to content

fix(schema): preserve precision when serializing integers - #190

Merged
donskov merged 3 commits into
masterfrom
fix/schema-integer-precision
Nov 16, 2026
Merged

donskov merged 3 commits into
masterfrom
fix/schema-integer-precision

Conversation

@microshine

Copy link
Copy Markdown
Contributor

Summary

Preserve the exact value of decimal strings when serializing ASN.1 INTEGERs. Previously, converting a parsed integer string to a JavaScript number rounded values outside the safe integer range, changing the encoded DER on a round trip.

Use asn1js.Integer.fromBigInt for strings while keeping the existing number serialization path. Add DER and round-trip coverage for signed values, safe-integer boundaries, and integers larger than 64 bits.

Closes #189

Validation

  • npm run lint
  • npm run format:check
  • npm test (462 passed, 1 skipped)
  • npm run coverage
  • npm run build (with a temporary npm cache)
  • npm run typecheck

@coveralls

coveralls commented Nov 15, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 43896967037

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.2%) to 74.432%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 7 of 7 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 3491
Covered Lines: 2690
Line Coverage: 77.06%
Relevant Branches: 909
Covered Branches: 585
Branch Coverage: 64.36%
Branches in Coverage %: Yes
Coverage Strength: 185.63 hits per line

💛 - Coveralls

@microshine microshine self-assigned this Nov 15, 2026
@microshine microshine added the bug Something isn't working label Nov 15, 2026
@microshine
microshine requested a review from donskov November 15, 2026 09:51
Comment thread packages/schema/src/converters.ts Resolved
Comment thread packages/schema/src/converters.ts Resolved
Comment thread packages/schema/src/converters.ts Resolved
Comment thread packages/schema/src/converters.ts Resolved
Comment thread packages/schema/src/converters.spec.ts Resolved
@microshine
microshine requested a review from donskov November 16, 2026 11:33

@donskov donskov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked each comment below by running the old (new asn1js.Integer({ value: +value })) and new toASN against asn1js 4.0.0.

Comment thread packages/schema/src/converters.ts Outdated
return asn1js.Integer.fromBigInt(value);
} catch (error) {
// Preserve legacy numeric syntax only when it produces a safe integer.
if (!(error instanceof SyntaxError) || !Number.isSafeInteger(+value)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression: exact integers above 2^53 written in exponent or decimal form used to encode and now throw.

input before after
"1e20" 056bc75e2d63100000 SyntaxError
"9007199254740992.0" 20000000000000 SyntaxError
"18446744073709551616.0" 010000000000000000 SyntaxError

BigInt() rejects these, and Number.isSafeInteger(+value) is false, so the error is rethrown. If this is intended, it's a breaking change (fix!: / BREAKING CHANGE:).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2979f77. Non-integer string forms now use the legacy conversion, restoring all three examples. Added exact DER regressions.

Comment thread packages/schema/src/converters.ts Outdated
if (typeof value === "string") {
try {
return asn1js.Integer.fromBigInt(value);
} catch (error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error type changed: invalid strings now throw SyntaxError. Before, they threw RangeError.

"invalid", "1.5", "1_000": before → RangeError: Integer value must be an integer. After → SyntaxError: Cannot convert ... to a BigInt. Numbers and undefined still throw RangeError, so the error type now depends on the input type, and existing instanceof RangeError handlers stop catching it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2979f77. Invalid numeric strings again go through the legacy constructor and throw RangeError; tests cover the reported inputs.

Comment thread packages/schema/src/converters.ts Outdated
toASN: (value: IntegerConverterType) => {
if (typeof value === "string") {
try {
return asn1js.Integer.fromBigInt(value);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returned object shape differs for strings of 4 bytes or more. fromBigInt builds the integer from valueHex, so the result is hex-only:

toASN("2147483647") → valueDec=0, isHexOnly=true
toASN(2147483647)   → valueDec=2147483647, isHexOnly=false

The DER is identical. This only affects callers that use the exported converter directly and read valueDec or toJSON().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2979f77. Exactly representable decimal strings retain the legacy constructor and metadata, verified with valueDec, isHexOnly, and toJSON() tests. Other decimal integers keep exact BigInt encoding.

? value.valueBlock.toString() // use string format
: value.valueBlock.valueDec, // use number format
toASN: (value: IntegerConverterType) => new asn1js.Integer({ value: +value }),
toASN: (value: IntegerConverterType) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: check the string format first instead of catching an exception. This avoids the regression and the error-type change above:

toASN: (value: IntegerConverterType) =>
  typeof value === "string" && /^\s*[+-]?\d+\s*$/.test(value)
    ? asn1js.Integer.fromBigInt(value)
    : new asn1js.Integer({ value: +value }),

Plain decimal strings get exact encoding. Anything else (1e20, 0x10, 1.0, invalid input) behaves exactly as it did before this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the format check in 2979f77, with an additional exact-value comparison to preserve legacy object metadata where possible. Other string forms retain their original conversion behavior; fromASN is unchanged.

Comment thread packages/schema/src/converters.spec.ts Outdated
const obj = new RepeatedTest();
delete obj.values[0];

expect(() => AsnConvert.serialize(obj)).toThrow();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bare toThrow() passes on any error. Assert the type, as the undefined test above does: toThrow(RangeError).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to toThrow(RangeError) in 2979f77.

@microshine
microshine requested a review from donskov November 16, 2026 15:32
@donskov
donskov merged commit 622ebcd into master Nov 16, 2026
2 checks passed
@donskov
donskov deleted the fix/schema-integer-precision branch November 16, 2026 15:34
@github-actions github-actions Bot mentioned this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsnIntegerConverter.toASN coerces strings with +value, rounding integers outside safe-integer range

3 participants