use super::*;
impl<N: Network, A: circuit::Aleo<Network = N>> RegistersLoad<N> for Registers<N, A> {
#[inline]
fn load(&self, stack: &(impl StackMatches<N> + StackProgram<N>), operand: &Operand<N>) -> Result<Value<N>> {
let register = match operand {
Operand::Literal(literal) => return Ok(Value::Plaintext(Plaintext::from(literal))),
Operand::Register(register) => register,
Operand::ProgramID(program_id) => {
return Ok(Value::Plaintext(Plaintext::from(Literal::Address(program_id.to_address()?))));
}
Operand::Signer => return Ok(Value::Plaintext(Plaintext::from(Literal::Address(self.signer()?)))),
Operand::Caller => return Ok(Value::Plaintext(Plaintext::from(Literal::Address(self.caller()?)))),
Operand::BlockHeight => bail!("Cannot load the block height in a non-finalize context"),
Operand::NetworkID => bail!("Cannot load the network ID in a non-finalize context"),
};
let stack_value =
self.console_registers.get(®ister.locator()).ok_or_else(|| anyhow!("'{register}' does not exist"))?;
let stack_value = match register {
Register::Locator(..) => stack_value.clone(),
Register::Access(_, ref path) => {
match stack_value {
Value::Plaintext(plaintext) => Value::Plaintext(plaintext.find(path)?),
Value::Record(record) => match record.find(path)? {
Entry::Constant(plaintext) | Entry::Public(plaintext) | Entry::Private(plaintext) => {
Value::Plaintext(plaintext)
}
},
Value::Future(future) => future.find(path)?,
}
}
};
match self.register_types.get_type(stack, register) {
Ok(register_type) => stack.matches_register_type(&stack_value, ®ister_type)?,
Err(error) => bail!("Register '{register}' is not a member of the function: {error}"),
};
Ok(stack_value)
}
}
impl<N: Network, A: circuit::Aleo<Network = N>> RegistersLoadCircuit<N, A> for Registers<N, A> {
#[inline]
fn load_circuit(
&self,
stack: &(impl StackMatches<N> + StackProgram<N>),
operand: &Operand<N>,
) -> Result<circuit::Value<A>> {
use circuit::Inject;
let register = match operand {
Operand::Literal(literal) => {
return Ok(circuit::Value::Plaintext(circuit::Plaintext::from(circuit::Literal::constant(
literal.clone(),
))));
}
Operand::Register(register) => register,
Operand::ProgramID(program_id) => {
return Ok(circuit::Value::Plaintext(circuit::Plaintext::from(circuit::Literal::constant(
Literal::Address(program_id.to_address()?),
))));
}
Operand::Signer => {
return Ok(circuit::Value::Plaintext(circuit::Plaintext::from(circuit::Literal::Address(
self.signer_circuit()?,
))));
}
Operand::Caller => {
return Ok(circuit::Value::Plaintext(circuit::Plaintext::from(circuit::Literal::Address(
self.caller_circuit()?,
))));
}
Operand::BlockHeight => bail!("Cannot load the block height in a non-finalize context"),
Operand::NetworkID => bail!("Cannot load the network ID in a non-finalize context"),
};
let circuit_value =
self.circuit_registers.get(®ister.locator()).ok_or_else(|| anyhow!("'{register}' does not exist"))?;
let circuit_value = match register {
Register::Locator(..) => circuit_value.clone(),
Register::Access(_, ref path) => {
let path = path.iter().map(|access| circuit::Access::constant(*access)).collect::<Vec<_>>();
match circuit_value {
circuit::Value::Plaintext(plaintext) => circuit::Value::Plaintext(plaintext.find(&path)?),
circuit::Value::Record(record) => match record.find(&path)? {
circuit::Entry::Constant(plaintext)
| circuit::Entry::Public(plaintext)
| circuit::Entry::Private(plaintext) => circuit::Value::Plaintext(plaintext),
},
circuit::Value::Future(future) => future.find(&path)?,
}
}
};
match self.register_types.get_type(stack, register) {
Ok(register_type) => {
stack.matches_register_type(&circuit::Eject::eject_value(&circuit_value), ®ister_type)?
}
Err(error) => bail!("Register '{register}' is not a member of the function: {error}"),
};
Ok(circuit_value)
}
}