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

智能推荐

[常用办公软件] wps怎么自动生成目录?wps自动生成目录的设置教程_wps目录自动生成-程序员宅基地

文章浏览阅读1.1w次,点赞3次,收藏5次。转载请说明来源于"厦门SEO"本文地址:http://www.96096.cc/Article/160880.html常用办公软件  WPS Office是由金山软件股份有限公司开发的一款针对个人永久免费的办公软件,在我们的日常生活和工作中,WPS Office比起微软Microsoft Office来说在文字上的处理会更深入国人用户的人心,熟悉操作WPS的办公小技巧,能够更高效的提高我们的工作效率,今天小编要为大家分享的是WPS怎么自动生成目录?快来一起看看WPS自动生成目录的设置教程吧。_wps目录自动生成

web项目-程序员宅基地

文章浏览阅读7.4k次,点赞2次,收藏19次。web项目是指服务端部署在服务器上,客户端使用浏览器通过网络传输进行访问获取数据的项目。通常我们看见的应用页面网站等等都可以称之为web项目。 在web项目的开发中可分为web前端开发和web后端开发 web前端:即是客户端能看得见碰得着得东西。包括Web页面结构、页面样式外观以及Web层面得交互展现。 前端特点:页面视觉效果良好(客户第一)、Web页面交互流畅(..._web项目

关于java操作excel导入导出三种方式_java导出excel的三种方法-程序员宅基地

文章浏览阅读5.6k次,点赞8次,收藏67次。java操作关于导入导出Excel的多种方式_java导出excel的三种方法

Windows系统环境变量path详解_windows path-程序员宅基地

文章浏览阅读1.1w次,点赞10次,收藏21次。Windows path系统变量编辑_windows path

Hadoop基础教程-第13章 源码编译(13.2 Hadoop2.7.3源码编译)_hadoop2.7.3-src源码下载-程序员宅基地

文章浏览阅读512次。第13章 源码编译13.2 Hadoop2.7.3源码编译13.2.1下载Hadoop源码包(1)到官网http://hadoop.apache.org/releases.html下载2.7.3的source源码包(2)解压缩tar -zxvf hadoop-2.7.3-src.tar.gz -C /opt1(3)打开解压目录下的BUILDING.txt,编译过程和需要的软件其实就是根据这个文档里..._hadoop2.7.3-src源码下载

Latex 语法_\latex-程序员宅基地

文章浏览阅读1k次。Latex 语法_\latex

随便推点

【智能排班系统】基于AOP和自定义注解实现接口幂等性-程序员宅基地

文章浏览阅读884次。使用多种方式实现接口幂等性,通过定义注解方便对方法进行幂等性控制

SpringBoot整合Swagger2 详解_springboot swagger2 开关-程序员宅基地

文章浏览阅读324次。SpringBoot、Swagger2 整合详解_springboot swagger2 开关

spring boot 项目报错 java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized_springboot项目里面报错 the server time zone value ' й-程序员宅基地

文章浏览阅读2.8w次,点赞96次,收藏115次。报错说是时区不对因为mysql-connection-java版本导致时区的问题。pom.xml:控制台报错信息:java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone. You must configure ei..._springboot项目里面报错 the server time zone value ' й

最全Android Kotlin 学习路线(Kotlin 从入门、进阶到实战)_kotlin学习-程序员宅基地

文章浏览阅读4.2k次。Kotlin 是由 jetBrains 开发的一门现代多平台应用的静态编程语言,Kotlin 代码即可以编译成 Java 字节码,又可以编译成 JavaScript,Kotlin 是开源的,源码在这。Kotlin 包含了大量的语法糖,在编码的时候,会大大的简化我们的代码量及工作效率。且相比传统的 Java 语言,Kotlin 种大量的简写,可以减少很多用Java 必须要写的样板代码,减少大量的 if…else 等嵌套,减少大量接口的实现,代码结构也会更加清晰。_kotlin学习

【前端素材】推荐优质新鲜绿色蔬菜商城网站设计Harmic平台模板(附源码)-程序员宅基地

文章浏览阅读753次,点赞30次,收藏21次。在线绿色新鲜果蔬商店网站是指一个专门销售新鲜、绿色、有机水果和蔬菜的电子商务平台。这类网站旨在为消费者提供方便、快捷的购买渠道,同时确保他们能够购买到高质量、新鲜的产品。

elementui表格添加fixed之后样式异常_element table fixed 样式异常-程序员宅基地

文章浏览阅读1k次。最近写项目碰到一个bug 大概就是一个表格组件两个页面都会使用 组件中表格的某些列就用v-if控制了 表格的首尾列都用了fixed 然后就发生了bug 如下图 具体原因不明看过很多网上的办法 有在fixed的列绑定key的 也有使用doLayout()的 测了都没用 最后在一个前端交流群里一位大佬给出的办法 实测有效.el-table__header, .el-table__body, .el-table__footer { width: 100%; tab_element table fixed 样式异常