fees.rs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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;
  19. use crate::zkas::{Opcode, VarType, ZkBinary};
  20. /// Fixed fee for verifying Schnorr signatures using the Pallas elliptic curve
  21. pub const PALLAS_SCHNORR_SIGNATURE_FEE: u64 = 1000;
  22. /// Calculate the gas use for verifying a given zkas circuit.
  23. /// This function assumes that the zkbin was properly decoded.
  24. pub fn circuit_gas_use(zkbin: &ZkBinary) -> u64 {
  25. let mut accumulator: u64 = 0;
  26. // Constants each with a cost of 10
  27. accumulator += 10 * zkbin.constants.len() as u64;
  28. // Literals each with a cost of 10 (for now there's only 1 type of literal)
  29. accumulator += 10 * zkbin.literals.len() as u64;
  30. // Witnesses have cost by type
  31. for witness in &zkbin.witnesses {
  32. let cost = match witness {
  33. VarType::Dummy => unreachable!(),
  34. VarType::EcPoint => 20,
  35. VarType::EcFixedPoint => unreachable!(),
  36. VarType::EcFixedPointShort => unreachable!(),
  37. VarType::EcFixedPointBase => unreachable!(),
  38. VarType::EcNiPoint => 20,
  39. VarType::Base => 10,
  40. VarType::BaseArray => unreachable!(),
  41. VarType::Scalar => 20,
  42. VarType::ScalarArray => unreachable!(),
  43. VarType::MerklePath => 10 * MERKLE_DEPTH_ORCHARD as u64,
  44. VarType::Uint32 => 10,
  45. VarType::Uint64 => 10,
  46. VarType::Any => 10,
  47. };
  48. accumulator += cost;
  49. }
  50. // Opcodes depending on how heavy they are
  51. for opcode in &zkbin.opcodes {
  52. let cost = match opcode.0 {
  53. Opcode::Noop => unreachable!(),
  54. Opcode::EcAdd => 30,
  55. Opcode::EcMul => 30,
  56. Opcode::EcMulBase => 30,
  57. Opcode::EcMulShort => 30,
  58. Opcode::EcMulVarBase => 30,
  59. Opcode::EcGetX => 5,
  60. Opcode::EcGetY => 5,
  61. Opcode::PoseidonHash => 20 + 10 * opcode.1.len() as u64,
  62. Opcode::MerkleRoot => 10 * MERKLE_DEPTH_ORCHARD as u64,
  63. Opcode::BaseAdd => 15,
  64. Opcode::BaseMul => 15,
  65. Opcode::BaseSub => 15,
  66. Opcode::WitnessBase => 10,
  67. Opcode::RangeCheck => 60,
  68. Opcode::LessThanStrict => 100,
  69. Opcode::LessThanLoose => 100,
  70. Opcode::BoolCheck => 20,
  71. Opcode::CondSelect => 10,
  72. Opcode::ZeroCondSelect => 10,
  73. Opcode::ConstrainEqualBase => 10,
  74. Opcode::ConstrainEqualPoint => 20,
  75. Opcode::ConstrainInstance => 10,
  76. Opcode::DebugPrint => 100,
  77. };
  78. accumulator += cost;
  79. }
  80. accumulator
  81. }