Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locking by spending and creating UTXOs #121

Merged
merged 29 commits into from
Jan 29, 2025
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
97f3894
spend inputs for locking
jimthematrix Jan 9, 2025
0e3de1a
Squashed commit of the following:
jimthematrix Jan 9, 2025
5a8e9f2
zeto lock with nullifiers
jimthematrix Jan 9, 2025
ac8c0f9
remove lock verifiers
jimthematrix Jan 14, 2025
4725fba
transfer() and transferLocked() with new circuits
jimthematrix Jan 14, 2025
61a0902
fix anon_nullifier and add locking support to nf_anon
jimthematrix Jan 15, 2025
e710d01
add locking support for NF token implementations
jimthematrix Jan 16, 2025
8a2504e
Add escrow contract based on Zeto_Anon for testing purposes
jimthematrix Jan 17, 2025
444a062
add escrow contracts to test locking flows
jimthematrix Jan 20, 2025
1a5a3a9
fix tests
jimthematrix Jan 22, 2025
eb8723f
fix circuit tests
jimthematrix Jan 22, 2025
7660df1
fix go-sdk integration test
jimthematrix Jan 22, 2025
7b3861b
update checks of inputs and output
jimthematrix Jan 22, 2025
a6a911e
update circuit integration tests
jimthematrix Jan 22, 2025
121185b
fix nf nullifier integration test
jimthematrix Jan 22, 2025
518cd5b
fix nf anon nullifier integration test
jimthematrix Jan 22, 2025
ff3fe10
fix test for the escrow flow
jimthematrix Jan 22, 2025
63a902a
fix anon_nullifier build and tests for batching
jimthematrix Jan 23, 2025
5f1a2df
fix deployment parameters for the factory
jimthematrix Jan 23, 2025
f4dff77
change the verifier initialization params to a struct
jimthematrix Jan 23, 2025
64966b5
move verifiers to the contracts/verifiers folder
jimthematrix Jan 23, 2025
c6c2d7e
fix factory unit tests
jimthematrix Jan 23, 2025
e7e96b7
fix tests for using the cloneable factory
jimthematrix Jan 24, 2025
073918c
consolidate solidity interfaces
jimthematrix Jan 24, 2025
7323357
use proof to check for existence in the locked commitments tree
jimthematrix Jan 27, 2025
a9c95c7
renaming of internal variables
jimthematrix Jan 27, 2025
1c54398
Update doc-site/docs/advanced/erc20-tokens-integration.md
jimthematrix Jan 28, 2025
8ced077
test for duplicate output utxos
Chengxuan Jan 29, 2025
925ee0c
Update inline comments in the transferLocked circuit
jimthematrix Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Squashed commit of the following:
commit 97a67ac88dba76eabb78f844be83a94d147939bf
Author: Jim Zhang <[email protected]>
Date:   Wed Jan 8 16:46:48 2025 -0500

    update locking for nullifier based tokens

    Signed-off-by: Jim Zhang <[email protected]>

commit dc5de95cc538736fbb2d4156ad952f51020a60a2
Author: Jim Zhang <[email protected]>
Date:   Wed Jan 8 16:45:38 2025 -0500

    spend inputs when locking

    Signed-off-by: Jim Zhang <[email protected]>

commit 07b42e72db8c94c81ec87c11c1b419507b858550
Merge: 7751a58 cb39891
Author: Jim Zhang <[email protected]>
Date:   Mon Jan 6 11:18:22 2025 -0500

    Merge branch 'main' into locking

commit 7751a5808ddcbf54db643d9f8c969d86b84677a3
Author: Jim Zhang <[email protected]>
Date:   Sun Jan 5 21:50:45 2025 -0500

    --amend

commit 5d1314b1262531a8cc4af9d084e5016e0984d2a8
Author: Jim Zhang <[email protected]>
Date:   Fri Jan 3 17:17:31 2025 -0500

    enhance locking with outputs

    Signed-off-by: Jim Zhang <[email protected]>

Signed-off-by: Jim Zhang <[email protected]>
jimthematrix committed Jan 14, 2025
commit 0e3de1af82ef7e64d090a2fed6bacd63ecbbd301
29 changes: 4 additions & 25 deletions solidity/contracts/lib/zeto_lock.sol
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
pragma solidity ^0.8.27;

import {IZetoBase} from "./interfaces/izeto_base.sol";
import {IZetoLockable, ILockVerifier, IBatchLockVerifier} from "./interfaces/izeto_lockable.sol";
import {IZetoLockable} from "./interfaces/izeto_lockable.sol";
import {Commonlib} from "./common.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

@@ -32,16 +32,10 @@ abstract contract ZetoLock is IZetoBase, IZetoLockable, OwnableUpgradeable {
// by the same party that did the locking.
mapping(uint256 => address) internal lockedUTXOs;

ILockVerifier internal _lockVerifier;
IBatchLockVerifier internal _batchLockVerifier;

function __ZetoLock_init(
ILockVerifier lockVerifier,
IBatchLockVerifier batchLockVerifier
) public onlyInitializing {
_lockVerifier = lockVerifier;
_batchLockVerifier = batchLockVerifier;
}
address lockVerifier,
address batchLockVerifier
) public onlyInitializing {}

// Locks the UTXOs so that they can only be spent by submitting the appropriate
// proof from the Eth account designated as the "delegate". This function
@@ -117,19 +111,4 @@ abstract contract ZetoLock is IZetoBase, IZetoLockable, OwnableUpgradeable {
}
return true;
}

function _verifyLockProof(
uint256[2] memory utxos,
Commonlib.Proof calldata proof
) internal view returns (bool) {
return _lockVerifier.verifyProof(proof.pA, proof.pB, proof.pC, utxos);
}

function _verifyBatchLockProof(
uint256[10] memory utxos,
Commonlib.Proof calldata proof
) internal view returns (bool) {
return
_batchLockVerifier.verifyProof(proof.pA, proof.pB, proof.pC, utxos);
}
}
124 changes: 124 additions & 0 deletions solidity/contracts/lib/zeto_lock_nullifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pragma solidity ^0.8.27;

import {IZetoBase} from "./interfaces/izeto_base.sol";
import {IZetoLockable, ILockVerifier, IBatchLockVerifier} from "./interfaces/izeto_lockable.sol";
import {Commonlib} from "./common.sol";
import {SmtLib} from "@iden3/contracts/lib/SmtLib.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

/// @title A sample base implementation of a Zeto based token contract
/// without using nullifiers. Each UTXO's spending status is explicitly tracked.
/// @author Kaleido, Inc.
/// @dev Implements common functionalities of Zeto based tokens without nullifiers
abstract contract ZetoLock is IZetoBase, IZetoLockable, OwnableUpgradeable {
// used for multi-step transaction flows that require counterparties
// to upload proofs. To protect the party that uploads their proof first,
// and prevent any other party from utilizing the uploaded proof to execute
// a transaction, the input UTXOs or nullifiers can be locked and only usable
// by the same party that did the locking.
SmtLib.Data internal _lockedCommitmentsTree;
using SmtLib for SmtLib.Data;
mapping(uint256 => bool) private _nullifiersForLockedCommitments;

ILockVerifier internal _lockVerifier;
IBatchLockVerifier internal _batchLockVerifier;

function __ZetoLock_init(
ILockVerifier lockVerifier,
IBatchLockVerifier batchLockVerifier
) public onlyInitializing {
_lockVerifier = lockVerifier;
_batchLockVerifier = batchLockVerifier;
_lockedCommitmentsTree.initialize(MAX_SMT_DEPTH);
}

// Locks the UTXOs so that they can only be spent by submitting the appropriate
// proof from the Eth account designated as the "delegate". This function
// should be called by escrow contracts that will use uploaded proofs
// to execute transactions, in order to prevent the proof from being used
// by parties other than the escrow contract.
function _lock(
uint256[] memory inputs,
uint256[] memory outputs,
uint256[] memory lockedOutputs,
address delegate,
bytes calldata data
) public {
for (uint256 i = 0; i < lockedOutputs.length; ++i) {
if (lockedOutputs[i] == 0) {
continue;
}
if (
lockedUTXOs[lockedOutputs[i]] != address(0) &&
lockedUTXOs[lockedOutputs[i]] != msg.sender
) {
revert UTXOAlreadyLocked(lockedOutputs[i]);
}
lockedUTXOs[lockedOutputs[i]] = delegate;
}

emit UTXOsLocked(
inputs,
outputs,
lockedOutputs,
delegate,
msg.sender,
data
);
}

// move the ability to spend the locked UTXOs to the delegate account.
// The sender must be the current delegate.
//
// Setting the delegate to address(0) will unlock the UTXOs.
function delegateLock(
uint256[] memory utxos,
address delegate,
bytes calldata data
) public {
for (uint256 i = 0; i < utxos.length; ++i) {
if (utxos[i] == 0) {
continue;
}
if (lockedUTXOs[utxos[i]] != msg.sender) {
revert NotLockDelegate(utxos[i], delegate, msg.sender);
}
lockedUTXOs[utxos[i]] = delegate;
}

emit LockDelegateChanged(utxos, msg.sender, delegate, data);
}

function validateLockedStates(
uint256[] memory utxos
) internal returns (bool) {
for (uint256 i = 0; i < utxos.length; ++i) {
if (utxos[i] == 0) {
continue;
}
// check if the UTXO has been locked
if (lockedUTXOs[utxos[i]] != address(0)) {
if (lockedUTXOs[utxos[i]] != msg.sender) {
revert UTXOAlreadyLocked(utxos[i]);
}
delete lockedUTXOs[utxos[i]];
}
}
return true;
}
}
2 changes: 1 addition & 1 deletion solidity/contracts/lib/zeto_nullifier.sol
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
pragma solidity ^0.8.27;

import {IZetoBase} from "./interfaces/izeto_base.sol";
import {MAX_SMT_DEPTH} from "./interfaces/izeto_nullifier.sol";
import {ZetoCommon} from "./zeto_common.sol";
import {SmtLib} from "@iden3/contracts/lib/SmtLib.sol";
import {PoseidonUnit3L} from "@iden3/contracts/lib/Poseidon.sol";
@@ -24,7 +25,6 @@ import {PoseidonUnit3L} from "@iden3/contracts/lib/Poseidon.sol";
/// @author Kaleido, Inc.
/// @dev Implements common functionalities of Zeto based tokens using nullifiers
abstract contract ZetoNullifier is IZetoBase, ZetoCommon {
uint256 public constant MAX_SMT_DEPTH = 64;
SmtLib.Data internal _commitmentsTree;
using SmtLib for SmtLib.Data;
mapping(uint256 => bool) private _nullifiers;
Loading