Skip to content

Commit 923bd42

Browse files
committed
fixed encoding for large values
1 parent a6073e9 commit 923bd42

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format
55
## Table of Contents
66

77
- [Unreleased](#unreleased)
8+
- [1.9.26 - 2025-12-09](#1926---2025-12-09)
89
- [1.9.25 - 2025-12-09](#1925---2025-12-09)
910
- [1.9.24 - 2025-12-09](#1924---2025-12-09)
1011
- [1.9.23 - 2025-12-08](#1923---2025-12-08)
@@ -198,6 +199,13 @@ All notable changes to this project will be documented in this file. The format
198199

199200
---
200201

202+
## [1.9.26] - 2025-12-09
203+
204+
### Fixed
205+
- Clarified and corrected byte-order helper behavior when converting numeric values to byte arrays.
206+
207+
---
208+
201209
## [1.9.25] - 2025-12-09
202210

203211
### Added

docs/reference/primitives.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,24 +5299,24 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
52995299
53005300
| | | |
53015301
| --- | --- | --- |
5302-
| [BI_EIGHT](#variable-bi_eight) | [biModSqrt](#variable-bimodsqrt) | [multiply](#variable-multiply) |
5303-
| [BI_FOUR](#variable-bi_four) | [biModSub](#variable-bimodsub) | [rightShift](#variable-rightshift) |
5304-
| [BI_ONE](#variable-bi_one) | [checkBit](#variable-checkbit) | [ripemd160](#variable-ripemd160) |
5305-
| [BI_THREE](#variable-bi_three) | [encode](#variable-encode) | [scalarMultiplyWNAF](#variable-scalarmultiplywnaf) |
5306-
| [BI_TWO](#variable-bi_two) | [exclusiveOR](#variable-exclusiveor) | [sha1](#variable-sha1) |
5307-
| [BI_ZERO](#variable-bi_zero) | [fromBase58](#variable-frombase58) | [sha256](#variable-sha256) |
5308-
| [GX_BIGINT](#variable-gx_bigint) | [fromBase58Check](#variable-frombase58check) | [sha256hmac](#variable-sha256hmac) |
5309-
| [GY_BIGINT](#variable-gy_bigint) | [getBytes](#variable-getbytes) | [sha512](#variable-sha512) |
5310-
| [MASK_256](#variable-mask_256) | [hash160](#variable-hash160) | [sha512hmac](#variable-sha512hmac) |
5311-
| [N_BIGINT](#variable-n_bigint) | [hash256](#variable-hash256) | [sign](#variable-sign) |
5312-
| [P_BIGINT](#variable-p_bigint) | [incrementLeastSignificantThirtyTwoBits](#variable-incrementleastsignificantthirtytwobits) | [toArray](#variable-toarray) |
5313-
| [P_PLUS1_DIV4](#variable-p_plus1_div4) | [jpAdd](#variable-jpadd) | [toBase58](#variable-tobase58) |
5314-
| [biMod](#variable-bimod) | [jpDouble](#variable-jpdouble) | [toBase58Check](#variable-tobase58check) |
5315-
| [biModAdd](#variable-bimodadd) | [jpNeg](#variable-jpneg) | [toHex](#variable-tohex) |
5316-
| [biModInv](#variable-bimodinv) | [minimallyEncode](#variable-minimallyencode) | [toUTF8](#variable-toutf8) |
5317-
| [biModMul](#variable-bimodmul) | [modInvN](#variable-modinvn) | [verify](#variable-verify) |
5318-
| [biModPow](#variable-bimodpow) | [modMulN](#variable-modmuln) | [zero2](#variable-zero2) |
5319-
| [biModSqr](#variable-bimodsqr) | [modN](#variable-modn) | |
5302+
| [BI_EIGHT](#variable-bi_eight) | [biModSqrt](#variable-bimodsqrt) | [modN](#variable-modn) |
5303+
| [BI_FOUR](#variable-bi_four) | [biModSub](#variable-bimodsub) | [multiply](#variable-multiply) |
5304+
| [BI_ONE](#variable-bi_one) | [checkBit](#variable-checkbit) | [rightShift](#variable-rightshift) |
5305+
| [BI_THREE](#variable-bi_three) | [encode](#variable-encode) | [ripemd160](#variable-ripemd160) |
5306+
| [BI_TWO](#variable-bi_two) | [exclusiveOR](#variable-exclusiveor) | [scalarMultiplyWNAF](#variable-scalarmultiplywnaf) |
5307+
| [BI_ZERO](#variable-bi_zero) | [fromBase58](#variable-frombase58) | [sha1](#variable-sha1) |
5308+
| [GX_BIGINT](#variable-gx_bigint) | [fromBase58Check](#variable-frombase58check) | [sha256](#variable-sha256) |
5309+
| [GY_BIGINT](#variable-gy_bigint) | [getBytes](#variable-getbytes) | [sha256hmac](#variable-sha256hmac) |
5310+
| [MASK_256](#variable-mask_256) | [getBytes64](#variable-getbytes64) | [sha512](#variable-sha512) |
5311+
| [N_BIGINT](#variable-n_bigint) | [hash160](#variable-hash160) | [sha512hmac](#variable-sha512hmac) |
5312+
| [P_BIGINT](#variable-p_bigint) | [hash256](#variable-hash256) | [sign](#variable-sign) |
5313+
| [P_PLUS1_DIV4](#variable-p_plus1_div4) | [incrementLeastSignificantThirtyTwoBits](#variable-incrementleastsignificantthirtytwobits) | [toArray](#variable-toarray) |
5314+
| [biMod](#variable-bimod) | [jpAdd](#variable-jpadd) | [toBase58](#variable-tobase58) |
5315+
| [biModAdd](#variable-bimodadd) | [jpDouble](#variable-jpdouble) | [toBase58Check](#variable-tobase58check) |
5316+
| [biModInv](#variable-bimodinv) | [jpNeg](#variable-jpneg) | [toHex](#variable-tohex) |
5317+
| [biModMul](#variable-bimodmul) | [minimallyEncode](#variable-minimallyencode) | [toUTF8](#variable-toutf8) |
5318+
| [biModPow](#variable-bimodpow) | [modInvN](#variable-modinvn) | [verify](#variable-verify) |
5319+
| [biModSqr](#variable-bimodsqr) | [modMulN](#variable-modmuln) | [zero2](#variable-zero2) |
53205320
53215321
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
53225322
@@ -5673,6 +5673,31 @@ getBytes = function (numericValue: number): number[] {
56735673
56745674
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
56755675
5676+
---
5677+
### Variable: getBytes64
5678+
5679+
```ts
5680+
getBytes64 = function (numericValue: number): number[] {
5681+
if (numericValue < 0 || numericValue > Number.MAX_SAFE_INTEGER) {
5682+
throw new Error("getBytes64: value out of range");
5683+
}
5684+
const hi = Math.floor(numericValue / 4294967296);
5685+
const lo = numericValue >>> 0;
5686+
return [
5687+
(hi >>> 24) & 255,
5688+
(hi >>> 16) & 255,
5689+
(hi >>> 8) & 255,
5690+
hi & 255,
5691+
(lo >>> 24) & 255,
5692+
(lo >>> 16) & 255,
5693+
(lo >>> 8) & 255,
5694+
lo & 255
5695+
];
5696+
}
5697+
```
5698+
5699+
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5700+
56765701
---
56775702
### Variable: hash160
56785703

src/primitives/AESGCM.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ export const getBytes = function (numericValue: number): number[] {
202202
]
203203
}
204204

205+
export const getBytes64 = function (numericValue: number): number[] {
206+
if (numericValue < 0 || numericValue > Number.MAX_SAFE_INTEGER) {
207+
throw new Error('getBytes64: value out of range')
208+
}
209+
210+
const hi = Math.floor(numericValue / 0x100000000)
211+
const lo = numericValue >>> 0
212+
213+
return [
214+
(hi >>> 24) & 0xFF,
215+
(hi >>> 16) & 0xFF,
216+
(hi >>> 8) & 0xFF,
217+
hi & 0xFF,
218+
(lo >>> 24) & 0xFF,
219+
(lo >>> 16) & 0xFF,
220+
(lo >>> 8) & 0xFF,
221+
lo & 0xFF
222+
]
223+
}
224+
205225
const createZeroBlock = function (length: number): number[] {
206226
return new Array(length).fill(0)
207227
}
@@ -398,8 +418,10 @@ export function AESGCM (
398418

399419
preCounterBlock = preCounterBlock.concat(createZeroBlock(8))
400420

401-
preCounterBlock = ghash(preCounterBlock.concat(createZeroBlock(4))
402-
.concat(getBytes(initializationVector.length * 8)), hashSubKey)
421+
preCounterBlock = ghash(
422+
preCounterBlock.concat(getBytes64(initializationVector.length * 8)),
423+
hashSubKey
424+
)
403425
}
404426

405427
const cipherText = gctr(plainText, incrementLeastSignificantThirtyTwoBits(preCounterBlock), key)
@@ -413,9 +435,9 @@ export function AESGCM (
413435
plainTag = plainTag.concat(createZeroBlock(16 - (cipherText.length % 16)))
414436
}
415437

416-
plainTag = plainTag.concat(createZeroBlock(4))
417-
.concat(getBytes(0))
418-
.concat(createZeroBlock(4)).concat(getBytes(cipherText.length * 8))
438+
plainTag = plainTag
439+
.concat(getBytes64(0))
440+
.concat(getBytes64(cipherText.length * 8))
419441

420442
return {
421443
result: cipherText,
@@ -457,7 +479,10 @@ export function AESGCMDecrypt (
457479

458480
preCounterBlock = preCounterBlock.concat(createZeroBlock(8))
459481

460-
preCounterBlock = ghash(preCounterBlock.concat(createZeroBlock(4)).concat(getBytes(initializationVector.length * 8)), hashSubKey)
482+
preCounterBlock = ghash(
483+
preCounterBlock.concat(getBytes64(initializationVector.length * 8)),
484+
hashSubKey
485+
)
461486
}
462487

463488
// Decrypt to obtain the plain text
@@ -472,10 +497,9 @@ export function AESGCMDecrypt (
472497
compareTag = compareTag.concat(createZeroBlock(16 - (cipherText.length % 16)))
473498
}
474499

475-
compareTag = compareTag.concat(createZeroBlock(4))
476-
.concat(getBytes(0))
477-
.concat(createZeroBlock(4))
478-
.concat(getBytes(cipherText.length * 8))
500+
compareTag = compareTag
501+
.concat(getBytes64(0))
502+
.concat(getBytes64(cipherText.length * 8))
479503

480504
// Generate the authentication tag
481505
const calculatedTag = gctr(ghash(compareTag, hashSubKey), preCounterBlock, key)

0 commit comments

Comments
 (0)