@zkpersona/noir-jwt
Version:
JWT Verification and claims attestation library for Noir
137 lines (132 loc) • 5.1 kB
text/typescript
import * as _noir_lang_noir_js from '@noir-lang/noir_js';
import { FixedSizeArray, Field } from '@zkpersona/noir-helpers';
import forge from 'node-forge';
/**
* Supported JWT signature algorithms.
*
*/
type JWTAlgorithm = 'H256' | 'RS256';
/**
* Class for parsing and converting JWT strings into Noir circuit inputs.
* Handles both HMAC-SHA256 and RSA-SHA256 signatures, converting them into
* appropriate field element representations for Noir circuit verification.
*/
declare class JWT {
/** Base64-encoded JWT header */
protected header: string;
/** Base64-encoded JWT payload */
protected payload: string;
/** Base64-encoded JWT signature */
protected signature: string;
/** The algorithm used for signature verification */
protected alg: JWTAlgorithm;
/**
* Creates a new JWT instance from a JWT string.
*
* @param jwt - The JWT string in the format 'header.payload.signature'
* @param alg - The algorithm to use for signature verification (defaults to H256)
* @throws Error if the JWT string is malformed or missing components
*/
constructor(jwt: string, alg?: JWTAlgorithm);
/**
* Converts the JWT components into Noir circuit compatible inputs.
*
* @param maxHeaderLength - Maximum length for the header vector
* @param maxPayloadLength - Maximum length for the payload vector
* @param maxSignatureLength - Maximum length for the signature vector
* @returns InputMap containing the JWT components as Noir circuit inputs.
* @throws Error if an unsupported algorithm is specified
*/
toCircuitInputs({ maxHeaderLength, maxPayloadLength, maxSignatureLength, }: {
maxHeaderLength: number;
maxPayloadLength: number;
maxSignatureLength: number;
}): _noir_lang_noir_js.InputMap;
}
/**
* Represents an RSA public key with immutable properties.
* Provides methods to create instances from different formats and convert to Noir circuit inputs.
*/
declare class RSAPubKey {
readonly modulus: bigint;
readonly exponent: bigint;
/**
* Private constructor to enforce immutability and proper initialization.
* Use factory methods `fromPem` or `fromAsn1` to create instances.
*
* @param modulus - The RSA modulus (n) as a bigint
* @param exponent - The RSA public exponent (e) as a bigint
*/
private constructor();
/**
* Creates an RSAPubKey instance from a PEM-encoded public key.
*
* @param pem - The RSA public key in PEM format
* @returns A new RSAPubKey instance
*/
static fromPem(pem: string): RSAPubKey;
/**
* Creates an RSAPubKey instance from an ASN.1 encoded public key.
*
* @param key - The RSA public key in ASN.1 format
* @returns A new RSAPubKey instance
*/
static fromAsn1(key: forge.asn1.Asn1): RSAPubKey;
/**
* Converts the RSA public key into field element arrays for Noir circuit input.
*
* @param numBits - Optional number of bits to use for the limb representation.
* If not provided, a default value will be used.
* @returns An object containing:
* - modulus: Array of field elements representing the RSA modulus
* - redc: Array of field elements representing the Montgomery reduction parameters
*/
toFieldArray(numBits?: number): {
modulus: FixedSizeArray<Field>;
redc: FixedSizeArray<Field>;
};
/**
* Converts the RSA public key into an object for Noir circuit input.
*/
toCircuitInputs(): _noir_lang_noir_js.InputMap;
}
/**
* Represents an RSA signature with immutable properties.
* Provides methods to create instances from different formats and convert to Noir circuit inputs.
*/
declare class RSASignature {
readonly bytes: number[];
/**
* Private constructor to enforce immutability and proper initialization.
* Use factory method `fromString` to create instances.
*
* @param bytes - The raw bytes of the RSA signature
*/
private constructor();
/**
* Creates an RSASignature instance from a string-encoded signature.
*
* @param signature - The RSA signature as a string
* @param encoding - The encoding of the signature string (e.g., 'base64', 'hex')
* @returns A new RSASignature instance
*/
static fromString(signature: string, encoding: BufferEncoding): RSASignature;
/**
* Converts the RSA signature into an array of field elements for Noir circuit input.
*
* @returns An array of field elements representing the signature
*/
toFieldArray(): FixedSizeArray<Field>;
/**
* Converts the RSA signature into an object for Noir circuit input.
*/
toCircuitInputs(): _noir_lang_noir_js.InputMap;
}
/**
* Converts a byte array to a bigint.
*
* @param bytes - The byte array to convert
* @returns The resulting bigint
*/
declare const bytesToBigInt: (bytes: number[] | Uint8Array) => bigint;
export { JWT, type JWTAlgorithm, RSAPubKey, RSASignature, bytesToBigInt };