ped92: move commitment to PublicKey

This commit is contained in:
Gijs Van Laer 2019-06-14 20:55:02 -04:00
parent 1195f0fdc2
commit 0b277eb2e1
1 changed files with 39 additions and 41 deletions

View File

@ -55,61 +55,59 @@ pub struct CSParams<E: Engine> {
// }
//}
/*
impl<E: Engine> PublicKey<E> {
/*
Implements the setup algorithm for the Pedersen92 commitment scheme
*/
pub fn ped92_setup<E: Engine>() -> PublicKey<E> {
println!("Run Setup...");
let rng = &mut thread_rng();
let g = E::G2::rand(rng);
let h = E::G2::rand(rng);
let pk = PublicKey { g, h };
return pk;
}
pub fn setup<R: Rng>(rng: &mut R) -> Self {
let g = E::G2::rand(rng);
let h = E::G2::rand(rng);
let pk = PublicKey { g, h };
return pk;
}
/*
/*
commit(pk, msg) -> cm where
- pk is the public key generated from setup()
- msg is the message structure for the commitment scheme
- cm is the output commitment message for the given message
*/
pub fn ped92_commit<E: Engine>(pk: &PublicKey<E>, m: E::Fr, R: Option<E::Fr>) -> Commitment<E> {
let rng = &mut thread_rng();
pub fn commit<R: Rng>(&self, rng: &mut R, m: E::Fr, R: Option<E::Fr>) -> Commitment<E> {
let r = R.unwrap_or(E::Fr::rand(rng));
//let r = Fr::random(rng);
let r = R.unwrap_or(E::Fr::rand(rng));
//let r = Fr::random(rng);
//let m = msg.hash();
let p = "commit -> m";
// c = g^m * h^r
let mut c = self.g.clone();
c.mul_assign(m);
let mut h = self.h.clone();
h.mul_assign(r);
c.add_assign(&h);
// return (c, r) <- d=r
let commitment = Commitment { c, r };
//let m = msg.hash();
let p = "commit -> m";
// c = g^m * h^r
let mut c = pk.g.clone();
c.mul_assign(m);
let mut h = pk.h.clone();
h.mul_assign(r);
c.add_assign(&h);
// return (c, r) <- d=r
let commitment = Commitment { c, r };
// debugging
return commitment;
}
// debugging
return commitment;
}
/*
/*
decommit(pk, cm, msg) -> bool where
- pk is the public key generated from setup()
- cm is the commitment
- m is the message to validate
- outputs T/F for whether the cm is a valid commitment to the msg
*/
pub fn ped92_decommit<E: Engine>(pk: &PublicKey<E>, cm: &Commitment<E>, m: E::Fr) -> bool {
let p = "decommit -> m";
pub fn decommit(&self, cm: &Commitment<E>, m: E::Fr) -> bool {
let p = "decommit -> m";
let mut dm = pk.g.clone();
dm.mul_assign(m);
let mut h = pk.h.clone();
h.mul_assign(cm.r.clone());
dm.add_assign(&h);
return dm == cm.c;
let mut dm = self.g.clone();
dm.mul_assign(m);
let mut h = self.h.clone();
h.mul_assign(cm.r.clone());
dm.add_assign(&h);
return dm == cm.c;
}
}
@ -165,16 +163,16 @@ mod tests {
#[test]
fn commit_one_message_works() {
let rng = &mut thread_rng();
let pk = ped92_setup::<Bls12>();
let pk = PublicKey::<Bls12>::setup(rng);
let m1 = Fr::rand(rng);
let mut m2 = m1.clone();
m2.add_assign(&Fr::one());
let r = Fr::rand(rng);
let c = ped92_commit(&pk, m1, Some(r));
let c = pk.commit(rng, m1, Some(r));
assert_eq!(true, ped92_decommit(&pk, &c, m1));
assert_eq!(false, ped92_decommit(&pk, &c, m2));
assert_eq!(true, pk.decommit(&c, m1));
assert_eq!(false, pk.decommit(&c, m2));
}
#[test]