1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
//! Encryption using the poseidon hash function:
//!
//! ## Example
//!
//! ```rust
//! #![cfg(feature = "encryption")]
//!
//! use dusk_curves::bls12_381::BlsScalar;
//! use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED, dhke};
//! use dusk_poseidon::{decrypt, encrypt, Error};
//! use ff::Field;
//! use rand::rngs::StdRng;
//! use rand::SeedableRng;
//!
//! // generate the keys and nonce needed for the encryption
//! let mut rng = StdRng::seed_from_u64(0x42424242);
//! let alice_secret = JubJubScalar::random(&mut rng);
//! let alice_public = GENERATOR_EXTENDED * &alice_secret;
//! let bob_secret = JubJubScalar::random(&mut rng);
//! let bob_public = GENERATOR_EXTENDED * &bob_secret;
//! let nonce = BlsScalar::random(&mut rng);
//!
//! // Alice encrypts a message of 3 BlsScalar using Diffie-Hellman key exchange
//! // with Bob's public key
//! let message = vec![BlsScalar::from(10), BlsScalar::from(20), BlsScalar::from(30)];
//! let shared_secret = dhke(&alice_secret, &bob_public);
//! let cipher = encrypt(&message, &shared_secret, &nonce)
//! .expect("Encryption should pass");
//!
//! // Bob decrypts the cipher using Diffie-Hellman key exchange with Alice's
//! // public key
//! let shared_secret = dhke(&bob_secret, &alice_public);
//! let decrypted_message = decrypt(&cipher, &shared_secret, &nonce)
//! .expect("Decryption should pass");
//!
//! assert_eq!(decrypted_message, message);
//! ```
pub
use Vec;
use BlsScalar;
use JubJubAffine;
use crateScalarPermutation;
use crate::;
/// This function encrypts a given message with a shared secret point on the
/// jubjub-curve and a bls-scalar nonce using the poseidon hash function.
///
/// The shared secret is expected to be a valid point on the jubjub-curve.
///
/// The cipher-text will always yield exactly one element more than the message.
/// This function decrypts a message from a given cipher-text with a shared
/// secret point on the jubjub-curve and a bls-scalar nonce using the poseidon
/// hash function.
///
/// The shared secret is expected to be a valid point on the jubjub-curve.
///
/// The cipher-text will always yield exactly one element more than the message.