Skip to content

Commit ffc05da

Browse files
committed
blind.rs build up, deving signer.rs
1 parent e594e27 commit ffc05da

7 files changed

Lines changed: 68 additions & 15 deletions

File tree

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! User functions impl for some user-side operations, including:
2+
//! blind, unblind and proof of knowledge of committed values.
3+
14
use ark_bn254::{G1Affine as G1, G1Projective as G1Projective, Fr as Scalar};
25
use ark_ec::CurveGroup;
36
use ark_ff::PrimeField;
@@ -7,20 +10,19 @@ use sha2::{Digest, Sha256};
710

811
use crate::bbs_bn254::structs::{BlindedCommitment, CommitmentProof, Parameters};
912

10-
/// User functions impl for BBS+ signatures over BN254.
11-
/// Includes blind, unblind, proofgen
12-
1313
/// Create a blinded commitment for a list of messages.
1414
/// Returns `Err` if message length exceeds available parameters.
15-
pub fn blind(params: &Parameters, messages: &[Scalar]) -> Result<BlindedCommitment, &'static str> {
15+
/// data with index at and behind `blind_index` will be blinded, should be between 1 and messages.len() - 1
16+
pub fn blind(params: &Parameters, messages: &[Scalar], blind_index: usize) -> Result<BlindedCommitment, &'static str> {
1617
let mut rng = ark_std::test_rng();
17-
blind_with_rng(params, messages, &mut rng)
18+
blind_with_rng(params, messages, blind_index, &mut rng)
1819
}
1920

2021
/// Create a blinded commitment using a caller-supplied RNG.
2122
pub fn blind_with_rng<R: RngCore>(
2223
params: &Parameters,
2324
messages: &[Scalar],
25+
blind_index: usize,
2426
rng: &mut R,
2527
) -> Result<BlindedCommitment, &'static str> {
2628
if messages.len() > params.L {
@@ -29,11 +31,17 @@ pub fn blind_with_rng<R: RngCore>(
2931
if params.H.len() < messages.len() + 1 {
3032
return Err("parameters do not include enough message base points");
3133
}
34+
if blind_index == 0 || blind_index >= messages.len() {
35+
return Err("invalid blind index");
36+
}
3237

38+
// calc r*H_1
3339
let blinding_factor = Scalar::rand(rng);
3440
let mut commitment = params.H[0] * blinding_factor;
35-
for (i, m) in messages.iter().enumerate() {
36-
commitment += params.H[i + 1] * m;
41+
42+
// calc m_j*H_{j+1}
43+
for j in (blind_index - 1)..messages.len() {
44+
commitment += params.H[j + 1] * messages[j];
3745
}
3846

3947
Ok(BlindedCommitment {
@@ -140,7 +148,7 @@ mod tests {
140148
let messages = vec![Scalar::from(1u64), Scalar::from(2u64), Scalar::from(3u64)];
141149

142150
let mut rng = ark_std::test_rng();
143-
let commitment = blind_with_rng(&params, &messages, &mut rng).unwrap();
151+
let commitment = blind_with_rng(&params, &messages, 1, &mut rng).unwrap();
144152
let proof = commitment_pok_prove_with_rng(&params, &commitment, &messages, &mut rng).unwrap();
145153

146154
let ok = commitment_pok_verify(&params, &commitment, &proof).unwrap();

bbs/src/bbs_bn254/keygen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// This is the key generation module for the BBS+ signature scheme over the BN254 curve.
2-
/// Provides functions to generate secret keys, public keys, and key pairs for signing and verification.
1+
//! This is the key generation module for the BBS+ signature scheme over the BN254 curve.
2+
//! Provides functions to generate secret keys, public keys, and key pairs for signing and verification.
33
44
use ark_bn254::{G1Affine as G1, G2Affine as G2, Fr as Scalar};
55
use ark_ec::{AffineRepr, CurveGroup};

bbs/src/bbs_bn254/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
pub mod structs;
22
pub mod keygen;
3-
pub mod user;
3+
pub mod blind;
4+
pub mod signer;
45

56
pub use keygen::{keygen, keygen_with_rng};
67
pub use structs::{Parameters, PrivateKey, PublicKey};
7-
pub use user::{
8+
pub use blind::{
89
blind,
910
blind_with_rng,
1011
commitment_pok_prove,

bbs/src/bbs_bn254/signer.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Impl signing related functions.
2+
3+
use ark_bn254::{G1Affine as G1, G2Affine as G2, Fr as Scalar};
4+
use ark_ec::CurveGroup;
5+
use ark_ff::PrimeField;
6+
use ark_std::{UniformRand, rand::RngCore, test_rng};
7+
8+
use crate::bbs_bn254::{Parameters, PrivateKey, structs::Signature};
9+
10+
pub fn sign_no_blind(
11+
params: &Parameters,
12+
sk: &PrivateKey,
13+
messages: &[Scalar]
14+
) -> Result<Signature, &'static str> {
15+
let mut rng = test_rng();
16+
sign_no_blind_with_rng(params, sk, messages, rng)
17+
}
18+
19+
pub fn sign_no_blind_with_rng<R: RngCore>(
20+
params: &Parameters,
21+
sk: &PrivateKey,
22+
messages: &[Scalar],
23+
rng: &mut R
24+
) -> Result<Signature, &'static str> {
25+
// Sample random scalars e and s
26+
let e = Scalar::rand(rng);
27+
let s = Scalar::rand(rng);
28+
29+
// Constrruct Commitment C = G_1 + s*H_0 + m_1*H_1 + ... + m_L*H_L
30+
let mut c = params.g1 + params.H[0] * s;
31+
for (j, m_j) in messages.iter().enumerate() {
32+
if j >= params.L {
33+
return Err("message length exceeds parameters");
34+
}
35+
c += params.H[j + 1] * m_j;
36+
}
37+
38+
// calc (x + e) ^ (-1)
39+
let mut tmp = sk.x + e;
40+
tmp = tmp.inverse().ok_or("failed to compute inverse")?;
41+
42+
Ok(())
43+
}

bbs/src/bbs_bn254/structs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Define the core data structures here.
2+
13
#![allow(non_snake_case, dead_code)]
24

35
use ark_bn254::{G1Affine as G1, G2Affine as G2, Fr as Scalar};

bbs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
//! BBS signature scheme implementation in Rust.
2+
//! Using bn254 curve.
13
pub mod bbs_bn254;

bbs/src/main.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)