Skip to content

Commit ae73b63

Browse files
committed
Rename bls_utils to dvt_common and prepare the code for integration of secp256k1 identity
1 parent de4ec8c commit ae73b63

File tree

17 files changed

+350
-294
lines changed

17 files changed

+350
-294
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ test:
2626
echo "Error: run_tests.sh not found in repository root."; \
2727
exit 1; \
2828
fi
29-
@cd "$(REPO_ROOT)/crates/bls_utils" && cargo test
29+
@cd "$(REPO_ROOT)/crates/dvt_common" && cargo test
3030
@cd "$(REPO_ROOT)" && $(TEST_SCRIPT) $(ARGS)
3131

crates/bad_encrypted_share_prove/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rand = "0.8.5"
1111
group = "0.13.0"
1212
serde = "1.0.216"
1313
dvt_abi = { path = "../dvt_abi" }
14-
bls_utils = { path = "../bls_utils" }
14+
dvt_common = { path = "../dvt_common" }
1515
bls12_381 = { git = "https://github.com/NDobrev/bls12_381", branch="fp-to-bytes", features = ["experimental"]}
1616
hex = "0.4"
1717
chacha20 = "0.9.1"

crates/bad_encrypted_share_prove/src/main.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ sp1_zkvm::entrypoint!(main);
44

55
use std::result;
66

7-
use bls_utils::{self, bls, VerificationErrors};
7+
use dvt_common::{self, VerificationErrors};
88

9-
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
9+
use chacha20::cipher::{KeyIvInit, StreamCipher};
1010
use chacha20::{ChaCha20, Key, Nonce};
1111

12-
use bls12_381::{self, G1Affine, G1Projective, G2Affine};
13-
use dvt_abi::{AbiInitialCommitment, BLS_ID_SIZE};
12+
use bls12_381::{self, G1Affine, G2Affine};
1413
use sha2::{Digest, Sha256};
1514
use std::fmt;
1615

17-
fn new_chacha20_cipher(base: &[u8], key_salt: &str, nonce_salt: &str) -> ChaCha20 {
16+
fn new_chacha20_cipher(base: &[u8], _key_salt: &str, _nonce_salt: &str) -> ChaCha20 {
1817
let mut key_hasher = Sha256::new();
1918
key_hasher.update(base);
2019
//key_hasher.update(key_salt.as_bytes());
@@ -197,7 +196,7 @@ fn parse_message(
197196
hash: [0u8; dvt_abi::SHA256_SIZE],
198197
};
199198

200-
let initial_commitment_hash = bls_utils::compute_initial_commitment_hash(&initial_commitment);
199+
let initial_commitment_hash = dvt_common::compute_initial_commitment_hash(&initial_commitment);
201200

202201
initial_commitment.hash = initial_commitment_hash.clone();
203202
// println!("gen_id {}", hex::encode(&gen_id));
@@ -225,7 +224,7 @@ fn parse_message(
225224
}
226225

227226
pub fn main() {
228-
let data = bls_utils::read_bad_encrypted_share();
227+
let data = dvt_common::read_bad_encrypted_share();
229228

230229
let pk = G1Affine::from_compressed(&data.sender_pubkey)
231230
.into_option()
@@ -251,6 +250,7 @@ pub fn main() {
251250
Ok(data) => data,
252251
Err(e) => {
253252
println!("Error: {}", e);
253+
sp1_zkvm::io::commit(&data.encrypted_message);
254254
return;
255255
}
256256
};
@@ -280,11 +280,11 @@ pub fn main() {
280280
);
281281
}
282282

283-
if !bls_utils::verify_initial_commitment_hash(&data.initial_commitment) {
283+
if !dvt_common::verify_initial_commitment_hash(&data.initial_commitment) {
284284
panic!("Unsalshable error while verifying commitment hash\n");
285285
}
286286

287-
match bls_utils::verify_seed_exchange_commitment(
287+
match dvt_common::verify_seed_exchange_commitment(
288288
&data.verification_hashes,
289289
&data.seeds_exchange_commitment,
290290
&data.initial_commitment,

crates/bad_parial_key_prove/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ rand = "0.8.5"
1111
group = "0.13.0"
1212
serde = "1.0.216"
1313
dvt_abi = { path = "../dvt_abi" }
14-
bls_utils = { path = "../bls_utils" }
14+
dvt_common = { path = "../dvt_common" }
1515
hex = "0.4"
1616
chacha20 = "0.9.1"

crates/bad_parial_key_prove/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ sp1_zkvm::entrypoint!(main);
44

55
use core::panic;
66

7-
use bls_utils::{self, VerificationErrors};
7+
use dvt_common::{self, VerificationErrors};
88

99
pub fn main() {
10-
let data = bls_utils::read_bad_partial_share_data();
11-
match bls_utils::prove_wrong_final_key_generation(&data) {
10+
let data = dvt_common::read_bad_partial_share_data();
11+
match dvt_common::prove_wrong_final_key_generation(&data) {
1212
Ok(()) => {
1313
panic!("Can't prove wrong doing");
1414
}

crates/bad_share_exchange_prove/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ rand = "0.8.5"
1111
group = "0.13.0"
1212
serde = "1.0.216"
1313
dvt_abi = { path = "../dvt_abi" }
14-
bls_utils = { path = "../bls_utils" }
14+
dvt_common = { path = "../dvt_common" }
1515
hex = "0.4"

crates/bad_share_exchange_prove/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ sp1_zkvm::entrypoint!(main);
44

55
use core::panic;
66

7-
use bls_utils::{self, VerificationErrors};
7+
use dvt_common::{self, VerificationErrors};
88

99
pub fn main() {
10-
let data = bls_utils::read_bls_shared_data_from_host();
10+
let data = dvt_common::read_bls_shared_data_from_host();
1111

1212
if data.verification_hashes.len() != data.initial_commitment.settings.n as usize {
1313
panic!("The number of verification hashes does not match the number of keys\n");
@@ -26,11 +26,11 @@ pub fn main() {
2626
panic!("The seed exchange commitment is not part of the verification hashes\n");
2727
}
2828

29-
if !bls_utils::verify_initial_commitment_hash(&data.initial_commitment) {
29+
if !dvt_common::verify_initial_commitment_hash(&data.initial_commitment) {
3030
panic!("Unsalshable error while verifying commitment hash\n");
3131
}
3232

33-
match bls_utils::verify_seed_exchange_commitment(
33+
match dvt_common::verify_seed_exchange_commitment(
3434
&data.verification_hashes,
3535
&data.seeds_exchange_commitment,
3636
&data.initial_commitment,

crates/bls_utils/Cargo.toml renamed to crates/dvt_common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bls_utils"
2+
name = "dvt_common"
33
version = { workspace = true }
44
authors = { workspace = true }
55
edition = { workspace = true }
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use bls12_381::{
2+
hash_to_curve::{ExpandMsgXmd, HashToCurve},
3+
pairing, G1Affine, G1Projective, G2Affine, G2Projective, Scalar,
4+
};
5+
6+
use bls_org;
7+
use dvt_abi::{self};
8+
use sha2::Sha256;
9+
10+
pub fn hash_message_to_g2(msg: &[u8]) -> G2Projective {
11+
let domain = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
12+
<G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(msg, domain)
13+
}
14+
15+
pub fn bls_verify_precomputed_hash(
16+
pubkey: &G1Affine,
17+
signature: &G2Affine,
18+
hashed_msg: &G2Affine,
19+
) -> bool {
20+
let left = pairing(&pubkey, &hashed_msg);
21+
let right = pairing(&G1Affine::generator(), &signature);
22+
23+
left == right
24+
}
25+
pub fn bls_verify(pubkey: &G1Affine, signature: &G2Affine, message: &[u8]) -> bool {
26+
let hashed_msg = hash_message_to_g2(message);
27+
let msg_affine = G2Affine::from(hashed_msg);
28+
bls_verify_precomputed_hash(pubkey, signature, &msg_affine)
29+
}
30+
31+
pub fn bls_id_from_u32(id: u32) -> Scalar {
32+
let unwrapped_le: [u8; 4] = (id as u32).to_le_bytes();
33+
let mut bytes = [0u8; 32];
34+
bytes[..4].copy_from_slice(&unwrapped_le);
35+
Scalar::from_bytes(&bytes).expect("Invalid id")
36+
}
37+
38+
fn uncompress_bls_pubkey_slow(
39+
pubkey: &dvt_abi::BLSPubkey,
40+
) -> Result<[u8; 96], Box<dyn std::error::Error>> {
41+
// We use the original bls library to verify the key
42+
// Becaus the sp1 library will crash if the key is invalid
43+
let key = bls_org::G1Affine::from_compressed(&pubkey);
44+
45+
return match key.into_option() {
46+
Some(key) => Ok(key.to_uncompressed()),
47+
None => Err(Box::new(std::io::Error::new(
48+
std::io::ErrorKind::InvalidData,
49+
format!("Invalid public key {}", hex::encode(&pubkey)),
50+
))),
51+
};
52+
}
53+
54+
pub fn to_g1_affine_slow(
55+
pubkey: &dvt_abi::BLSPubkey,
56+
) -> Result<G1Affine, Box<dyn std::error::Error>> {
57+
let bytes = uncompress_bls_pubkey_slow(&pubkey)?;
58+
59+
let key = G1Affine::from_uncompressed(&bytes);
60+
match key.into_option() {
61+
Some(key) => Ok(key),
62+
None => Err(Box::new(std::io::Error::new(
63+
std::io::ErrorKind::InvalidData,
64+
format!("Invalid public key {}", hex::encode(&pubkey)),
65+
))),
66+
}
67+
}
68+
69+
fn uncompress_bls_signature_slow(
70+
signature: &dvt_abi::BLSSignature,
71+
) -> Result<[u8; 192], Box<dyn std::error::Error>> {
72+
// We use the original bls library to verify the key
73+
// Becaus the sp1 library will crash if the key is invalid
74+
let key = bls_org::G2Affine::from_compressed(&signature);
75+
76+
return match key.into_option() {
77+
Some(key) => Ok(key.to_uncompressed()),
78+
None => Err(Box::new(std::io::Error::new(
79+
std::io::ErrorKind::InvalidData,
80+
"Invalid signature",
81+
))),
82+
};
83+
}
84+
pub fn to_g2_affine_slow(
85+
signature: &dvt_abi::BLSSignature,
86+
) -> Result<G2Affine, Box<dyn std::error::Error>> {
87+
let bytes = uncompress_bls_signature_slow(&signature)?;
88+
89+
let key = G2Affine::from_uncompressed(&bytes);
90+
match key.into_option() {
91+
Some(key) => Ok(key),
92+
None => Err(Box::new(std::io::Error::new(
93+
std::io::ErrorKind::InvalidData,
94+
"Invalid signature",
95+
))),
96+
}
97+
}
98+
99+
pub fn to_g1_affine(pubkey: &dvt_abi::BLSPubkey) -> G1Affine {
100+
G1Affine::from_compressed(&pubkey)
101+
.into_option()
102+
.expect("G1 point is not torsion free.")
103+
}
104+
105+
pub fn to_g1_projection(pubkey: &dvt_abi::BLSPubkey) -> G1Projective {
106+
G1Projective::from(to_g1_affine(pubkey))
107+
}

0 commit comments

Comments
 (0)