Skip to content

Commit

Permalink
contracts: darkpool: Update contract interface to match redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed May 8, 2023
1 parent ee9bc1e commit 4f5fe1d
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 228 deletions.
187 changes: 92 additions & 95 deletions contracts/darkpool/Darkpool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func upgrade_nullifier{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_che
//

// @notice returns the hash of the most recent transaction to update a given wallet
// as identified by pk_view
// as identified by the wallet's public blinder share
// @return the tx hash
@view
func get_wallet_update{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
pk_view: felt
) -> (tx_hash: felt) {
let (tx_hash) = Darkpool.get_wallet_update(pk_view=pk_view);
func get_public_blinder_transaction{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr
}(public_blinder_share: felt) -> (tx_hash: felt) {
let (tx_hash) = Darkpool.get_public_blinder_transaction(public_blinder_share=public_blinder_share);
return (tx_hash=tx_hash);
}

Expand Down Expand Up @@ -117,132 +117,129 @@ func is_nullifier_used{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_che
//

// @dev adds a new wallet to the commitment tree
// @param pk_view the public view key of the wallet, used for indexing
// @param commitment the commitment to the new wallet
// @param encryption_blob the wallet's ciphertext blob
// @param proof_blob the proof of `VALID NEW WALLET`
// @param public_blinder_share the public secret share of the wallet's blinder
// @param public_share_commitment a commitment to the public wallet share, this will
// be removed in the future, added here only for testing
// @param public_wallet_share the public secret share of the wallet
// @param proof_blob the proof of `VALID WALLET CREATE` authorizing this tx
// @return the new root after the wallet is inserted into the tree
@external
func new_wallet{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
pk_view: felt,
commitment: felt,
encryption_blob_len: felt,
encryption_blob: felt*,
public_blinder_share: felt,
public_share_commitment: felt,
private_share_commitment: felt,
public_wallet_share_len: felt,
public_wallet_share: felt*,
proof_blob_len: felt,
proof_blob: felt*,
) -> (new_root: felt) {
let (new_root) = Darkpool.new_wallet(commitment=commitment);
let (new_root) = Darkpool.new_wallet(
public_share_commitment=public_share_commitment,
private_share_commitment=private_share_commitment,
);

Darkpool.mark_wallet_updated(pk_view=pk_view);
Darkpool.mark_wallet_updated(public_blinder_share=public_blinder_share);
return (new_root=new_root);
}

// @notice update a wallet in the commitment tree
// @param pk_view the public view key of the wallet, used for indexing
// @param commitment the commitment to the updated wallet
// @param match_nullifier the wallet match nullifier for the wallet before it was updated
// @param spend_nullifier the wallet spend nullifier for the wallet before it was updated
// @param internal_transfer_ciphertext the encryption of an internal transfer if present
// @param external_transfers the external transfers (ERC20 deposit/withdrawl)
// @param encryption_blob the encryption of the updated wallet
// @param proof_blob the proof of `VALID WALLET UPDATE`
// @param old_public_share_nullifier the nullifier of the previous wallet's public share
// @param old_private_share_nullifier the nullifier of the previous wallet's private share
// @param public_blinder_share the public share of the updated wallet's blinder
// @param public_share_commitment a commitment to the public secret share of the
// updated wallet. This will be removed in the future and computed in the
// contract instead
// @param private_share_commitment a commitment to the private secret share of the
// updated wallet
// @param external_transfers the external transfers (deposit/withdrawal) made to the contract
// @param public_wallet_share the public secret share of the updated wallet
// @param proof_blob the proof of `VALID WALLET UPDATE` submitted with the tx
// @return the root of the state tree after the new commitment is inserted
@external
func update_wallet{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
pk_view: felt,
commitment: felt,
match_nullifier: felt,
spend_nullifier: felt,
internal_transfer_ciphertext_len: felt,
internal_transfer_ciphertext: felt*,
old_public_share_nullifier: felt,
old_private_share_nullifier: felt,
public_blinder_share: felt,
public_share_commitment: felt,
private_share_commitment: felt,
external_transfers_len: felt,
external_transfers: ExternalTransfer*,
encryption_blob_len: felt,
encryption_blob: felt*,
public_wallet_share_len: felt,
public_wallet_share: felt*,
proof_blob_len: felt,
proof_blob: felt*,
) -> (new_root: felt) {
let (new_root) = Darkpool.update_wallet(
commitment=commitment,
match_nullifier=match_nullifier,
spend_nullifier=spend_nullifier,
internal_transfer_ciphertext_len=internal_transfer_ciphertext_len,
internal_transfer_ciphertext=internal_transfer_ciphertext,
old_public_share_nullifier=old_public_share_nullifier,
old_private_share_nullifier=old_private_share_nullifier,
public_share_commitment=public_share_commitment,
private_share_commitment=private_share_commitment,
external_transfers_len=external_transfers_len,
external_transfers=external_transfers,
);

Darkpool.mark_wallet_updated(pk_view=pk_view);
Darkpool.mark_wallet_updated(public_blinder_share=public_blinder_share);
return (new_root=new_root);
}

// @notice encumber two wallets by submitting a successfully completed match to the contract
// @dev for now the arguments are overly verbose, considering that we'll be rewriting this all
// in Cairo 1.0 soon, it's not worth cleaning this up for now.
// @param party0_public_share_nullifier the public share nullifier of the first party's pre-wallet
// @param party0_private_share_nullifier the private share nullifer of the first party's pre-wallet
// @param party1_public_share_nullifier the public share nullifier of the second party's pre-wallet
// @param party1_private_share_nullifier the private share nullifier of the second party's pre-wallet
// @param party0_public_blinder_share the public share of the first party's post-wallet blinder
// @param party1_public_blinder_share the public share of the second party's post-wallet blinder
// @param party0_public_share_commitment a commitment to the first party's public post-wallet share
// @param party0_private_share_commitment a commitment to the first party's private post-wallet share
// @param party1_public_share_commitment a commitment to the second party's public post-wallet share
// @param party1_private_share_commitment a commitment to the second party's private post-wallet share
// @param party0_public_share: The public share of the first party's post-wallet
// @param party1_public_share: The public share of the second party's post-wallet
// @param party0_validity_proof: The validity proof bundle for the first party's match engine inputs
// @param party1_validity_proof: The validity proof bundle for the second party's match engine inputs
// @param valid_match_proof: The proof of `VALID MATCH` for the match submitted
// @param valid_settle_proof: The proof of `VALID SETTLE` for the match settlment
@external
func match{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
match_nullifier1: felt,
match_nullifier2: felt,
party0_note_commit: felt,
party0_note_ciphertext_len: felt,
party0_note_ciphertext: felt*,
party1_note_commit: felt,
party1_note_ciphertext_len: felt,
party1_note_ciphertext: felt*,
relayer0_note_commit: felt,
relayer0_note_ciphertext_len: felt,
relayer0_note_ciphertext: felt*,
relayer1_note_commit: felt,
relayer1_note_ciphertext_len: felt,
relayer1_note_ciphertext: felt*,
protocol_note_commit: felt,
protocol_note_ciphertext_len: felt,
protocol_note_ciphertext: felt*,
proof_blob_len: felt,
proof_blob: felt*,
party0_public_share_nullifier: felt,
party0_private_share_nullifier: felt,
party1_public_share_nullifier: felt,
party1_private_share_nullifier: felt,
party0_public_blinder_share: felt,
party1_public_blinder_share: felt,
party0_public_share_commitment: felt,
party0_private_share_commitment: felt,
party1_public_share_commitment: felt,
party1_private_share_commitment: felt,
party0_public_share_len: felt,
party0_public_share: felt*,
party1_public_share_len: felt,
party1_public_share: felt*,
party0_validity_proof_len: felt,
party0_validity_proof: felt*,
party1_validity_proof_len: felt,
party1_validity_proof: felt*,
valid_match_proof_len: felt,
valid_match_proof: felt*,
valid_settle_proof_len: felt,
valid_settle_proof: felt*,
) -> (new_root: felt) {
let (new_root) = Darkpool.process_match(
match_nullifier1=match_nullifier1,
match_nullifier2=match_nullifier2,
party0_note_commit=party0_note_commit,
party1_note_commit=party1_note_commit,
relayer0_note_commit=relayer0_note_commit,
relayer1_note_commit=relayer1_note_commit,
protocol_note_commit=protocol_note_commit,
party0_public_share_nullifier=party0_public_share_nullifier,
party0_private_share_nullifier=party0_private_share_nullifier,
party1_public_share_nullifier=party1_public_share_nullifier,
party1_private_share_nullifier=party1_private_share_nullifier,
party0_public_share_commitment=party0_public_share_commitment,
party0_private_share_commitment=party0_private_share_commitment,
party1_public_share_commitment=party1_public_share_commitment,
party1_private_share_commitment=party1_private_share_commitment,
);

return (new_root=new_root);
}

// @dev process a settlement, this involves updating the balance of a wallet by nullifying a note
// @param pk_view the public view key of the wallet being updated, used for indexing
// @param from_internal_transfer whether or not the note was generated by an internal transfer
// @param wallet_commitment a commitment to the new wallet
// @param match_nullifier the match nullifier of the old wallet
// @param spend_nullifier the spend nullifier of the old wallet
// @param note_redeem_nullifier a nullifier for the note being redeemed into the wallet
// @return the merkle root after update
@external
func settle{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
pk_view: felt,
from_internal_transfer: felt,
wallet_commitment: felt,
match_nullifier: felt,
spend_nullifier: felt,
note_redeem_nullifier: felt,
wallet_ciphertext_len: felt,
wallet_ciphertext: felt*,
proof_blob_len: felt,
proof_blob: felt*,
) -> (new_root: felt) {
let (new_root) = Darkpool.process_settle(
from_internal_transfer=from_internal_transfer,
wallet_commitment=wallet_commitment,
match_nullifier=match_nullifier,
spend_nullifier=spend_nullifier,
note_redeem_nullifier=note_redeem_nullifier,
);
// Mark both wallets as updated
Darkpool.mark_wallet_updated(public_blinder_share=party0_public_blinder_share);
Darkpool.mark_wallet_updated(public_blinder_share=party1_public_blinder_share);

Darkpool.mark_wallet_updated(pk_view=pk_view);
return (new_root=new_root);
}
Loading

0 comments on commit 4f5fe1d

Please sign in to comment.