Zcash中的Notes_zcash shielded protocol-程序员宅基地

技术标签: Zcash  

1. 引言

本文主要关注Sapling note。

Zcash中Sapling note以 n \mathbf{n} n表示,其代表a value v v v is spendable by the recipient who holds the spending key corresponding to a given shielded payment address。

1.1 相关约定

  • MAX_MONEY \text{MAX\_MONEY} MAX_MONEY:为 2.1 ∗ 1 0 15 2.1*10^{15} 2.11015 (zatoshi)
  • l P R F n f S a p l i n g l_{PRFnfSapling} lPRFnfSapling:为256。
  • l d l_d ld: 为88。
  • N o t e C o m m i t S a p l i n g NoteCommit^{Sapling} NoteCommitSapling:为Windowed Pedersen commitment。
  • K A S a p l i n g KA^{Sapling} KASapling:为Sapling Key Agreement。
  • D i v e r s i f y H a s h S a p l i n g DiversifyHash^{Sapling} DiversifyHashSapling:为Hash Function。
  • l v a l u e l_{value} lvalue:为64。
  • l M e r k l e S a p l i n g l_{Merkle}^{Sapling} lMerkleSapling:为255。

2. Sapling note

Sapling note的组成为tuple n = ( d , p k d , v , r c m ) \mathbf{n}=(d, pk_d, v, rcm) n=(d,pkd,v,rcm),其中:

  • d d d:为接收方的shielded payment address对应的diversifier值。【为88bit, B [ l d ] \mathbb{B}^{[l_d]} B[ld]
  • p k d pk_d pkd:为接收方的shielded payment address对应的diversified transmission key。【为Jubjub point, K A S a p l i n g . P u b l i c P r i m e S u b g r o u p KA^{Sapling}.PublicPrimeSubgroup KASapling.PublicPrimeSubgroup
  • v v v:取值范围为 MAX_MONEY \text{MAX\_MONEY} MAX_MONEY,代表note中的value值,单位为zatoshi。
  • r c m rcm rcm:为random commitment trapdoor。【 N o t e C o m m i t S a p l i n g . T r a p d o o r NoteCommit^{Sapling}.Trapdoor NoteCommitSapling.Trapdoor

可将a Sapling note表示为 N o t e S a p l i n g Note^{Sapling} NoteSapling
N o t e S a p l i n g = B [ l d ] × K A S a p l i n g . P u b l i c P r i m e S u b g r o u p × MAX_MONEY × N o t e C o m m i t S a p l i n g . T r a p d o o r Note^{Sapling}=\mathbb{B}^{[l_d]}\times KA^{Sapling}.PublicPrimeSubgroup\times \text{MAX\_MONEY}\times NoteCommit^{Sapling}.Trapdoor NoteSapling=B[ld]×KASapling.PublicPrimeSubgroup×MAX_MONEY×NoteCommitSapling.Trapdoor

可通过”Sending Notes“来创建新的note,当note已发送,仅对以上值的commitment会对外公开,同时增加一个叫做”note commitment tree“的data structure。而具体的value值和接收方可继续保密,当需要spend the note时,仅需在zk-SNARK proof中使用该commitment值,来验证该note确实存在于链上。

n = ( d , p k d , v , r c m ) \mathbf{n}=(d, pk_d, v, rcm) n=(d,pkd,v,rcm) Sapling note的commit计算方式为:

  • g d = D i v e r s i f y H a s h S a p l i n g ( d ) g_d=DiversifyHash^{Sapling}(d) gd=DiversifyHashSapling(d)
  • g d g_d gd为无效point,则返回 ⊥ \perp ;否则返回 N o t e C o m m i t r c m S a p l i n g ( r e p r J ( g d ) , r e p r J ( p k d ) , v ) NoteCommit_{rcm}^{Sapling}(repr_{\mathbb{J}}(g_d),repr_{\mathbb{J}}(pk_d),v) NoteCommitrcmSapling(reprJ(gd),reprJ(pkd),v)

注意Sapling note中并不包含 ρ \rho ρ元素,若已知该note在note commitment tree中的位置,可计算出相应的 ρ \rho ρ值。已知note position p o s pos pos 的Sapling note可称为 positioned note。

note的nullifier可表示为 n f nf nf
Sapling note的nullifier值可根据 ρ \rho ρ值和接收方的nullifier deriving key n k nk nk 计算获得。

在nullifier 的计算过程中,会用到Pseudo Random Function。

Sapling note spent的过程为:

  • 对外公开spent note对应的nullifier n f nf nf值, n f nf nf值可用于防止双花;
  • prove knowledge of ( ρ , a k , n s k ) (\rho, ak,nsk) (ρ,ak,nsk) in zero knowledge;
  • 额外还需要a spend authorization signature,用于证明knowledge of a s k ask ask

2.1 Sapling note plaintext and memo field

区块链上的transmitted notes是以密文形式存储,同时附加有该note的commitment c m cm cm值。

Sapling note plaintext可表示为 n p \mathbf{np} np,其组成有:
( l e a d B y t e : B Y , d : B [ l d ] , v : { 0.. 2 l v a l u e − 1 } , m e m o : B Y [ 512 ] ) (leadByte:\mathbb{B}^{\mathbb{Y}}, d:\mathbb{B}^{[l_d]},v:\{0..2^{l_{value}}-1\}, memo:\mathbb{B}^{\mathbb{Y}^{[512]}}) (leadByte:BY,d:B[ld],v:{ 0..2lvalue1},memo:BY[512])

其中 d , v d,v d,v的含义同上, m e m o memo memo为与该note关联的512字节的memo field。
The usage of the m e m o memo memo field is by agreement between the sender and recipient of the note。

  • JoinSplit description中:采用相应的transmission keys p k e n c , 1.. N n e w n e w pk_{enc,1..N^{new}}^{new} pkenc,1..Nnewnew 来对note plaintext进行加密。
  • Output description中:采用diversified payment address ( d , p k d ) (d,pk_d) (d,pkd) 来对note plaintext进行加密。

3. Zcash区块链

在特定时间,每一个full validator都会感知到a set of candidate blocks。这些候选区块以tree的形式存在,该tree的root根为genesis block。tree中的每个节点都通过区块头中的”hashPrevBlock“ field来指向其父节点。
tree中,由根节点到某叶子节点的路径中包含了一个或多个符合共识规则的区块,该路径称为valid block chain。

区块链中的每个区块都有相应的block height。genesis block的区块高度为0,后续区块的区块高度依次加1。

为了从整个block tree中选择best valid block chain,区块链节点会对每一个valid block chain中的work求和,并认为work值最大的路径为the best valid block chain。为了断开leaf block之间的关系,区块链节点会优先选择其先收到的区块。

所谓共识协议,是用于保证在任意区块高度,绝大多数的区块链节点都对到那高度的best valid block chain 达成共识。

3.1 Zcash区块链交易及treestate

每个区块包含1笔或多笔交易。

交易中的transparent inputs值会插入到交易对应的”transparent transaction value pool“中,而transparent outputs会将相应的值从该pool中移除。在Bitcoin中,pool中剩余的值则作为矿工的费用。同时要求pool中剩余的值必须为非负数。

Sapling中,每笔交易都有相应的initial treestates,每个treestate包含有:

  • a note commitment tree
  • a nullifier set

UTXO (Unspent Transaction Output) validate state 针对的是transaprent inputs and outputs。

note commitment tree的Merkle tree root称为anchor。根据该Merkle tree hash function的安全假设属性,anchor可唯一确定该note commitment tree的state。
由于nullifier set总是和note commitment tree同步更新,anchor也可标识所关联nullifier set的特定状态。

Sapling区块链中,treestates的链接方式为:

  • 第一个区块的input treestate为空;
  • 区块中第一笔交易的input treestate为前一区块的final treestate;
  • 同一区块中(除第一笔交易外)的每一笔交易的input treestate为前一笔交易的output treestate。
  • 区块的final treestate为其最后一笔交易的output treestate。

Sprout中的JoinSplit description存在interstitial input and output treestate,而Sapling中,不存在interstitial treestate。

JoinSplit transfer中不会用到Sapling notes。

Zcash中设计为:

  • 对于每个shielded input,有单独的Spend transfer;【交易中Spend transfer相关数据为Spend description】
  • 对于每个shielded output,有单独的Output transfer。【交易中Output transfer相关数据为Output description】

每笔交易中包含了一系列的Spend descriptions 和 一系列的Output descriptions。

  • Spend transfer
    Spend transfer 花费 note n o l d \mathbf{n}^{old} nold,其Spend description中包含了 a Pedersen value commitment to the value of the note。其与an instance of a Spend statement关联,会提供a zk-SNARK proof。

  • Output transfer
    Output transfer 创建 note n n e w \mathbf{n}^{new} nnew,其Output description中包含了 a Pedersen value commitment to the value of the note。其与an instance of a Output statement关联,会提供a zk-SNARK proof。

3.2 Note commitment tree

在这里插入图片描述
note commitment tree为具有固定depth的incremental Merkle tree,其用于存储由JoinSplit transfer或Spend transfer产生的note commitments。

note commitment tree 与 Bitcoin中的unspent transaction output set (UTXO set) 的相同之处在于:

  • 都可以express the existence of value and the capability to spend it。

note commitment tree 与 Bitcoin中的unspent transaction output set (UTXO set) 的不同之处在于:

  • note commitment tree为append-only的,无法用其来防止双花。

note commitment tree的root 与 每个treestate 关联。

该incremental Merkle tree中的每个节点都与一个hash值关联,该hash值的长度为255-bit。

令root在0层,则在第 h h h层,有 2 h 2^h 2h个节点,编号为0~ 2 h − 1 2^h-1 2h1。将 h h h i i i节点对应的hash值表示为 M i h M_i^h Mih

所谓note position,是指:
the index of a note’s commitment at the leafmost layer。

3.3 Nullifier set

每个full validator都维护了a nullifier set logically associated with each treestate。
由于valid transaction中可包含JoinSplit transfer或Spend transfer,则相应JoinSplit description或Spend description中的nullifier值会插入到与new treestate 相关的 nullifier set中。

为了防止双花,valid block chain中的nullifier值被强制要求为唯一的。

valid block chain中,同一交易内和跨交易的nullifier值都必须为不重复的。Sprout nullifier和Sapling nullifier尽管二者具有相同的bit pattern,但是可认为二者是不相交的。

3.4 区块奖励,funding streams and founders’ reward

与Bitcoin类似,Zcash也是通过挖区块来铸币,挖区块所创造的值称为区块奖励。
与Bitcoin类似,区块矿工可获得交易费用。

区块奖励,矿工奖励,Founders’ Reward以及funding streams的计算依赖于区块高度。

3.5 coinbase transaction

区块中的第一笔交易称为coinbase transaction。
coinbase transaction会collects and spends any miner subsidy and transaction fees paid by transactions included in this block。

3.6 Zcash的主网和测试网

Zcash protocol 由Electric Coin Company和Zcash Foundation共同管理。

Zcash主网Mainnet的token为ZEC,测试网Testnet的token为TAZ。 1   Z E C = 1 0 8   z a t o s h i , 1   T A Z = 1 0 8   z a t o s h i 1\ ZEC=10^8\ zatoshi,1\ TAZ=10^8\ zatoshi 1 ZEC=108 zatoshi,1 TAZ=108 zatoshi

本节所有的区块hash值都以RPC byte order形式表示(为byte-reversed relative to the normal order for a SHA-256 hash)。
主网Mainnet的genesis block hash值为:00040fe8ec8471911baa1db1266ea15dd06b4a8a5c453883c000b031973dce08
测试网Testnet的genesis block hash值为:
05a60a92d99d85997cce3b87616c089f6124d7342af37106edc76126334a2c38

4. Sending notes (Sapling)

为了send Sapling shielded value,sender需在交易中构建一个或多个 Output description。

o v k ovk ovk为可用于decrypt 该payment的outgoing viewing key, o v k ovk ovk的取值可为以下之一:

  • payment发送方(或发送方之一)的outgoing viewing key;
  • the outgoing viewing key for all payments associated with an “account”,具体见ZIP-32中定义。
  • ⊥ \perp ,if the sender should not be able to decrypt the payment once it has deleted its own copy。

相比于comprise of its own secrets,若sender想要具有forward secrecy of the payment information,可选择 o v k ovk ovk ⊥ \perp

sender为每个Output description选择a value v : { 0.. MAX_MONEY } v:\{0..\text{MAX\_MONEY}\} v:{ 0..MAX_MONEY} 和 a destination Sapling shielded payment address ( d , p k d ) (d,pk_d) (d,pkd),然后执行以下操作:

  • 1)check p k d pk_d pkd K A S a p l i n g . P u b l i c P r i m e S u b g r o u p KA^{Sapling}.PublicPrimeSubgroup KASapling.PublicPrimeSubgroup类型,即 p k d pk_d pkd必须为Jubjub curve上一个有效的ctEdwards curve point,且 [ r J ] p k d = O J [r_{\mathbb{J}}]pk_d=\mathcal{O}_{\mathbb{J}} [rJ]pkd=OJ

  • 2)计算 g d = D i v e r s i f y H a s h S a p l i n g ( d ) g_d=DiversifyHash^{Sapling}(d) gd=DiversifyHashSapling(d),并验证 g d g_d gd不为 ⊥ \perp

  • 3)选择uniformly random commitment trapdoor r c v ← R V a l u e C o m m i t S a p l i n g . G e n T r a p d o o r ( ) rcv\leftarrow_R ValueCommit^{Sapling}.GenTrapdoor() rcvRValueCommitSapling.GenTrapdoor()

  • 4)选择uniformly random ephemeral private key e s k ← R K A S a p l i n g . P r i v a t e ∖ { 0 } esk\leftarrow_R KA^{Sapling}.Private\setminus\{0\} eskRKASapling.Private{ 0}

  • 5)计算 c v = V a l u e C o m m i t r c v S a p l i n g ( v ) cv=ValueCommit_{rcv}^{Sapling}(v) cv=ValueCommitrcvSapling(v)

  • 6)计算 c m = N o t e C o m m i t r c m S a p l i n g ( r e p r J ( g d ) , r e p r J ( p k d ) , v ) cm=NoteCommit_{rcm}^{Sapling}(repr_{\mathbb{J}}(g_d),repr_{\mathbb{J}}(pk_d),v) cm=NoteCommitrcmSapling(reprJ(gd),reprJ(pkd),v)

  • 7)令 n p = ( l e a d B y t e , d , v , m e m o ) \mathbf{np}=(leadByte,d,v,memo) np=(leadByte,d,v,memo),分别用接收方的diversified transmission key p k d pk_d pkd、outgoing viewing key o v k ovk ovk n p \mathbf{np} np进行加密,并用diversified base g d g_d gd根据 e s k esk esk计算 e p k epk epk,最终输出transmitted note ciphertext ( e p k , C e n c , C o u t ) (epk, C^{enc}, C^{out}) (epk,Cenc,Cout)。具体可参见”Encryption (Sapling)“ 章节内容,也可以根据 c v , c m u cv,cmu cv,cmu来计算 o c k ock ock,然后以 e s k esk esk为输入进行加密。【 c m u cmu cmu为Output description中的 c m u cm_u cmu field。】
    【 详细的加解密流程可参看博客 Zcash中的加解密机制

  • 8)为Output statement生成proof π Z K O u t p u t \pi_{ZKOutput} πZKOutput

  • 10)最终sending note流程的输出为 ( c v , c m , e p k , C e n c , C o u t , π Z K O u t p u t ) (cv,cm,epk,C^{enc}, C^{out},\pi_{ZKOutput}) (cv,cm,epk,Cenc,Cout,πZKOutput)

为了减少信息泄露,sender应随机打乱 交易中的Output descriptions的顺序。

最终,the encoded transaction会提交到peer-to-peer网络。

Sapling中没必要使用Dummy notes。

一笔交易中可以有1个或多个JoinSplit descriptions或Spend descriptions。当进入到区块链中,会将所有的候选constituent note commitments添加到note commitment tree中。所有的constituent nullifiers也会进入到关联的treestate的nullifier set中。若某nullifier在nullifier set中已存在,则该nullifier不允许添加,且可判定该交易为not valid的。

在Sapling中,每个positioned note有关联的 ρ \rho ρ值,该 ρ \rho ρ值可根据note commitment c m cm cm 和 note position p o s pos pos计算出来:
ρ = M i x i n g P e d e r s e n H a s h ( c m , p o s ) \rho = MixingPedersenHash(cm,pos) ρ=MixingPedersenHash(cm,pos)

对于Sapling note,其nullifier n f nf nf 可derived as P R F n k ⋆ n f S a p l i n g ( ρ ⋆ ) PRF_{nk\star}^{nfSapling}(\rho\star) PRFnknfSapling(ρ),其中 n k ⋆ nk\star nk代表与note关联的nullifier deriving key, ρ ⋆ = r e p r J ( ρ ) \rho\star=repr_{\mathbb{J}}(\rho) ρ=reprJ(ρ)

Security requirement:对于每一个shielded protocol,对nullifier derivation的要求如下:

  • derived 必须由note的fields完全确定,由其position可能确定。使得其可被checked in the corresponding statement that controls spends。(即JoinSplit statement,或Spend statement)
  • 已假设 ρ \rho ρ值为唯一的,则要求generate two notes with distinct note commitments but the same nullifier是不可能的。
  • 给定一组未知notes的nullifiers set,无法以多于随机的概率来将note和nullifier值关联,甚至于adversary知道相应的incoming viewing keys(而不是full viewing keys) 的情况下也无法确定相应的关联关系,甚至于其中的某些notes是由adversary创建的。

参考资料

[1] Zcash Protocol Specification

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mutourend/article/details/115493240

智能推荐

Docker 快速上手学习入门教程_docker菜鸟教程-程序员宅基地

文章浏览阅读2.5w次,点赞6次,收藏50次。官方解释是,docker 容器是机器上的沙盒进程,它与主机上的所有其他进程隔离。所以容器只是操作系统中被隔离开来的一个进程,所谓的容器化,其实也只是对操作系统进行欺骗的一种语法糖。_docker菜鸟教程

电脑技巧:Windows系统原版纯净软件必备的两个网站_msdn我告诉你-程序员宅基地

文章浏览阅读5.7k次,点赞3次,收藏14次。该如何避免的,今天小编给大家推荐两个下载Windows系统官方软件的资源网站,可以杜绝软件捆绑等行为。该站提供了丰富的Windows官方技术资源,比较重要的有MSDN技术资源文档库、官方工具和资源、应用程序、开发人员工具(Visual Studio 、SQLServer等等)、系统镜像、设计人员工具等。总的来说,这两个都是非常优秀的Windows系统镜像资源站,提供了丰富的Windows系统镜像资源,并且保证了资源的纯净和安全性,有需要的朋友可以去了解一下。这个非常实用的资源网站的创建者是国内的一个网友。_msdn我告诉你

vue2封装对话框el-dialog组件_<el-dialog 封装成组件 vue2-程序员宅基地

文章浏览阅读1.2k次。vue2封装对话框el-dialog组件_

MFC 文本框换行_c++ mfc同一框内输入二行怎么换行-程序员宅基地

文章浏览阅读4.7k次,点赞5次,收藏6次。MFC 文本框换行 标签: it mfc 文本框1.将Multiline属性设置为True2.换行是使用"\r\n" (宽字符串为L"\r\n")3.如果需要编辑并且按Enter键换行,还要将 Want Return 设置为 True4.如果需要垂直滚动条的话将Vertical Scroll属性设置为True,需要水平滚动条的话将Horizontal Scroll属性设_c++ mfc同一框内输入二行怎么换行

redis-desktop-manager无法连接redis-server的解决方法_redis-server doesn't support auth command or ismis-程序员宅基地

文章浏览阅读832次。检查Linux是否是否开启所需端口,默认为6379,若未打开,将其开启:以root用户执行iptables -I INPUT -p tcp --dport 6379 -j ACCEPT如果还是未能解决,修改redis.conf,修改主机地址:bind 192.168.85.**;然后使用该配置文件,重新启动Redis服务./redis-server redis.conf..._redis-server doesn't support auth command or ismisconfigured. try

实验四 数据选择器及其应用-程序员宅基地

文章浏览阅读4.9k次。济大数电实验报告_数据选择器及其应用

随便推点

灰色预测模型matlab_MATLAB实战|基于灰色预测河南省社会消费品零售总额预测-程序员宅基地

文章浏览阅读236次。1研究内容消费在生产中占据十分重要的地位,是生产的最终目的和动力,是保持省内经济稳定快速发展的核心要素。预测河南省社会消费品零售总额,是进行宏观经济调控和消费体制改变创新的基础,是河南省内人民对美好的全面和谐社会的追求的要求,保持河南省经济稳定和可持续发展具有重要意义。本文建立灰色预测模型,利用MATLAB软件,预测出2019年~2023年河南省社会消费品零售总额预测值分别为21881...._灰色预测模型用什么软件

log4qt-程序员宅基地

文章浏览阅读1.2k次。12.4-在Qt中使用Log4Qt输出Log文件,看这一篇就足够了一、为啥要使用第三方Log库,而不用平台自带的Log库二、Log4j系列库的功能介绍与基本概念三、Log4Qt库的基本介绍四、将Log4qt组装成为一个单独模块五、使用配置文件的方式配置Log4Qt六、使用代码的方式配置Log4Qt七、在Qt工程中引入Log4Qt库模块的方法八、获取示例中的源代码一、为啥要使用第三方Log库,而不用平台自带的Log库首先要说明的是,在平时开发和调试中开发平台自带的“打印输出”已经足够了。但_log4qt

100种思维模型之全局观思维模型-67_计算机中对于全局观的-程序员宅基地

文章浏览阅读786次。全局观思维模型,一个教我们由点到线,由线到面,再由面到体,不断的放大格局去思考问题的思维模型。_计算机中对于全局观的

线程间控制之CountDownLatch和CyclicBarrier使用介绍_countdownluach于cyclicbarrier的用法-程序员宅基地

文章浏览阅读330次。一、CountDownLatch介绍CountDownLatch采用减法计算;是一个同步辅助工具类和CyclicBarrier类功能类似,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。二、CountDownLatch俩种应用场景: 场景一:所有线程在等待开始信号(startSignal.await()),主流程发出开始信号通知,既执行startSignal.countDown()方法后;所有线程才开始执行;每个线程执行完发出做完信号,既执行do..._countdownluach于cyclicbarrier的用法

自动化监控系统Prometheus&Grafana_-自动化监控系统prometheus&grafana实战-程序员宅基地

文章浏览阅读508次。Prometheus 算是一个全能型选手,原生支持容器监控,当然监控传统应用也不是吃干饭的,所以就是容器和非容器他都支持,所有的监控系统都具备这个流程,_-自动化监控系统prometheus&grafana实战

React 组件封装之 Search 搜索_react search-程序员宅基地

文章浏览阅读4.7k次。输入关键字,可以通过键盘的搜索按钮完成搜索功能。_react search