summaryrefslogtreecommitdiffstats
path: root/vendor/primeorder/src/dev.rs
blob: 77f563fa03840baae5f3283766730703e1cb85a5 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Development-related functionality.

// TODO(tarcieri): move all development-related macros into this module

/// Implement projective arithmetic tests.
#[macro_export]
macro_rules! impl_projective_arithmetic_tests {
    (
        $affine:tt,
        $projective:tt,
        $scalar:ty,
        $add_vectors:expr,
        $mul_vectors:expr
    ) => {
        /// Assert that the provided projective point matches the given test vector.
        // TODO(tarcieri): use coordinate APIs. See zkcrypto/group#30
        macro_rules! assert_point_eq {
            ($actual:expr, $expected:expr) => {
                let (expected_x, expected_y) = $expected;

                let point = $actual.to_affine().to_encoded_point(false);
                let (actual_x, actual_y) = match point.coordinates() {
                    sec1::Coordinates::Uncompressed { x, y } => (x, y),
                    _ => unreachable!(),
                };

                assert_eq!(&expected_x, actual_x.as_slice());
                assert_eq!(&expected_y, actual_y.as_slice());
            };
        }

        #[test]
        fn affine_to_projective() {
            let basepoint_affine = $affine::GENERATOR;
            let basepoint_projective = $projective::GENERATOR;

            assert_eq!($projective::from(basepoint_affine), basepoint_projective,);
            assert_eq!(basepoint_projective.to_affine(), basepoint_affine);
            assert!(!bool::from(basepoint_projective.to_affine().is_identity()));

            assert!(bool::from($projective::IDENTITY.to_affine().is_identity()));
        }

        #[test]
        fn projective_identity_addition() {
            let identity = $projective::IDENTITY;
            let generator = $projective::GENERATOR;

            assert_eq!(identity + &generator, generator);
            assert_eq!(generator + &identity, generator);
        }

        #[test]
        fn projective_mixed_addition() {
            let identity = $projective::IDENTITY;
            let basepoint_affine = $affine::GENERATOR;
            let basepoint_projective = $projective::GENERATOR;

            assert_eq!(identity + &basepoint_affine, basepoint_projective);
            assert_eq!(
                basepoint_projective + &basepoint_affine,
                basepoint_projective + &basepoint_projective
            );
        }

        #[test]
        fn test_vector_repeated_add() {
            let generator = $projective::GENERATOR;
            let mut p = generator;

            for i in 0..$add_vectors.len() {
                assert_point_eq!(p, $add_vectors[i]);
                p += &generator;
            }
        }

        #[test]
        fn test_vector_repeated_add_mixed() {
            let generator = $affine::GENERATOR;
            let mut p = $projective::GENERATOR;

            for i in 0..$add_vectors.len() {
                assert_point_eq!(p, $add_vectors[i]);
                p += &generator;
            }
        }

        #[test]
        fn test_vector_add_mixed_identity() {
            let generator = $projective::GENERATOR;
            let p0 = generator + $projective::IDENTITY;
            let p1 = generator + $affine::IDENTITY;
            assert_eq!(p0, p1);
        }

        #[test]
        fn test_vector_double_generator() {
            let generator = $projective::GENERATOR;
            let mut p = generator;

            for i in 0..2 {
                assert_point_eq!(p, $add_vectors[i]);
                p = p.double();
            }
        }

        #[test]
        fn projective_add_vs_double() {
            let generator = $projective::GENERATOR;
            assert_eq!(generator + &generator, generator.double());
        }

        #[test]
        fn projective_add_and_sub() {
            let basepoint_affine = $affine::GENERATOR;
            let basepoint_projective = $projective::GENERATOR;

            assert_eq!(
                (basepoint_projective + &basepoint_projective) - &basepoint_projective,
                basepoint_projective
            );
            assert_eq!(
                (basepoint_projective + &basepoint_affine) - &basepoint_affine,
                basepoint_projective
            );
        }

        #[test]
        fn projective_double_and_sub() {
            let generator = $projective::GENERATOR;
            assert_eq!(generator.double() - &generator, generator);
        }

        #[test]
        fn test_vector_scalar_mult() {
            let generator = $projective::GENERATOR;

            for (k, coords) in $add_vectors
                .iter()
                .enumerate()
                .map(|(k, coords)| (<$scalar>::from(k as u64 + 1), *coords))
                .chain(
                    $mul_vectors
                        .iter()
                        .cloned()
                        .map(|(k, x, y)| (<$scalar>::from_repr(k.into()).unwrap(), (x, y))),
                )
            {
                let p = generator * &k;
                assert_point_eq!(p, coords);
            }
        }
    };
}