snarkvm_circuit_program/response/from_outputs.rs
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
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::*;
impl<A: Aleo> Response<A> {
/// Initializes a response, given the number of inputs, tvk, tcm, outputs, output types, and output registers.
pub fn from_outputs(
network_id: &U16<A>,
program_id: &ProgramID<A>,
function_name: &Identifier<A>,
num_inputs: usize,
tvk: &Field<A>,
tcm: &Field<A>,
outputs: Vec<Value<A>>,
output_types: &[console::ValueType<A::Network>], // Note: Console type
output_registers: &[Option<console::Register<A::Network>>], // Note: Console type
) -> Self {
// Compute the function ID.
let function_id = compute_function_id(network_id, program_id, function_name);
// Compute the output IDs.
let output_ids = outputs
.iter()
.zip_eq(output_types)
.zip_eq(output_registers)
.enumerate()
.map(|(index, ((output, output_type), output_register))| {
match output_type {
// For a constant output, compute the hash (using `tcm`) of the output.
console::ValueType::Constant(..) => {
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u16((num_inputs + index) as u16));
// Construct the preimage as `(function ID || output || tcm || index)`.
let mut preimage = Vec::new();
preimage.push(function_id.clone());
preimage.extend(output.to_fields());
preimage.push(tcm.clone());
preimage.push(output_index);
// Hash the output to a field element.
match &output {
// Return the output ID.
Value::Plaintext(..) => OutputID::constant(A::hash_psd8(&preimage)),
// Ensure the output is a plaintext.
Value::Record(..) => A::halt("Expected a plaintext output, found a record output"),
Value::Future(..) => A::halt("Expected a plaintext output, found a future output"),
}
}
// For a public output, compute the hash (using `tcm`) of the output.
console::ValueType::Public(..) => {
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u16((num_inputs + index) as u16));
// Construct the preimage as `(function ID || output || tcm || index)`.
let mut preimage = Vec::new();
preimage.push(function_id.clone());
preimage.extend(output.to_fields());
preimage.push(tcm.clone());
preimage.push(output_index);
// Hash the output to a field element.
match &output {
// Return the output ID.
Value::Plaintext(..) => OutputID::public(A::hash_psd8(&preimage)),
// Ensure the output is a plaintext.
Value::Record(..) => A::halt("Expected a plaintext output, found a record output"),
Value::Future(..) => A::halt("Expected a plaintext output, found a future output"),
}
}
// For a private output, compute the ciphertext (using `tvk`) and hash the ciphertext.
console::ValueType::Private(..) => {
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u16((num_inputs + index) as u16));
// Compute the output view key as `Hash(function ID || tvk || index)`.
let output_view_key = A::hash_psd4(&[function_id.clone(), tvk.clone(), output_index]);
// Compute the ciphertext.
let ciphertext = match &output {
Value::Plaintext(plaintext) => plaintext.encrypt_symmetric(output_view_key),
// Ensure the output is a plaintext.
Value::Record(..) => A::halt("Expected a plaintext output, found a record output"),
Value::Future(..) => A::halt("Expected a plaintext output, found a future output"),
};
// Return the output ID.
OutputID::private(A::hash_psd8(&ciphertext.to_fields()))
}
// For a record output, compute the record commitment, and encrypt the record (using `tvk`).
console::ValueType::Record(record_name) => {
// Retrieve the record.
let record = match &output {
Value::Record(record) => record,
// Ensure the output is a record.
Value::Plaintext(..) => A::halt("Expected a record output, found a plaintext output"),
Value::Future(..) => A::halt("Expected a record output, found a future output"),
};
// Retrieve the output register.
let output_register = match output_register {
Some(output_register) => output_register,
None => A::halt("Expected a register to be paired with a record output"),
};
// Compute the record commitment.
let commitment = record.to_commitment(program_id, &Identifier::constant(*record_name));
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u64(output_register.locator()));
// Compute the encryption randomizer as `HashToScalar(tvk || index)`.
let randomizer = A::hash_to_scalar_psd2(&[tvk.clone(), output_index]);
// Encrypt the record, using the randomizer.
let encrypted_record = record.encrypt(&randomizer);
// Compute the record checksum, as the hash of the encrypted record.
let checksum = A::hash_bhp1024(&encrypted_record.to_bits_le());
// Return the output ID.
OutputID::record(commitment, checksum)
}
// For an external record output, compute the hash (using `tvk`) of the output.
console::ValueType::ExternalRecord(..) => {
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u16((num_inputs + index) as u16));
// Construct the preimage as `(function ID || output || tvk || index)`.
let mut preimage = Vec::new();
preimage.push(function_id.clone());
preimage.extend(output.to_fields());
preimage.push(tvk.clone());
preimage.push(output_index);
// Return the output ID.
match &output {
Value::Record(..) => OutputID::external_record(A::hash_psd8(&preimage)),
// Ensure the output is a record.
Value::Plaintext(..) => A::halt("Expected a record output, found a plaintext output"),
Value::Future(..) => A::halt("Expected a record output, found a future output"),
}
}
// For a future output, compute the hash (using `tcm`) of the output.
console::ValueType::Future(..) => {
// Prepare the index as a constant field element.
let output_index = Field::constant(console::Field::from_u16((num_inputs + index) as u16));
// Construct the preimage as `(function ID || output || tcm || index)`.
let mut preimage = Vec::new();
preimage.push(function_id.clone());
preimage.extend(output.to_fields());
preimage.push(tcm.clone());
preimage.push(output_index);
// Hash the output to a field element.
match &output {
// Return the output ID.
Value::Future(..) => OutputID::future(A::hash_psd8(&preimage)),
// Ensure the output is a future.
Value::Plaintext(..) => A::halt("Expected a future output, found a plaintext output"),
Value::Record(..) => A::halt("Expected a future output, found a record output"),
}
}
}
})
.collect();
// Return the response.
Self { output_ids, outputs }
}
}
#[cfg(all(test, feature = "console"))]
mod tests {
use super::*;
use crate::Circuit;
use snarkvm_utilities::{TestRng, Uniform};
use anyhow::Result;
use snarkvm_circuit_types::U16;
pub(crate) const ITERATIONS: usize = 20;
fn check_from_outputs(
mode: Mode,
num_constants: u64,
num_public: u64,
num_private: u64,
num_constraints: u64,
) -> Result<()> {
use console::Network;
let rng = &mut TestRng::default();
for i in 0..ITERATIONS {
// Sample a `tvk`.
let tvk = console::Field::rand(rng);
// Compute the transition commitment as `Hash(tvk)`.
let tcm = <Circuit as Environment>::Network::hash_psd2(&[tvk])?;
// Compute the nonce.
let index = console::Field::from_u64(8);
let randomizer = <Circuit as Environment>::Network::hash_to_scalar_psd2(&[tvk, index]).unwrap();
let nonce = <Circuit as Environment>::Network::g_scalar_multiply(&randomizer);
// Construct the outputs.
let output_constant = console::Value::<<Circuit as Environment>::Network>::Plaintext(
console::Plaintext::from_str("{ token_amount: 9876543210u128 }").unwrap(),
);
let output_public = console::Value::<<Circuit as Environment>::Network>::Plaintext(
console::Plaintext::from_str("{ token_amount: 9876543210u128 }").unwrap(),
);
let output_private = console::Value::<<Circuit as Environment>::Network>::Plaintext(
console::Plaintext::from_str("{ token_amount: 9876543210u128 }").unwrap(),
);
let output_record = console::Value::<<Circuit as Environment>::Network>::Record(console::Record::from_str(&format!("{{ owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah.private, token_amount: 100u64.private, _nonce: {nonce}.public }}")).unwrap());
let output_external_record = console::Value::<<Circuit as Environment>::Network>::Record(console::Record::from_str("{ owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah.private, token_amount: 100u64.private, _nonce: 0group.public }").unwrap());
let outputs = vec![output_constant, output_public, output_private, output_record, output_external_record];
// Construct the output types.
let output_types = vec![
console::ValueType::from_str("amount.constant").unwrap(),
console::ValueType::from_str("amount.public").unwrap(),
console::ValueType::from_str("amount.private").unwrap(),
console::ValueType::from_str("token.record").unwrap(),
console::ValueType::from_str("token.aleo/token.record").unwrap(),
];
// Construct the output registers.
let output_registers = vec![
Some(console::Register::Locator(5)),
Some(console::Register::Locator(6)),
Some(console::Register::Locator(7)),
Some(console::Register::Locator(8)),
Some(console::Register::Locator(9)),
];
// Construct a network ID.
let network_id = console::U16::new(<Circuit as Environment>::Network::ID);
// Construct a program ID.
let program_id = console::ProgramID::from_str("test.aleo")?;
// Construct a function name.
let function_name = console::Identifier::from_str("check")?;
// Construct the response.
let response = console::Response::new(
&network_id,
&program_id,
&function_name,
4,
&tvk,
&tcm,
outputs.clone(),
&output_types,
&output_registers,
)?;
// Inject the network ID, program ID, function name, `tvk`, `tcm`, and outputs.
let network_id = U16::<Circuit>::constant(network_id);
let program_id = ProgramID::<Circuit>::new(mode, program_id);
let function_name = Identifier::<Circuit>::new(mode, function_name);
let tvk = Field::<Circuit>::new(mode, tvk);
let tcm = Field::<Circuit>::new(mode, tcm);
let outputs = Inject::new(mode, outputs);
Circuit::scope(format!("Response {i}"), || {
// Compute the response using outputs (circuit).
let candidate = Response::from_outputs(
&network_id,
&program_id,
&function_name,
4,
&tvk,
&tcm,
outputs,
&output_types,
&output_registers,
);
assert_eq!(response, candidate.eject_value());
match mode.is_constant() {
true => assert_scope!(<=num_constants, <=num_public, <=num_private, <=num_constraints),
false => assert_scope!(<=num_constants, num_public, num_private, num_constraints),
}
});
Circuit::reset();
}
Ok(())
}
// Note: These counts are correct. At this (high) level of a program, we override the default mode in many cases,
// based on the user-defined visibility in the types. Thus, we have nonzero public, private, and constraint values.
#[test]
fn test_from_outputs_constant() -> Result<()> {
check_from_outputs(Mode::Constant, 26000, 6, 9500, 9500)
}
#[test]
fn test_from_outputs_public() -> Result<()> {
check_from_outputs(Mode::Public, 24849, 6, 13962, 13983)
}
#[test]
fn test_from_outputs_private() -> Result<()> {
check_from_outputs(Mode::Private, 24849, 6, 13962, 13983)
}
}