pbc_contract_codegen

Attribute Macro zk_on_secret_input

source
#[zk_on_secret_input]
Expand description

Secret input/action contract annotation

OPTIONAL HOOK: This is an optional hook. Most zero-knowledge contracts will need at least one.

Annotated function is a contract invocation that allows a user to deliver secret input to the contract. Can be thought of as the Zk variant of action. The notable change is the introduction of a required return value, of type ZkInputDef, that contains contract-supplied metadata, along with some other configuration for the secret variable.

If the annotated function panics the input will be rejected, and changes to the contract state will be rolled back.

Must have a signature of the following format:

use pbc_zk_core::Sbi32;


#[zk_on_secret_input(shortname = 0xDEADB00F)]
pub fn function_name(
  context: ContractContext,
  state: ContractState,
  zk_state: ZkState<Metadata>,
  // ... Invocation RPC arguments.
) -> (ContractState, Vec<EventGroup>, ZkInputDef<Metadata, Sbi32>) {
    (state, vec![], ZkInputDef::with_metadata(Some(SHORTNAME_MY_ON_INPUTTED), 0))
}

#[zk_on_variable_inputted(shortname=0x13)]
pub fn my_on_inputted(
  context: ContractContext,
  state: ContractState,
  zk_state: ZkState<Metadata>,
  variable_id: SecretVarId,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>) {
    // ...
}

with following constraints:

The function receives the previous states ContractState and ZkState<Metadata>, along with the ContractContext, and the declared RPC arguments.

The function must return a tuple containing:

  • New public state.
  • Vector of EventGroup; a list of interactions with other contracts.
  • Instance of ZkInputDef<Metadata> for declaring the layout and metadata of a secret variable.

ยงExample

use pbc_zk_core::Sbi32;
type ContractState = u32;
type Metadata = u32;


#[zk_on_secret_input(shortname = 0x13, secret_type = "Sbi32")]
pub fn receive_bitlengths_10_10(
  context: ContractContext,
  state: ContractState,
  zk_state: ZkState<u32>,
) -> (ContractState, Vec<EventGroup>, ZkInputDef<u32, Sbi32>) {
    let def = ZkInputDef::with_metadata(Some(SHORTNAME_MY_ON_INPUTTED), 23u32);
    (state, vec![], def)
}

#[zk_on_variable_inputted(shortname=0x13)]
pub fn my_on_inputted(
  context: ContractContext,
  state: ContractState,
  zk_state: ZkState<Metadata>,
  variable_id: SecretVarId,
) -> (ContractState, Vec<EventGroup>, Vec<ZkStateChange>) {
    // ...
}