main.rs 4.6 KB

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