Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve point size calculation in toWebCryptoSignature method #69

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v4
Expand All @@ -25,21 +25,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v4
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install global dependencies
run: npm i yarn -g
cache: yarn

- name: Install dependencies
run: yarn
Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,26 @@
"shake"
],
"dependencies": {
"@peculiar/asn1-schema": "^2.3.8",
"@peculiar/asn1-schema": "^2.3.13",
"@peculiar/json-schema": "^1.1.12",
"asn1js": "^3.0.1",
"asn1js": "^3.0.5",
"pvtsutils": "^1.3.5",
"tslib": "^2.6.2"
"tslib": "^2.7.0"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "^20.12.12",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.5",
"@typescript-eslint/eslint-plugin": "^7.11.0",
"@typescript-eslint/parser": "^7.11.0",
"coveralls": "^3.1.1",
"eslint": "^8.57.0",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"rimraf": "^5.0.7",
"rollup": "^4.18.0",
"mocha": "^10.7.3",
"nyc": "^17.1.0",
"rimraf": "^6.0.1",
"rollup": "^4.24.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-typescript2": "^0.36.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.6.2"
},
"author": "PeculiarVentures",
"license": "MIT",
Expand Down Expand Up @@ -91,4 +90,4 @@
"test/**/*.ts"
]
}
}
}
11 changes: 10 additions & 1 deletion src/asn1/ec_signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ export class EcDsaSignature {
* @returns ECDSA signature in X9.62 signature format
*/
public toWebCryptoSignature(pointSize?: number): ArrayBuffer {
pointSize ??= Math.max(this.r.byteLength, this.s.byteLength) * 8;
if (!pointSize) {
const maxPointLength = Math.max(this.r.byteLength, this.s.byteLength);
if (maxPointLength <= 32) {
pointSize = 256;
} else if (maxPointLength <= 48) {
pointSize = 384;
} else {
pointSize = 521;
}
}

const signature = EcUtils.encodeSignature(this, pointSize);

Expand Down
36 changes: 34 additions & 2 deletions test/asn1/ec_signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ context("ASN1", () => {
asn1: "3081880242011e0ff3c825b1133ef2779bbffd05374b17eeeff37444108a4c480b881ba3f3f426c3344fb1173dcec305f3e49408965092f946e609dfb845efaaaa25a43c679b0c024201a1366cd7b11efe7a41418cf83156bfdac56bb6253fd018a23974fc182948f3a84d5241922f09b8a60c4366f58b2b86461886515bd79872bb55e9840c412db766da",
webCrypto: "011e0ff3c825b1133ef2779bbffd05374b17eeeff37444108a4c480b881ba3f3f426c3344fb1173dcec305f3e49408965092f946e609dfb845efaaaa25a43c679b0c01a1366cd7b11efe7a41418cf83156bfdac56bb6253fd018a23974fc182948f3a84d5241922f09b8a60c4366f58b2b86461886515bd79872bb55e9840c412db766da",
},
]
];

context("From WebCrypto to DER", () => {
vectors.forEach((vector) => {
Expand All @@ -66,6 +66,38 @@ context("ASN1", () => {
});
});

});
describe("toWebCryptoSignature", () => {
it("default point size should be 256", () => {
const signature = new EcDsaSignature();
signature.r = new ArrayBuffer(32);
signature.s = new ArrayBuffer(32);
const result = signature.toWebCryptoSignature();
assert.strictEqual(result.byteLength, 64);
});

it("default point size should be 384", () => {
const signature = new EcDsaSignature();
signature.r = new ArrayBuffer(48);
signature.s = new ArrayBuffer(48);
const result = signature.toWebCryptoSignature();
assert.strictEqual(result.byteLength, 96);
});

it("default point size should be 521", () => {
const signature = new EcDsaSignature();
signature.r = new ArrayBuffer(65);
signature.s = new ArrayBuffer(65);
const result = signature.toWebCryptoSignature();
assert.strictEqual(result.byteLength, 132);
});

it("should be given point size", () => {
const signature = new EcDsaSignature();
signature.r = new ArrayBuffer(32);
signature.s = new ArrayBuffer(32);
const result = signature.toWebCryptoSignature(384);
assert.strictEqual(result.byteLength, 96);
});
});
});
});
Loading