Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ members = [
[dependencies]
hex = "0.4.3"
sp1-sdk = "3.4.0"

text_io = "0.1.12"
bls12_381 = "0.8.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4", features = ["derive"] }
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
# Variables
SHELL := /usr/bin/env bash
REPO_ROOT := $(shell git rev-parse --show-toplevel)
TEST_SCRIPT := $(REPO_ROOT)/scripts/run.sh
TEST_SCRIPT := $(REPO_ROOT)/script/run.sh

help:
@echo "Makefile targets:"
@echo " test Run all tests using the run_tests.sh script"
@echo " help Show this help message"

install-git-hooks:
@ls -R ./.git/hooks > before.txt
@cp -r ./script/hooks/ ./.git/
@chmod +x ./.git/hooks/pre-commit
@ls -R ./.git/hooks > after.txt
@diff before.txt after.txt || true
@rm before.txt after.txt
@echo "Hooks installed successfully."

test:
@if [ ! -f "$(TEST_SCRIPT)" ]; then \
echo "Error: run_tests.sh not found in repository root."; \
Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
fn main() {
sp1_build::build_program("crates/share_exchange_prove");
sp1_build::build_program("crates/finalization_prove");
sp1_build::build_program("crates/wrong_final_key_generation_prove");
sp1_build::build_program("crates/bad_encrypted_share_prove");
}
16 changes: 16 additions & 0 deletions crates/bad_encrypted_share_prove/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "bad_encrypted_share_prove"
version = "1.1.0"
edition = "2021"
publish = false

[dependencies]
sp1-zkvm = "3.4.0"
ff = "0.13.0"
rand = "0.8.5"
group = "0.13.0"
serde = "1.0.216"
dvt_abi = { path = "../dvt_abi" }
bls_utils = { path = "../bls_utils" }
hex = "0.4"
chacha20 = "0.9.1"
52 changes: 52 additions & 0 deletions crates/bad_encrypted_share_prove/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![no_main]

sp1_zkvm::entrypoint!(main);

use core::panic;

use bls_utils::{self};

use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
use chacha20::ChaCha20;

pub fn main() {
let data = bls_utils::read_wrong_final_key_generation_data();
print!("{:?}", data);

let ok = bls_utils::prove_wrong_final_key_generation(&data);
if ok.is_err() {
panic!("{:?}", ok.unwrap_err().to_string());
}

let key = [0x42; 32];
let nonce = [0x24; 12];
let plaintext = hex::decode("000102030405060708090A0B0C0D0E0F").unwrap();
let ciphertext = hex::decode("e405626e4f1236b3670ee428332ea20e").unwrap();

// Key and IV must be references to the `GenericArray` type.
// Here we use the `Into` trait to convert arrays into it.
let mut cipher = ChaCha20::new(&key.into(), &nonce.into());

let mut buffer = plaintext.clone();

// apply keystream (encrypt)
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, ciphertext);

let ciphertext = buffer.clone();

// ChaCha ciphers support seeking
cipher.seek(0u32);

// decrypt ciphertext by applying keystream again
cipher.apply_keystream(&mut buffer);
assert_eq!(buffer, plaintext);

// stream ciphers can be used with streaming messages
cipher.seek(0u32);
for chunk in buffer.chunks_mut(3) {
cipher.apply_keystream(chunk);
}
assert_eq!(buffer, ciphertext);
print!("{:?}", ciphertext);
}
7 changes: 4 additions & 3 deletions crates/bls_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ authors = { workspace = true }
edition = { workspace = true }

[dependencies]
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", features = ["experimental"] }
ff = "0.13.0"
rand = "0.8.5"
group = "0.13.0"
serde = "1.0.216"
dvt_abi = { path = "../dvt_abi" }
sha2 = "0.10"
hex = "0.4"
sp1-zkvm = "3.4.0"
sp1-zkvm = "3.4.0"
sp1-lib = "3.4.0"
sha2 = "0.9"
bls12_381 = { git = "https://github.com/sp1-patches/bls12_381", tag = "bls12_381-v0.8.0-patch-v1" , features = ["experimental"]}
49 changes: 34 additions & 15 deletions crates/bls_utils/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ pub fn evaluate_polynomial(cfs: Vec<G1Affine>, x: Scalar) -> G1Affine {
}
}

pub fn evaluate_polynomial_g1_projection(cfs: &Vec<G1Projective>, x: Scalar) -> G1Projective {
let count = cfs.len();
if count == 0 {
return G1Projective::identity();
} else if count == 1 {
return G1Projective::from(cfs[0]);
} else {
let mut y = cfs[count - 1];
for i in 2..(count + 1) {
y = y * x + cfs[count - i];
}
return y;
}
}

pub fn lagrange_interpolation(
y_vec: &Vec<G1Affine>,
x_vec: &Vec<Scalar>,
Expand Down Expand Up @@ -89,21 +104,26 @@ pub fn lagrange_interpolation(
Ok(G1Affine::from(r))
}

pub fn hash_message_to_g2(msg: &[u8], domain: &[u8]) -> G2Projective {
<G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve([msg], domain)
}

pub fn bls_verify(pubkey: &G1Affine, signature: &G2Affine, message: &[u8]) -> bool {
pub fn hash_message_to_g2(msg: &[u8]) -> G2Projective {
let domain = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
let pk_projective = G1Projective::from(pubkey);
let sig_projective = G2Projective::from(signature);
<G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(msg, domain)
}

let hashed_msg = hash_message_to_g2(message, domain);
let left = pairing(&G1Affine::from(pk_projective), &G2Affine::from(hashed_msg));
let right = pairing(&G1Affine::generator(), &G2Affine::from(sig_projective));
pub fn bls_verify_precomputed_hash(
pubkey: &G1Affine,
signature: &G2Affine,
hashed_msg: &G2Affine,
) -> bool {
let left = pairing(&pubkey, &hashed_msg);
let right = pairing(&G1Affine::generator(), &signature);

left == right
}
pub fn bls_verify(pubkey: &G1Affine, signature: &G2Affine, message: &[u8]) -> bool {
let hashed_msg = hash_message_to_g2(message);
let msg_affine = G2Affine::from(hashed_msg);
bls_verify_precomputed_hash(pubkey, signature, &msg_affine)
}

pub fn bls_id_from_u32(id: u32) -> Scalar {
let unwrapped_le: [u8; 4] = (id as u32).to_le_bytes();
Expand Down Expand Up @@ -168,14 +188,13 @@ impl SecretKey {

let sk = Scalar::from_bytes(&le_bytes);

if sk.is_none().into() {
return Err(Box::new(std::io::Error::new(
match sk.into_option() {
Some(sk) => Ok(SecretKey { key: sk }),
None => Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid secret key",
)));
))),
}

Ok(SecretKey { key: sk.unwrap() })
}

pub fn to_bytes(&self) -> [u8; 32] {
Expand Down
17 changes: 16 additions & 1 deletion crates/bls_utils/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn read_generation_data(n: u8, k: u8) -> Vec<dvt_abi::AbiGeneration> {
pub fn read_bls_shared_data_from_host() -> dvt_abi::AbiBlsSharedData {
let inital_commitment = read_initial_commitment_from_host();
let seeds_exchange_commitment = read_seeds_exchange_commitment_from_host();
let verification_hashes = read_vec_from_host(inital_commitment.settings.k);
let verification_hashes = read_vec_from_host(inital_commitment.settings.n);
dvt_abi::AbiBlsSharedData {
verification_hashes: verification_hashes,
initial_commitment: inital_commitment,
Expand All @@ -160,3 +160,18 @@ pub fn read_finalization_data() -> dvt_abi::AbiFinalizationData {
aggregate_pubkey: aggregate_pubkey,
}
}

pub fn read_wrong_final_key_generation_data() -> dvt_abi::AbiWrongFinalKeyGeneration {
let settings = read_settings_from_host();
let generations = read_generation_data(settings.n, settings.k);
let perpatrator_hash = read_hash_from_host();
dvt_abi::AbiWrongFinalKeyGeneration {
settings: settings,
generations: generations,
perpatrator_hash: perpatrator_hash,
}
}

pub fn read_bad_encrypted_share() -> dvt_abi::AbiBadEncryptedShare {
dvt_abi::AbiBadEncryptedShare {}
}
7 changes: 5 additions & 2 deletions crates/bls_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ pub mod bls;
pub mod input;
pub mod verification;

pub use input::{read_bls_shared_data_from_host, read_finalization_data};
pub use input::{
read_bls_shared_data_from_host, read_finalization_data, read_wrong_final_key_generation_data,
};

pub use verification::{
verify_generations, verify_initial_commitment_hash, verify_seed_exchange_commitment, VerificationErrors
prove_wrong_final_key_generation, verify_generations, verify_initial_commitment_hash,
verify_seed_exchange_commitment, VerificationErrors,
};
Loading