This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Testing Boneh Franklin IBE on BN curve #4
Open
Geal
wants to merge
8
commits into
zcash-hackworks:master
Choose a base branch
from
Geal:ibe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a2c8f7
Fix import: num_bigint library is accessible from num as num::bigint.
clemensw a34b8f9
Examples of BLS signatures and aggregate BLS signatures.
zmanian 7d0851d
Forgot to add the sighash module
zmanian 1bd89b2
-Remove the Agg sigs from BLS sigs
zmanian 91dd1e4
Fix nits on MSG names and assert false
zmanian adb2a7d
Merge commit 'refs/pull/2/head' of https://github.com/zcash/bn into ibe
Geal 70163a3
fix compilation of examples
Geal e4fb5cd
test implementation of Boneh Franklin 'BasicIdent' IBE scheme
Geal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
extern crate rand; | ||
extern crate bn; | ||
|
||
use bn::{Field, Scalar, G1, G2, Gt, Fq12, pairing}; | ||
mod sighash; | ||
use std::collections::HashMap; | ||
|
||
|
||
fn verify_aggregate_sig(agg_sig: &G1, msg_key_pairs: &[(&str, &G2)]) -> bool { | ||
let mut unique = HashMap::new(); | ||
let mut agg_verifier = Gt::new(Fq12::one()); | ||
for &(msg, ref pub_key) in msg_key_pairs { | ||
match unique.get(msg) { | ||
Some(&true) => return false, //fail on duplicate messages | ||
_ => {} // do nothing | ||
} | ||
unique.insert(msg, true); | ||
let msg_e = G1::random(&mut sighash::SignatureHash::from(msg)); | ||
agg_verifier = agg_verifier * pairing(&msg_e, &pub_key); | ||
} | ||
if pairing(agg_sig, &G2::one()) != agg_verifier { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
fn main() { | ||
let rng = &mut rand::thread_rng(); | ||
|
||
const MSG1: &'static str = "Hello!"; | ||
const MSG2: &'static str = "Hello!2"; | ||
|
||
// Generate Keys | ||
let alice_sk = Scalar::random(rng); | ||
let bob_sk = Scalar::random(rng); | ||
|
||
// Generate Public Keys | ||
let alice_pk = G2::one() * &alice_sk; | ||
let bob_pk = G2::one() * &bob_sk; | ||
// Generate Signatures | ||
let msgm1 = G1::random(&mut sighash::SignatureHash::from(MSG1)); | ||
let sigm1_a = &msgm1 * &alice_sk; | ||
|
||
let msgm2 = G1::random(&mut sighash::SignatureHash::from(MSG2)); | ||
let sigm2_b = &msgm2 * &bob_sk; | ||
|
||
// Verify single signatures | ||
assert_eq!(pairing(&sigm1_a, &G2::one()), pairing(&msgm1, &alice_pk)); | ||
assert_eq!(pairing(&sigm2_b, &G2::one()), pairing(&msgm2, &bob_pk)); | ||
|
||
// Generate Aggregate Signature | ||
let sig_m1m2 = &sigm1_a + &sigm2_b; | ||
|
||
// Verify the Aggregate Signature | ||
assert!(verify_aggregate_sig(&sig_m1m2, &[(MSG1, &alice_pk), (MSG2, &bob_pk)])); | ||
|
||
//Test duplicate messages | ||
//Generate bob's sig of MSG | ||
let sigm1_b = &msgm1 * &bob_sk; | ||
|
||
//Generate duplicate aggregate signature | ||
let sig_m1_dup = &sigm1_a + &sigm1_b; | ||
assert!(!verify_aggregate_sig(&sig_m1_dup, &[(MSG1, &alice_pk), (MSG1, &bob_pk)])); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extern crate rand; | ||
extern crate bn; | ||
|
||
use bn::{Field, Scalar, G1, G2, pairing}; | ||
mod sighash; | ||
|
||
fn main() { | ||
let rng = &mut rand::thread_rng(); | ||
|
||
// Generate Keys | ||
let alice_sk = Scalar::random(rng); | ||
|
||
// Generate Public Keys | ||
let alice_pk = G2::one() * &alice_sk; | ||
// Generate Signature | ||
let msgm1 = G1::random(&mut sighash::SignatureHash::from("Hello!")); | ||
let sigm1_a = &msgm1 * &alice_sk; | ||
|
||
// Verify single signature | ||
assert_eq!(pairing(&sigm1_a, &G2::one()), pairing(&msgm1, &alice_pk)); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
extern crate bn; | ||
extern crate rand; | ||
extern crate sodiumoxide; | ||
|
||
mod sighash; | ||
|
||
use bn::*; | ||
use sodiumoxide::crypto::stream::chacha20; | ||
use sodiumoxide::crypto::hash::sha256; | ||
use std::str; | ||
|
||
fn main() { | ||
ibe(); | ||
} | ||
|
||
fn ibe() { | ||
let rng = &mut rand::thread_rng(); | ||
let master_sk = Scalar::random(rng); | ||
// do we need another generator than G1::one() here? | ||
// we use G1 since Ppub is used in the first arg of pairing | ||
let master_pk = G1::one() * &master_sk; | ||
|
||
let id = b"test"; | ||
|
||
let derived = G2::random(&mut sighash::SignatureHash::from(&id[..])); | ||
println!("derived: {:?}", derived); | ||
|
||
let id_sk = &derived * &master_sk; | ||
|
||
//encrypting with BasicIdent | ||
let r = Scalar::random(rng); | ||
let g_id = pairing(&master_pk, &derived) ^ &r; | ||
println!("g_id: {:?}", g_id); | ||
let badly_serialized = format!("{:?}", g_id); | ||
let hash = sha256::hash(badly_serialized.as_bytes()); | ||
|
||
println!("hash: {:?}", hash); | ||
let sym_key = chacha20::Key::from_slice(&hash[..32]).unwrap(); | ||
let nonce = chacha20::gen_nonce(); | ||
|
||
let plaintext = "We propose a fully functional identity-based encryption scheme (IBE). The scheme has chosen ciphertext security in the random oracle model assuming a variant of the computational Diffie-Hellman problem"; | ||
let ciphertext = chacha20::stream_xor(plaintext.as_bytes(), &nonce, &sym_key); | ||
|
||
// do we need another generator than G1::one() here? | ||
let result = (G1::one() * &r, ciphertext); | ||
println!("ciphertext: {:?}", result); | ||
|
||
//decrypting | ||
let decrypting_seed = pairing(&result.0, &id_sk); | ||
let badly_serialized_again = format!("{:?}", decrypting_seed); | ||
println!("seed: {:?}", decrypting_seed); | ||
assert_eq!(g_id, decrypting_seed); | ||
let hash2 = sha256::hash(badly_serialized_again.as_bytes()); | ||
println!("hash2: {:?}", hash2); | ||
let sym_key_2 = chacha20::Key::from_slice(&hash2[..32]).unwrap(); | ||
let decrypted = chacha20::stream_xor(&result.1, &nonce, &sym_key_2); | ||
println!("decrypted: \"{}\"", str::from_utf8(&decrypted).unwrap()); | ||
assert_eq!(plaintext.as_bytes(), &decrypted[..]); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
extern crate rand; | ||
extern crate sodiumoxide; | ||
|
||
pub struct SignatureHash(rand::ChaChaRng); | ||
|
||
impl rand::Rng for SignatureHash { | ||
fn next_u32(&mut self) -> u32 { | ||
self.0.next_u32() | ||
} | ||
} | ||
|
||
impl<'a> From<&'a str> for SignatureHash { | ||
fn from(v: &str) -> SignatureHash { | ||
SignatureHash::from(v.as_bytes()) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a [u8]> for SignatureHash { | ||
fn from(v: &[u8]) -> SignatureHash { | ||
use rand::SeedableRng; | ||
use std::slice; | ||
use std::mem; | ||
|
||
let hash = sodiumoxide::crypto::hash::sha256::hash(v); | ||
assert_eq!(hash.0.len(), 32); | ||
|
||
SignatureHash(rand::ChaChaRng::from_seed(unsafe { | ||
slice::from_raw_parts(mem::transmute::<&u8, &u32>(&hash.0[0]), 8) | ||
})) | ||
} | ||
} | ||
|
||
fn main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we break out a
point_to_symmetric_key
function that does sha256(serialized point)