Skip to content

Commit

Permalink
refactor: remove publicKey parameter from sign
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The publicKey parameter was removed from the sign method
  • Loading branch information
Pavel Zarecky committed Jan 25, 2022
1 parent 546db13 commit 35e28a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const publicKey = Ed448.getPublicKey(privateKey);

The private key can be any random generated byte-array of length 57

### `sign(privateKey, publicKey, message, context?): number[]`
### `sign(privateKey, message, context?): number[]`

Calculate the EdDSA signature.
The result is represented as plain number array (length: 114). It can be converted using `Buffer.from`, or `Uint8Array.from`.
Expand Down
21 changes: 15 additions & 6 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,41 @@ describe("Ed448", () => {

const message = Array.from(Buffer.from("testing content"));

const signature = Ed448.sign(privateKey, publicKey, message);
const signature = Ed448.sign(privateKey, message);
expect(signature).toHaveLength(114);

expect(Ed448.verify(publicKey, message, signature)).toBe(true);
});

it("changed signature doesn't validate", () => {
const privateKey = random(57);
const publicKey = Ed448.getPublicKey(privateKey);
const message = Array.from(Buffer.from("testing content"));
const signature = Ed448.sign(privateKey, message);
signature[1] = signature[1] ? 0x00 : 0x01;

expect(Ed448.verify(publicKey, message, signature)).toBe(false);
});

it.each(VECTORS)("matches test vector from RFC: '%s'", (_, vector) => {
const privateKey = toArray(vector.privateKey);
const publicKey = Ed448.getPublicKey(privateKey);
expect(publicKey).toEqual(toArray(vector.publicKey));

const message = toArray(vector.message);
const context = vector.context ? toArray(vector.context) : null;
const signature = Ed448.sign(privateKey, publicKey, message, context);
const signature = Ed448.sign(privateKey, message, context);
expect(signature).toEqual(toArray(vector.signature));

expect(Ed448.verify(publicKey, message, signature, context)).toBe(true);
});

it("fails with missing input", () => {
const missing = null as unknown as number[];
const missing = (null as unknown) as number[];
expect(() => Ed448.getPublicKey(missing)).toThrow();

expect(() => Ed448.sign(missing, random(57), [])).toThrow();
expect(() => Ed448.sign(random(57), missing, [])).toThrow();
expect(() => Ed448.sign(random(57), random(57), missing)).toThrow();
expect(() => Ed448.sign(missing, [])).toThrow();
expect(() => Ed448.sign(random(57), missing)).toThrow();
});

it("fails with invalid size of input", () => {
Expand Down
17 changes: 4 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,7 @@ type Input = number[] | Uint8Array | Buffer;
export interface EdDSA {
getPublicKey(privateKey: Input): number[];

sign(
privateKey: Input,
publicKey: Input,
data: Input,
context?: null | Input,
): number[];
sign(privateKey: Input, data: Input, context?: null | Input): number[];

verify(
publicKey: Input,
Expand All @@ -525,15 +520,11 @@ class EdDSAImpl implements EdDSA {
return this.pure.keygen(Array.from(privateKey));
}

sign(
privateKey: Input,
publicKey: Input,
data: Input,
context: null | Input = null,
) {
sign(privateKey: Input, data: Input, context: null | Input = null) {
const publicKey = this.getPublicKey(privateKey);
return this.pure.sign(
Array.from(privateKey),
Array.from(publicKey),
publicKey,
Array.from(data),
context ? Array.from(context) : [],
this.pflag,
Expand Down

0 comments on commit 35e28a4

Please sign in to comment.