@seda-protocol/secp256k1-vrf
Version:
A TypeScript implementation of Verifiable Random Functions (VRF) for secp256k1
100 lines • 3.55 kB
TypeScript
import type { Bytes, Hex, PrivKey } from "@noble/secp256k1";
interface VerifyValidResult {
isValid: true;
hash: string;
}
interface VerifyInvalidResult {
isValid: false;
reason: string;
}
/**
* Verifiable Random Function (VRF) implementation using @noble/secp256k1
* Based on RFC 9381 (Verifiable Random Functions (VRFs))
* https://datatracker.ietf.org/doc/rfc9381/
*
* This implementation focuses specifically on the secp256k1 curve.
*/
export declare class Secp256k1Vrf {
/**
* Extension beyond RFC 9381 - secp256k1 with SHA-256 and TAI
* Note: This is not defined in RFC 9381 and is a custom extension
*/
private readonly suiteID;
private readonly cLen;
private readonly scalarSize;
private readonly ptLen;
private readonly CHALLENGE_GENERATION_DOMAIN_SEPARATOR_FRONT;
private readonly CHALLENGE_GENERATION_DOMAIN_SEPARATOR_BACK;
private readonly ENCODE_TO_CURVE_DST_FRONT;
private readonly ENCODE_TO_CURVE_DST_BACK;
private readonly PROOF_TO_HASH_DOMAIN_SEPARATOR_FRONT;
private readonly PROOF_TO_HASH_DOMAIN_SEPARATOR_BACK;
private readonly COMPRESSED_POINT_EVEN_Y_PREFIX;
/**
* Generate a VRF proof for a message using a private key
* Implements algorithm from RFC 9381 Section 5.1
* @param secret Private key as bytes
* @param message Message to prove as bytes
* @returns VRF proof as bytes
*/
prove(secret: PrivKey, message: Bytes): Bytes;
/**
* Verify a VRF proof and return the resulting hash if valid
* Implements algorithm from RFC 9381 Section 5.3
* @param publicKey Public key as bytes
* @param proof VRF proof as bytes
* @param message Original message as bytes
* @returns Hash as a hex string if valid, "INVALID" if invalid
*/
verify(publicKey: Hex, proof: Bytes, message: Bytes): VerifyValidResult | VerifyInvalidResult;
/**
* Convert a VRF proof to its corresponding hash output
* Implements algorithm from RFC 9381 Section 5.2
* @param proof VRF proof as bytes
* @returns Hash output as a hex string
*/
proofToHash(proof: Bytes): string;
/**
* Generate a key pair for use with VRF
* @returns Object containing secret key and public key as hex strings
*/
keygen(): {
secretKey: string;
publicKey: string;
};
/**
* Decode a VRF proof into its components
* @param pi Proof to decode as bytes
* @returns Decoded gamma, c, and s components as bytes
*/
private decodeProof;
/**
* Challenge generation function
* @param points Concatenated point data as bytes
* @param truncateLen Length to truncate the output hash to
* @returns Challenge value as bytes
*/
private challengeGeneration;
/**
* Encode a message to an elliptic curve point using try-and-increment method
* @param encodeToCurveSalt Salt value (usually the public key) as bytes
* @param alpha Message to encode as bytes
* @returns Point on the curve as bytes
*/
private encodeToCurveTAI;
/**
* Generate a deterministic nonce for ECDSA signatures using RFC 6979
* @param secretKey Secret key
* @param data Input data
* @returns Nonce as bytes
*/
private generateNonce;
/**
* Convert a gamma point to its corresponding hash output
* @param gamma Gamma point as bytes
* @returns Hash output as bytes
*/
private gammaToHash;
}
export {};
//# sourceMappingURL=secp256k1-vrf.d.ts.map