curv/cryptographic_primitives/commitments/
pedersen_commitment.rs

1/*
2    This file is part of Curv library
3    Copyright 2018 by Kzen Networks
4    (https://github.com/KZen-networks/curv)
5    License MIT: https://github.com/KZen-networks/curv/blob/master/LICENSE
6*/
7
8use std::marker::PhantomData;
9
10use super::traits::Commitment;
11use super::SECURITY_BITS;
12use crate::arithmetic::traits::*;
13
14use crate::elliptic::curves::{Curve, Point, Scalar};
15use crate::BigInt;
16
17/// compute c = mG + rH
18/// where m is the commited value, G is the group generator,
19/// H is a random point and r is a blinding value.
20///
21pub struct PedersenCommitment<E: Curve>(PhantomData<E>);
22
23impl<E: Curve> Commitment<Point<E>> for PedersenCommitment<E> {
24    fn create_commitment_with_user_defined_randomness(
25        message: &BigInt,
26        blinding_factor: &BigInt,
27    ) -> Point<E> {
28        let g = Point::generator();
29        let h = Point::base_point2();
30        let message_scalar: Scalar<E> = Scalar::from(message);
31        let blinding_scalar: Scalar<E> = Scalar::from(blinding_factor);
32        let mg = g * message_scalar;
33        let rh = h * blinding_scalar;
34        mg + rh
35    }
36
37    fn create_commitment(message: &BigInt) -> (Point<E>, BigInt) {
38        let blinding_factor = BigInt::sample(SECURITY_BITS);
39        let com = PedersenCommitment::create_commitment_with_user_defined_randomness(
40            message,
41            &blinding_factor,
42        );
43        (com, blinding_factor)
44    }
45}