1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 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 */
1819#[cfg(feature = "async")]
20use darkfi_serial::async_trait;
21use darkfi_serial::{SerialDecodable, SerialEncodable};
22use halo2_gadgets::ecc::chip::FixedPoint;
23use pasta_curves::{
24 group::{ff::PrimeField, Group, GroupEncoding},
25 pallas,
26};
2728use super::{
29 constants::{NullifierK, DRK_SCHNORR_DOMAIN},
30 util::{fp_mod_fv, hash_to_scalar},
31 PublicKey, SecretKey,
32};
3334/// Schnorr signature with a commit and response
35#[derive(Debug, Clone, Copy, Eq, PartialEq, SerialEncodable, SerialDecodable)]
36pub struct Signature {
37 commit: pallas::Point,
38 response: pallas::Scalar,
39}
4041impl Signature {
42/// Return a dummy identity `Signature`
43pub fn dummy() -> Self {
44Self { commit: pallas::Point::identity(), response: pallas::Scalar::zero() }
45 }
46}
4748/// Trait for secret keys that implements a signature creation
49pub trait SchnorrSecret {
50/// Sign a given message
51fn sign(&self, message: &[u8]) -> Signature;
52}
5354/// Trait for public keys that implements a signature verification
55pub trait SchnorrPublic {
56/// Verify a given message is valid given a signature.
57fn verify(&self, message: &[u8], signature: &Signature) -> bool;
58}
5960/// Schnorr signature trait implementations for the stuff in `keypair.rs`
61impl SchnorrSecret for SecretKey {
62fn sign(&self, message: &[u8]) -> Signature {
63// Derive a deterministic nonce
64let mask = hash_to_scalar(DRK_SCHNORR_DOMAIN, &[&self.inner().to_repr(), message]);
6566let commit = NullifierK.generator() * mask;
6768let commit_bytes = commit.to_bytes();
69let pubkey_bytes = PublicKey::from_secret(*self).to_bytes();
70let transcript = &[&commit_bytes, &pubkey_bytes, message];
7172let challenge = hash_to_scalar(DRK_SCHNORR_DOMAIN, transcript);
73let response = mask + challenge * fp_mod_fv(self.inner());
7475 Signature { commit, response }
76 }
77}
7879impl SchnorrPublic for PublicKey {
80fn verify(&self, message: &[u8], signature: &Signature) -> bool {
81let commit_bytes = signature.commit.to_bytes();
82let pubkey_bytes = self.to_bytes();
83let transcript = &[&commit_bytes, &pubkey_bytes, message];
8485let challenge = hash_to_scalar(DRK_SCHNORR_DOMAIN, transcript);
86 NullifierK.generator() * signature.response - self.inner() * challenge == signature.commit
87 }
88}
8990#[cfg(test)]
91mod tests {
92use super::*;
93use darkfi_serial::{deserialize, serialize};
94use rand::rngs::OsRng;
9596#[test]
97fn test_schnorr_signature() {
98let secret = SecretKey::random(&mut OsRng);
99let message: &[u8] = b"aaaahhhh i'm signiiinngg";
100let signature = secret.sign(message);
101let public = PublicKey::from_secret(secret);
102assert!(public.verify(message, &signature));
103104// Check out if it's also fine with serialization
105let ser = serialize(&signature);
106let de = deserialize(&ser).unwrap();
107assert!(public.verify(message, &de));
108 }
109}