国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 js教程 探索 Mina 協(xié)議:zk 應(yīng)用程序的實(shí)際用例

探索 Mina 協(xié)議:zk 應(yīng)用程序的實(shí)際用例

Dec 29, 2024 am 07:32 AM

Zkapps(零知識(shí)應(yīng)用)是由零知識(shí)證明支持的 mina 協(xié)議智能合約,特別是 zk-Snarks [零知識(shí)簡(jiǎn)潔非交互式知識(shí)論證]。zkapps 取代了 snapps [智能非交互式知識(shí)論證]應(yīng)用]。 ZkApp 智能合約是使用 o1js(一個(gè) TypeScript 庫(kù))編寫的。 zkApps 在用戶的 Web 瀏覽器中運(yùn)行客戶端,并僅發(fā)布一個(gè)小的有效性證明,然后由 Mina 節(jié)點(diǎn)進(jìn)行驗(yàn)證。 Zkapp 由智能合約和 UI 組成,我將在下一節(jié)中進(jìn)一步描述。

應(yīng)用

我創(chuàng)建了關(guān)于年齡驗(yàn)證的 zkapp,其中用戶年齡在不干預(yù)個(gè)人數(shù)據(jù)的情況下得到驗(yàn)證。

我繼續(xù)安裝 zkapp-cli npm 包,它實(shí)際上創(chuàng)建了用于繼續(xù)使用證明器函數(shù)和驗(yàn)證器函數(shù)的模板,作為 zk 證明構(gòu)建過程的一部分

執(zhí)行

下面是添加驗(yàn)證自定義邏輯的實(shí)現(xiàn)。它定義了 zk-SNARK 的電路邏輯,在證明生成過程中使用。實(shí)際的證明者函數(shù)由 o1js 庫(kù)管理,并在使用私有輸入在鏈外執(zhí)行 zkApp 方法時(shí)調(diào)用。

import { Field, SmartContract, state, State, method } from 'o1js';

/**
 * Private Age Verification Contract
 * The contract will verify if the user's age is greater than or equal to the threshold age.
 * The contract uses zero-knowledge proofs to keep the user's age private.
 */
export class AgeVerification extends SmartContract {
  // State variable to store the verification result (valid or invalid)
  @state(Field) valid = State<Field>();

  // Method to initialize the state
  init() {
    super.init();
    this.valid.set(Field(0)); // Default is invalid
  }

  // Method to verify the age
  @method async verifyAge(age: Field, threshold: Field) {


    // Compute age - threshold
    const difference = age.sub(threshold);

    // Use circuit-compatible logic to check if the difference is non-negative
    const isValid = difference.equals(Field(0)).or(difference.greaterThanOrEqual(Field(0)))
      ? Field(1)
      : Field(0);

    // Set the validity of the verification result
    this.valid.set(isValid);
  }
}


以下腳本是與 AgeVerification zkApp 交互的測(cè)試套件。它在 txn.prove() 期間調(diào)用證明者邏輯,并通過檢查其更新狀態(tài)來驗(yàn)證 zkApp 的行為。

實(shí)際的證明者功能位于底層的 zkApp 方法(verifyAge)中,txn.prove() 是在測(cè)試過程中生成證明的機(jī)制。

為了測(cè)試輸入,我編輯了測(cè)試腳本,如下所示。

import { AccountUpdate, Field, Mina, PrivateKey, PublicKey } from 'o1js';
import { AgeVerification } from './AgeVerification'; // Import the correct contract

let proofsEnabled = false;

describe('AgeVerification', () => {
  let deployerAccount: Mina.TestPublicKey,
    deployerKey: PrivateKey,
    senderAccount: Mina.TestPublicKey,
    senderKey: PrivateKey,
    zkAppAddress: PublicKey,
    zkAppPrivateKey: PrivateKey,
    zkApp: AgeVerification; // Update to use AgeVerification instead of Add

  beforeAll(async () => {
    if (proofsEnabled) await AgeVerification.compile(); // Update compile for AgeVerification
  });

  beforeEach(async () => {
    const Local = await Mina.LocalBlockchain({ proofsEnabled });
    Mina.setActiveInstance(Local);
    [deployerAccount, senderAccount] = Local.testAccounts;
    let feePayer = Local.testAccounts[0].key;
    deployerKey = deployerAccount.key;
    senderKey = senderAccount.key;

    zkAppPrivateKey = PrivateKey.random();
    zkAppAddress = zkAppPrivateKey.toPublicKey();
    zkApp = new AgeVerification(zkAppAddress); // Instantiate AgeVerification contract
  });



  async function localDeploy() {
    const txn = await Mina.transaction(deployerAccount, async () => {
      AccountUpdate.fundNewAccount(deployerAccount);
      await zkApp.deploy();
    });
    await txn.prove();
    // this tx needs .sign(), because `deploy()` adds an account update that requires signature authorization
    await txn.sign([deployerKey, zkAppPrivateKey]).send();
  }

  it('generates and deploys the `AgeVerification` smart contract', async () => {
    await localDeploy();
    const valid = zkApp.valid.get(); // Access the 'valid' state variable
    expect(valid).toEqual(Field(0)); // Initially, the contract should set 'valid' to Field(0)
  });

  it('correctly verifies the age in the `AgeVerification` smart contract', async () => {
    await localDeploy();

    const age = Field(25); // Example age value
    const threshold = Field(18); // Example threshold value

    // Call the verifyAge method
    const txn = await Mina.transaction(senderAccount, async () => {
      await zkApp.verifyAge(age, threshold); // Use the verifyAge method
    });
    await txn.prove();
    await txn.sign([senderKey]).send();

    const valid = zkApp.valid.get(); // Check the validity state after verification
    expect(valid).toEqual(Field(1)); // Expected to be valid if age >= threshold
  });
});


以下是測(cè)試結(jié)果

Exploring the Mina Protocol: Practical Use Cases for zk Applications

我在interact.ts文件中添加了證明者機(jī)制,它基本上生成一個(gè)zk-SNARK證明,并在mina區(qū)塊鏈中進(jìn)行交易時(shí)提交證明。當(dāng) interact.ts 腳本生成證明時(shí),驗(yàn)證是在處理交易時(shí)由 Mina 區(qū)塊鏈執(zhí)行的。這是 zk-SNARK 系統(tǒng)的一個(gè)關(guān)鍵方面,證明者生成驗(yàn)證者(Mina 網(wǎng)絡(luò))檢查的證明。

import fs from 'fs/promises';
import { Mina, NetworkId, PrivateKey, Field } from 'o1js';
import { AgeVerification } from './AgeVerification'; 

// check command line arg
let deployAlias = process.argv[2];
if (!deployAlias)
  throw Error(`Missing <deployAlias> argument.

Usage:
node build/src/interact.js <deployAlias>
`);
Error.stackTraceLimit = 1000;
const DEFAULT_NETWORK_ID = 'testnet';

// parse config and private key from file
type Config = {
  deployAliases: Record<
    string,
    {
      networkId?: string;
      url: string;
      keyPath: string;
      fee: string;
      feepayerKeyPath: string;
      feepayerAlias: string;
    }
  >;
};
let configJson: Config = JSON.parse(await fs.readFile('config.json', 'utf8'));
let config = configJson.deployAliases[deployAlias];
let feepayerKeysBase58: { privateKey: string; publicKey: string } = JSON.parse(
  await fs.readFile(config.feepayerKeyPath, 'utf8')
);

let zkAppKeysBase58: { privateKey: string; publicKey: string } = JSON.parse(
  await fs.readFile(config.keyPath, 'utf8')
);

let feepayerKey = PrivateKey.fromBase58(feepayerKeysBase58.privateKey);
let zkAppKey = PrivateKey.fromBase58(zkAppKeysBase58.privateKey);

// set up Mina instance and contract we interact with
const Network = Mina.Network({
  // We need to default to the testnet networkId if none is specified for this deploy alias in config.json
  // This is to ensure the backward compatibility.
  networkId: (config.networkId ?? DEFAULT_NETWORK_ID) as NetworkId,
  mina: config.url,
});

const fee = Number(config.fee) * 1e9; // in nanomina (1 billion = 1.0 mina)
Mina.setActiveInstance(Network);
let feepayerAddress = feepayerKey.toPublicKey();
let zkAppAddress = zkAppKey.toPublicKey();
let zkApp = new AgeVerification(zkAppAddress);

let age = Field(25); // Example age
let threshold = Field(18); // Example threshold age

// compile the contract to create prover keys
console.log('compile the contract...');
await AgeVerification.compile();

try {
  // call verifyAge() and send transaction
  console.log('build transaction and create proof...');
  let tx = await Mina.transaction(
    { sender: feepayerAddress, fee },
    async () => {
      await zkApp.verifyAge(age, threshold); // Replacing update() with verifyAge
    }
  );
  await tx.prove();

  console.log('send transaction...');
  const sentTx = await tx.sign([feepayerKey]).send();
  if (sentTx.status === 'pending') {
    console.log(
      '\nSuccess! Age verification transaction sent.\n' +
        '\nYour smart contract state will be updated' +
        `\nas soon as the transaction is included in a block:` +
        `\n${getTxnUrl(config.url, sentTx.hash)}`
    );
  }
} catch (err) {
  console.log(err);
}

function getTxnUrl(graphQlUrl: string, txnHash: string | undefined) {
  const hostName = new URL(graphQlUrl).hostname;
  const txnBroadcastServiceName = hostName
    .split('.')
    .filter((item) => item === 'minascan')?.[0];
  const networkName = graphQlUrl
    .split('/')
    .filter((item) => item === 'mainnet' || item === 'devnet')?.[0];
  if (txnBroadcastServiceName && networkName) {
    return `https://minascan.io/${networkName}/tx/${txnHash}?type=zk-tx`;
  }
  return `Transaction hash: ${txnHash}`;
}


我使用的年齡和閾值輸入為 25 和 18。

由于測(cè)試已通過運(yùn)行 npm run test 成功完成。我繼續(xù)使用 zk config 在 devnet 上進(jìn)行部署

我提供了以下輸入:

部署別名:test
網(wǎng)絡(luò)類型:測(cè)試網(wǎng)
網(wǎng)址:https://api.minascan.io/node/devnet/v1/graphql
付費(fèi)者:新的付費(fèi)者密鑰
交易:0.1

可以從這里檢索 URL:

Exploring the Mina Protocol: Practical Use Cases for zk Applications

然后在部署后我得到了以下響應(yīng)。

Exploring the Mina Protocol: Practical Use Cases for zk Applications

Exploring the Mina Protocol: Practical Use Cases for zk Applications

合約部署在以下devnet

部署后,我繼續(xù)使用 UI,通過提供 RPC URL 和部署的合約地址,選擇簡(jiǎn)單的 html、css 和 js,這是最終的 UI。

Exploring the Mina Protocol: Practical Use Cases for zk Applications

將智能合約與UI集成后zkapp的創(chuàng)建就完成了。在為 AgeVerification zkApp 構(gòu)建用戶界面 (UI) 后,前端與智能合約的集成允許用戶與零知識(shí)證明系統(tǒng)無縫交互。 UI 有助于向合約提交用戶年齡和閾值數(shù)據(jù),同時(shí)通過 zk-SNARK 維護(hù)隱私。這使得用戶能夠在不透露實(shí)際值的情況下驗(yàn)證自己的年齡,從而保持機(jī)密性。后端利用證明者功能生成證明,Mina 區(qū)塊鏈對(duì)其進(jìn)行有效驗(yàn)證。這種端到端解決方案可確保安全、用戶友好的體驗(yàn),同時(shí)充分利用 Mina 基于 zk-SNARK 的架構(gòu)提供的隱私和可擴(kuò)展性功能。

以上是探索 Mina 協(xié)議:zk 應(yīng)用程序的實(shí)際用例的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

在JavaScript中使用哪些評(píng)論符號(hào):一個(gè)明確的解釋 在JavaScript中使用哪些評(píng)論符號(hào):一個(gè)明確的解釋 Jun 12, 2025 am 10:27 AM

在JavaScript中,選擇單行注釋(//)還是多行注釋(//)取決于注釋的目的和項(xiàng)目需求:1.使用單行注釋進(jìn)行快速、內(nèi)聯(lián)的解釋;2.使用多行注釋進(jìn)行詳細(xì)的文檔說明;3.保持注釋風(fēng)格的一致性;4.避免過度注釋;5.確保注釋與代碼同步更新。選擇合適的注釋風(fēng)格有助于提高代碼的可讀性和可維護(hù)性。

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語(yǔ)言,各自適用于不同的應(yīng)用場(chǎng)景。Java用于大型企業(yè)和移動(dòng)應(yīng)用開發(fā),而JavaScript主要用于網(wǎng)頁(yè)開發(fā)。

JavaScript評(píng)論:簡(jiǎn)短說明 JavaScript評(píng)論:簡(jiǎn)短說明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

掌握J(rèn)avaScript評(píng)論:綜合指南 掌握J(rèn)avaScript評(píng)論:綜合指南 Jun 14, 2025 am 12:11 AM

評(píng)論arecrucialinjavascriptformaintainingclarityclarityandfosteringCollaboration.1)heelpindebugging,登機(jī),andOnderStandingCodeeVolution.2)使用林格forquickexexplanations andmentmentsmmentsmmentsmments andmmentsfordeffordEffordEffordEffordEffordEffordEffordEffordEddeScriptions.3)bestcractices.3)bestcracticesincracticesinclud

JavaScript數(shù)據(jù)類型:深度潛水 JavaScript數(shù)據(jù)類型:深度潛水 Jun 13, 2025 am 12:10 AM

JavaScripthasseveralprimitivedatatypes:Number,String,Boolean,Undefined,Null,Symbol,andBigInt,andnon-primitivetypeslikeObjectandArray.Understandingtheseiscrucialforwritingefficient,bug-freecode:1)Numberusesa64-bitformat,leadingtofloating-pointissuesli

JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

如何在JS中與日期和時(shí)間合作? 如何在JS中與日期和時(shí)間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時(shí)間處理需注意以下幾點(diǎn):1.創(chuàng)建Date對(duì)象有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設(shè)置時(shí)間信息可用get和set方法,注意月份從0開始;3.手動(dòng)格式化日期需拼接字符串,也可使用第三方庫(kù);4.處理時(shí)區(qū)問題建議使用支持時(shí)區(qū)的庫(kù),如Luxon。掌握這些要點(diǎn)能有效避免常見錯(cuò)誤。

JavaScript:探索用于高效編碼的數(shù)據(jù)類型 JavaScript:探索用于高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

See all articles