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
use std::fmt;
use bellperson::{ConstraintSystem, SynthesisError};
use fil_sapling_crypto::circuit::num::AllocatedNum;
use paired::Engine;
#[derive(Clone)]
pub enum Root<E: Engine> {
Var(AllocatedNum<E>),
Val(Option<E::Fr>),
}
impl<E: Engine> fmt::Debug for Root<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Root::Var(num) => write!(f, "Root::Var({:?})", num.get_value()),
Root::Val(val) => write!(f, "Root::Val({:?})", val),
}
}
}
impl<E: Engine> Root<E> {
pub fn allocated<CS: ConstraintSystem<E>>(
&self,
cs: CS,
) -> Result<AllocatedNum<E>, SynthesisError> {
match self {
Root::Var(allocated) => Ok(allocated.clone()),
Root::Val(fr) => {
AllocatedNum::alloc(cs, || fr.ok_or_else(|| SynthesisError::AssignmentMissing))
}
}
}
pub fn var<CS: ConstraintSystem<E>>(cs: CS, fr: E::Fr) -> Self {
Root::Var(AllocatedNum::alloc(cs, || Ok(fr)).unwrap())
}
pub fn is_some(&self) -> bool {
match self {
Root::Var(_) => true,
Root::Val(Some(_)) => true,
Root::Val(None) => false,
}
}
}