Skip to content

Commit

Permalink
feat: Allow any array-like input
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Zarecky committed Jan 25, 2022
1 parent 1f89970 commit 546db13
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const Ed448 = createEd448();
Calculate public key from a provided private key

```ts
const privateKey = Array.from(crypto.randomFillSync(new Uint8Array(57)));
const privateKey = crypto.randomFillSync(new Uint8Array(57));
const publicKey = Ed448.getPublicKey(privateKey);
```

Expand Down
68 changes: 35 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ class PureEdDSA {
}

keygen(privkey: number[]): number[] {
if (privkey?.length !== truediv(this.b, 8)) {
throw new Error(`Invalid private key length: ${privkey?.length} `);
if (privkey.length !== truediv(this.b, 8)) {
throw new Error(`Invalid private key length: ${privkey.length} `);
}

const khash = this.H(privkey, null, false);
Expand All @@ -436,11 +436,11 @@ class PureEdDSA {
ctx: null | number[],
hflag: boolean,
): number[] {
if (privkey?.length !== truediv(this.b, 8)) {
throw new Error(`Invalid private key length: ${privkey?.length} `);
if (privkey.length !== truediv(this.b, 8)) {
throw new Error(`Invalid private key length: ${privkey.length} `);
}
if (pubkey?.length !== truediv(this.b, 8)) {
throw new Error(`Invalid public key length: ${pubkey?.length} `);
if (pubkey.length !== truediv(this.b, 8)) {
throw new Error(`Invalid public key length: ${pubkey.length} `);
}
if (!msg) {
throw new Error("Missing message input");
Expand Down Expand Up @@ -494,21 +494,23 @@ class PureEdDSA {
}
}

type Input = number[] | Uint8Array | Buffer;

export interface EdDSA {
getPublicKey(privateKey: number[]): number[];
getPublicKey(privateKey: Input): number[];

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

verify(
publicKey: number[],
data: number[],
signature: number[],
context?: null | number[],
publicKey: Input,
data: Input,
signature: Input,
context?: null | Input,
): boolean;
}

Expand All @@ -519,36 +521,36 @@ class EdDSAImpl implements EdDSA {
this.pure = pureScheme;
}

getPublicKey(privateKey: number[]) {
return this.pure.keygen(privateKey);
getPublicKey(privateKey: Input) {
return this.pure.keygen(Array.from(privateKey));
}

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

verify(
publicKey: number[],
data: number[],
signature: number[],
context: null | number[] = null,
publicKey: Input,
data: Input,
signature: Input,
context: null | Input = null,
) {
return this.pure.verify(
publicKey,
data,
signature,
context || [],
Array.from(publicKey),
Array.from(data),
Array.from(signature),
context ? Array.from(context) : [],
this.pflag,
);
}
Expand Down

0 comments on commit 546db13

Please sign in to comment.