import { Schema } from 'effect'; import { ByteArray, Hex } from 'viem'; import { Bytes32 } from '../binary.js'; /** * A function that encrypts a plaintext value, embedding its {@link InputContext} into the resulting ciphertext. * * @typeParam S - The encryption scheme (e.g. X-Wing). * @example * ```ts * const result = await encrypt({ plaintext: { scheme: 2, type: 5, value: 42n }, context }); * // result.handle, result.ciphertext * ``` */ export type Encryptor = (plaintext: PlaintextWithContextOf) => Promise>; /** * A function that decrypts a ciphertext back to its plaintext value. * * @typeParam S - The encryption scheme (e.g. X-Wing). * @example * ```ts * const plaintext = await decrypt({ scheme: 2, type: 5, value: '0x...' }); * ``` */ export type Decryptor = (ciphertext: CiphertextOf) => Promise>; /** * Subset of ENCRYPTION types currently supported for encryption/decryption. * * @remarks TODO: review need of `euint64` and `euint160` — `Lib.sol` only supports `euint256` and `ebool`. */ export declare const supportedTeeTypes: { readonly euint64: 5; readonly euint160: 7; readonly euint256: 8; readonly ebool: 0; }; /** Schema that validates a string is one of the supported ENCRYPTION type names. */ export declare const SupportedTeeTypeName: Schema.SchemaClass<"ebool" | "euint64" | "euint160" | "euint256", "ebool" | "euint64" | "euint160" | "euint256", never>; /** A supported ENCRYPTION type name (e.g. `"euint256"`, `"ebool"`). */ export type SupportedTeeTypeName = typeof SupportedTeeTypeName.Type; /** Schema that validates a number is one of the supported ENCRYPTION type integer identifiers. */ export declare const SupportedTeeType: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>; /** Integer identifier of a supported ENCRYPTION type (a subset of all ENCRYPTION types). */ export type SupportedTeeType = typeof SupportedTeeType.Type; /** Map of encryption scheme names to their integer identifiers. Currently only X-Wing (2) is supported. */ export declare const encryptionSchemes: { readonly xwing: 2; }; /** * Returns the human-readable name of an encryption scheme. * @param scheme - The encryption scheme integer identifier. * @returns The scheme name (e.g. `"X-Wing"`). * @throws If the scheme identifier is unknown. */ export declare function getEncryptionSchemeName(scheme: number): string; /** The typeof {@link encryptionSchemes} — mapping from scheme names to integer IDs. */ export type EncryptionSchemes = typeof encryptionSchemes; /** The integer identifier for the X-Wing encryption scheme (`2`). */ export type XwingScheme = EncryptionSchemes['xwing']; /** Schema that validates a value is a known encryption scheme identifier. */ export declare const EncryptionScheme: Schema.Literal<[2]>; /** An encryption scheme identifier (currently only X-Wing = `2`). */ export type EncryptionScheme = typeof EncryptionScheme.Type; type DistType = P extends any ? P & { scheme: S; type: T; } : never; /** Schema for an ENCRYPTION ciphertext: encryption scheme, ENCRYPTION type, and the encrypted value as a hex string. */ export declare const Ciphertext: Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>; value: Schema.TemplateLiteral<`0x${string}`>; }>; /** An ENCRYPTION ciphertext containing the encryption scheme, ENCRYPTION type, and encrypted hex value. */ export type Ciphertext = typeof Ciphertext.Type; export type CiphertextOf = DistType; /** Schema for a ciphertext paired with the {@link InputContext} it was encrypted under. */ export declare const CiphertextWithContext: Schema.Struct<{ ciphertext: Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>; value: Schema.TemplateLiteral<`0x${string}`>; }>; context: Schema.Struct<{ hostChainId: typeof Schema.BigInt; aclAddress: Schema.TemplateLiteral<`0x${string}`>; userAddress: Schema.TemplateLiteral<`0x${string}`>; contractAddress: Schema.TemplateLiteral<`0x${string}`>; version: typeof Schema.Number; inputType: typeof Schema.Number; }>; }>; /** A ciphertext paired with the {@link InputContext} it was encrypted under. */ export type CiphertextWithContext = typeof CiphertextWithContext.Type; export type CiphertextWithContextOf = CiphertextWithContext & { ciphertext: CiphertextOf; }; /** Schema for the result of an encryption operation: ciphertext, input context, and final handle. */ export declare const EncryptResult: Schema.Struct<{ ciphertext: Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>; value: Schema.TemplateLiteral<`0x${string}`>; }>; context: Schema.Struct<{ hostChainId: typeof Schema.BigInt; aclAddress: Schema.TemplateLiteral<`0x${string}`>; userAddress: Schema.TemplateLiteral<`0x${string}`>; contractAddress: Schema.TemplateLiteral<`0x${string}`>; version: typeof Schema.Number; inputType: typeof Schema.Number; }>; handle: Schema.brand, Schema.Schema>]>, Schema.TemplateLiteral<`0x${string}`>, never>>, "Bytes32">; }>; /** The result of an encryption operation, containing the ciphertext, context, and deterministic handle. */ export type EncryptResult = typeof EncryptResult.Type; export type EncryptResultOf = EncryptResult & { ciphertext: CiphertextOf; }; /** * Schema for an ENCRYPTION plaintext value. The `value` field type depends on the ENCRYPTION type: * - `euint64` / `euint160` / `euint256`: `bigint` * - `ebool`: `boolean` */ export declare const Plaintext: Schema.Union<[Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.Literal<[5, 7, 8]>; value: typeof Schema.BigInt; }>, Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.Literal<[0]>; value: typeof Schema.Boolean; }>]>; /** An ENCRYPTION plaintext value — `bigint` for integer types, `boolean` for `ebool`. */ export type Plaintext = typeof Plaintext.Type; export type PlaintextOf = DistType; /** Schema for a plaintext paired with the {@link InputContext} it will be encrypted under. */ export declare const PlaintextWithContext: Schema.Struct<{ plaintext: Schema.Union<[Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.Literal<[5, 7, 8]>; value: typeof Schema.BigInt; }>, Schema.Struct<{ scheme: Schema.Literal<[2]>; type: Schema.Literal<[0]>; value: typeof Schema.Boolean; }>]>; context: Schema.Struct<{ hostChainId: typeof Schema.BigInt; aclAddress: Schema.TemplateLiteral<`0x${string}`>; userAddress: Schema.TemplateLiteral<`0x${string}`>; contractAddress: Schema.TemplateLiteral<`0x${string}`>; version: typeof Schema.Number; inputType: typeof Schema.Number; }>; }>; /** A plaintext paired with the {@link InputContext} it will be encrypted under. */ export type PlaintextWithContext = typeof PlaintextWithContext.Type; export type PlaintextWithContextOf = PlaintextWithContext & { plaintext: PlaintextOf; }; /** * Converts a `bigint` to a typed {@link Plaintext} value. * * For integer ENCRYPTION types (`euint64`, `euint160`, `euint256`) the value is passed through as-is. * For `ebool`, non-zero values become `true`. * * @param scheme - The encryption scheme identifier. * @param type - The ENCRYPTION type to interpret the value as. * @param bigPt - The raw bigint plaintext value. * @returns A typed `Plaintext` matching the given scheme and ENCRYPTION type. * @throws If `type` is not a supported ENCRYPTION type. */ export declare function bigintToPlaintext(scheme: S, type: T, bigPt: bigint): PlaintextOf; /** * Decodes an ABI-encoded ciphertext input into its components. * * The input format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`. * * @param input - The raw encoded input as a byte array or hex string. * @returns The decoded version, handle, and ciphertext. */ export declare function decodeCiphertextInput(input: ByteArray | Hex): { version: number; handle: Hex; ciphertext: Hex; }; /** * Encodes a handle and ciphertext into the on-chain input format. * * The output format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`. * * @param version - The encoding version (int32). * @param handle - The 32-byte handle as a hex string. * @param ciphertext - The ciphertext as a hex string. * @returns The ABI-encoded input as a hex string. */ export declare function encodeCiphertextInput(version: number, handle: Hex, ciphertext: Hex): Hex; /** * Converts a {@link Plaintext} to its `bigint` representation. * * Integer types pass through directly; `ebool` maps `true` → `1n`, `false` → `0n`. * * @param plaintext - The plaintext to convert. * @returns The bigint representation of the plaintext value. */ export declare function plaintextToBigint(plaintext: Plaintext): bigint; /** * Converts a {@link Plaintext} to a {@link Bytes32} hex string. * @param plaintext - The plaintext to convert. * @returns A 32-byte hex representation of the plaintext value. */ export declare function plaintextToBytes32(plaintext: Plaintext): Bytes32; /** * Converts a {@link Plaintext} to a 32-byte `Buffer`. * @param plaintext - The plaintext to convert. * @returns A 32-byte big-endian buffer of the plaintext value. */ export declare function plaintextToBytes(plaintext: Plaintext): Buffer; /** * Parses a {@link Bytes32} hex string into a typed {@link Plaintext}. * @param plaintext - The 32-byte hex value to interpret. * @param scheme - The encryption scheme identifier. * @param type - The ENCRYPTION type to interpret the bytes as. * @returns A typed `Plaintext` value. */ export declare function bytes32ToPlaintext(plaintext: Bytes32, scheme: EncryptionScheme, type: SupportedTeeType): Plaintext; /** * Parses a `Uint8Array` into a typed {@link Plaintext}. * @param plaintext - The byte array to interpret. * @param scheme - The encryption scheme identifier. * @param type - The ENCRYPTION type to interpret the bytes as. * @returns A typed `Plaintext` value. */ export declare function bytesToPlaintext(plaintext: Uint8Array, scheme: EncryptionScheme, type: SupportedTeeType): Plaintext; export {};