fees.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::crypto::constants::{MERKLE_DEPTH_ORCHARD, SPARSE_MERKLE_DEPTH};
  19. use darkfi_serial::{async_trait, SerialDecodable, SerialEncodable};
  20. use crate::zkas::{Opcode, VarType, ZkBinary};
  21. /// Fixed fee for verifying Schnorr signatures using the Pallas elliptic curve
  22. pub const PALLAS_SCHNORR_SIGNATURE_FEE: u64 = 1000;
  23. /// Calculate the gas use for verifying a given zkas circuit.
  24. /// This function assumes that the zkbin was properly decoded.
  25. pub fn circuit_gas_use(zkbin: &ZkBinary) -> u64 {
  26. let mut accumulator: u64 = 0;
  27. // Constants each with a cost of 10
  28. accumulator += 10 * zkbin.constants.len() as u64;
  29. // Literals each with a cost of 10 (for now there's only 1 type of literal)
  30. accumulator += 10 * zkbin.literals.len() as u64;
  31. // Witnesses have cost by type
  32. for witness in &zkbin.witnesses {
  33. let cost = match witness {
  34. VarType::Dummy => unreachable!(),
  35. VarType::EcPoint => 20,
  36. VarType::EcFixedPoint => unreachable!(),
  37. VarType::EcFixedPointShort => unreachable!(),
  38. VarType::EcFixedPointBase => unreachable!(),
  39. VarType::EcNiPoint => 20,
  40. VarType::Base => 10,
  41. VarType::BaseArray => unreachable!(),
  42. VarType::Scalar => 20,
  43. VarType::ScalarArray => unreachable!(),
  44. VarType::MerklePath => 10 * MERKLE_DEPTH_ORCHARD as u64,
  45. VarType::SparseMerklePath => 10 * SPARSE_MERKLE_DEPTH as u64,
  46. VarType::Uint32 => 10,
  47. VarType::Uint64 => 10,
  48. VarType::Any => 10,
  49. };
  50. accumulator += cost;
  51. }
  52. // Opcodes depending on how heavy they are
  53. for opcode in &zkbin.opcodes {
  54. let cost = match opcode.0 {
  55. Opcode::Noop => unreachable!(),
  56. Opcode::EcAdd => 30,
  57. Opcode::EcMul => 30,
  58. Opcode::EcMulBase => 30,
  59. Opcode::EcMulShort => 30,
  60. Opcode::EcMulVarBase => 30,
  61. Opcode::EcGetX => 5,
  62. Opcode::EcGetY => 5,
  63. Opcode::PoseidonHash => 20 + 10 * opcode.1.len() as u64,
  64. Opcode::MerkleRoot => 10 * MERKLE_DEPTH_ORCHARD as u64,
  65. Opcode::SparseMerkleRoot => 10 * SPARSE_MERKLE_DEPTH as u64,
  66. Opcode::BaseAdd => 15,
  67. Opcode::BaseMul => 15,
  68. Opcode::BaseSub => 15,
  69. Opcode::WitnessBase => 10,
  70. Opcode::RangeCheck => 60,
  71. Opcode::LessThanStrict => 100,
  72. Opcode::LessThanLoose => 100,
  73. Opcode::BoolCheck => 20,
  74. Opcode::CondSelect => 10,
  75. Opcode::ZeroCondSelect => 10,
  76. Opcode::ConstrainEqualBase => 10,
  77. Opcode::ConstrainEqualPoint => 20,
  78. Opcode::ConstrainInstance => 10,
  79. Opcode::DebugPrint => 100,
  80. };
  81. accumulator += cost;
  82. }
  83. accumulator
  84. }
  85. /// Auxiliary struct representing the full gas usage breakdown of a transaction.
  86. ///
  87. /// This data is used for accounting of fees, providing details relating to
  88. /// resource consumption across different transactions.
  89. #[derive(Default, Clone, Eq, PartialEq, SerialEncodable, SerialDecodable)]
  90. pub struct GasData {
  91. /// Wasm calls gas consumption
  92. pub wasm: u64,
  93. /// ZK circuits gas consumption
  94. pub zk_circuits: u64,
  95. /// Signature fee
  96. pub signatures: u64,
  97. /// Contract deployment gas
  98. pub deployments: u64,
  99. /// Transaction paid fee
  100. pub paid: u64,
  101. }
  102. impl GasData {
  103. /// Calculates the total gas used by summing all individual gas usage fields.
  104. pub fn total_gas_used(&self) -> u64 {
  105. self.wasm + self.zk_circuits + self.signatures + self.deployments
  106. }
  107. }
  108. /// Implements custom debug trait to include [`GasData::total_gas_used`].
  109. impl std::fmt::Debug for GasData {
  110. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  111. f.debug_struct("GasData")
  112. .field("total", &self.total_gas_used())
  113. .field("wasm", &self.wasm)
  114. .field("zk_circuits", &self.zk_circuits)
  115. .field("signatures", &self.signatures)
  116. .field("deployments", &self.deployments)
  117. .field("paid", &self.paid)
  118. .finish()
  119. }
  120. }
  121. /// Auxiliary function to compute the corresponding fee value
  122. /// for the provided gas.
  123. ///
  124. /// Currently we simply divide the gas value by 100.
  125. pub fn compute_fee(gas: &u64) -> u64 {
  126. gas / 100
  127. }