fees.rs 3.1 KB

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