Skip to content

Commit

Permalink
Add the fungilble token sample implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Aug 14, 2024
1 parent be50e14 commit 933521e
Show file tree
Hide file tree
Showing 3 changed files with 478 additions and 11 deletions.
48 changes: 37 additions & 11 deletions solidity/contracts/zeto_anon_enc_nullifier_non_repudiation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pragma solidity ^0.8.20;

import {Groth16Verifier_CheckHashesValue} from "./lib/verifier_check_hashes_value.sol";
import {Groth16Verifier_CheckNullifierValue} from "./lib/verifier_check_nullifier_value.sol";
import {Groth16Verifier_AnonEncNullifier} from "./lib/verifier_anon_enc_nullifier.sol";
import {Groth16Verifier_AnonEncNullifierNonRepudiation} from "./lib/verifier_anon_enc_nullifier_non_repudiation.sol";
import {ZetoNullifier} from "./lib/zeto_nullifier.sol";
import {ZetoFungibleWithdrawWithNullifiers} from "./lib/zeto_fungible_withdraw_nullifier.sol";
import {Registry} from "./lib/registry.sol";
Expand Down Expand Up @@ -53,6 +53,14 @@ contract Zeto_AnonEncNullifierNonRepudiation is
verifier = _verifier;
}

function setAuthority(uint256[2] memory _authority) public onlyOwner {
authority = _authority;
}

function getAuthority() public view returns (uint256[2] memory) {
return authority;
}

/**
* @dev the main function of the contract, which transfers values from one account (represented by Babyjubjub public keys)
* to one or more receiver accounts (also represented by Babyjubjub public keys). One of the two nullifiers may be zero
Expand All @@ -72,7 +80,7 @@ contract Zeto_AnonEncNullifierNonRepudiation is
uint256[2] memory outputs,
uint256 root,
uint256 encryptionNonce,
uint256[2] memory encryptedValues,
uint256[16] memory encryptedValues,
Commonlib.Proof calldata proof
) public returns (bool) {
require(
Expand All @@ -81,17 +89,33 @@ contract Zeto_AnonEncNullifierNonRepudiation is
);

// construct the public inputs
uint256[10] memory publicInputs;
uint256[26] memory publicInputs;
publicInputs[0] = encryptedValues[0]; // encrypted value for the receiver UTXO
publicInputs[1] = encryptedValues[1]; // encrypted salt for the receiver UTXO
publicInputs[2] = nullifiers[0];
publicInputs[3] = nullifiers[1];
publicInputs[4] = root;
publicInputs[5] = (nullifiers[0] == 0) ? 0 : 1; // if the first nullifier is empty, disable its MT proof verification
publicInputs[6] = (nullifiers[1] == 0) ? 0 : 1; // if the second nullifier is empty, disable its MT proof verification
publicInputs[7] = outputs[0];
publicInputs[8] = outputs[1];
publicInputs[9] = encryptionNonce;
publicInputs[2] = encryptedValues[2]; // encrypted input owner public key[0]
publicInputs[3] = encryptedValues[3]; // encrypted input owner public key[1]
publicInputs[4] = encryptedValues[4]; // encrypted input value[0]
publicInputs[5] = encryptedValues[5]; // encrypted input salt[0]
publicInputs[6] = encryptedValues[6]; // encrypted input value[1]
publicInputs[7] = encryptedValues[7]; // encrypted input salt[1]
publicInputs[8] = encryptedValues[8]; // encrypted first output owner public key[0]
publicInputs[9] = encryptedValues[9]; // encrypted first output owner public key[1]
publicInputs[10] = encryptedValues[10]; // encrypted second output owner public key[0]
publicInputs[11] = encryptedValues[11]; // encrypted second output owner public key[1]
publicInputs[12] = encryptedValues[12]; // encrypted output value[0]
publicInputs[13] = encryptedValues[13]; // encrypted output salt[0]
publicInputs[14] = encryptedValues[14]; // encrypted output value[1]
publicInputs[15] = encryptedValues[15]; // encrypted output salt[1]
publicInputs[16] = nullifiers[0];
publicInputs[17] = nullifiers[1];
publicInputs[18] = root;
publicInputs[19] = (nullifiers[0] == 0) ? 0 : 1; // if the first nullifier is empty, disable its MT proof verification
publicInputs[20] = (nullifiers[1] == 0) ? 0 : 1; // if the second nullifier is empty, disable its MT proof verification
publicInputs[21] = outputs[0];
publicInputs[22] = outputs[1];
publicInputs[23] = encryptionNonce;
publicInputs[24] = authority[0];
publicInputs[25] = authority[1];

// // Check the proof
require(
Expand All @@ -110,6 +134,8 @@ contract Zeto_AnonEncNullifierNonRepudiation is
for (uint256 i = 0; i < nullifiers.length; ++i) {
nullifierArray[i] = nullifiers[i];
outputArray[i] = outputs[i];
}
for (uint256 i = 0; i < encryptedValues.length; ++i) {
encryptedValuesArray[i] = encryptedValues[i];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
import { SmtLibModule, DepositVerifierModule, WithdrawNullifierVerifierModule } from "./lib/deps";

const VerifierModule = buildModule("Groth16Verifier_AnonEncNullifierNonRepudiation", (m) => {
const verifier = m.contract('Groth16Verifier_AnonEncNullifierNonRepudiation', []);
return { verifier };
});

export default buildModule("Zeto_AnonEncNullifierNonRepudiation", (m) => {
const { smtLib, poseidon3 } = m.useModule(SmtLibModule);
const { verifier } = m.useModule(VerifierModule);
const { verifier: depositVerifier } = m.useModule(DepositVerifierModule);
const { verifier: withdrawVerifier } = m.useModule(WithdrawNullifierVerifierModule);
const commonlib = m.library('Commonlib');

const zeto = m.contract('Zeto_AnonEncNullifierNonRepudiation', [depositVerifier, withdrawVerifier, verifier], {
libraries: {
SmtLib: smtLib,
PoseidonUnit3L: poseidon3,
Commonlib: commonlib,
},
});

return { zeto };
});
Loading

0 comments on commit 933521e

Please sign in to comment.