Add schnorrkelVrf{Sign, Verify} functions (#845)
* Add schnorrkelVrf{Sign, Verify} functions
* Apply suggestions from code review
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/networks": "5.3.2-1",
|
||||
"@polkadot/util": "5.3.2-1",
|
||||
"@polkadot/wasm-crypto": "^3.1.1",
|
||||
"@polkadot/wasm-crypto": "^3.1.2-4",
|
||||
"@polkadot/x-randomvalues": "5.3.2-1",
|
||||
"base-x": "^3.0.8",
|
||||
"blakejs": "^1.1.0",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1851,7 +1851,7 @@ __metadata:
|
||||
"@babel/runtime": ^7.12.5
|
||||
"@polkadot/networks": 5.3.2-1
|
||||
"@polkadot/util": 5.3.2-1
|
||||
"@polkadot/wasm-crypto": ^3.1.1
|
||||
"@polkadot/wasm-crypto": ^3.1.2-4
|
||||
"@polkadot/x-randomvalues": 5.3.2-1
|
||||
"@types/bn.js": ^4.11.6
|
||||
"@types/create-hash": ^1.2.2
|
||||
@@ -1887,35 +1887,35 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@polkadot/wasm-crypto-asmjs@npm:^3.1.1":
|
||||
version: 3.1.1
|
||||
resolution: "@polkadot/wasm-crypto-asmjs@npm:3.1.1"
|
||||
"@polkadot/wasm-crypto-asmjs@npm:^3.1.2-4":
|
||||
version: 3.1.2-4
|
||||
resolution: "@polkadot/wasm-crypto-asmjs@npm:3.1.2-4"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.12.5
|
||||
checksum: 6f8f10452e89ba7e3cabbd38f521cd379180c6efec644a79b7e39c04eb00dbba50a121a695cfc02578ec123c3139ce2aa128b193f11ed49e1e9f624685350e8a
|
||||
checksum: e23ecec8538ff4ec46bd9894c3900860272b12ea71864f746d5c1ba203d8cd412e86001f0f3c98b5ddd9807d1be33a4cc2206f3a53236572bc202de9f8e3beb3
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/wasm-crypto-wasm@npm:^3.1.1":
|
||||
version: 3.1.1
|
||||
resolution: "@polkadot/wasm-crypto-wasm@npm:3.1.1"
|
||||
"@polkadot/wasm-crypto-wasm@npm:^3.1.2-4":
|
||||
version: 3.1.2-4
|
||||
resolution: "@polkadot/wasm-crypto-wasm@npm:3.1.2-4"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.12.5
|
||||
checksum: f96eda239fa4632359983ef12d776268ed2f16a12cc888e594f3ca43f09b42cc389ad248bbccb99b801b59ad672e2961a9e3dff9377ede067588d4de541df6fa
|
||||
checksum: e11c5065dbcfbd26d54cc9b483fba08e3feeb00ac0000eea7269bd33b0ee1114c3e3feca5a4f3b888cfb4587ae4112b0bc38510143703123a2d17cb5d9184134
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/wasm-crypto@npm:^3.1.1":
|
||||
version: 3.1.1
|
||||
resolution: "@polkadot/wasm-crypto@npm:3.1.1"
|
||||
"@polkadot/wasm-crypto@npm:^3.1.2-4":
|
||||
version: 3.1.2-4
|
||||
resolution: "@polkadot/wasm-crypto@npm:3.1.2-4"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.12.5
|
||||
"@polkadot/wasm-crypto-asmjs": ^3.1.1
|
||||
"@polkadot/wasm-crypto-wasm": ^3.1.1
|
||||
"@polkadot/wasm-crypto-asmjs": ^3.1.2-4
|
||||
"@polkadot/wasm-crypto-wasm": ^3.1.2-4
|
||||
peerDependencies:
|
||||
"@polkadot/util": "*"
|
||||
"@polkadot/x-randomvalues": "*"
|
||||
checksum: a7b3d029d88669ea9775c8cfe166a44a06ba243283c03fa0547bf4c6144c5440260a0bd1a6acc9c67b57ab707c406d681a2e52af5e7da3ebe8bb4fdeff071c66
|
||||
checksum: db473558ee494d6da64ee315f1d8af2f13c5d3f8bffab7708929379ba4cb56526a169640a5071575868da326e86c93c9d4a5c83f8d1b206dbab8b1d965170dca
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user