wasm_utils/note/
vanchor.rs

1use ark_bn254::Fr as Bn254Fr;
2use ark_ff::{BigInteger, PrimeField};
3use ark_std::UniformRand;
4use arkworks_setups::{Curve as ArkCurve, VAnchorProver};
5use rand::rngs::OsRng;
6
7use crate::types::{Curve, OpStatusCode, OperationError};
8use crate::utxo::JsUtxo;
9use crate::VAnchorR1CSProverBn254_30_2_2_2;
10
11pub fn generate_secrets(
12	amount: u128,
13	exponentiation: i8,
14	width: usize,
15	curve: Curve,
16	chain_id: u64,
17	index: Option<u64>,
18	rng: &mut OsRng,
19) -> Result<JsUtxo, OperationError> {
20	let utxo: JsUtxo = match (curve, exponentiation, width) {
21		(Curve::Bn254, 5, 5) => {
22			VAnchorR1CSProverBn254_30_2_2_2::create_random_leaf(ArkCurve::Bn254, chain_id, amount, index, rng)
23				.map(JsUtxo::new_from_bn254_utxo)
24		}
25		_ => {
26			let message = format!(
27				"No VAnchor leaf setup for curve {}, exponentiation {}, and width {}",
28				curve, exponentiation, width
29			);
30			return Err(OperationError::new_with_message(OpStatusCode::SecretGenFailed, message));
31		}
32	}
33	.map_err(|e| OperationError::new_with_message(OpStatusCode::SecretGenFailed, e.to_string()))?;
34
35	Ok(utxo)
36}
37#[allow(clippy::too_many_arguments)]
38pub fn get_leaf_with_private_raw(
39	curve: Curve,
40	width: usize,
41	exponentiation: i8,
42	private_key: Option<Vec<u8>>,
43	blinding: Option<Vec<u8>>,
44	chain_id: u64,
45	amount: u128,
46	index: Option<u64>,
47) -> Result<JsUtxo, OperationError> {
48	let utxo: JsUtxo = match (curve, exponentiation, width) {
49		(Curve::Bn254, 5, 5) => {
50			let private_key = private_key.unwrap_or_else(|| Bn254Fr::rand(&mut OsRng).into_repr().to_bytes_le());
51			let blinding = blinding.unwrap_or_else(|| Bn254Fr::rand(&mut OsRng).into_repr().to_bytes_le());
52			VAnchorR1CSProverBn254_30_2_2_2::create_leaf_with_privates(
53				ArkCurve::Bn254,
54				chain_id,
55				amount,
56				index,
57				private_key.to_vec(),
58				blinding.to_vec(),
59			)
60			.map(JsUtxo::new_from_bn254_utxo)
61		}
62		_ => {
63			let message = format!(
64				"No VAnchor leaf setup for curve {}, exponentiation {}, and width {}",
65				curve, exponentiation, width
66			);
67			return Err(OperationError::new_with_message(
68				OpStatusCode::FailedToGenerateTheLeaf,
69				message,
70			));
71		}
72	}
73	.map_err(|e| OperationError::new_with_message(OpStatusCode::FailedToGenerateTheLeaf, e.to_string()))?;
74
75	Ok(utxo)
76}