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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! Sigma boolean types

use self::cand::Cand;
use self::cor::Cor;
use self::cthreshold::Cthreshold;

use crate::ergo_tree::{ErgoTree, ErgoTreeError};
use crate::has_opcode::{HasOpCode, HasStaticOpCode};
use crate::mir::constant::Constant;
use crate::mir::expr::Expr;
use crate::serialization::op_code::OpCode;
use crate::serialization::SigmaSerializable;
use ergo_chain_types::EcPoint;
use std::convert::TryFrom;
use std::convert::TryInto;

extern crate derive_more;
use bounded_vec::BoundedVec;
use derive_more::From;
use derive_more::Into;
use derive_more::TryInto;

pub mod cand;
pub mod cor;
pub mod cthreshold;

/// Sigma conjecture items type with bounds check (2..=255)
pub type SigmaConjectureItems<T> = BoundedVec<T, 2, 255>;

/// Construct a new SigmaBoolean value representing public key of discrete logarithm signature protocol.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ProveDlog {
    /// public key
    pub h: Box<EcPoint>,
}

impl ProveDlog {
    /// create new public key
    pub fn new(ecpoint: EcPoint) -> ProveDlog {
        ProveDlog {
            h: Box::new(ecpoint),
        }
    }
}

impl HasStaticOpCode for ProveDlog {
    const OP_CODE: OpCode = OpCode::PROVE_DLOG;
}

impl From<EcPoint> for ProveDlog {
    fn from(p: EcPoint) -> Self {
        ProveDlog::new(p)
    }
}

/// Construct a new SigmaProp value representing public key of Diffie Hellman signature protocol.
/// Used in a proof that of equality of discrete logarithms (i.e., a proof of a Diffie-Hellman tuple):
/// given group elements g, h, u, v, the proof convinces a verifier that the prover knows `w` such
/// that `u = g^w` and `v = h^w`, without revealing `w`
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ProveDhTuple {
    /// Generator g
    pub g: Box<EcPoint>,
    /// Point h
    pub h: Box<EcPoint>,
    /// Point `u = g^w`
    pub u: Box<EcPoint>,
    /// Point `v= h^w`
    pub v: Box<EcPoint>,
}

impl HasStaticOpCode for ProveDhTuple {
    const OP_CODE: OpCode = OpCode::PROVE_DIFFIE_HELLMAN_TUPLE;
}

impl ProveDhTuple {
    /// Create new instance
    pub fn new(g: EcPoint, h: EcPoint, u: EcPoint, v: EcPoint) -> Self {
        Self {
            g: g.into(),
            h: h.into(),
            u: u.into(),
            v: v.into(),
        }
    }
}

/// Sigma proposition
#[derive(PartialEq, Eq, Debug, Clone, From)]
pub enum SigmaProofOfKnowledgeTree {
    /// public key of Diffie Hellman signature protocol
    ProveDhTuple(ProveDhTuple),
    /// public key of discrete logarithm signature protocol
    ProveDlog(ProveDlog),
}

impl HasOpCode for SigmaProofOfKnowledgeTree {
    fn op_code(&self) -> OpCode {
        match self {
            SigmaProofOfKnowledgeTree::ProveDhTuple(dh) => dh.op_code(),
            SigmaProofOfKnowledgeTree::ProveDlog(dlog) => dlog.op_code(),
        }
    }
}

/// Conjunctions for sigma propositions
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum SigmaConjecture {
    /// AND
    Cand(Cand),
    /// OR
    Cor(Cor),
    /// THRESHOLD
    Cthreshold(Cthreshold),
}

impl HasOpCode for SigmaConjecture {
    fn op_code(&self) -> OpCode {
        match self {
            SigmaConjecture::Cand(cand) => cand.op_code(),
            SigmaConjecture::Cor(cor) => cor.op_code(),
            SigmaConjecture::Cthreshold(ct) => ct.op_code(),
        }
    }
}

/// Algebraic data type of sigma proposition expressions
/// Values of this type are used as values of SigmaProp type
#[derive(PartialEq, Eq, Debug, Clone, From, TryInto)]
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
    feature = "json",
    serde(
        try_from = "crate::chain::json::sigma_protocol::SigmaBooleanJson",
        into = "crate::chain::json::sigma_protocol::SigmaBooleanJson"
    )
)]
pub enum SigmaBoolean {
    /// Represents boolean values (true/false)
    TrivialProp(bool),
    /// Sigma proposition
    ProofOfKnowledge(SigmaProofOfKnowledgeTree),
    /// Conjunctions for sigma propositions
    SigmaConjecture(SigmaConjecture),
}

impl HasOpCode for SigmaBoolean {
    /// get OpCode for serialization
    fn op_code(&self) -> OpCode {
        match self {
            SigmaBoolean::ProofOfKnowledge(kt) => kt.op_code(),
            SigmaBoolean::SigmaConjecture(sc) => sc.op_code(),
            SigmaBoolean::TrivialProp(tp) => {
                if *tp {
                    OpCode::TRIVIAL_PROP_TRUE
                } else {
                    OpCode::TRIVIAL_PROP_FALSE
                }
            }
        }
    }
}

/// Failed to extract specified underlying type from SigmaBoolean
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ConversionError;

impl TryInto<ProveDlog> for SigmaBoolean {
    type Error = ConversionError;
    fn try_into(self) -> Result<ProveDlog, Self::Error> {
        match self {
            SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDlog(pd)) => Ok(pd),
            _ => Err(ConversionError),
        }
    }
}

impl From<ProveDlog> for SigmaBoolean {
    fn from(v: ProveDlog) -> Self {
        SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDlog(v))
    }
}

impl TryInto<ProveDhTuple> for SigmaBoolean {
    type Error = ConversionError;
    fn try_into(self) -> Result<ProveDhTuple, Self::Error> {
        match self {
            SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDhTuple(pdh)) => Ok(pdh),
            _ => Err(ConversionError),
        }
    }
}

impl From<ProveDhTuple> for SigmaBoolean {
    fn from(v: ProveDhTuple) -> Self {
        SigmaBoolean::ProofOfKnowledge(SigmaProofOfKnowledgeTree::ProveDhTuple(v))
    }
}

impl TryInto<Cand> for SigmaBoolean {
    type Error = ConversionError;
    fn try_into(self) -> Result<Cand, Self::Error> {
        match self {
            SigmaBoolean::SigmaConjecture(SigmaConjecture::Cand(c)) => Ok(c),
            _ => Err(ConversionError),
        }
    }
}

impl From<Cand> for SigmaBoolean {
    fn from(v: Cand) -> Self {
        SigmaBoolean::SigmaConjecture(SigmaConjecture::Cand(v))
    }
}

impl TryInto<Cor> for SigmaBoolean {
    type Error = ConversionError;
    fn try_into(self) -> Result<Cor, Self::Error> {
        match self {
            SigmaBoolean::SigmaConjecture(SigmaConjecture::Cor(c)) => Ok(c),
            _ => Err(ConversionError),
        }
    }
}

impl From<Cor> for SigmaBoolean {
    fn from(v: Cor) -> Self {
        SigmaBoolean::SigmaConjecture(SigmaConjecture::Cor(v))
    }
}

impl TryInto<Cthreshold> for SigmaBoolean {
    type Error = ConversionError;
    fn try_into(self) -> Result<Cthreshold, Self::Error> {
        match self {
            SigmaBoolean::SigmaConjecture(SigmaConjecture::Cthreshold(c)) => Ok(c),
            _ => Err(ConversionError),
        }
    }
}

impl From<Cthreshold> for SigmaBoolean {
    fn from(v: Cthreshold) -> Self {
        SigmaBoolean::SigmaConjecture(SigmaConjecture::Cthreshold(v))
    }
}

/// Proposition which can be proven and verified by sigma protocol.
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct SigmaProp(SigmaBoolean);

impl SigmaProp {
    /// create new sigma proposition from [`SigmaBoolean`] value
    pub fn new(sbool: SigmaBoolean) -> Self {
        SigmaProp(sbool)
    }

    /// get [`SigmaBoolean`] value
    pub fn value(&self) -> &SigmaBoolean {
        &self.0
    }

    /// Serialized bytes of a SigmaProp value
    pub fn prop_bytes(&self) -> Result<Vec<u8>, ErgoTreeError> {
        // in order to have comparisons like  `box.propositionBytes == pk.propBytes` we need to make sure
        // the same serialization method is used in both cases
        let c: Constant = self.clone().into();
        let e: Expr = c.into();
        let ergo_tree: ErgoTree = e.try_into()?;
        Ok(ergo_tree.sigma_serialize_bytes()?)
    }
}

impl TryFrom<SigmaProp> for bool {
    type Error = ConversionError;

    fn try_from(value: SigmaProp) -> Result<Self, Self::Error> {
        value.0.try_into().map_err(|_| ConversionError)
    }
}

impl From<ProveDlog> for SigmaProp {
    fn from(pd: ProveDlog) -> Self {
        SigmaProp(SigmaBoolean::ProofOfKnowledge(
            SigmaProofOfKnowledgeTree::ProveDlog(pd),
        ))
    }
}

impl From<ProveDhTuple> for SigmaProp {
    fn from(dh: ProveDhTuple) -> Self {
        SigmaProp(SigmaBoolean::ProofOfKnowledge(
            SigmaProofOfKnowledgeTree::ProveDhTuple(dh),
        ))
    }
}
/// Arbitrary impl for ProveDlog
#[cfg(feature = "arbitrary")]
#[allow(clippy::unwrap_used)]
mod arbitrary {
    use super::*;
    use proptest::collection::vec;
    use proptest::prelude::*;

    impl Arbitrary for ProveDlog {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
            (any::<EcPoint>()).prop_map(ProveDlog::new).boxed()
        }
    }

    impl Arbitrary for ProveDhTuple {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
            (
                any::<EcPoint>(),
                any::<EcPoint>(),
                any::<EcPoint>(),
                any::<EcPoint>(),
            )
                .prop_map(|(g, h, u, v)| ProveDhTuple::new(g, h, u, v))
                .boxed()
        }
    }

    pub fn primitive_type_value() -> BoxedStrategy<SigmaBoolean> {
        prop_oneof![
            any::<ProveDlog>().prop_map_into(),
            any::<ProveDhTuple>().prop_map_into(),
            // TODO: enable and fix sigma_conj eval tests
            // any::<bool>().prop_map_into(),
        ]
        .boxed()
    }

    impl Arbitrary for SigmaBoolean {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
            primitive_type_value()
                .prop_recursive(1, 8, 4, |elem| {
                    prop_oneof![
                        vec(elem.clone(), 2..=4)
                            .prop_map(|elems| Cand {
                                items: elems.try_into().unwrap()
                            })
                            .prop_map_into(),
                        vec(elem.clone(), 2..=4)
                            .prop_map(|elems| Cor {
                                items: elems.try_into().unwrap()
                            })
                            .prop_map_into(),
                        vec(elem, 2..=5)
                            .prop_map(|elems| Cthreshold {
                                k: (elems.len() - 1) as u8,
                                children: elems.try_into().unwrap()
                            })
                            .prop_map_into(),
                    ]
                })
                .boxed()
        }
    }

    impl Arbitrary for SigmaProp {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
            (any::<SigmaBoolean>()).prop_map(SigmaProp::new).boxed()
        }
    }
}

#[allow(clippy::panic)]
#[cfg(test)]
#[allow(clippy::panic)]
mod tests {
    use super::*;
    use crate::serialization::sigma_serialize_roundtrip;
    use proptest::prelude::*;

    proptest! {

        #[test]
        fn sigma_boolean_ser_roundtrip(
            v in any::<SigmaBoolean>()) {
                prop_assert_eq![sigma_serialize_roundtrip(&v), v]
        }
    }
}