A group shares one pot of PCASH, and any member can pay from it without anyone, other members included, learning which member paid. You create a contract account whose single function approves a payment from the contract's notes to any recipient when the caller proves they hold a secret from a committed list of members, and when the payment, its fees, and its burn together stay within a fixed cap. This is an example: unaudited and not for real funds.
How it works
- Members commit. Each member picks a secret and hands the creator only its hash, a member commitment. The creator builds a Merkle tree of the commitments and puts its root and the cap in the function's configuration, which the contract commits forever.
- Create the treasury. The contract publishes its nullifier-key root, from which each note's spend marker is derived, so every member can spend the contract's notes, and it carries a receive key whose secret key every member holds, so note openings sent to the contract reach all of them.
- Fund it. Anyone pays native PCASH to the contract's address.
- Pay. A member builds a transaction that spends treasury notes, pays a recipient, and returns the change to the contract, then proves the contract's function, showing that their secret's commitment is a leaf of the tree without revealing which leaf.
- Settle. The function returns a statement naming the contract, its configuration, and a commitment to the exact transaction. PCASH accepts the payment only when that statement matches the transaction it executes, so no one can change the recipient or an amount after the proof is made.
The circuit
The configuration is the member tree's root and the cap. A member commitment is a domain-separated hash of the member's secret.
// The member tree's root and the largest amount one call may move out of the
// treasury, a note amount.
pub struct Configuration {
pub member_root: Field,
pub cap: Field,
}
pub fn configuration_commitment(configuration: Configuration) -> Field {
pcash_hash(
[REFERENCE_CONFIGURATION_DOMAIN, configuration.member_root, configuration.cap],
)
}
// A member tree leaf. Each member keeps the secret and publishes only this.
pub fn member_commitment(member_secret: Field) -> Field {
pcash_hash([MEMBER_COMMITMENT_DOMAIN, member_secret])
}
The first check: the member's secret opens a leaf of the configured tree. The leaf's index and path are private witnesses, so the proof shows membership without saying which member.
assert(
binary_root_from_index(
member_commitment(membership.member_secret),
membership.index,
membership.siblings,
)
== configuration.member_root,
"member must belong to the committed group",
);
Every input must be a native note or padding. An asset input could otherwise leave as an asset burn that the cap never sees.
// An asset input would let its value leave as an uncapped asset burn.
for i in 0..S_INPUTS {
let class = private_value.input_slot_classes[i];
assert(
(class == SLOT_CLASS_DUMMY) | (class == SLOT_CLASS_NATIVE),
"input must be a native note or padding",
);
}
A transaction has fixed numbered input and output slots, each a real note or padding. The function opens each output slot it cares about with this helper: a zero amount must be padding, and anything else must be a native note with exactly that owner and amount.
fn assert_native_output<let N: u32>(
private_value: PrivateValue,
slot: u32,
output: NativeOutput,
message: str<N>,
) {
if output.amount == 0 {
assert(private_value.output_slot_classes[slot] == SLOT_CLASS_DUMMY, message);
} else {
assert(private_value.output_slot_classes[slot] == SLOT_CLASS_NATIVE, message);
assert(
private_value.output_body_commitments[slot]
== note_body_commitment(output.owner_commitment, 0, output.amount),
message,
);
}
}
The outputs follow one layout. Slot 0 pays any recipient the member chooses. Slot 1 returns change to the contract's own address, so the change stays in the treasury. The last slot may pay a relayer, and every slot between is padding, so no second payment can hide there.
assert_native_output(private_value, 0, payment, "slot 0 must pay the recipient");
assert_native_output(
private_value,
1,
NativeOutput {
owner_commitment: owner_commitment(address, change_note_secret),
amount: change_amount,
},
"slot 1 must return change to the treasury",
);
for i in 2..S_OUTPUTS - 1 {
assert(
private_value.output_slot_classes[i] == SLOT_CLASS_DUMMY,
"padding slot must be a dummy",
);
}
assert_relay_terms(private_value, relay_fee_amount, relay_fee_owner_commitment);
assert_native_output(
private_value,
S_OUTPUTS - 1,
NativeOutput { owner_commitment: relay_fee_owner_commitment, amount: relay_fee_amount },
"last slot must pay the relay fee",
);
The cap covers everything that leaves the treasury, not just the payment. The open fee counts too, because a member who posts the transaction collects it.
// Everything that leaves the treasury counts. Each term is a note amount:
// the kernel bounds the payment it builds the slot-0 body from,
// `assert_relay_terms` the relay fee, and the private-value commitment the
// open fee and burn, so no term truncates.
let outflow = (payment.amount as u128)
+ (relay_fee_amount as u128)
+ (private_value.open_fee_amount as u128)
+ (private_value.native_burn_amount as u128);
assert(outflow <= configuration.cap as u128, "payment, fees, and burn must not exceed the cap");
Last, the function returns the rule statement. The action commitment covers the whole transaction: every input, output, amount, fee, and burn. PCASH compares it with the transaction it executes, which is what binds the proof to this one payment.
rule_statement(
authorization_commitment(address, configuration_commitment(configuration)),
transfer_action_commitment(pcash_chain_id, private_value_commitment(private_value)),
recent_pcash_block_hash,
event_entry,
)
The whole circuit
// Approves a payment of up to a cap from this treasury contract by any member of a committed group.
// Example only: unaudited, not for real funds.
// A member proves that the secret behind one leaf of the configured member
// tree is theirs, without revealing which leaf. The function opens the whole
// private value: input slots hold the treasury's native notes or padding,
// slot 0 pays any recipient, slot 1 returns change to this contract, the last
// slot may pay a relayer, and the slots between are padding. The payment, the
// relay fee, the open fee, and the native burn together are at most the cap.
// Contract-owned native value moves only in the transfer role, so the function
// returns the transfer action.
use pcash_protocol::authorization::statement::{
authorization_commitment, rule_statement, RULE_STATEMENT_LENGTH, transfer_action_commitment,
};
use pcash_protocol::generated::private_value_profiles::{S_INPUTS, S_OUTPUTS};
use pcash_protocol::ledger::note::{note_body_commitment, owner_commitment};
use pcash_protocol::ledger::private_value::{
private_value_commitment, SLOT_CLASS_DUMMY, SLOT_CLASS_NATIVE,
SmallPrivateValue as PrivateValue,
};
use pcash_protocol::primitives::hash::pcash_hash;
use pcash_protocol::state::merkle::binary_root_from_index;
use reference_rule_common::generated::reference_rule_constants::REFERENCE_CONFIGURATION_DOMAIN;
use reference_rule_common::value_rule::assert_relay_terms;
pub global MEMBER_TREE_DEPTH: u32 = 16;
// MEMBER_COMMITMENT_DOMAIN: the domain tag of context "anonymous_treasury_member"
global MEMBER_COMMITMENT_DOMAIN: Field =
0x1aed8e651365d766b3c6368a50a05c9b5e2bb3086d9cefe1a0f04c2c850b0604;
// The member tree's root and the largest amount one call may move out of the
// treasury, a note amount.
pub struct Configuration {
pub member_root: Field,
pub cap: Field,
}
pub fn configuration_commitment(configuration: Configuration) -> Field {
pcash_hash(
[REFERENCE_CONFIGURATION_DOMAIN, configuration.member_root, configuration.cap],
)
}
// A member tree leaf. Each member keeps the secret and publishes only this.
pub fn member_commitment(member_secret: Field) -> Field {
pcash_hash([MEMBER_COMMITMENT_DOMAIN, member_secret])
}
pub struct Membership {
pub member_secret: Field,
pub index: Field,
pub siblings: [Field; MEMBER_TREE_DEPTH],
}
// One native output's owner commitment and amount. A zero amount is padding.
pub struct NativeOutput {
pub owner_commitment: Field,
pub amount: Field,
}
fn assert_native_output<let N: u32>(
private_value: PrivateValue,
slot: u32,
output: NativeOutput,
message: str<N>,
) {
if output.amount == 0 {
assert(private_value.output_slot_classes[slot] == SLOT_CLASS_DUMMY, message);
} else {
assert(private_value.output_slot_classes[slot] == SLOT_CLASS_NATIVE, message);
assert(
private_value.output_body_commitments[slot]
== note_body_commitment(output.owner_commitment, 0, output.amount),
message,
);
}
}
fn main(
recent_pcash_block_hash: Field,
address: Field,
pcash_chain_id: Field,
configuration: Configuration,
membership: Membership,
private_value: PrivateValue,
payment: NativeOutput,
change_note_secret: Field,
change_amount: Field,
relay_fee_amount: Field,
relay_fee_owner_commitment: Field,
event_entry: Field,
) -> return_data [Field; RULE_STATEMENT_LENGTH] {
assert(
binary_root_from_index(
member_commitment(membership.member_secret),
membership.index,
membership.siblings,
)
== configuration.member_root,
"member must belong to the committed group",
);
// An asset input would let its value leave as an uncapped asset burn.
for i in 0..S_INPUTS {
let class = private_value.input_slot_classes[i];
assert(
(class == SLOT_CLASS_DUMMY) | (class == SLOT_CLASS_NATIVE),
"input must be a native note or padding",
);
}
assert_native_output(private_value, 0, payment, "slot 0 must pay the recipient");
assert_native_output(
private_value,
1,
NativeOutput {
owner_commitment: owner_commitment(address, change_note_secret),
amount: change_amount,
},
"slot 1 must return change to the treasury",
);
for i in 2..S_OUTPUTS - 1 {
assert(
private_value.output_slot_classes[i] == SLOT_CLASS_DUMMY,
"padding slot must be a dummy",
);
}
assert_relay_terms(private_value, relay_fee_amount, relay_fee_owner_commitment);
assert_native_output(
private_value,
S_OUTPUTS - 1,
NativeOutput { owner_commitment: relay_fee_owner_commitment, amount: relay_fee_amount },
"last slot must pay the relay fee",
);
// Everything that leaves the treasury counts. Each term is a note amount:
// the kernel bounds the payment it builds the slot-0 body from,
// `assert_relay_terms` the relay fee, and the private-value commitment the
// open fee and burn, so no term truncates.
let outflow = (payment.amount as u128)
+ (relay_fee_amount as u128)
+ (private_value.open_fee_amount as u128)
+ (private_value.native_burn_amount as u128);
assert(outflow <= configuration.cap as u128, "payment, fees, and burn must not exceed the cap");
rule_statement(
authorization_commitment(address, configuration_commitment(configuration)),
transfer_action_commitment(pcash_chain_id, private_value_commitment(private_value)),
recent_pcash_block_hash,
event_entry,
)
}
mod tests {
use super::{
Configuration, configuration_commitment, main, member_commitment, MEMBER_TREE_DEPTH,
Membership, NativeOutput,
};
use pcash_protocol::authorization::statement::{
authorization_commitment, rule_statement, RULE_STATEMENT_LENGTH, transfer_action_commitment,
};
use pcash_protocol::ledger::note::{note_body_commitment, owner_commitment};
use pcash_protocol::ledger::private_value::{
private_value_commitment, SLOT_CLASS_ASSET, SLOT_CLASS_DUMMY, SLOT_CLASS_NATIVE,
SmallPrivateValue as PrivateValue,
};
use pcash_protocol::state::merkle::binary_root_from_index;
global TREASURY: Field = 0x77;
global CHAIN_ID: Field = 7;
global RECENT_BLOCK: Field = 0x5eed;
global MEMBER_SECRET: Field = 0x1234;
global MEMBER_INDEX: Field = 5;
global CAP: Field = 100;
global TREASURY_NOTE: Field = 1000;
global RECIPIENT: Field = 0x55;
global RELAYER: Field = 0xabc;
global CHANGE_SECRET: Field = 0x66;
fn membership(member_secret: Field) -> Membership {
let mut siblings = [0; MEMBER_TREE_DEPTH];
for i in 0..MEMBER_TREE_DEPTH {
siblings[i] = 0x100 + i as Field;
}
Membership { member_secret, index: MEMBER_INDEX, siblings }
}
fn configuration() -> Configuration {
let member = membership(MEMBER_SECRET);
Configuration {
member_root: binary_root_from_index(
member_commitment(MEMBER_SECRET),
member.index,
member.siblings,
),
cap: CAP,
}
}
// One treasury note pays `amount` to the recipient and the network fees,
// and returns the rest to the treasury in slot 1.
struct TreasuryPayment {
private_value: PrivateValue,
payment: NativeOutput,
change_amount: Field,
relay_fee_amount: Field,
}
fn treasury_payment(
amount: Field,
open_fee_amount: Field,
native_burn_amount: Field,
relay_fee_amount: Field,
) -> TreasuryPayment {
let change_amount =
TREASURY_NOTE - amount - open_fee_amount - native_burn_amount - relay_fee_amount;
let payment = NativeOutput { owner_commitment: RECIPIENT, amount };
let mut private_value = PrivateValue {
open_fee_amount,
native_burn_amount,
issuance_owner_commitment: 0,
not_before_timestamp: 0,
valid_until_timestamp: 2000000000,
carrier_context_hash: 0,
asset_burn_amount: 0,
delivery_commitment: 0x61,
account_creation_commitment: 0,
input_slot_classes: [
SLOT_CLASS_NATIVE,
SLOT_CLASS_DUMMY,
SLOT_CLASS_DUMMY,
SLOT_CLASS_DUMMY,
],
input_commitments: [0x71, 0, 0, 0],
nullifiers: [0x81, 0x82, 0x83, 0x84],
output_slot_classes: [
SLOT_CLASS_NATIVE,
SLOT_CLASS_NATIVE,
SLOT_CLASS_DUMMY,
SLOT_CLASS_DUMMY,
SLOT_CLASS_DUMMY,
],
output_body_commitments: [
note_body_commitment(RECIPIENT, 0, amount),
note_body_commitment(owner_commitment(TREASURY, CHANGE_SECRET), 0, change_amount),
0x93,
0x94,
0x95,
],
};
if relay_fee_amount != 0 {
private_value.output_slot_classes[4] = SLOT_CLASS_NATIVE;
private_value.output_body_commitments[4] =
note_body_commitment(RELAYER, 0, relay_fee_amount);
}
TreasuryPayment { private_value, payment, change_amount, relay_fee_amount }
}
fn pay(fixture: TreasuryPayment, member_secret: Field) -> [Field; RULE_STATEMENT_LENGTH] {
main(
RECENT_BLOCK,
TREASURY,
CHAIN_ID,
configuration(),
membership(member_secret),
fixture.private_value,
fixture.payment,
CHANGE_SECRET,
fixture.change_amount,
fixture.relay_fee_amount,
if fixture.relay_fee_amount == 0 {
0
} else {
RELAYER
},
0,
)
}
#[test]
fn test_member_pays_up_to_the_cap() {
let fixture = treasury_payment(CAP, 0, 0, 0);
let private_value = fixture.private_value;
assert(
pay(fixture, MEMBER_SECRET)
== rule_statement(
authorization_commitment(TREASURY, configuration_commitment(configuration())),
transfer_action_commitment(CHAIN_ID, private_value_commitment(private_value)),
RECENT_BLOCK,
0,
),
);
}
#[test]
fn test_fees_burn_and_relay_fit_under_the_cap() {
let _ = pay(treasury_payment(85, 5, 10, 0), MEMBER_SECRET);
let _ = pay(treasury_payment(85, 0, 10, 5), MEMBER_SECRET);
}
#[test(should_fail_with = "member must belong to the committed group")]
fn test_non_member_cannot_pay() {
let _ = pay(treasury_payment(10, 0, 0, 0), MEMBER_SECRET + 1);
}
#[test(should_fail_with = "payment, fees, and burn must not exceed the cap")]
fn test_payment_above_the_cap_fails() {
let _ = pay(treasury_payment(CAP + 1, 0, 0, 0), MEMBER_SECRET);
}
// A member who posts the batch would collect the open fee.
#[test(should_fail_with = "payment, fees, and burn must not exceed the cap")]
fn test_open_fee_counts_against_the_cap() {
let _ = pay(treasury_payment(CAP, 1, 0, 0), MEMBER_SECRET);
}
#[test(should_fail_with = "payment, fees, and burn must not exceed the cap")]
fn test_relay_fee_counts_against_the_cap() {
let _ = pay(treasury_payment(CAP, 0, 0, 1), MEMBER_SECRET);
}
#[test(should_fail_with = "slot 1 must return change to the treasury")]
fn test_change_cannot_leave_the_treasury() {
let mut fixture = treasury_payment(CAP, 0, 0, 0);
fixture.private_value.output_body_commitments[1] = note_body_commitment(
owner_commitment(TREASURY + 1, CHANGE_SECRET),
0,
fixture.change_amount,
);
let _ = pay(fixture, MEMBER_SECRET);
}
#[test(should_fail_with = "padding slot must be a dummy")]
fn test_a_second_payment_slot_fails() {
let mut fixture = treasury_payment(CAP, 0, 0, 0);
fixture.private_value.output_slot_classes[2] = SLOT_CLASS_NATIVE;
let _ = pay(fixture, MEMBER_SECRET);
}
#[test(should_fail_with = "input must be a native note or padding")]
fn test_asset_input_fails() {
let mut fixture = treasury_payment(CAP, 0, 0, 0);
fixture.private_value.input_slot_classes[1] = SLOT_CLASS_ASSET;
let _ = pay(fixture, MEMBER_SECRET);
}
}
Use it from an app
createTreasury builds the contract with its one function and returns the creation transaction. payFromTreasury builds the payment with the SDK's buildContractFunctionCall, maps the member's witness to the circuit's inputs, and proves it. Members learn the treasury's notes by decrypting the openings sent to its receive key.
// Creates the anonymous-member treasury and proves one member's capped payment from it.
// Example only: unaudited, not for real funds.
import { REFERENCE_CONFIGURATION_DOMAIN } from "@pcash/sdk/constants";
import { domainTag, poseidon } from "@pcash/sdk/crypto";
import { DEFAULT_EVENT_ENTRY, functionSelector, nativeBurnAmount, ownerCommitment, type ContractBody, type NativeBurn } from "@pcash/sdk/protocol";
import { buildContractFunctionCall, buildUnfundedContractCreation, fieldArrayMap, fm, privateValueMap, snapshotFromAccountProof, type TransferInputNote } from "@pcash/sdk/builders";
import type { AccountProofResult } from "@pcash/sdk/browser";
import type { Prover } from "@pcash/sdk/browser-prover";
export const PAY_SELECTOR = functionSelector(0);
const MEMBER_COMMITMENT_DOMAIN = domainTag("anonymous_treasury_member");
/** The member tree leaf a member hands to the treasury's creator; the secret stays with the member. */
export const memberCommitment = (memberSecret: bigint): bigint => poseidon([MEMBER_COMMITMENT_DOMAIN, memberSecret]);
export interface TreasuryConfiguration { memberRoot: bigint; cap: bigint }
/**
* The treasury's standalone creation, valid only while the base fee is zero. Its
* nkRoot is published so any member can spend its notes; funders and change
* encrypt note openings to `receiveKey`, whose secret key every member holds.
*/
export function createTreasury(p: {
pcashChainId: bigint; nkRoot: bigint; receiveKey: Uint8Array; applicationVkHash: bigint;
configuration: TreasuryConfiguration; recentPcashBlockHash: bigint; validUntilTimestamp: bigint;
}) {
const configurationCommitment = poseidon([REFERENCE_CONFIGURATION_DOMAIN, p.configuration.memberRoot, p.configuration.cap]);
return buildUnfundedContractCreation({
pcashChainId: p.pcashChainId,
contract: {
nullifierKey: { publishesNkRoot: true, nkRoot: p.nkRoot },
mlKemPublicKey: p.receiveKey,
functions: [{ selector: PAY_SELECTOR, applicationVkHash: p.applicationVkHash, configurationCommitment }],
},
recentPcashBlockHash: p.recentPcashBlockHash,
validUntilTimestamp: p.validUntilTimestamp,
});
}
/**
* One member pays `amount` from the treasury notes it opens, all beneath the
* block of the treasury's account proof. Slot 0 pays the recipient, slot 1
* returns change to the treasury, and the burn comes out of treasury value.
*/
export async function payFromTreasury(p: {
pcashChainId: bigint; contract: ContractBody; configuration: TreasuryConfiguration;
member: { secret: bigint; index: bigint; siblings: readonly bigint[] };
account: Extract<AccountProofResult, { status: "present" }>; treasuryNotes: readonly TransferInputNote[];
recipient: bigint; recipientReceiveKey: Uint8Array; amount: bigint; burn: NativeBurn;
validUntilTimestamp: bigint; circuitUrl: string; prover: Prover;
}): Promise<Uint8Array> {
const call = buildContractFunctionCall({
chainId: p.pcashChainId, contract: p.contract, selector: PAY_SELECTOR,
recipientAddress: p.recipient, recipientReceiveKey: p.recipientReceiveKey, paymentAmount: p.amount,
inputs: p.treasuryNotes, ...snapshotFromAccountProof(p.account),
accountIndex: Number(p.account.account.index), accountSiblings: p.account.accountSiblings,
openFeeAmount: 0n, burn: p.burn, issuanceOwnerCommitment: 0n, validUntilTimestamp: p.validUntilTimestamp,
});
const spent = p.treasuryNotes.reduce((sum, note) => sum + note.amount, 0n);
const witness = {
recent_pcash_block_hash: fm(call.recentPcashBlockHash),
address: fm(call.address),
pcash_chain_id: fm(p.pcashChainId),
configuration: { member_root: fm(p.configuration.memberRoot), cap: fm(p.configuration.cap) },
membership: { member_secret: fm(p.member.secret), index: fm(p.member.index), siblings: fieldArrayMap(p.member.siblings) },
private_value: privateValueMap(call.transaction),
payment: { owner_commitment: fm(ownerCommitment(p.recipient, call.outputNoteSecrets[0]!)), amount: fm(p.amount) },
change_note_secret: fm(call.outputNoteSecrets[1]!),
change_amount: fm(spent - p.amount - nativeBurnAmount(p.burn)),
relay_fee_amount: fm(0n),
relay_fee_owner_commitment: fm(0n),
event_entry: fm(DEFAULT_EVENT_ENTRY),
};
const proof = await p.prover.proveTransfer({
authorizationWitnessMap: witness, buildKernelWitnessMap: call.buildKernelWitnessMap,
expectedPublicInputs: call.publicInputs, authorizationCircuitUrl: p.circuitUrl, authorizationLabel: "treasury payment",
});
return call.encodeTransaction(proof.proof);
}
Limits
- The cap limits each payment, not the total. Any member can empty the treasury with repeated payments. Fund it in small tranches, or keep a total limit outside PCASH.
- The paying member chooses the change note's secret and sends its opening to the treasury. Nothing checks that delivery, so a member who withholds it is the only one who can find and spend that change.
- Members can't be added or removed: the contract never changes. Moving the funds to a new treasury also goes through capped payments, so a leaked member secret stays dangerous until the treasury is empty.
- The treasury holds native PCASH only.
Run the tests
Run nargo test --program-dir examples/anonymous-treasury, or just test-examples-noir for every example's tests.
Further reading: the rule statement, action commitments, slot classes, and contract-account creation.

