1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::MessageGenerators;
use bls12_381_plus::{multi_miller_loop, G1Affine, G1Projective, G2Affine, G2Prepared, Scalar};
use core::convert::TryFrom;
use core::ops::Neg;
use digest::Update;
use group::{Curve, Group, GroupEncoding};
use serde::{Deserialize, Serialize};
use signature_bls::PublicKey;
use signature_core::{constants::*, lib::*};
use subtle::{Choice, CtOption};

/// The actual proof that is sent from prover to verifier.
///
/// Contains the proof of 2 discrete log relations.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PokSignatureProof {
    // TODO lower viz again after serde hacking
    pub a_prime: G1Projective,
    pub a_bar: G1Projective,
    pub d: G1Projective,
    pub proofs1: [Challenge; 2],
    #[serde(with = "VecSerializer")]
    pub proofs2: Vec<Challenge, 130>,
}

impl PokSignatureProof {
    /// Store the proof as a sequence of bytes
    /// Each point is compressed to big-endian format
    /// Needs (N + 2) * 32 + 48 * 3 space otherwise it will panic
    /// where N is the number of hidden messages
    pub fn to_bytes(&self, buffer: &mut [u8]) {
        buffer[0..COMMITMENT_BYTES].copy_from_slice(&self.a_prime.to_affine().to_compressed());
        let mut offset = COMMITMENT_BYTES;
        let mut end = offset + COMMITMENT_BYTES;
        buffer[offset..end].copy_from_slice(&self.a_bar.to_affine().to_compressed());
        offset = end;
        end = offset + COMMITMENT_BYTES;
        buffer[offset..end].copy_from_slice(&self.d.to_affine().to_compressed());
        offset = end;
        end = offset + FIELD_BYTES;

        for i in 0..self.proofs1.len() {
            buffer[offset..end].copy_from_slice(&self.proofs1[i].to_bytes());
            offset = end;
            end += FIELD_BYTES;
        }
        for i in 0..self.proofs2.len() {
            buffer[offset..end].copy_from_slice(&self.proofs2[i].to_bytes());
            offset = end;
            end += FIELD_BYTES;
        }
    }

    /// Convert a byte sequence into the blind signature context
    /// Expected size is (N + 1) * 32 + 48 bytes
    pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Option<Self> {
        let size = FIELD_BYTES * 4 + COMMITMENT_BYTES * 3;
        let buffer = bytes.as_ref();
        if buffer.len() < size {
            return None;
        }
        if buffer.len() - COMMITMENT_BYTES % FIELD_BYTES != 0 {
            return None;
        }

        let hid_msg_cnt = (buffer.len() - size) / FIELD_BYTES;
        let mut offset = COMMITMENT_BYTES;
        let mut end = COMMITMENT_BYTES + FIELD_BYTES;
        let a_prime = G1Affine::from_compressed(slicer!(buffer, 0, offset, COMMITMENT_BYTES))
            .map(G1Projective::from);
        let a_bar = G1Affine::from_compressed(slicer!(buffer, offset, end, COMMITMENT_BYTES))
            .map(G1Projective::from);
        offset = end;
        end = offset + COMMITMENT_BYTES;
        let d = G1Affine::from_compressed(slicer!(buffer, offset, end, COMMITMENT_BYTES))
            .map(G1Projective::from);

        if a_prime.is_none().unwrap_u8() == 1
            || a_bar.is_none().unwrap_u8() == 1
            || d.is_none().unwrap_u8() == 1
        {
            return None;
        }

        offset = end;
        end = offset + FIELD_BYTES;

        let mut proofs1 = [
            CtOption::new(Challenge::default(), Choice::from(0u8)),
            CtOption::new(Challenge::default(), Choice::from(0u8)),
        ];

        #[allow(clippy::needless_range_loop)]
        for i in 0..proofs1.len() {
            proofs1[i] = Challenge::from_bytes(slicer!(buffer, offset, end, FIELD_BYTES));
            offset = end;
            end = offset + FIELD_BYTES;
        }
        if proofs1[0].is_none().unwrap_u8() == 1 || proofs1[1].is_none().unwrap_u8() == 1 {
            return None;
        }

        let mut proofs2 = Vec::<Challenge, 130>::new();
        for _ in 0..(hid_msg_cnt + 2) {
            let c = Challenge::from_bytes(slicer!(buffer, offset, end, FIELD_BYTES));
            offset = end;
            end = offset + FIELD_BYTES;
            if c.is_none().unwrap_u8() == 1 {
                return None;
            }

            proofs2.push(c.unwrap()).expect(ALLOC_MSG);
        }
        Some(Self {
            a_prime: a_prime.unwrap(),
            a_bar: a_bar.unwrap(),
            d: d.unwrap(),
            proofs1: [proofs1[0].unwrap(), proofs1[1].unwrap()],
            proofs2,
        })
    }

    /// Convert the committed values to bytes for the fiat-shamir challenge
    pub fn add_challenge_contribution(
        &self,
        generators: &MessageGenerators,
        rvl_msgs: &[(usize, Message)],
        challenge: Challenge,
        hasher: &mut impl Update,
    ) {
        hasher.update(self.a_prime.to_affine().to_uncompressed());
        hasher.update(self.a_bar.to_affine().to_uncompressed());
        hasher.update(self.d.to_affine().to_uncompressed());

        let proof1_points = [self.a_bar - self.d, self.a_prime, generators.h0];
        let mut proof1_scalars = [challenge.0, self.proofs1[0].0, self.proofs1[1].0];
        let commitment_proofs1 =
            G1Projective::sum_of_products_in_place(&proof1_points, &mut proof1_scalars);
        hasher.update(commitment_proofs1.to_affine().to_bytes());

        let mut r_points = Vec::<G1Projective, 130>::new();
        let mut r_scalars = Vec::<Scalar, 130>::new();

        r_points.push(G1Projective::generator()).expect(ALLOC_MSG);
        r_scalars.push(Scalar::one()).expect(ALLOC_MSG);

        let mut hidden = HashSet::new();
        for (idx, msg) in rvl_msgs {
            hidden.insert(*idx);
            r_points.push(generators.get(*idx)).expect(ALLOC_MSG);
            r_scalars.push(msg.0).expect(ALLOC_MSG);
        }

        let r = G1Projective::sum_of_products_in_place(r_points.as_ref(), r_scalars.as_mut());

        let mut proof2_points = Vec::<G1Projective, 130>::new();
        let mut proof2_scalars = Vec::<Scalar, 130>::new();

        // r^c
        proof2_points.push(r).expect(ALLOC_MSG);
        proof2_scalars.push(challenge.0).expect(ALLOC_MSG);

        // d^-r3_hat
        proof2_points.push(self.d.neg()).expect(ALLOC_MSG);
        proof2_scalars.push(self.proofs2[0].0).expect(ALLOC_MSG);

        // h0^s_tick_hat
        proof2_points.push(generators.h0).expect(ALLOC_MSG);
        proof2_scalars.push(self.proofs2[1].0).expect(ALLOC_MSG);

        let mut j = 2;
        for i in 0..generators.len() {
            if hidden.contains(&i) {
                continue;
            }
            proof2_points.push(generators.get(i)).expect(ALLOC_MSG);
            proof2_scalars.push(self.proofs2[j].0).expect(ALLOC_MSG);
            j += 1;
        }
        let commitment_proofs2 =
            G1Projective::sum_of_products_in_place(proof2_points.as_ref(), proof2_scalars.as_mut());
        hasher.update(commitment_proofs2.to_affine().to_bytes());
    }

    /// Validate the proof, only checks the signature proof
    /// the selective disclosure proof is checked by verifying
    /// self.challenge == computed_challenge
    pub fn verify(&self, public_key: PublicKey) -> bool {
        // check the signature proof
        if self.a_prime.is_identity().unwrap_u8() == 1 {
            return false;
        }
        multi_miller_loop(&[
            (
                &self.a_prime.to_affine(),
                &G2Prepared::from(public_key.0.to_affine()),
            ),
            (
                &self.a_bar.to_affine(),
                &G2Prepared::from(G2Affine::generator().neg()),
            ),
        ])
        .final_exponentiation()
        .is_identity()
        .unwrap_u8()
            == 1
    }
}