Skip to content

Commit

Permalink
add erc demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
waterflier committed Dec 25, 2023
1 parent 1e7cac8 commit b0f4c94
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 7 deletions.
93 changes: 89 additions & 4 deletions doc/erc/darft.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 一种支持链下数据存储证明的Hash算法
title: 一种支持链下数据存储证明的Hash算法 (或则存储证明)
description: 在默克尔树的根Hash上进行升级,让保存在链上的数据Hash可以通过对应的密码学流程和简单的博弈流程提高其数据的可用性和可靠性。
author: Liu Zhicong,waterflier
discussions-to: <URL>
Expand All @@ -13,14 +13,21 @@ requires: 721,1155 # Only required when you reference an EIP in the `Specificati

## Abstract


什么是存储证明
本文提出的存储证明结构的基础设计
新的存储证明的好处
本文建议
本文还建议对ERC721和ERC1155进行必要的扩展

<!--
The Abstract is a multi-sentence (short paragraph) technical summary. This should be a very terse and human-readable version of the specification section. Someone should be able to read only the abstract to get the gist of what this specification does.
TODO: Remove this comment before submitting
-->

## Motivation

最后写,内容是存储证明的发展和迫切需要解决的问题
<!--
This section is optional.
Expand All @@ -32,6 +39,8 @@ requires: 721,1155 # Only required when you reference an EIP in the `Specificati
-->

## Specification
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174.


<!--
The Specification section should describe the syntax and semantics of any new feature. The specification should be detailed enough to allow competing, interoperable implementations for any of the current Ethereum platforms (besu, erigon, ethereumjs, go-ethereum, nethermind, or others).
Expand All @@ -40,8 +49,72 @@ requires: 721,1155 # Only required when you reference an EIP in the `Specificati
TODO: Remove this comment before submitting
-->
### MixHash
MixHash是包含了内容的长度信息的Merkle树的根节点。其构造方法如下:

High |-1-|----63----|----------192----------| Low
1:为0使用SHA256,为1使用Keccak256
63:文件大小
192:根节点Hash的低192位

1. Split the file into 1KB chunks. Pad zeros to the end of the last chunk if needed.

2. Calculate the SHA256 hash for each chunk and the low 128bits is the Merkle tree leaf value

3. Construct a Merkle tree , root node hash algorithm is SHA256, other node use low 128bits of the SHA256

4. Return the combination of the file size at high 64bits and the low 192 bits of the Merkle tree root node hash.

使用MixHash替代被广泛使用的Kaekk256和SHA256,没有任何额外的成本。在高64bits包含了文件的长度虽然在安全性上有一些损失,但192bits的Hash安全性实际上完全足够用了。



### 公有数据的存储证明
0. 能提交存储证明获得奖励的用户被称作Supplier,Supplier需要准备一定的质押币。
1. 区块高度为h的区块Hash得到 32bytes的nonce值和 32-992 的插入位置Pos
2. 为了生成正确的存储证明,Supplier遍历所有的叶子节点,在该位置插入nonce值,选择最合适的叶子节点m。让插入后的根Hash最小
3. Supplier在插入位置之前再计算一个32bytes的noise值,使得新的LeafData可以让默克尔树根Hash符合一个难度条件(比如最低位多少是0).对于同时进块的存储证明,难度高者胜出并得到奖励。
4. Supplier把存储证明{m,path,leaf_data,noise}提交到链上,即为一个有效的存储证明。可以拿到奖励.不需要PoW的场景可以进一步简化到 {h,m,path_m,m_leaf_data}
5. 链无法验证m是否正确,但其它拥有全量数据的Miner,如果发现m是伪造的,可以提交真实的{new_m,new_path_m,new_m_leaf_data} 来对已上连的存储证明进行挑战并在成功后赢得Supplier的质押币。
6. 上述设计也可改成Supplier只提交m,挑战者提供path_m, m_leaf_data,但这会导致挑战者需要多1倍的手续费。如果获得的质奖励太少,那么挑战者可能不会提交挑战。

#### 限制
不解决数据是否是公共的问题,也不解决数据是否被访问的问题。该证明的存在只是说明该数据的副本是存在的。
最小文件大小问题:基于上述逻辑不适合保存太小的文件

### 私有数据的存储证明
0. 用户(User)持有待保存的原始私有数据D
1. User决定把数据保存到供应商A,为A准备一个一次性的秘钥K,D通过K加密后得到D'。User将D'保存到A那,然后本地保留基于原始数据构造的挑战本和K
2. User认为供应商A丢失了D'(通常是通过链下判断),基于自己的挑战本在链上提出挑战:(一个32bytes Hash值)
3. 供应商如果没有丢失数据,可以在Calldata里包含leaf_data (1KB)。挑战结束。供应商获胜。
4. 如果供应商认为Hash并不包含在D'中,提出挑战非法 1byte
5. 用户通过Call Data中的(index 4byte,默克尔路径,1KB )来证明挑战合法,用户获胜。

### IERCPublicDataContract
```
//Review:这个作为ERC的一部分,要仔细考虑一下
interface IERCPublicDataContract {
//return the owner of the data
function getDataOwner(bytes32 dataHash) external view returns (address);
}
```

### IERC721VerfiyDataHash
```
interface IERC721VerfiyDataHash{
//return token data hash
function tokenDataHash(uint256 _tokenId) external view returns (bytes32);
}
```

#### 限制
存储证明主要用在near-line backup system上。并不适用于在线的数据删除/更新/读取。






The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174.

## Rationale

Expand All @@ -53,7 +126,7 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S
TODO: Remove this comment before submitting
-->

TBD


## Backwards Compatibility

Expand Down Expand Up @@ -92,6 +165,14 @@ No backward compatibility issues found.
TODO: Remove this comment before submitting
-->

```solidity
function verifyDataProof(bytes32 meta) {
}
```

## Security Considerations

<!--
Expand All @@ -102,6 +183,10 @@ No backward compatibility issues found.
TODO: Remove this comment before submitting
-->

隐私安全

数据可靠性

Needs discussion.

## Copyright
Expand Down
48 changes: 48 additions & 0 deletions doc/erc/demo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
contract PublicStorageProofDemo {
struct StoargeProof {
uint256 nonce_block_high;
uint256 proof_block;
uint256 proof_result;
address prover;
}
mapping(bytes32 => StoargeProof) show_datas;

function showStorageProof(bytes32 dataMixedHash, uint256 nonce_block_high,uint32 index_m, bytes32[] calldata m_path, bytes calldata leafdata) public {
StoargeProof storage last_proof = show_datas[dataMixedHash];
// 如果已经存在,判断区块高度差,决定这是一个新的挑战还是对旧的挑战的更新
bool is_new_show = false;

if(is_new_show) {

} else {
// 旧挑战:判断是否结果更好,如果更好,更新结果,并更新区块高度
require(last_proof.nonce_block_high == nonce_block_high, "nonce_block_high not match");
uint256 root_hash = _verifyDataProof(dataMixedHash,nonce_block_high,index_m,m_path,leafdata,0);
require(root_hash > 0, "verify failed");

if(root_hash < last_proof.proof_result) {
//根据经济学模型对虚假的proof提供者进行惩罚
last_proof.proof_result = root_hash;
last_proof.proof_block = block.number;
last_proof.prover = msg.sender;
}
}

}

function _verifyDataProof(bytes32 dataMixedHash,uint256 nonce_block_high, uint32 index, bytes32[] calldata m_path, bytes calldata leafdata,bytes32 noise) private returns(uint256) {
uint256 nonce = 0;
uint16 pos = 0;

//验证leaf_data+index+path 和 dataMixedHash是匹配的,不匹配返回0

//计算在leaf_data中插入nonce,noise后的root_hash

//返回root_hash
}

function showStorageProofWihtPoW(bytes32 dataMixedHash, uint256 nonce_block_high,uint32 index_m, bytes32[] calldata m_path, bytes calldata leafdata,bytes32 noise) public {

}

}
3 changes: 3 additions & 0 deletions doc/erc/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// 给定文件计算MixHash

// 根据nonce block high 计算存储证明(注意区分是否enable pow)
15 changes: 13 additions & 2 deletions doc/erc/ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
4. 对数据进行PoW构造的工具
5. 对数据进行PoW验证的Solidy代码

## 可验证Hash格式
## MixHash
High |-2-|----63----|----------192----------| Low
2:2bits的Hash算法选择为00使用SHA256,为10使用Keccak256 , 01,11保留
63:文件大小
192:根节点Hash的低192位

目标:存储证明里大量的数据只需要存储一次,然后通过存储证明来验证数据的存在



节点hash的大小是16byte (128bits), 1024/16*2 = 32, 2^32*1K = 4T


Expand Down Expand Up @@ -71,6 +77,11 @@
不解决数据是否是公共的问题,也不解决数据是否被访问的问题。该证明的存在只是说明该数据的副本是存在的。


## 用于BTC网络
0. 存储方将一定的奖励保存到一个特定地址,该地址使用存储证明可以解开,并设定难度
1. 矿工提交有正确难度的noise,揭开该地址后可以得到BTC奖励



## 已知问题
最小文件大小问题:基于上述逻辑不适合保存太小的文件
Expand Down
1 change: 0 additions & 1 deletion doc/inscribe protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

铭刻公共数据需要用户支付数量为n的DMC。
```
data_size基础值:2100-3100
基础分 = f(data_size) = 999 / (1 + exp(-0.00000762939453125*(x-127,999,999))) + 1
基本倍率 = f(point) = 19 / (1 + e^(-0.15(x-90))) + 1
n = 基础分*基础倍率*2
Expand Down

0 comments on commit b0f4c94

Please sign in to comment.