Просмотр исходного кода

crypto: Introduce Keypair struct.

parazyd 4 лет назад
Родитель
Сommit
219c299360
2 измененных файлов с 29 добавлено и 0 удалено
  1. 28 0
      src/crypto/keypair.rs
  2. 1 0
      src/crypto/mod.rs

+ 28 - 0
src/crypto/keypair.rs

@@ -0,0 +1,28 @@
+use halo2_gadgets::ecc::FixedPoints;
+use pasta_curves::{arithmetic::Field, pallas};
+use rand::RngCore;
+
+use crate::crypto::{constants::OrchardFixedBases, util::mod_r_p};
+
+#[derive(Clone, Debug)]
+pub struct Keypair {
+    secret: pallas::Base,
+    public: pallas::Point,
+}
+
+impl Keypair {
+    fn derive_pub(s: pallas::Base) -> pallas::Point {
+        OrchardFixedBases::NullifierK.generator() * mod_r_p(s)
+    }
+
+    pub fn new(secret: pallas::Base) -> Self {
+        let public = Keypair::derive_pub(secret);
+        Keypair { secret, public }
+    }
+
+    pub fn random(mut rng: impl RngCore) -> Self {
+        let secret = pallas::Base::random(&mut rng);
+        let public = Keypair::derive_pub(secret);
+        Keypair { secret, public }
+    }
+}

+ 1 - 0
src/crypto/mod.rs

@@ -2,6 +2,7 @@ pub mod arith_chip;
 pub mod coin;
 pub mod constants;
 pub mod diffie_hellman;
+pub mod keypair;
 pub mod merkle;
 pub mod merkle_node2;
 pub mod mint_proof;