Add schnorrkelVrf{Sign, Verify} functions (#845)
* Add schnorrkelVrf{Sign, Verify} functions
* Apply suggestions from code review
This commit is contained in:
@@ -7,3 +7,5 @@ export { schnorrkelDeriveSoft } from './deriveSoft';
|
||||
export { schnorrkelKeypairFromSeed } from './keypair/fromSeed';
|
||||
export { schnorrkelSign } from './sign';
|
||||
export { schnorrkelVerify } from './verify';
|
||||
export { schnorrkelVrfSign } from './vrfSign';
|
||||
export { schnorrkelVrfVerify } from './vrfVerify';
|
||||
|
||||
@@ -14,7 +14,5 @@ export function schnorrkelSign (message: Uint8Array | string, { publicKey, secre
|
||||
assert(publicKey?.length === 32, 'Expected a valid publicKey, 32-bytes');
|
||||
assert(secretKey?.length === 64, 'Expected a valid secretKey, 64-bytes');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return sr25519Sign(publicKey, secretKey, messageU8a);
|
||||
return sr25519Sign(publicKey, secretKey, u8aToU8a(message));
|
||||
}
|
||||
|
||||
@@ -9,12 +9,11 @@ import { sr25519Verify } from '@polkadot/wasm-crypto';
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export function schnorrkelVerify (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string): boolean {
|
||||
const messageU8a = u8aToU8a(message);
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert(publicKeyU8a.length === 32, `Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
assert(signatureU8a.length === 64, `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return sr25519Verify(signatureU8a, messageU8a, publicKeyU8a);
|
||||
return sr25519Verify(signatureU8a, u8aToU8a(message), publicKeyU8a);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Keypair } from '../types';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { vrfSign } from '@polkadot/wasm-crypto';
|
||||
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name schnorrkelVrfSign
|
||||
* @description Sign with sr25519 vrf signing (deterministic)
|
||||
*/
|
||||
export function schnorrkelVrfSign (message: Uint8Array | string, { secretKey }: Partial<Keypair>, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): Uint8Array {
|
||||
assert(secretKey?.length === 64, 'Invalid secretKey, expected 64-bytes');
|
||||
|
||||
return vrfSign(secretKey, u8aToU8a(context), u8aToU8a(message), u8aToU8a(extra));
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { stringToU8a, u8aEq } from '@polkadot/util';
|
||||
import { waitReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a';
|
||||
import { schnorrkelKeypairFromSeed } from './keypair/fromSeed';
|
||||
import { schnorrkelVrfSign } from './vrfSign';
|
||||
import { schnorrkelVrfVerify } from './vrfVerify';
|
||||
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
describe('vrf sign and verify', (): void => {
|
||||
beforeEach(async (): Promise<void> => {
|
||||
await waitReady();
|
||||
});
|
||||
|
||||
it('has 96-byte proofs', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
|
||||
expect(schnorrkelVrfSign(MESSAGE, pair)).toHaveLength(96);
|
||||
});
|
||||
|
||||
it('signing is deterministic', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof1 = schnorrkelVrfSign(MESSAGE, pair);
|
||||
const proof2 = schnorrkelVrfSign(MESSAGE, pair);
|
||||
|
||||
expect(u8aEq(proof1.subarray(0, 32), proof2.subarray(0, 32))).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message', (): void => {
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message (with context)', (): void => {
|
||||
const context = 'my context';
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair, context);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey, context)).toBe(true);
|
||||
});
|
||||
|
||||
it('can sign and verify a message (with context & extra)', (): void => {
|
||||
const context = 'my context';
|
||||
const extra = 'some extra transcript data';
|
||||
const pair = schnorrkelKeypairFromSeed(randomAsU8a());
|
||||
const proof = schnorrkelVrfSign(MESSAGE, pair, context, extra);
|
||||
|
||||
expect(schnorrkelVrfVerify(MESSAGE, proof, pair.publicKey, context, extra)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws error when publicKey lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(96),
|
||||
new Uint8Array(31)
|
||||
)
|
||||
).toThrow(/Invalid publicKey/);
|
||||
});
|
||||
|
||||
it('throws error when proof lengths do not match', (): void => {
|
||||
expect(
|
||||
() => schnorrkelVrfVerify(
|
||||
new Uint8Array([0x61, 0x62, 0x63, 0x64]),
|
||||
new Uint8Array(99),
|
||||
new Uint8Array(32)
|
||||
)
|
||||
).toThrow(/Invalid vrfSign/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { vrfVerify } from '@polkadot/wasm-crypto';
|
||||
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
|
||||
/**
|
||||
* @name schnorrkelVrfVerify
|
||||
* @description Verify with sr25519 vrf verification
|
||||
*/
|
||||
export function schnorrkelVrfVerify (message: Uint8Array | string, signOutput: string | Uint8Array, publicKey: Uint8Array | string, context: string | Uint8Array = EMPTY_U8A, extra: string | Uint8Array = EMPTY_U8A): boolean {
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const proofU8a = u8aToU8a(signOutput);
|
||||
|
||||
assert(publicKeyU8a.length === 32, 'Invalid publicKey, expected 32-bytes');
|
||||
assert(proofU8a.length === 96, 'Invalid vrfSign output, expected 96 bytes');
|
||||
|
||||
return vrfVerify(publicKeyU8a, u8aToU8a(context), u8aToU8a(message), u8aToU8a(extra), proofU8a);
|
||||
}
|
||||
Reference in New Issue
Block a user