-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy patheip4844.rs
215 lines (188 loc) · 7.73 KB
/
eip4844.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use kzg::kzg_types::{ZFr, ZG1};
use kzg_traits::{
eip_4844::{
blob_to_kzg_commitment_rust, blob_to_polynomial, compute_kzg_proof_rust,
evaluate_polynomial_in_evaluation_form, hash_to_bls_field, Blob,
},
Fr, G1,
};
use once_cell::sync::Lazy;
use reth_primitives::B256;
use sha2::{Digest as _, Sha256};
pub use kzg::{eip_4844::deserialize_blob_rust, kzg_proofs::KZGSettings};
pub static KZG_SETTINGS_BIN: &[u8] = include_bytes!("../../kzg_settings/zkcrypto_kzg_settings.bin");
// The KZG settings under the concrete type of kzg backend
// We directly include the serialzed struct to avoid conversion cost in guest
// To generate the bytes, run:
//
// cargo run --bin gen_kzg_settings
pub static KZG_SETTINGS: Lazy<KZGSettings> = Lazy::new(|| {
bincode::deserialize(KZG_SETTINGS_BIN)
.expect("failed to load trusted setup, please run `cargo run --bin gen_kzg_settings`")
});
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;
pub type KzgGroup = [u8; 48];
pub type KzgField = [u8; 32];
pub type KzgCommitment = KzgGroup;
#[derive(Debug, thiserror::Error)]
pub enum Eip4844Error {
#[error("Failed to deserialize blob to field elements")]
DeserializeBlob,
#[error("Failed to evaluate polynomial at hashed point: {0}")]
EvaluatePolynomial(String),
#[error("Failed to compute KZG proof")]
ComputeKzgProof(String),
#[error("Failed set commitment proof")]
KzgDataPoison(String),
}
pub fn get_evaluation_point(blob: &[u8], versioned_hash: &B256) -> ZFr {
let blob_hash = Sha256::digest(blob);
let x = Sha256::digest([blob_hash.to_vec(), versioned_hash.to_vec()].concat()).into();
hash_to_bls_field(&x)
}
pub fn proof_of_equivalence(
blob: &[u8],
versioned_hash: &B256,
) -> Result<(KzgField, KzgField), Eip4844Error> {
let blob_fields = Blob::from_bytes(blob)
.and_then(|b| deserialize_blob_rust(&b))
.map_err(|_| Eip4844Error::DeserializeBlob)?;
let poly = blob_to_polynomial(&blob_fields).unwrap();
let x = get_evaluation_point(blob, versioned_hash);
let y = evaluate_polynomial_in_evaluation_form(&poly, &x, &KZG_SETTINGS.clone())
.map(|fr| fr.to_bytes())
.map_err(|e| Eip4844Error::EvaluatePolynomial(e.to_string()))?;
Ok((x.to_bytes(), y))
}
pub fn calc_kzg_proof(blob: &[u8], versioned_hash: &B256) -> Result<ZG1, Eip4844Error> {
calc_kzg_proof_with_point(blob, get_evaluation_point(blob, versioned_hash))
}
pub fn calc_kzg_proof_with_point(blob: &[u8], z: ZFr) -> Result<ZG1, Eip4844Error> {
let blob_fields = Blob::from_bytes(blob)
.and_then(|b| deserialize_blob_rust(&b))
.map_err(|_| Eip4844Error::DeserializeBlob)?;
let (proof, _) = compute_kzg_proof_rust(&blob_fields, &z, &KZG_SETTINGS.clone())
.map_err(Eip4844Error::ComputeKzgProof)?;
Ok(proof)
}
pub fn calc_kzg_proof_commitment(blob: &[u8]) -> Result<KzgGroup, Eip4844Error> {
let blob_fields = Blob::from_bytes(blob)
.and_then(|b| deserialize_blob_rust(&b))
.map_err(|_| Eip4844Error::DeserializeBlob)?;
Ok(
blob_to_kzg_commitment_rust(&blob_fields, &KZG_SETTINGS.clone())
.map_err(Eip4844Error::ComputeKzgProof)?
.to_bytes(),
)
}
pub fn commitment_to_version_hash(commitment: &[u8; 48]) -> B256 {
let mut hash = Sha256::digest(commitment);
hash[0] = VERSIONED_HASH_VERSION_KZG;
B256::new(hash.into())
}
pub fn kzg_proof_to_bytes(proof: &ZG1) -> KzgGroup {
proof.to_bytes()
}
#[cfg(test)]
mod test {
use super::*;
use kzg_traits::{
eip_4844::{verify_kzg_proof_rust, BYTES_PER_FIELD_ELEMENT},
G1,
};
use reth_primitives::revm_primitives::kzg::{G1Points, G2Points, G1_POINTS, G2_POINTS};
use reth_primitives::revm_primitives::Bytes;
pub fn verify_kzg_proof_evm(
commitment: &KzgCommitment,
z: &ZFr,
y: &ZFr,
proof: &ZG1,
) -> Result<bool, Eip4844Error> {
// The input is encoded as follows:
// | versioned_hash | z | y | commitment | proof |
// | 32 | 32 | 32 | 48 | 48 |
let version_hash = commitment_to_version_hash(commitment);
let mut input = [0u8; 192];
input[..32].copy_from_slice(&(*version_hash));
input[32..64].copy_from_slice(&z.to_bytes());
input[64..96].copy_from_slice(&y.to_bytes());
input[96..144].copy_from_slice(commitment);
input[144..192].copy_from_slice(&kzg_proof_to_bytes(proof));
Ok(reth_primitives::revm_precompile::kzg_point_evaluation::run(
&Bytes::copy_from_slice(&input),
u64::MAX,
&reth_primitives::revm_primitives::env::Env::default(),
)
.is_ok())
}
#[test]
fn test_kzg_settings_equivalence() {
let kzg_settings: KZGSettings = kzg_traits::eip_4844::load_trusted_setup_rust(
G1Points::as_ref(G1_POINTS).flatten(),
G2Points::as_ref(G2_POINTS).flatten(),
)
.expect("failed to load trusted setup");
assert_eq!(KZG_SETTINGS.clone().secret_g1, kzg_settings.secret_g1);
assert_eq!(KZG_SETTINGS.clone().secret_g2, kzg_settings.secret_g2);
}
#[test]
fn test_blob_to_kzg_commitment() {
let blob = Blob::from_bytes(&[0u8; 131072]).unwrap();
let commitment = blob_to_kzg_commitment_rust(
&deserialize_blob_rust(&blob).unwrap(),
&KZG_SETTINGS.clone(),
)
.map(|c| c.to_bytes())
.unwrap();
assert_eq!(
commitment_to_version_hash(&commitment).to_string(),
"0x010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014"
);
}
#[test]
fn test_verify_kzg_proof() {
let kzg_settings = KZG_SETTINGS.clone();
let data = (0u64..131072).map(|v| (v % 64) as u8).collect::<Vec<u8>>();
let blob = Blob::from_bytes(&data).unwrap();
let blob_fields = deserialize_blob_rust(&blob).unwrap();
let commitment = calc_kzg_proof_commitment(&blob.bytes).unwrap();
let poly = blob_to_polynomial(&blob_fields).unwrap();
// Random number hash to field
let x = hash_to_bls_field(&[5; BYTES_PER_FIELD_ELEMENT]);
let y = evaluate_polynomial_in_evaluation_form(&poly, &x, &kzg_settings).unwrap();
let proof = calc_kzg_proof_with_point(&blob.bytes, x).unwrap();
assert!(verify_kzg_proof_rust(
&ZG1::from_bytes(&commitment).unwrap(),
&x,
&y,
&proof,
&kzg_settings,
)
.unwrap());
}
#[test]
fn test_verify_kzg_proof_in_precompile() {
let data = (0u64..131072).map(|v| (v % 64) as u8).collect::<Vec<u8>>();
let blob = Blob::from_bytes(&data).unwrap();
let blob_fields = deserialize_blob_rust(&blob).unwrap();
let commitment = calc_kzg_proof_commitment(&blob.bytes).unwrap();
let poly = blob_to_polynomial(&blob_fields).unwrap();
// Random number hash to field
let x = hash_to_bls_field(&[5; BYTES_PER_FIELD_ELEMENT]);
let y = evaluate_polynomial_in_evaluation_form(&poly, &x, &KZG_SETTINGS.clone()).unwrap();
let proof = calc_kzg_proof_with_point(&blob.bytes, x).unwrap();
// Verify a correct proof
assert!(verify_kzg_proof_evm(&commitment, &x, &y, &proof,).unwrap());
// Create a proof for a different point
{
let x = hash_to_bls_field(&[6; BYTES_PER_FIELD_ELEMENT]);
let proof = calc_kzg_proof_with_point(&blob.bytes, x).unwrap();
assert!(!verify_kzg_proof_evm(&commitment, &x, &y, &proof,).unwrap());
}
// Try to prove a different evaluated point
{
let y = y.add(&ZFr::one());
assert!(!verify_kzg_proof_evm(&commitment, &x, &y, &proof,).unwrap());
}
}
}