Skip to content

Commit

Permalink
fix map message to scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoSvg committed Feb 1, 2024
1 parent f190975 commit fb93ab0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/bbsplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod bbsplus_example {
let generators = Generators::create::<S::Ciphersuite>(Some(issuer_pk), msgs.len()+2);
//Map Messages to Scalars

let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();

log::info!("Computing pedersen commitment on messages");
let commitment = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars, None, &issuer_pk, &unrevealed_message_indexes);
Expand Down Expand Up @@ -139,7 +139,7 @@ mod bbsplus_example {
const update_index: usize = 0usize;


let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(new_message).unwrap(), Some(&dst));
let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(new_message).unwrap(), Some(&dst));
let old_message_scalar = revealed_msgs.get(update_index).unwrap();

let new_blind_signature = blind_signature.update_signature(issuer_sk, issuer_pk, msgs_scalars.len(), old_message_scalar, &new_message_scalar, update_index);
Expand Down
10 changes: 6 additions & 4 deletions src/utils/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use serde::{Serialize, Deserialize};

#[cfg(feature = "cl03")]
use crate::cl03::ciphersuites::CLCiphersuite;
use crate::schemes::algorithms::Scheme;
#[cfg(feature = "cl03")]
use rug::{Integer, integer::Order};

Expand Down Expand Up @@ -61,11 +62,12 @@ impl BBSplusMessage {
Self{value: msg}
}

pub fn map_message_to_scalar_as_hash<C: BbsCiphersuite>(data: &[u8], dst: Option<&[u8]>) -> Self
pub fn map_message_to_scalar_as_hash<C: Scheme>(data: &[u8], dst: Option<&[u8]>) -> Self
where
C::Expander: for<'a> ExpandMsg<'a>,
C::Ciphersuite: BbsCiphersuite,
<C::Ciphersuite as BbsCiphersuite>::Expander: for<'a> ExpandMsg<'a>,
{
let binding = [C::ID, "MAP_MSG_TO_SCALAR_AS_HASH_".as_bytes()].concat();
let binding = [C::Ciphersuite::ID, "MAP_MSG_TO_SCALAR_AS_HASH_".as_bytes()].concat();
let default_dst = binding.as_slice();
let dst = dst.unwrap_or(default_dst);

Expand All @@ -74,7 +76,7 @@ impl BBSplusMessage {
}

// let scalar = hash_to_scalar::<C>(data, Some(dst));
let scalar = hash_to_scalar_old::<C>(data, 1, Some(dst))[0];
let scalar = hash_to_scalar_old::<C::Ciphersuite>(data, 1, Some(dst))[0];
Self { value: scalar }

}
Expand Down
22 changes: 11 additions & 11 deletions tests/bbsplus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ mod bbsplus_tests {

let msg_hex = hex::decode(msg.as_str().unwrap()).unwrap();

let out = hex::encode(BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&msg_hex, Some(&dst)).to_bytes_be());
let out = hex::encode(BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&msg_hex, Some(&dst)).to_bytes_be());
let out_expected = cases[idx]["scalar"].as_str().unwrap();

if out != out_expected{
Expand Down Expand Up @@ -518,7 +518,7 @@ mod bbsplus_tests {

let dst = hex::decode(msg_scalars["dst"].as_str().unwrap()).unwrap();

let msg_scalars: Vec<BBSplusMessage> = msgs_hex.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msg_scalars: Vec<BBSplusMessage> = msgs_hex.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();

//Precompute generators
let generators = Generators::create::<S::Ciphersuite>(None, msg_scalars.len() + 2);
Expand Down Expand Up @@ -703,7 +703,7 @@ mod bbsplus_tests {

let mut msg_scalars: Vec<BBSplusMessage> = Vec::new();
for m in messages {
msg_scalars.push(BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst)));
msg_scalars.push(BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst)));
}


Expand Down Expand Up @@ -777,8 +777,8 @@ mod bbsplus_tests {
let scalars_json: serde_json::Value = serde_json::from_str(&data_scalars).expect("Unable to parse");
let dst = hex::decode(scalars_json["dst"].as_str().unwrap()).unwrap();

let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars_wrong: Vec<BBSplusMessage> = msgs_wrong.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars_wrong: Vec<BBSplusMessage> = msgs_wrong.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();

let commitment = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars, Some(&generators), &pk, &unrevealed_message_indexes);
let commitment_wrong = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars_wrong, Some(&generators), &pk, &unrevealed_message_indexes);
Expand Down Expand Up @@ -845,8 +845,8 @@ mod bbsplus_tests {
let scalars_json: serde_json::Value = serde_json::from_str(&data_scalars).expect("Unable to parse");
let dst = hex::decode(scalars_json["dst"].as_str().unwrap()).unwrap();

let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars_wrong: Vec<BBSplusMessage> = msgs_wrong.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars_wrong: Vec<BBSplusMessage> = msgs_wrong.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();

let commitment = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars, Some(&generators), pk, &unrevealed_message_indexes);
let commitment_wrong = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars_wrong, Some(&generators), pk, &unrevealed_message_indexes);
Expand Down Expand Up @@ -944,7 +944,7 @@ mod bbsplus_tests {
let scalars_json: serde_json::Value = serde_json::from_str(&data_scalars).expect("Unable to parse");
let dst = hex::decode(scalars_json["dst"].as_str().unwrap()).unwrap();

let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();

let commitment = Commitment::<BBSplus<S::Ciphersuite>>::commit(&msgs_scalars, Some(&generators), pk, &unrevealed_message_indexes);

Expand Down Expand Up @@ -985,7 +985,7 @@ mod bbsplus_tests {

const new_message: &str = "8872ad089e452c7b6e283dfac2a80d58e8d0ff71cc4d5e310a1debdda4a45f02";
const update_index: usize = 0usize;
let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(new_message).unwrap(), Some(&dst));
let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(new_message).unwrap(), Some(&dst));
let old_message_scalar = msgs_scalars.get(update_index).unwrap();

let mut new_msgs_scalars = msgs_scalars.clone();
Expand Down Expand Up @@ -1026,7 +1026,7 @@ mod bbsplus_tests {
let scalars_json: serde_json::Value = serde_json::from_str(&data_scalars).expect("Unable to parse");
let dst = hex::decode(scalars_json["dst"].as_str().unwrap()).unwrap();

let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(m).unwrap(), Some(&dst))).collect();
let msgs_scalars: Vec<BBSplusMessage> = msgs.iter().map(|m| BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(m).unwrap(), Some(&dst))).collect();


let signature = Signature::<BBSplus<S::Ciphersuite>>::sign(Some(&msgs_scalars), sk, pk, Some(&generators), Some(&header));
Expand All @@ -1039,7 +1039,7 @@ mod bbsplus_tests {

const new_message: &str = "8872ad089e452c7b6e283dfac2a80d58e8d0ff71cc4d5e310a1debdda4a45f02";
const update_index: usize = 0usize;
let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<S::Ciphersuite>(&hex::decode(new_message).unwrap(), Some(&dst));
let new_message_scalar = BBSplusMessage::map_message_to_scalar_as_hash::<BBSplus<S::Ciphersuite>>(&hex::decode(new_message).unwrap(), Some(&dst));
let old_message_scalar = msgs_scalars.get(update_index).unwrap();

let updated_signature = signature.update_signature(sk, pk, msgs_scalars.len(), &old_message_scalar, &new_message_scalar, update_index);
Expand Down

0 comments on commit fb93ab0

Please sign in to comment.