ped92: move commitment to PublicKey
This commit is contained in:
parent
1195f0fdc2
commit
0b277eb2e1
38
src/ped92.rs
38
src/ped92.rs
|
@ -55,36 +55,33 @@ pub struct CSParams<E: Engine> {
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
/*
|
impl<E: Engine> PublicKey<E> {
|
||||||
|
/*
|
||||||
Implements the setup algorithm for the Pedersen92 commitment scheme
|
Implements the setup algorithm for the Pedersen92 commitment scheme
|
||||||
*/
|
*/
|
||||||
pub fn ped92_setup<E: Engine>() -> PublicKey<E> {
|
pub fn setup<R: Rng>(rng: &mut R) -> Self {
|
||||||
println!("Run Setup...");
|
|
||||||
let rng = &mut thread_rng();
|
|
||||||
let g = E::G2::rand(rng);
|
let g = E::G2::rand(rng);
|
||||||
let h = E::G2::rand(rng);
|
let h = E::G2::rand(rng);
|
||||||
let pk = PublicKey { g, h };
|
let pk = PublicKey { g, h };
|
||||||
return pk;
|
return pk;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
commit(pk, msg) -> cm where
|
commit(pk, msg) -> cm where
|
||||||
- pk is the public key generated from setup()
|
- pk is the public key generated from setup()
|
||||||
- msg is the message structure for the commitment scheme
|
- msg is the message structure for the commitment scheme
|
||||||
- cm is the output commitment message for the given message
|
- 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> {
|
pub fn commit<R: Rng>(&self, rng: &mut R, m: E::Fr, R: Option<E::Fr>) -> Commitment<E> {
|
||||||
let rng = &mut thread_rng();
|
|
||||||
|
|
||||||
let r = R.unwrap_or(E::Fr::rand(rng));
|
let r = R.unwrap_or(E::Fr::rand(rng));
|
||||||
//let r = Fr::random(rng);
|
//let r = Fr::random(rng);
|
||||||
|
|
||||||
//let m = msg.hash();
|
//let m = msg.hash();
|
||||||
let p = "commit -> m";
|
let p = "commit -> m";
|
||||||
// c = g^m * h^r
|
// c = g^m * h^r
|
||||||
let mut c = pk.g.clone();
|
let mut c = self.g.clone();
|
||||||
c.mul_assign(m);
|
c.mul_assign(m);
|
||||||
let mut h = pk.h.clone();
|
let mut h = self.h.clone();
|
||||||
h.mul_assign(r);
|
h.mul_assign(r);
|
||||||
c.add_assign(&h);
|
c.add_assign(&h);
|
||||||
// return (c, r) <- d=r
|
// return (c, r) <- d=r
|
||||||
|
@ -92,24 +89,25 @@ pub fn ped92_commit<E: Engine>(pk: &PublicKey<E>, m: E::Fr, R: Option<E::Fr>) ->
|
||||||
|
|
||||||
// debugging
|
// debugging
|
||||||
return commitment;
|
return commitment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
decommit(pk, cm, msg) -> bool where
|
decommit(pk, cm, msg) -> bool where
|
||||||
- pk is the public key generated from setup()
|
- pk is the public key generated from setup()
|
||||||
- cm is the commitment
|
- cm is the commitment
|
||||||
- m is the message to validate
|
- m is the message to validate
|
||||||
- outputs T/F for whether the cm is a valid commitment to the msg
|
- 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 {
|
pub fn decommit(&self, cm: &Commitment<E>, m: E::Fr) -> bool {
|
||||||
let p = "decommit -> m";
|
let p = "decommit -> m";
|
||||||
|
|
||||||
let mut dm = pk.g.clone();
|
let mut dm = self.g.clone();
|
||||||
dm.mul_assign(m);
|
dm.mul_assign(m);
|
||||||
let mut h = pk.h.clone();
|
let mut h = self.h.clone();
|
||||||
h.mul_assign(cm.r.clone());
|
h.mul_assign(cm.r.clone());
|
||||||
dm.add_assign(&h);
|
dm.add_assign(&h);
|
||||||
return dm == cm.c;
|
return dm == cm.c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -165,16 +163,16 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn commit_one_message_works() {
|
fn commit_one_message_works() {
|
||||||
let rng = &mut thread_rng();
|
let rng = &mut thread_rng();
|
||||||
let pk = ped92_setup::<Bls12>();
|
let pk = PublicKey::<Bls12>::setup(rng);
|
||||||
|
|
||||||
let m1 = Fr::rand(rng);
|
let m1 = Fr::rand(rng);
|
||||||
let mut m2 = m1.clone();
|
let mut m2 = m1.clone();
|
||||||
m2.add_assign(&Fr::one());
|
m2.add_assign(&Fr::one());
|
||||||
let r = Fr::rand(rng);
|
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!(true, pk.decommit(&c, m1));
|
||||||
assert_eq!(false, ped92_decommit(&pk, &c, m2));
|
assert_eq!(false, pk.decommit(&c, m2));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue