Skip to content

Commit 740a7ea

Browse files
committed
Using @types/sjcl types.
1 parent 33b1051 commit 740a7ea

9 files changed

+43
-60
lines changed

eslint.config.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default eslintTS.config(
1414
'jest.config.mjs',
1515
'eslint.config.mjs',
1616
'coverage/*',
17-
'src/groupSjcl.ts',
1817
'src/sjcl/index.js',
1918
'src/sjcl/index.d.ts',
2019
'lib/*'

package-lock.json

-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"@eslint/js": "9.11.1",
4444
"@types/benchmark": "2.1.5",
4545
"@types/jest": "29.5.13",
46-
"@types/sjcl": "1.0.34",
4746
"benchmark": "2.1.4",
4847
"eslint": "9.11.1",
4948
"eslint-config-prettier": "9.1.0",

sjcl.Makefile

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44
# Run this file from the root of the project.
55
# $ make -f sjcl.Makefile
66

7-
SJCL_OUTPUT_PATH=src/sjcl
8-
SJCL_SRC_PATH=node_modules/sjcl
7+
SJCL_OUTPUT_PATH=$(CURDIR)/src/sjcl
98

10-
all:
11-
cd ${SJCL_SRC_PATH} && \
9+
.ONESHELL:
10+
11+
all: ${SJCL_OUTPUT_PATH}/index.js ${SJCL_OUTPUT_PATH}/index.d.ts
12+
13+
${SJCL_OUTPUT_PATH}/index.js:
14+
cd node_modules/sjcl
1215
./configure --without-all --with-bn --with-convenience --compress=none \
13-
--with-codecBytes --with-codecHex --with-codecArrayBuffer && \
16+
--with-codecBytes --with-codecHex --with-codecArrayBuffer
1417
make
15-
npm i -D dts-gen
16-
npx dts-gen -m sjcl -o -f ${SJCL_OUTPUT_PATH}/index
17-
npm un -D dts-gen
18-
echo "export default sjcl;" >> ${SJCL_SRC_PATH}/sjcl.js
19-
cp ${SJCL_SRC_PATH}/sjcl.js ${SJCL_OUTPUT_PATH}/index.js
18+
cp sjcl.js $@
19+
echo "export default sjcl;" >> $@
20+
21+
${SJCL_OUTPUT_PATH}/index.d.ts:
22+
npm i -D @types/sjcl
23+
cp node_modules/@types/sjcl/index.d.ts $@
24+
npm un -D @types/sjcl
2025

2126
clean:
22-
rm -f ${SJCL_OUTPUT_PATH}/index.js ${SJCL_OUTPUT_PATH}/index.d.ts
27+
rm -f ${SJCL_OUTPUT_PATH}/index.d.ts ${SJCL_OUTPUT_PATH}/index.js

src/sjcl/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"sourceMap": false,
88
"allowJs": true
99
},
10-
"files": [
11-
"index.js"
10+
"include": [
11+
"."
1212
]
13-
}
13+
}

src/util.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ export function int_to_bytes(num: number, byteLength: number): Uint8Array {
4747

4848
export function joinAll(a: Uint8Array[]): Uint8Array {
4949
let size = 0;
50-
for (let i = 0; i < a.length; i++) {
51-
size += a[i].length;
50+
for (const ai of a) {
51+
size += ai.length;
5252
}
5353
const ret = new Uint8Array(new ArrayBuffer(size));
54-
for (let i = 0, offset = 0; i < a.length; i++) {
55-
ret.set(a[i], offset);
56-
offset += a[i].length;
54+
let offset = 0;
55+
for (const ai of a) {
56+
ret.set(ai, offset);
57+
offset += ai.length;
5758
}
5859
return ret;
5960
}
@@ -62,12 +63,9 @@ export function xor(a: Uint8Array, b: Uint8Array): Uint8Array {
6263
if (a.length !== b.length || a.length === 0) {
6364
throw new Error(`arrays of different length: ${a.length} - ${b.length}`);
6465
}
65-
const n = a.length;
66-
const c = new Uint8Array(n);
67-
for (let i = 0; i < n; i++) {
68-
c[i] = a[i] ^ b[i];
69-
}
70-
return c;
66+
const ai: IterableIterator<number> = a[Symbol.iterator]();
67+
const bi: IterableIterator<number> = b[Symbol.iterator]();
68+
return new Uint8Array(a.length).map(() => ai.next().value ^ bi.next().value);
7169
}
7270

7371
function incCounter(c: Uint8Array): void {

test/blindrsa.test.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Copyright (c) 2023 Cloudflare, Inc.
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33

4-
import sjcl from '../src/sjcl/index.js';
5-
import type { BigNumber } from 'sjcl';
64
import { jest } from '@jest/globals';
5+
import sjcl from '../src/sjcl/index.js';
76

87
import { i2osp } from '../src/util.js';
98
import { BlindRSA, RSABSSA, getSuiteByName } from '../src/index.js';
@@ -32,10 +31,10 @@ function paramsFromVector(v: Vector): {
3231
const q = hexNumToB64URL(v.q);
3332

3433
// Calculate CRT values
35-
const bnD: BigNumber = new sjcl.bn(v.d);
36-
const bnP: BigNumber = new sjcl.bn(v.p);
37-
const bnQ: BigNumber = new sjcl.bn(v.q);
38-
const one: BigNumber = new sjcl.bn(1);
34+
const bnD = new sjcl.bn(v.d);
35+
const bnP = new sjcl.bn(v.p);
36+
const bnQ = new sjcl.bn(v.q);
37+
const one = new sjcl.bn(1);
3938
const dp = hexNumToB64URL(bnD.mod(bnP.sub(one)).toString());
4039
const dq = hexNumToB64URL(bnD.mod(bnQ.sub(one)).toString());
4140
const qi = hexNumToB64URL(bnQ.inverseMod(bnP).toString());
@@ -120,9 +119,9 @@ describe.each(vectors)('Errors-vec$#', (v: Vector) => {
120119

121120
describe.each(vectors)('TestVectors', (v: Vector) => {
122121
beforeEach(() => {
123-
const n: BigNumber = new sjcl.bn(v.n);
122+
const n = new sjcl.bn(v.n);
124123
const kLen = Math.ceil(n.bitLength() / 8);
125-
const rInv: BigNumber = new sjcl.bn(v.inv);
124+
const rInv = new sjcl.bn(v.inv);
126125
const r = rInv.inverseMod(n);
127126
const rBytes = i2osp(r, kLen);
128127

test/partially_blindrsa.test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33

44
import sjcl from '../src/sjcl/index.js';
5-
import type { BigNumber } from 'sjcl';
65
import { jest } from '@jest/globals';
76

87
import { i2osp } from '../src/util.js';
@@ -33,10 +32,10 @@ function paramsFromVector(v: Vector): {
3332
const q = hexNumToB64URL(v.q);
3433

3534
// Calculate CRT values
36-
const bnD: BigNumber = new sjcl.bn(v.d);
37-
const bnP: BigNumber = new sjcl.bn(v.p);
38-
const bnQ: BigNumber = new sjcl.bn(v.q);
39-
const one: BigNumber = new sjcl.bn(1);
35+
const bnD = new sjcl.bn(v.d);
36+
const bnP = new sjcl.bn(v.p);
37+
const bnQ = new sjcl.bn(v.q);
38+
const one = new sjcl.bn(1);
4039
const dp = hexNumToB64URL(bnD.mod(bnP.sub(one)).toString());
4140
const dq = hexNumToB64URL(bnD.mod(bnQ.sub(one)).toString());
4241
const qi = hexNumToB64URL(bnQ.inverseMod(bnP).toString());

test/util.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
1-
import type { SjclCodec } from 'sjcl';
2-
import sjcl from '../src/sjcl';
1+
import sjcl from '../src/sjcl/index.js';
32

43
export function hexToUint8(x: string): Uint8Array {
54
if (x.startsWith('0x')) {
65
x = x.slice(2);
76
}
8-
const hexCodec: SjclCodec<string> = sjcl.codec.hex;
9-
const bytesCodec: SjclCodec<number[]> = sjcl.codec.bytes;
10-
return new Uint8Array(bytesCodec.fromBits(hexCodec.toBits(x)));
7+
return new Uint8Array(sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(x)));
118
}
129

1310
export function uint8ToHex(x: Uint8Array): string {
14-
const hexCodec: SjclCodec<string> = sjcl.codec.hex;
15-
const bytesCodec: SjclCodec<number[]> = sjcl.codec.bytes;
16-
return hexCodec.fromBits(bytesCodec.toBits(Array.from(x)));
11+
return sjcl.codec.hex.fromBits(sjcl.codec.bytes.toBits(Array.from(x)));
1712
}
1813

1914
export function hexNumToB64URL(x: string): string {
2015
if (x.startsWith('0x')) {
2116
x = x.slice(2);
2217
}
23-
24-
const hexCodec: SjclCodec<string> = sjcl.codec.hex;
25-
const b64Codec: SjclCodec<string> = sjcl.codec.base64url;
26-
return b64Codec.fromBits(hexCodec.toBits(x));
18+
return sjcl.codec.base64url.fromBits(sjcl.codec.hex.toBits(x));
2719
}

0 commit comments

Comments
 (0)