Skip to content

Commit 46a2df6

Browse files
committed
Add no_auth precompile option
1 parent a622df7 commit 46a2df6

File tree

104 files changed

+1718
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1718
-79
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ categories = { workspace = true }
88
license = { workspace = true }
99

1010

11+
[features]
12+
default=[]
13+
auth_commitment = ["dkg/auth_commitment"]
1114

1215
[workspace.package]
1316
edition = "2021"
@@ -33,7 +36,7 @@ serde_json = "1.0"
3336
serde_cbor = "0.10"
3437
schemars = "0.8.22"
3538
clap = { version = "4", features = ["derive"] }
36-
dkg = { path = "crates/dkg" }
39+
dkg = { path = "crates/dkg", default-features = false }
3740
jsonschema = "0.16"
3841
anyhow = "1.0.96"
3942
colored = "3.0.0"

build.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,22 @@ pub use git_info_contents::*;
5353

5454
fs::write(dest_path, git_info_content).expect("Failed to write git_info.rs");
5555

56-
sp1_build::build_program("crates/bad_share_exchange_prove");
57-
sp1_build::build_program("crates/finalization_prove");
58-
sp1_build::build_program("crates/bad_parial_key_prove");
59-
sp1_build::build_program("crates/bad_encrypted_share_prove");
56+
#[cfg(feature = "auth_commitment")]
57+
{
58+
let args = sp1_build::BuildArgs {
59+
features: vec!["auth_commitment".to_string()],
60+
..Default::default()
61+
};
62+
sp1_build::build_program_with_args("crates/bad_share_exchange_prove", args.clone());
63+
sp1_build::build_program_with_args("crates/finalization_prove", args.clone());
64+
sp1_build::build_program_with_args("crates/bad_parial_key_prove", args.clone());
65+
sp1_build::build_program_with_args("crates/bad_encrypted_share_prove", args);
66+
}
67+
#[cfg(not(feature = "auth_commitment"))]
68+
{
69+
sp1_build::build_program("crates/bad_share_exchange_prove");
70+
sp1_build::build_program("crates/finalization_prove");
71+
sp1_build::build_program("crates/bad_parial_key_prove");
72+
sp1_build::build_program("crates/bad_encrypted_share_prove");
73+
}
6074
}

crates/bad_encrypted_share_prove/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ version = "1.1.0"
44
edition = "2021"
55
publish = false
66

7+
8+
[features]
9+
default = []
10+
auth_commitment = ["dkg/auth_commitment"]
11+
712
[dependencies]
813
sp1-zkvm = "4.2.1"
914
ff = "0.13.0"
1015
rand = "0.8.5"
1116
group = "0.13.0"
1217
serde = "1.0.216"
1318
serde_cbor = "0.10"
14-
dkg = { path = "../dkg" }
19+
dkg = { path = "../dkg", default-features = false }
1520
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", tag = "patch-0.8.0-sp1-4.0.0-v2", features = ["experimental"]}
1621
hex = "0.4"
1722
chacha20 = "0.9.1"

crates/bad_encrypted_share_prove/src/main.rs

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
sp1_zkvm::entrypoint!(main);
44

5-
use dkg::{self, compute_initial_commitment_hash, for_each_raw_type, VerificationErrors};
5+
use dkg::{self, compute_initial_commitment_hash};
66

77
use chacha20::cipher::{KeyIvInit, StreamCipher};
88
use chacha20::{ChaCha20, Key, Nonce};
99

10-
use bls12_381::{self, G1Affine, G2Affine};
1110
use dkg::crypto::*;
1211
use dkg::types::*;
1312
use serde::Deserialize;
@@ -123,11 +122,21 @@ impl BinaryStream {
123122
Ok(T::from_bytes(bytes))
124123
}
125124

125+
pub fn remain_len(&self) -> usize {
126+
self.data.len() - self.pos
127+
}
128+
126129
pub fn finalize(&mut self) {
130+
println!(
131+
"Read {} bytes, {} remain",
132+
self.pos,
133+
self.data.len() - self.pos
134+
);
127135
assert!(self.pos == self.data.len());
128136
}
129137
}
130138

139+
#[cfg(feature = "auth_commitment")]
131140
fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
132141
msg: Vec<u8>,
133142
settings: dkg::GenerateSettings,
@@ -141,22 +150,28 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
141150
let gen_id = stream
142151
.read::<DkgGenId>()
143152
.map_err(|e| format!("Invalid gen_id: {e}"))?;
153+
154+
println!("remain_len {}", stream.remain_len());
144155
let msg_type = stream
145156
.read_byte_array::<1>()
146157
.map_err(|e| format!("Invalid msg_type: {e}"))?[0];
147-
158+
println!("remain_len {}", stream.remain_len());
148159
let secret = stream
149160
.read::<RawBytes<Setup::DkgSecretKey>>()
150161
.map_err(|e| format!("Invalid secret: {e}"))?;
162+
println!("remain_len {}", stream.remain_len());
151163
let commitment_hash = stream
152164
.read::<SHA256Raw>()
153165
.map_err(|e| format!("Invalid commitment_hash: {e}"))?;
166+
println!("remain_len {}", stream.remain_len());
154167
let commitment_pubkey = stream
155168
.read::<RawBytes<Setup::CommitmentPubkey>>()
156169
.map_err(|e| format!("Invalid commitment_pubkey: {e}"))?;
170+
println!("remain_len {}", stream.remain_len());
157171
let commitment_signature = stream
158172
.read::<RawBytes<Setup::CommitmentSignature>>()
159173
.map_err(|e| format!("Invalid commitment_signature: {e}"))?;
174+
println!("remain_len {}", stream.remain_len());
160175

161176
stream.finalize();
162177

@@ -172,7 +187,7 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
172187
return Err("Invalid msg_type".to_string());
173188
}
174189

175-
let mut initial_commitment = dkg::InitialCommitment::<Setup> {
190+
let initial_commitment = dkg::InitialCommitment::<Setup> {
176191
settings: settings,
177192
base_pubkeys: base_pubkeys,
178193
hash: sender_commitment_hash.clone(),
@@ -196,6 +211,69 @@ fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
196211
})
197212
}
198213

214+
#[cfg(not(feature = "auth_commitment"))]
215+
fn parse_message<Setup: dkg::DkgSetup + dkg::DkgSetupTypes<Setup>>(
216+
msg: Vec<u8>,
217+
settings: dkg::GenerateSettings,
218+
base_pubkeys: Vec<RawBytes<Setup::Point>>,
219+
commitment_hashes: Vec<SHA256Raw>,
220+
receiver_commitment_hash: SHA256Raw,
221+
sender_commitment_hash: SHA256Raw,
222+
) -> Result<dkg::SharedData<Setup>, String> {
223+
let mut stream = BinaryStream { data: msg, pos: 0 };
224+
225+
let gen_id = stream
226+
.read::<DkgGenId>()
227+
.map_err(|e| format!("Invalid gen_id: {e}"))?;
228+
//println!("remain_len {}", stream.remain_len());
229+
let msg_type = stream
230+
.read_byte_array::<1>()
231+
.map_err(|e| format!("Invalid msg_type: {e}"))?[0];
232+
//println!("remain_len {}", stream.remain_len());
233+
let secret = stream
234+
.read::<RawBytes<Setup::DkgSecretKey>>()
235+
.map_err(|e| format!("Invalid secret: {e}"))?;
236+
//println!("remain_len {}", stream.remain_len());
237+
let commitment_pubkey = stream
238+
.read::<RawBytes<Setup::CommitmentPubkey>>()
239+
.map_err(|e| format!("Invalid commitment_pubkey: {e}"))?;
240+
//println!("remain_len {}", stream.remain_len());
241+
stream.finalize();
242+
243+
if stream.bytes_left() != 0 {
244+
return Err("Invalid message".to_string());
245+
}
246+
247+
if settings.gen_id != gen_id {
248+
return Err("Invalid gen_id".to_string());
249+
}
250+
251+
if msg_type != 3 {
252+
return Err("Invalid msg_type".to_string());
253+
}
254+
255+
let initial_commitment = dkg::InitialCommitment::<Setup> {
256+
settings: settings,
257+
base_pubkeys: base_pubkeys,
258+
hash: sender_commitment_hash.clone(),
259+
};
260+
261+
Ok(dkg::SharedData::<Setup> {
262+
verification_hashes: commitment_hashes,
263+
initial_commitment: initial_commitment,
264+
seeds_exchange_commitment: dkg::SeedExchangeCommitment {
265+
initial_commitment_hash: sender_commitment_hash,
266+
shared_secret: dkg::ExchangedSecret {
267+
secret: secret,
268+
dst_base_hash: receiver_commitment_hash,
269+
},
270+
commitment: dkg::Commitment {
271+
pubkey: commitment_pubkey,
272+
},
273+
},
274+
})
275+
}
276+
199277
pub fn main() {
200278
run::<BlsDkgWithSecp256kCommitment>();
201279
}

crates/bad_parial_key_prove/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ version = "1.1.0"
44
edition = "2021"
55
publish = false
66

7+
[features]
8+
default = []
9+
auth_commitment = ["dkg/auth_commitment"]
10+
711
[dependencies]
812
sp1-zkvm = "4.2.1"
913
ff = "0.13.0"
1014
rand = "0.8.5"
1115
group = "0.13.0"
1216
serde = "1.0.216"
1317
serde_cbor = "0.10"
14-
dkg = { path = "../dkg" }
18+
dkg = { path = "../dkg", default-features = false }
1519
hex = "0.4"
1620
chacha20 = "0.9.1"

crates/bad_parial_key_prove/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ where
2929
match verification_error {
3030
VerificationErrors::SlashableError(e) => {
3131
for h in data.generations.iter() {
32-
println!("Verification hash: {}", h.base_hash.to_hex());
32+
println!("Verification hash: {}, {}", h.base_hash.to_hex(), e);
3333
sp1_zkvm::io::commit(h.base_hash.as_ref());
3434
}
3535

crates/bad_share_exchange_prove/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ version = "1.1.0"
44
edition = "2021"
55
publish = false
66

7+
[features]
8+
default = []
9+
auth_commitment = ["dkg/auth_commitment"]
10+
711
[dependencies]
812
sp1-zkvm = "4.2.1"
913
ff = "0.13.0"
1014
rand = "0.8.5"
1115
group = "0.13.0"
1216
serde = "1.0.216"
1317
serde_cbor = "0.10"
14-
dkg = { path = "../dkg" }
18+
dkg = { path = "../dkg", default-features = false }
1519
hex = "0.4"

crates/dkg/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ version = { workspace = true }
44
authors = { workspace = true }
55
edition = { workspace = true }
66

7+
[features]
8+
default = []
9+
auth_commitment = []
10+
711
[dependencies]
812
ff = "0.13.0"
913
rand = "0.8.5"

crates/dkg/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ pub struct Commitment<Setup>
6868
where
6969
Setup: DkgSetup + DkgSetupTypes<Setup>,
7070
{
71+
#[cfg(feature = "auth_commitment")]
7172
#[serde(rename = "hash")]
7273
pub hash: SHA256Raw,
7374
#[serde(rename = "pubkey")]
7475
pub pubkey: RawBytes<Setup::CommitmentPubkey>,
76+
#[cfg(feature = "auth_commitment")]
7577
#[serde(rename = "signature")]
7678
pub signature: RawBytes<Setup::CommitmentSignature>,
7779
}

crates/dkg/src/verification.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl std::error::Error for VerificationErrors {
2626
}
2727
}
2828

29+
#[cfg(feature = "auth_commitment")]
2930
pub fn compute_seed_exchange_hash<Setup>(seed_exchange: &SeedExchangeCommitment<Setup>) -> SHA256Raw
3031
where
3132
Setup: DkgSetup + DkgSetupTypes<Setup>,
@@ -72,20 +73,22 @@ pub fn verify_seed_exchange_commitment<Setup>(
7273
where
7374
Setup: DkgSetup + DkgSetupTypes<Setup>,
7475
{
75-
let commitment = &seed_exchange.commitment;
76+
#[cfg(feature = "auth_commitment")]
77+
{
78+
let commitment = &seed_exchange.commitment;
7679

77-
let shared_secret = &seed_exchange.shared_secret;
78-
79-
if !verify_commitment(&seed_exchange.commitment) {
80-
return Err(Box::new(VerificationErrors::UnslashableError(format!(
81-
"Invalid field seeds_exchange_commitment.commitment.signature {},
82-
message: {}
83-
pubkey: {},
84-
\n",
85-
commitment.signature, commitment.hash, commitment.pubkey
86-
))));
80+
if !verify_commitment(&seed_exchange.commitment) {
81+
return Err(Box::new(VerificationErrors::UnslashableError(format!(
82+
"Invalid field seeds_exchange_commitment.commitment.signature {},
83+
message: {}
84+
pubkey: {},
85+
\n",
86+
commitment.signature, commitment.hash, commitment.pubkey
87+
))));
88+
}
8789
}
8890

91+
let shared_secret = &seed_exchange.shared_secret;
8992
let sk = match Setup::DkgSecretKey::from_bytes(&shared_secret.secret) {
9093
Ok(sk) => sk,
9194
Err(e) => {
@@ -95,16 +98,19 @@ where
9598
}
9699
};
97100

98-
let computed_commitment_hash = compute_seed_exchange_hash::<Setup>(seed_exchange);
99-
100-
if computed_commitment_hash.to_vec() != seed_exchange.commitment.hash.as_ref() {
101-
return Err(Box::new(VerificationErrors::SlashableError(
102-
format!(
103-
"Invalid field seeds_exchange_commitment.commitment.hash. Expected: {:?}, got hash: {:?}\n",
104-
seed_exchange.commitment.hash,
105-
hex::encode(computed_commitment_hash.to_vec())
106-
),
107-
)));
101+
#[cfg(feature = "auth_commitment")]
102+
{
103+
let computed_commitment_hash = compute_seed_exchange_hash::<Setup>(seed_exchange);
104+
105+
if computed_commitment_hash.to_vec() != seed_exchange.commitment.hash.as_ref() {
106+
return Err(Box::new(VerificationErrors::SlashableError(
107+
format!(
108+
"Invalid field seeds_exchange_commitment.commitment.hash. Expected: {:?}, got hash: {:?}\n",
109+
seed_exchange.commitment.hash,
110+
hex::encode(computed_commitment_hash.to_vec())
111+
),
112+
)));
113+
}
108114
}
109115

110116
let dest_id = match get_index_in_commitments(
@@ -324,6 +330,7 @@ where
324330
Ok(())
325331
}
326332

333+
#[cfg(feature = "auth_commitment")]
327334
pub fn compute_partial_share_hash<Setup>(
328335
settings: &GenerateSettings,
329336
partial_share: &BadPartialShare<Setup>,
@@ -354,6 +361,7 @@ where
354361
hasher.finalize().to_vec()
355362
}
356363

364+
#[cfg(feature = "auth_commitment")]
357365
pub fn verify_commitment<Setup>(commitment: &Commitment<Setup>) -> bool
358366
where
359367
Setup: DkgSetup + DkgSetupTypes<Setup>,
@@ -417,7 +425,10 @@ pub fn prove_wrong_final_key_generation<Setup>(
417425
where
418426
Setup: DkgSetup + DkgSetupTypes<Setup>,
419427
{
420-
verify_commitment_signature(data)?;
428+
#[cfg(feature = "auth_commitment")]
429+
{
430+
verify_commitment_signature(data)?;
431+
}
421432
verify_generation_base_hashes(data)?;
422433

423434
let mut sorted_generation = data.generations.to_vec();
@@ -454,6 +465,7 @@ where
454465
Ok(())
455466
}
456467

468+
#[cfg(feature = "auth_commitment")]
457469
fn verify_commitment_signature<Setup>(
458470
data: &BadPartialShareData<Setup>,
459471
) -> Result<(), Box<dyn std::error::Error>>

0 commit comments

Comments
 (0)