circuits/test/utils/keypair.rs
1use zkhash::{ark_ff::Zero, fields::bn256::FpBN256 as Scalar};
2
3use super::general::{poseidon2_hash2, poseidon2_hash3};
4
5/// Derive a public key from a private key using Poseidon2 hash
6///
7/// Computes `publicKey = Poseidon2(privateKey, 0)` with domain separation value
8/// 3. Please note the 0 is used as padding as Poseidon2 hash does not support
9/// T=1 inputs (over BN256).
10///
11/// # Arguments
12///
13/// * `private_key` - Private key scalar value
14///
15/// # Returns
16///
17/// Returns the derived public key as a scalar value.
18pub fn derive_public_key(private_key: Scalar) -> Scalar {
19 poseidon2_hash2(private_key, Scalar::zero(), Some(Scalar::from(3))) // We use 3 as domain separation for Keypair
20}
21
22/// Generate a signature using Poseidon2 hash
23///
24/// Computes `signature = Poseidon2(privateKey, commitment, merklePath)` with
25/// domain separation value 4.
26///
27/// # Arguments
28///
29/// * `private_key` - Private key scalar value
30/// * `commitment` - Commitment scalar value
31/// * `merkle_path` - Merkle path scalar value
32///
33/// # Returns
34///
35/// Returns the signature as a scalar value.
36pub fn sign(private_key: Scalar, commitment: Scalar, merkle_path: Scalar) -> Scalar {
37 poseidon2_hash3(private_key, commitment, merkle_path, Some(Scalar::from(4))) // We use 4 as domain separation for Signature
38}