main.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 std::{
  19. io,
  20. io::Write,
  21. process::{Command, Stdio},
  22. };
  23. use anyhow::Result;
  24. use darkfi::consensus::lead_coin::LeadCoin;
  25. use darkfi_sdk::{crypto::pasta_prelude::PrimeField, pasta::pallas};
  26. /// Generate a string represenation of a `pallas::Base` constant
  27. fn to_constant(name: &str, x: pallas::Base, public: bool) -> String {
  28. let repr = x.to_repr();
  29. let mut res = [0_u64; 4];
  30. res[0] = u64::from_le_bytes(repr[0..8].try_into().unwrap());
  31. res[1] = u64::from_le_bytes(repr[8..16].try_into().unwrap());
  32. res[2] = u64::from_le_bytes(repr[16..24].try_into().unwrap());
  33. res[3] = u64::from_le_bytes(repr[24..32].try_into().unwrap());
  34. let p = if public { "pub" } else { "" };
  35. format!("{p} const {name}: pallas::Base = pallas::Base::from_raw({res:?});\n")
  36. }
  37. /// Generate constants for corresponding `pallas::Base`
  38. fn main() -> Result<()> {
  39. let mut source = String::new();
  40. source.push_str(&to_constant("REWARD_PALLAS", pallas::Base::from(100_000_000), true));
  41. source.push_str(&to_constant("SERIAL_PREFIX", pallas::Base::from(2), true));
  42. source.push_str(&to_constant("SEED_PREFIX", pallas::Base::from(3), true));
  43. source.push_str(&to_constant("MU_Y_PREFIX", pallas::Base::from(22), true));
  44. source.push_str(&to_constant("MU_RHO_PREFIX", pallas::Base::from(5), true));
  45. source.push_str(&to_constant("HEADSTART", LeadCoin::headstart(), true));
  46. let mut cmd = Command::new("rustfmt");
  47. cmd.stdin(Stdio::piped()).stdout(Stdio::piped());
  48. cmd.args(["--edition=2021"]);
  49. let mut child = cmd.spawn()?;
  50. let mut child_stdin = child.stdin.take().unwrap();
  51. let mut child_stdout = child.stdout.take().unwrap();
  52. let stdin_handle = std::thread::spawn(move || {
  53. let _ = child_stdin.write_all(source.as_bytes());
  54. source
  55. });
  56. let mut output = vec![];
  57. io::copy(&mut child_stdout, &mut output)?;
  58. let _ = stdin_handle.join().unwrap();
  59. let output = String::from_utf8(output)?;
  60. print!("{}", output);
  61. Ok(())
  62. }
  63. #[cfg(test)]
  64. mod tests {
  65. use darkfi::consensus::lead_coin::LeadCoin;
  66. use darkfi_sdk::pasta::pallas;
  67. #[test]
  68. fn consistency() {
  69. let zero = pallas::Base::zero();
  70. let zero_arr = [0, 0, 0, 0];
  71. let reward = pallas::Base::one();
  72. let reward_arr = [1, 0, 0, 0];
  73. let serial_prefix = pallas::Base::from(2);
  74. let serial_prefix_arr = [2, 0, 0, 0];
  75. let seed_prefix = pallas::Base::from(3);
  76. let seed_prefix_arr = [3, 0, 0, 0];
  77. let mu_y_prefix = pallas::Base::from(22);
  78. let mu_y_prefix_arr = [22, 0, 0, 0];
  79. let mu_rho_prefix = pallas::Base::from(5);
  80. let mu_rho_prefix_arr = [5, 0, 0, 0];
  81. let headstart = LeadCoin::headstart();
  82. let headstart_arr =
  83. [11731824086999220879, 11830614503713258191, 737869762948382064, 46116860184273879];
  84. assert_eq!(zero, pallas::Base::from_raw(zero_arr));
  85. assert_eq!(reward, pallas::Base::from_raw(reward_arr));
  86. assert_eq!(serial_prefix, pallas::Base::from_raw(serial_prefix_arr));
  87. assert_eq!(seed_prefix, pallas::Base::from_raw(seed_prefix_arr));
  88. assert_eq!(mu_y_prefix, pallas::Base::from_raw(mu_y_prefix_arr));
  89. assert_eq!(mu_rho_prefix, pallas::Base::from_raw(mu_rho_prefix_arr));
  90. assert_eq!(headstart, pallas::Base::from_raw(headstart_arr));
  91. }
  92. }