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
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{Circuit, CircuitBody, CircuitMember, CircuitMemberBody, Identifier, Scope, Type, Variable, WeakType};

use indexmap::IndexMap;
use std::{
    cell::RefCell,
    sync::{Arc, Weak},
};

/// Stores program input values as asg nodes.
#[derive(Clone)]
pub struct Input {
    pub registers: Arc<CircuitBody>,
    pub state: Arc<CircuitBody>,
    pub state_leaf: Arc<CircuitBody>,
    pub record: Arc<CircuitBody>,
    pub container_circuit: Arc<CircuitBody>,
    pub container: Variable,
}

pub const CONTAINER_PSEUDO_CIRCUIT: &str = "$InputContainer";
pub const REGISTERS_PSEUDO_CIRCUIT: &str = "$InputRegister";
pub const RECORD_PSEUDO_CIRCUIT: &str = "$InputRecord";
pub const STATE_PSEUDO_CIRCUIT: &str = "$InputState";
pub const STATE_LEAF_PSEUDO_CIRCUIT: &str = "$InputStateLeaf";

impl Input {
    fn make_header(name: &str) -> Arc<Circuit> {
        Arc::new(Circuit {
            id: uuid::Uuid::new_v4(),
            name: RefCell::new(Identifier::new(name.to_string())),
            body: RefCell::new(Weak::new()),
            members: RefCell::new(IndexMap::new()),
            core_mapping: RefCell::new(None),
        })
    }

    fn make_body(scope: &Scope, circuit: &Arc<Circuit>) -> Arc<CircuitBody> {
        let body = Arc::new(CircuitBody {
            scope: scope.clone(),
            span: None,
            circuit: circuit.clone(),
            members: RefCell::new(IndexMap::new()),
        });
        circuit.body.replace(Arc::downgrade(&body));
        body
    }

    pub fn new(scope: &Scope) -> Self {
        let registers = Self::make_header(REGISTERS_PSEUDO_CIRCUIT);
        let record = Self::make_header(RECORD_PSEUDO_CIRCUIT);
        let state = Self::make_header(STATE_PSEUDO_CIRCUIT);
        let state_leaf = Self::make_header(STATE_LEAF_PSEUDO_CIRCUIT);

        let mut container_members = IndexMap::new();
        container_members.insert(
            "registers".to_string(),
            CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&registers))),
        );
        container_members.insert(
            "record".to_string(),
            CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&record))),
        );
        container_members.insert(
            "state".to_string(),
            CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state))),
        );
        container_members.insert(
            "state_leaf".to_string(),
            CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state_leaf))),
        );

        let container_circuit = Arc::new(Circuit {
            id: uuid::Uuid::new_v4(),
            name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.to_string())),
            body: RefCell::new(Weak::new()),
            members: RefCell::new(container_members),
            core_mapping: RefCell::new(None),
        });

        let registers_body = Self::make_body(scope, &registers);
        let record_body = Self::make_body(scope, &record);
        let state_body = Self::make_body(scope, &state);
        let state_leaf_body = Self::make_body(scope, &state_leaf);

        let mut container_body_members = IndexMap::new();
        container_body_members.insert(
            "registers".to_string(),
            CircuitMemberBody::Variable(Type::Circuit(registers)),
        );
        container_body_members.insert("record".to_string(), CircuitMemberBody::Variable(Type::Circuit(record)));
        container_body_members.insert("state".to_string(), CircuitMemberBody::Variable(Type::Circuit(state)));
        container_body_members.insert(
            "state_leaf".to_string(),
            CircuitMemberBody::Variable(Type::Circuit(state_leaf)),
        );

        let container_circuit_body = Arc::new(CircuitBody {
            scope: scope.clone(),
            span: None,
            circuit: container_circuit.clone(),
            members: RefCell::new(container_body_members),
        });
        container_circuit.body.replace(Arc::downgrade(&container_circuit_body));

        Input {
            registers: registers_body,
            record: record_body,
            state: state_body,
            state_leaf: state_leaf_body,
            container_circuit: container_circuit_body,
            container: Arc::new(RefCell::new(crate::InnerVariable {
                id: uuid::Uuid::new_v4(),
                name: Identifier::new("input".to_string()),
                type_: Type::Circuit(container_circuit),
                mutable: false,
                declaration: crate::VariableDeclaration::Input,
                references: vec![],
                assignments: vec![],
            })),
        }
    }
}

impl Circuit {
    pub fn is_input_pseudo_circuit(&self) -> bool {
        matches!(
            &*self.name.borrow().name,
            REGISTERS_PSEUDO_CIRCUIT | RECORD_PSEUDO_CIRCUIT | STATE_PSEUDO_CIRCUIT | STATE_LEAF_PSEUDO_CIRCUIT
        )
    }
}