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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// implementation of SCRAPE: Scalable Randomness Attested by Public Entities
// https://eprint.iacr.org/2017/216.pdf

use super::crypto::*;
use super::dleq;
use super::math;
use super::pdleq;
use super::types::*;

pub type Secret = Point;

// a new escrowing context.
// this contains secret values (polynomial & secret) that are newly created.
// this also contains by-product (extra_generator & proof) which are useful for
// the protocol
pub struct Escrow {
    pub threshold: Threshold,
    pub extra_generator: Point,
    pub polynomial: math::Polynomial,
    pub secret: Secret,
    pub proof: dleq::Proof,
}

// Public values for a successful run of secret sharing.
//
// This contains everything for self verification and
// and the shares of each participants
//
// there should be N encrypted_shares and N commitments
// the parallel proofs should N elements too.
pub struct PublicShares {
    pub threshold: Threshold,
    pub extra_generator: Point,
    pub secret_proof: dleq::Proof,
    pub encrypted_shares: Vec<EncryptedShare>,
    pub commitments: Vec<Commitment>,
    pub proofs: pdleq::Proof,
}

pub struct Commitment {
    point: Point,
}

pub struct EncryptedShare {
    pub id: ShareId,
    encrypted_val: Point,
}

pub struct DecryptedShare {
    pub id: ShareId,
    decrypted_val: Point,
    proof: dleq::Proof,
}

// create a new escrow parameter.
// the only parameter needed is the threshold necessary to be able to reconstruct.
pub fn escrow(t: Threshold) -> Escrow {
    assert!(t >= 1, "threshold is invalid; < 1");

    let poly = math::Polynomial::generate(t - 1);
    let gen = Point::from_scalar(&Scalar::generate());

    let secret = poly.at_zero();
    let g_s = Point::from_scalar(&secret);

    let challenge = Scalar::generate();
    let dleq = dleq::DLEQ {
        g1: Point::generator(),
        h1: g_s.clone(),
        g2: gen.clone(),
        h2: gen.mul(&secret),
    };
    let proof = dleq::Proof::create(challenge, secret, dleq);

    Escrow {
        threshold: t,
        extra_generator: gen,
        polynomial: poly,
        secret: g_s,
        proof,
    }
}

pub fn create_shares(escrow: &Escrow, pubs: &[PublicKey]) -> PublicShares {
    let n = pubs.len();
    let mut shares = Vec::with_capacity(n);
    let mut commitments = Vec::with_capacity(n);
    let mut pparams = Vec::with_capacity(n);

    for (i, public) in pubs.iter().enumerate() {
        //let ref public = pubs[i];
        let eval_point = i + 1;
        let si = escrow
            .polynomial
            .evaluate(Scalar::from_u32(eval_point as u32));
        let esi = public.point.mul(&si);
        let vi = escrow.extra_generator.mul(&si);

        shares.push(EncryptedShare {
            id: eval_point as ShareId,
            encrypted_val: esi.clone(),
        });
        commitments.push(Commitment { point: vi.clone() });

        {
            let w = Scalar::generate();
            let dleq = dleq::DLEQ {
                g1: escrow.extra_generator.clone(),
                h1: vi,
                g2: public.point.clone(),
                h2: esi,
            };
            pparams.push((w, si, dleq));
        }
    }

    // now create the parallel proof for all shares
    let pdleq = pdleq::Proof::create(pparams.as_slice());

    PublicShares {
        threshold: escrow.threshold,
        extra_generator: escrow.extra_generator.clone(),
        secret_proof: escrow.proof.clone(),
        encrypted_shares: shares,
        commitments,
        proofs: pdleq,
    }
}

impl PublicShares {
    pub fn number_participants(&self) -> u32 {
        self.commitments.len() as u32
    }

    pub fn verify(&self, publics: &[PublicKey]) -> bool {
        // recreate all the DLEQs
        let mut dleqs = Vec::with_capacity(publics.len());
        for (i, public) in publics.iter().enumerate() {
            let vi = &self.commitments[i].point;
            let esi = &self.encrypted_shares[i].encrypted_val;
            let dleq = dleq::DLEQ {
                g1: self.extra_generator.clone(),
                h1: vi.clone(),
                g2: public.point.clone(),
                h2: esi.clone(),
            };
            dleqs.push(dleq);
        }
        // verify the parallel proof
        if !self.proofs.verify(dleqs.as_slice()) {
            return false;
        }

        // reed solomon check
        let n = self.number_participants();
        let poly = math::Polynomial::generate(n - self.threshold - 1);

        let mut v = Point::infinity();
        for i in 0..n {
            let idx = i as usize;

            let mut cperp = poly.evaluate(Scalar::from_u32(i));
            for j in 0..n {
                if i != j {
                    cperp = cperp * (Scalar::from_u32(i) - Scalar::from_u32(j)).inverse();
                }
            }

            let commitment = &self.commitments[idx];
            v = v + commitment.point.mul(&cperp);
        }

        v == Point::infinity()
    }
}

impl DecryptedShare {
    pub fn verify(&self, public: &PublicKey, eshare: &EncryptedShare) -> bool {
        let dleq = dleq::DLEQ {
            g1: Point::generator(),
            h1: public.point.clone(),
            g2: self.decrypted_val.clone(),
            h2: eshare.encrypted_val.clone(),
        };
        self.proof.verify(dleq)
    }
}

pub fn decrypt_share(
    private: &PrivateKey,
    public: &PublicKey,
    share: &EncryptedShare,
) -> DecryptedShare {
    let challenge = Scalar::generate();
    let xi = private.scalar.clone();
    let yi = public.point.clone();
    let lifted_yi = share.encrypted_val.clone();
    let si = lifted_yi.mul(&xi.inverse());
    let dleq = dleq::DLEQ {
        g1: Point::generator(),
        h1: yi,
        g2: si.clone(),
        h2: lifted_yi,
    };
    let proof = dleq::Proof::create(challenge, xi, dleq);
    DecryptedShare {
        id: share.id,
        decrypted_val: si,
        proof,
    }
}

fn interpolate_one(t: Threshold, sid: usize, shares: &[DecryptedShare]) -> Scalar {
    let mut v = Scalar::multiplicative_identity();
    for j in 0..(t as usize) {
        if j != sid {
            let sj = Scalar::from_u32(shares[j].id);
            let si = Scalar::from_u32(shares[sid].id);
            let d = sj.clone() - si;
            v = v * sj * d.inverse();
        }
    }
    v
}

// Try to recover a secret
pub fn recover(t: Threshold, shares: &[DecryptedShare]) -> Result<Secret, ()> {
    if t as usize > shares.len() {
        return Err(());
    };
    let mut result = Point::infinity();
    for i in 0..(t as usize) {
        let v = interpolate_one(t, i, shares);
        result = result + shares[i].decrypted_val.mul(&v);
    }
    Ok(result)
}

pub fn verify_secret(secret: Secret, public_shares: &PublicShares) -> bool {
    let mut commitment_interpolate = Point::infinity();
    for i in 0..(public_shares.threshold as usize) {
        let x = public_shares.commitments[i].point.clone();
        let li = {
            let mut v = Scalar::multiplicative_identity();
            for j in 0..(public_shares.threshold as usize) {
                if j != i {
                    let sj = Scalar::from_u32((j + 1) as u32);
                    let si = Scalar::from_u32((i + 1) as u32);
                    let d = sj.clone() - si;
                    v = v * sj * d.inverse();
                }
            }
            v
        };
        commitment_interpolate = commitment_interpolate + x.mul(&li);
    }
    let dleq = dleq::DLEQ {
        g1: Point::generator(),
        h1: secret,
        g2: public_shares.extra_generator.clone(),
        h2: commitment_interpolate,
    };
    public_shares.secret_proof.verify(dleq)
}