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

智能推荐

http隧道 java_使用java语言实现http隧道技术-程序员宅基地

文章浏览阅读119次。该楼层疑似违规已被系统折叠隐藏此楼查看此楼/***Getaparametervalue**@paramkeyString*@paramdefString*@returnString*/publicStringgetParameter(Stringkey,Stringdef){returnisStandalone?System.getProperty(ke..._java http隧道

Keepalived高可用+邮件告警_keepalived sendmail-程序员宅基地

文章浏览阅读913次。IP主机名备注192.168.117.14keepalived-master主节点192.168.117.15keepalived-slaver备节点192.168.117.100VIP1.主备节点均安装keepalived# yum install -y keepalived httpd2.主备节点均修改keepalived日志存放路径..._keepalived sendmail

SPFILE 错误导致数据库无法启动(ORA-01565)_ora01565 ora27046-程序员宅基地

文章浏览阅读469次。--==========================================--SPFILE错误导致数据库无法启动(ORA-01565)--========================================== SPFILE错误导致数据库无法启动 SQL> startup ORA-01078: failurein proce_ora01565 ora27046

功能测试基础知识(1)-程序员宅基地

文章浏览阅读6.1k次,点赞2次,收藏54次。功能测试基础知识总结_功能测试

postgresql 中文排序_pg中文排序-程序员宅基地

文章浏览阅读3.2k次,点赞3次,收藏2次。pg 中文首字母排序_pg中文排序

[Mysql] CONVERT函数_mysql convert-程序员宅基地

文章浏览阅读3.1w次,点赞23次,收藏109次。本文主要讲解CONVERT函数_mysql convert

随便推点

HTML5与微信开发(2)-视频播放事件及API属性_微信开发者工具视频快进-程序员宅基地

文章浏览阅读8.6k次,点赞2次,收藏2次。HTML5 的视频播放事件想必大家已经期待很久了吧,在HTML4.1、4.0之前我们如果在网页上播放视频无外乎两种方法: 第一种:安装FLASH插件或者微软发布的插件 第二种:在本地安装播放器,在线播放组件之类的 因为并不是所有的浏览器都安装了FLASH插件,就算安装也不一定所有的都能安装成功。像苹果系统就是默认禁用FLASH的,安卓虽然一开始的时候支持FLASH,但是在安卓4.0以后也开始不_微信开发者工具视频快进

JedisConnectionException Connection Reset_jedisconnectionexception: java.net.socketexception-程序员宅基地

文章浏览阅读5.4k次,点赞3次,收藏4次。在使用redis的过程常见错误总结1.JedisConnectionException Connection Reset参考这边文章:Connection reset原因分析和解决方案https://blog.csdn.net/cwclw/article/details/527971311.1问题描述Exception in thread "main" redis.clients...._jedisconnectionexception: java.net.socketexception: connection reset

Lua5.3版GC机制理解_lua5.3 gc-程序员宅基地

文章浏览阅读8.3k次,点赞8次,收藏42次。目录1.Lua垃圾回收算法原理简述2.Lua垃圾回收中的三种颜色3.Lua垃圾回收详细过程4.步骤源码详解4.1新建对象阶段4.2触发条件4.3 GC函数状态机4.4标记阶段4.5清除阶段5.总结参考资料lua垃圾回收(Garbage Collect)是lua中一个比较重要的部分。由于lua源码版本变迁,目前大多数有关这个方面的文章都还是基于lua5.1版本,有一定的滞后性。因此本文通过参考当前..._lua5.3 gc

手机能打开的表白代码_能远程打开,各种手机电脑进行监控操作,最新黑科技...-程序员宅基地

文章浏览阅读511次。最近家中的潮人,老妈闲着没事干,开始学玩电脑,引起他的各种好奇心。如看看新闻,上上微信或做做其他的事情。但意料之中的是电脑上会莫名出现各种问题?不翼而飞的图标?照片又不见了?文件被删了,卡机或者黑屏,无声音了,等等问题。常常让她束手无策,求助于我,可惜在电话中说不清,往往只能苦等我回家后才能解决,那种开心乐趣一下子消失了。想想,这样也不是办法啊, 于是,我潜心寻找了两款优秀的远程控制软件。两款软件...

成功Ubuntu18.04 ROS melodic安装Cartograhper+Ceres1.13.0,以及错误总结_ros18.04 安装ca-程序员宅基地

文章浏览阅读1.8k次。二.初始化工作空间三.设置下载地址四.下载功能包此处可能会报错,请看:rosdep update遇到ERROR: error loading sources list: The read operation timed out问题_DD᭄ꦿng的博客-程序员宅基地接下来一次安装所有功能包,注意对应ROS版本 五.编译功能包isolated:单独编译各个功能包,每个功能包之间不产生依赖。编译过程时间比较长,可能需要几分钟时间。此处可能会报错:缺少absl依赖包_ros18.04 安装ca

Harbor2.2.1配置(trivy扫描器、镜像签名)_init error: db error: failed to download vulnerabi-程序员宅基地

文章浏览阅读4.1k次,点赞3次,收藏7次。Haobor2.2.1配置(trivy扫描器、镜像签名)docker-compose下载https://github.com/docker/compose/releases安装cp docker-compose /usr/local/binchmod +x /usr/local/bin/docker-composeharbor下载https://github.com/goharbor/harbor/releases解压tar xf xxx.tgx配置harbor根下建立:mkd_init error: db error: failed to download vulnerability db: database download

推荐文章

热门文章

相关标签