fourq/
lib.rs

1// Copyright 982945902@qq.com.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod scalar;
16
17pub mod point;
18
19#[cfg(test)]
20mod tests {
21    use crate::point::Point;
22    use crate::scalar::Scalar;
23    use rand::{thread_rng, Rng};
24    #[test]
25    fn it_works() {
26        let scalar = Scalar::new(thread_rng().gen());
27        let point = Point::from_hash(&scalar.bytes);
28
29        let a = scalar * point;
30        let b = point * scalar;
31        assert!(a == b);
32    }
33
34    #[test]
35    fn ecdh_test() {
36        let p1_key = Scalar::new(thread_rng().gen());
37        let p2_key = Scalar::new(thread_rng().gen());
38
39        let example_id: [u8; 32] = thread_rng().gen();
40
41        let p1_example = example_id;
42        let p2_example = example_id;
43
44        let p1_example_point = Point::from_hash(&p1_example);
45        let p2_example_point = Point::from_hash(&p2_example);
46
47        let p1_ecc_k = p1_key * p1_example_point * p2_key;
48        let p2_ecc_k = p2_key * p2_example_point * p1_key;
49
50        assert!(p1_ecc_k == p2_ecc_k);
51    }
52}