Skip to content

Commit 37b9f69

Browse files
committed
Refactor PADMÉ padding functions and update tests
Renamed padmePaddedLength to createPadmePaddedLength and replaced padmePaddingLength with createPadmePadding, which returns a zero-filled Uint8Array.
1 parent d069152 commit 37b9f69

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

packages/common/src/Crypto.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,13 @@ export const createSymmetricCrypto = (
172172
* Returns the PADMÉ padded length for a given input length.
173173
*
174174
* PADMÉ limits information leakage about the length of the plain-text for a
175-
* wide range of encrypted data sizes. See the PURBs paper for details:
176-
* https://bford.info/pub/sec/purb.pdf
175+
* wide range of encrypted data sizes.
176+
*
177+
* See the PURBs paper for details: https://bford.info/pub/sec/purb.pdf
177178
*/
178-
export const padmePaddedLength = (length: NonNegativeInt): NonNegativeInt => {
179+
export const createPadmePaddedLength = (
180+
length: NonNegativeInt,
181+
): NonNegativeInt => {
179182
if (length <= 0) return NonNegativeInt.orThrow(0);
180183
const e = 31 - Math.clz32(length >>> 0);
181184
const s = 32 - Math.clz32(e >>> 0);
@@ -184,12 +187,11 @@ export const padmePaddedLength = (length: NonNegativeInt): NonNegativeInt => {
184187
return NonNegativeInt.orThrow((length + mask) & ~mask);
185188
};
186189

187-
/**
188-
* Returns the PADMÉ padding length for a given input length. Uses
189-
* {@link padmePaddedLength}.
190-
*/
191-
export const padmePaddingLength = (length: NonNegativeInt): NonNegativeInt => {
192-
return NonNegativeInt.orThrow(padmePaddedLength(length) - length);
190+
/** Creates a PADMÉ padding array of zeros for the given input length. */
191+
export const createPadmePadding = (length: NonNegativeInt): Uint8Array => {
192+
const paddedLength = createPadmePaddedLength(length);
193+
const paddingLength = NonNegativeInt.orThrow(paddedLength - length);
194+
return new globalThis.Uint8Array(paddingLength);
193195
};
194196

195197
/**

packages/common/test/Crypto.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { assert, expect, test } from "vitest";
33
import {
44
createSlip21,
55
createSymmetricCrypto,
6-
padmePaddedLength,
6+
createPadmePaddedLength,
7+
createPadmePadding,
78
} from "../src/Crypto.js";
89
import { mnemonicToOwnerSecret } from "../src/index.js";
910
import { ok } from "../src/Result.js";
@@ -34,7 +35,7 @@ test("SymmetricCrypto", () => {
3435
expect(result.error.type).toBe("SymmetricCryptoDecryptError");
3536
});
3637

37-
test("padmePaddedLength", () => {
38+
test("createPadmePaddedLength", () => {
3839
[
3940
[0, 0],
4041
[1, 1],
@@ -67,7 +68,10 @@ test("padmePaddedLength", () => {
6768
[100000, 100352],
6869
[1048576, 1048576],
6970
].forEach(([input, expected]) => {
70-
expect(padmePaddedLength(input as NonNegativeInt)).toBe(expected);
71+
expect(createPadmePaddedLength(input as NonNegativeInt)).toBe(expected);
72+
expect(createPadmePadding(input as NonNegativeInt).length).toBe(
73+
expected - input,
74+
);
7175
});
7276
});
7377

0 commit comments

Comments
 (0)