mod.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 pub mod genesis; 17 pub use genesis::*; 18 19 /// The restrictions list as a JSON-compatible string. 20 pub const RESTRICTIONS_LIST: &str = include_str!("./resources/restrictions.json"); 21 22 const REMOTE_URL: &str = "https://parameters.provable.com/testnet"; 23 24 // BondPublic 25 impl_remote!(BondPublicProver, REMOTE_URL, "resources/", "bond_public", "prover", "credits"); 26 impl_local!(BondPublicVerifier, "resources/", "bond_public", "verifier", "credits"); 27 // BondValidator 28 impl_remote!(BondValidatorProver, REMOTE_URL, "resources/", "bond_validator", "prover", "credits"); 29 impl_local!(BondValidatorVerifier, "resources/", "bond_validator", "verifier", "credits"); 30 // UnbondPublic 31 impl_remote!(UnbondPublicProver, REMOTE_URL, "resources/", "unbond_public", "prover", "credits"); 32 impl_local!(UnbondPublicVerifier, "resources/", "unbond_public", "verifier", "credits"); 33 // ClaimUnbondPublic 34 impl_remote!(ClaimUnbondPublicProver, REMOTE_URL, "resources/", "claim_unbond_public", "prover", "credits"); 35 impl_local!(ClaimUnbondPublicVerifier, "resources/", "claim_unbond_public", "verifier", "credits"); 36 // SetValidatorState 37 impl_remote!(SetValidatorStateProver, REMOTE_URL, "resources/", "set_validator_state", "prover", "credits"); 38 impl_local!(SetValidatorStateVerifier, "resources/", "set_validator_state", "verifier", "credits"); 39 // TransferPrivate 40 impl_remote!(TransferPrivateProver, REMOTE_URL, "resources/", "transfer_private", "prover", "credits"); 41 impl_local!(TransferPrivateVerifier, "resources/", "transfer_private", "verifier", "credits"); 42 // TransferPublic 43 impl_remote!(TransferPublicProver, REMOTE_URL, "resources/", "transfer_public", "prover", "credits"); 44 impl_local!(TransferPublicVerifier, "resources/", "transfer_public", "verifier", "credits"); 45 // TransferPublicAsSigner 46 impl_remote!(TransferPublicAsSignerProver, REMOTE_URL, "resources/", "transfer_public_as_signer", "prover", "credits"); 47 impl_local!(TransferPublicAsSignerVerifier, "resources/", "transfer_public_as_signer", "verifier", "credits"); 48 // TransferPrivateToPublic 49 impl_remote!(TransferPrivateToPublicProver, REMOTE_URL, "resources/", "transfer_private_to_public", "prover", "credits"); 50 impl_local!(TransferPrivateToPublicVerifier, "resources/", "transfer_private_to_public", "verifier", "credits"); 51 // TransferPublicToPrivate 52 impl_remote!(TransferPublicToPrivateProver, REMOTE_URL, "resources/", "transfer_public_to_private", "prover", "credits"); 53 impl_local!(TransferPublicToPrivateVerifier, "resources/", "transfer_public_to_private", "verifier", "credits"); 54 // Join 55 impl_remote!(JoinProver, REMOTE_URL, "resources/", "join", "prover", "credits"); 56 impl_local!(JoinVerifier, "resources/", "join", "verifier", "credits"); 57 // Split 58 impl_remote!(SplitProver, REMOTE_URL, "resources/", "split", "prover", "credits"); 59 impl_local!(SplitVerifier, "resources/", "split", "verifier", "credits"); 60 // FeePrivate 61 impl_remote!(FeePrivateProver, REMOTE_URL, "resources/", "fee_private", "prover", "credits"); 62 impl_local!(FeePrivateVerifier, "resources/", "fee_private", "verifier", "credits"); 63 // FeePublic 64 impl_remote!(FeePublicProver, REMOTE_URL, "resources/", "fee_public", "prover", "credits"); 65 impl_local!(FeePublicVerifier, "resources/", "fee_public", "verifier", "credits"); 66 // Upgrade 67 impl_remote!(UpgradeProver, REMOTE_URL, "resources/", "upgrade", "prover", "credits"); 68 impl_local!(UpgradeVerifier, "resources/", "upgrade", "verifier", "credits"); 69 70 // V0 Credits Keys 71 72 // BondPublic 73 impl_remote!(BondPublicV0Prover, REMOTE_URL, "resources/", "bond_public", "prover", "credits_v0"); 74 impl_local!(BondPublicV0Verifier, "resources/", "bond_public", "verifier", "credits_v0"); 75 // BondValidator 76 impl_remote!(BondValidatorV0Prover, REMOTE_URL, "resources/", "bond_validator", "prover", "credits_v0"); 77 impl_local!(BondValidatorV0Verifier, "resources/", "bond_validator", "verifier", "credits_v0"); 78 // UnbondPublic 79 impl_remote!(UnbondPublicV0Prover, REMOTE_URL, "resources/", "unbond_public", "prover", "credits_v0"); 80 impl_local!(UnbondPublicV0Verifier, "resources/", "unbond_public", "verifier", "credits_v0"); 81 // ClaimUnbondPublic 82 impl_remote!(ClaimUnbondPublicV0Prover, REMOTE_URL, "resources/", "claim_unbond_public", "prover", "credits_v0"); 83 impl_local!(ClaimUnbondPublicV0Verifier, "resources/", "claim_unbond_public", "verifier", "credits_v0"); 84 // SetValidatorState 85 impl_remote!(SetValidatorStateV0Prover, REMOTE_URL, "resources/", "set_validator_state", "prover", "credits_v0"); 86 impl_local!(SetValidatorStateV0Verifier, "resources/", "set_validator_state", "verifier", "credits_v0"); 87 // TransferPrivate 88 impl_remote!(TransferPrivateV0Prover, REMOTE_URL, "resources/", "transfer_private", "prover", "credits_v0"); 89 impl_local!(TransferPrivateV0Verifier, "resources/", "transfer_private", "verifier", "credits_v0"); 90 // TransferPublic 91 impl_remote!(TransferPublicV0Prover, REMOTE_URL, "resources/", "transfer_public", "prover", "credits_v0"); 92 impl_local!(TransferPublicV0Verifier, "resources/", "transfer_public", "verifier", "credits_v0"); 93 // TransferPublicAsSigner 94 impl_remote!(TransferPublicAsSignerV0Prover, REMOTE_URL, "resources/", "transfer_public_as_signer", "prover", "credits_v0"); 95 impl_local!(TransferPublicAsSignerV0Verifier, "resources/", "transfer_public_as_signer", "verifier", "credits_v0"); 96 // TransferPrivateToPublic 97 impl_remote!(TransferPrivateToPublicV0Prover, REMOTE_URL, "resources/", "transfer_private_to_public", "prover", "credits_v0"); 98 impl_local!(TransferPrivateToPublicV0Verifier, "resources/", "transfer_private_to_public", "verifier", "credits_v0"); 99 // TransferPublicToPrivate 100 impl_remote!(TransferPublicToPrivateV0Prover, REMOTE_URL, "resources/", "transfer_public_to_private", "prover", "credits_v0"); 101 impl_local!(TransferPublicToPrivateV0Verifier, "resources/", "transfer_public_to_private", "verifier", "credits_v0"); 102 // Join 103 impl_remote!(JoinV0Prover, REMOTE_URL, "resources/", "join", "prover", "credits_v0"); 104 impl_local!(JoinV0Verifier, "resources/", "join", "verifier", "credits_v0"); 105 // Split 106 impl_remote!(SplitV0Prover, REMOTE_URL, "resources/", "split", "prover", "credits_v0"); 107 impl_local!(SplitV0Verifier, "resources/", "split", "verifier", "credits_v0"); 108 // FeePrivate 109 impl_remote!(FeePrivateV0Prover, REMOTE_URL, "resources/", "fee_private", "prover", "credits_v0"); 110 impl_local!(FeePrivateV0Verifier, "resources/", "fee_private", "verifier", "credits_v0"); 111 // FeePublic 112 impl_remote!(FeePublicV0Prover, REMOTE_URL, "resources/", "fee_public", "prover", "credits_v0"); 113 impl_local!(FeePublicV0Verifier, "resources/", "fee_public", "verifier", "credits_v0"); 114 // Upgrade 115 impl_remote!(UpgradeV0Prover, REMOTE_URL, "resources/", "upgrade", "prover", "credits_v0"); 116 impl_local!(UpgradeV0Verifier, "resources/", "upgrade", "verifier", "credits_v0"); 117 118 #[macro_export] 119 macro_rules! insert_testnet_credit_keys { 120 ($map:ident, $type:ident<$network:ident>, $variant:ident) => {{ 121 paste::paste! { 122 let string = stringify!([<$variant:lower>]); 123 $crate::insert_testnet_key!($map, string, $type<$network>, ("bond_public", $crate::testnet::[<BondPublic $variant>]::load_bytes())); 124 $crate::insert_testnet_key!($map, string, $type<$network>, ("bond_validator", $crate::testnet::[<BondValidator $variant>]::load_bytes())); 125 $crate::insert_testnet_key!($map, string, $type<$network>, ("unbond_public", $crate::testnet::[<UnbondPublic $variant>]::load_bytes())); 126 $crate::insert_testnet_key!($map, string, $type<$network>, ("claim_unbond_public", $crate::testnet::[<ClaimUnbondPublic $variant>]::load_bytes())); 127 $crate::insert_testnet_key!($map, string, $type<$network>, ("set_validator_state", $crate::testnet::[<SetValidatorState $variant>]::load_bytes())); 128 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_private", $crate::testnet::[<TransferPrivate $variant>]::load_bytes())); 129 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public", $crate::testnet::[<TransferPublic $variant>]::load_bytes())); 130 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public_as_signer", $crate::testnet::[<TransferPublicAsSigner $variant>]::load_bytes())); 131 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_private_to_public", $crate::testnet::[<TransferPrivateToPublic $variant>]::load_bytes())); 132 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public_to_private", $crate::testnet::[<TransferPublicToPrivate $variant>]::load_bytes())); 133 $crate::insert_testnet_key!($map, string, $type<$network>, ("join", $crate::testnet::[<Join $variant>]::load_bytes())); 134 $crate::insert_testnet_key!($map, string, $type<$network>, ("split", $crate::testnet::[<Split $variant>]::load_bytes())); 135 $crate::insert_testnet_key!($map, string, $type<$network>, ("fee_private", $crate::testnet::[<FeePrivate $variant>]::load_bytes())); 136 $crate::insert_testnet_key!($map, string, $type<$network>, ("fee_public", $crate::testnet::[<FeePublic $variant>]::load_bytes())); 137 $crate::insert_testnet_key!($map, string, $type<$network>, ("upgrade", $crate::testnet::[<Upgrade $variant>]::load_bytes())); 138 } 139 }}; 140 } 141 142 #[macro_export] 143 macro_rules! insert_testnet_credit_v0_keys { 144 ($map:ident, $type:ident<$network:ident>, $variant:ident) => {{ 145 paste::paste! { 146 let string = stringify!([<$variant:lower>]); 147 $crate::insert_testnet_key!($map, string, $type<$network>, ("bond_public", $crate::testnet::[<BondPublicV0 $variant>]::load_bytes())); 148 $crate::insert_testnet_key!($map, string, $type<$network>, ("bond_validator", $crate::testnet::[<BondValidatorV0 $variant>]::load_bytes())); 149 $crate::insert_testnet_key!($map, string, $type<$network>, ("unbond_public", $crate::testnet::[<UnbondPublicV0 $variant>]::load_bytes())); 150 $crate::insert_testnet_key!($map, string, $type<$network>, ("claim_unbond_public", $crate::testnet::[<ClaimUnbondPublicV0 $variant>]::load_bytes())); 151 $crate::insert_testnet_key!($map, string, $type<$network>, ("set_validator_state", $crate::testnet::[<SetValidatorStateV0 $variant>]::load_bytes())); 152 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_private", $crate::testnet::[<TransferPrivateV0 $variant>]::load_bytes())); 153 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public", $crate::testnet::[<TransferPublicV0 $variant>]::load_bytes())); 154 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public_as_signer", $crate::testnet::[<TransferPublicAsSignerV0 $variant>]::load_bytes())); 155 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_private_to_public", $crate::testnet::[<TransferPrivateToPublicV0 $variant>]::load_bytes())); 156 $crate::insert_testnet_key!($map, string, $type<$network>, ("transfer_public_to_private", $crate::testnet::[<TransferPublicToPrivateV0 $variant>]::load_bytes())); 157 $crate::insert_testnet_key!($map, string, $type<$network>, ("join", $crate::testnet::[<JoinV0 $variant>]::load_bytes())); 158 $crate::insert_testnet_key!($map, string, $type<$network>, ("split", $crate::testnet::[<SplitV0 $variant>]::load_bytes())); 159 $crate::insert_testnet_key!($map, string, $type<$network>, ("fee_private", $crate::testnet::[<FeePrivateV0 $variant>]::load_bytes())); 160 $crate::insert_testnet_key!($map, string, $type<$network>, ("fee_public", $crate::testnet::[<FeePublicV0 $variant>]::load_bytes())); 161 $crate::insert_testnet_key!($map, string, $type<$network>, ("upgrade", $crate::testnet::[<UpgradeV0 $variant>]::load_bytes())); 162 } 163 }}; 164 } 165 166 #[macro_export] 167 macro_rules! insert_testnet_key { 168 ($map:ident, $string:tt, $type:ident<$network:ident>, ($name:tt, $circuit_key:expr)) => {{ 169 // Load the circuit key bytes. 170 let key_bytes: Vec<u8> = $circuit_key.expect(&format!("Failed to load {} bytes", $string)); 171 // Recover the circuit key. 172 let key = $type::<$network>::from_bytes_le(&key_bytes[1..]).expect(&format!("Failed to recover {}", $string)); 173 // Insert the circuit key. 174 $map.insert($name.to_string(), std::sync::Arc::new(key)); 175 }}; 176 } 177 178 // Inclusion 179 impl_remote!(InclusionV0Prover, REMOTE_URL, "resources/", "inclusion", "prover", "credits_v0"); 180 impl_local!(InclusionV0Verifier, "resources/", "inclusion", "verifier", "credits_v0"); 181 impl_remote!(InclusionProver, REMOTE_URL, "resources/", "inclusion", "prover", "credits"); 182 impl_local!(InclusionVerifier, "resources/", "inclusion", "verifier", "credits"); 183 184 /// The function name for the inclusion circuit. 185 pub const NETWORK_INCLUSION_FUNCTION_NAME: &str = "inclusion"; 186 187 lazy_static! { 188 pub static ref INCLUSION_V0_PROVING_KEY: Vec<u8> = 189 InclusionV0Prover::load_bytes().expect("Failed to load inclusion_v0 proving key"); 190 pub static ref INCLUSION_V0_VERIFYING_KEY: Vec<u8> = 191 InclusionV0Verifier::load_bytes().expect("Failed to load inclusion_v0 verifying key"); 192 pub static ref INCLUSION_PROVING_KEY: Vec<u8> = InclusionProver::load_bytes().expect("Failed to load inclusion proving key"); 193 pub static ref INCLUSION_VERIFYING_KEY: Vec<u8> = 194 InclusionVerifier::load_bytes().expect("Failed to load inclusion verifying key"); 195 } 196 197 #[cfg(test)] 198 mod tests { 199 use super::*; 200 use wasm_bindgen_test::*; 201 wasm_bindgen_test_configure!(run_in_browser); 202 203 #[allow(dead_code)] 204 #[wasm_bindgen_test] 205 fn test_load_bytes() { 206 BondPublicVerifier::load_bytes().expect("Failed to load bond_public verifier"); 207 BondValidatorVerifier::load_bytes().expect("Failed to load bond_validator verifier"); 208 UnbondPublicVerifier::load_bytes().expect("Failed to load unbond_public verifier"); 209 ClaimUnbondPublicVerifier::load_bytes().expect("Failed to load claim_unbond_public verifier"); 210 SetValidatorStateVerifier::load_bytes().expect("Failed to load set_validator_state verifier"); 211 TransferPrivateVerifier::load_bytes().expect("Failed to load transfer_private verifier"); 212 TransferPublicVerifier::load_bytes().expect("Failed to load transfer_public verifier"); 213 TransferPublicAsSignerVerifier::load_bytes().expect("Failed to load transfer_public_as_signer verifier"); 214 TransferPrivateToPublicVerifier::load_bytes().expect("Failed to load transfer_private_to_public verifier"); 215 TransferPublicToPrivateVerifier::load_bytes().expect("Failed to load transfer_public_to_private verifier"); 216 FeePrivateProver::load_bytes().expect("Failed to load fee_private prover"); 217 FeePrivateVerifier::load_bytes().expect("Failed to load fee_private verifier"); 218 FeePublicProver::load_bytes().expect("Failed to load fee_public prover"); 219 FeePublicVerifier::load_bytes().expect("Failed to load fee_public verifier"); 220 UpgradeProver::load_bytes().expect("Failed to load upgrade prover"); 221 UpgradeVerifier::load_bytes().expect("Failed to load upgrade verifier"); 222 InclusionProver::load_bytes().expect("Failed to load inclusion prover"); 223 InclusionVerifier::load_bytes().expect("Failed to load inclusion verifier"); 224 } 225 }