xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/compute_session_id_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2018 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "fcp/secagg/shared/compute_session_id.h"
17 
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 
21 namespace fcp {
22 namespace secagg {
23 namespace {
24 
25 using ::testing::Eq;
26 using ::testing::Ne;
27 
TEST(ComputeSessionIdTest,OutputIsCorrectLength)28 TEST(ComputeSessionIdTest, OutputIsCorrectLength) {
29   ShareKeysRequest request;
30   PairOfPublicKeys* keys = request.add_pairs_of_public_keys();
31   keys->set_noise_pk("abcdefghijklmnopqrstuvwxyz7890123");
32   keys->set_enc_pk("1234567abcdefghijklmnopqrstuvwxyz");
33 
34   SessionId session_id = ComputeSessionId(request);
35   EXPECT_THAT(session_id.data.size(), Eq(32));
36 }
37 
TEST(ComputeSessionIdTest,OutputIsDeterministic)38 TEST(ComputeSessionIdTest, OutputIsDeterministic) {
39   ShareKeysRequest request1;
40   PairOfPublicKeys* keys1 = request1.add_pairs_of_public_keys();
41   keys1->set_noise_pk("abcdefghijklmnopqrstuvwxyz7890123");
42   keys1->set_enc_pk("1234567abcdefghijklmnopqrstuvwxyz");
43   ShareKeysRequest request2;
44   PairOfPublicKeys* keys2 = request2.add_pairs_of_public_keys();
45   keys2->set_noise_pk("abcdefghijklmnopqrstuvwxyz7890123");
46   keys2->set_enc_pk("1234567abcdefghijklmnopqrstuvwxyz");
47 
48   SessionId session_id_1 = ComputeSessionId(request1);
49   SessionId session_id_2 = ComputeSessionId(request2);
50   EXPECT_THAT(session_id_2.data, Eq(session_id_1.data));
51 }
52 
TEST(ComputeSessionIdTest,OutputChangesOnDifferentInputs)53 TEST(ComputeSessionIdTest, OutputChangesOnDifferentInputs) {
54   ShareKeysRequest request1;
55   PairOfPublicKeys* keys1 = request1.add_pairs_of_public_keys();
56   keys1->set_noise_pk("abcdefghijklmnopqrstuvwxyz7890123");
57   keys1->set_enc_pk("1234567abcdefghijklmnopqrstuvwxyz");
58   ShareKeysRequest request2;
59   PairOfPublicKeys* keys2 = request2.add_pairs_of_public_keys();
60   keys2->set_noise_pk("123456789012345678901234567890123");
61   keys2->set_enc_pk("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG");
62 
63   SessionId session_id_1 = ComputeSessionId(request1);
64   SessionId session_id_2 = ComputeSessionId(request2);
65   EXPECT_THAT(session_id_2.data, Ne(session_id_1.data));
66 }
67 
68 // Hard coded keys and output generated by Java
TEST(ComputeSessionIdTest,OutputMatchesHardCodedValues)69 TEST(ComputeSessionIdTest, OutputMatchesHardCodedValues) {
70   ShareKeysRequest request1;
71   PairOfPublicKeys* keys1 = request1.add_pairs_of_public_keys();
72   keys1->set_noise_pk(
73       "\002Y\256\332c\202\214\367\234F\f\370M;\301P}\b\220)\267\206C*"
74       "\253f\363\375Z\262\300\214(");
75   keys1->set_enc_pk(
76       "\002m\003C\234\217\"\037\025{\354~\345G\233\277~"
77       "\222\220\036Tkl\334C\241Ln\256\023\315k]");
78   PairOfPublicKeys* keys2 = request1.add_pairs_of_public_keys();
79   keys2->set_noise_pk(
80       "\002\023\313\267\331\211\031\332fn8\035Qx\241\217\002K\345\"\260\377:"
81       "\231~\222\246,\232?\030m\032");
82   keys2->set_enc_pk(
83       "\003\204\243\326["
84       "I\273\326\301\336\254X\300\332\201\334\371\023\351\021\022\323\371\234`"
85       "\301\352p\251\vR\217I");
86 
87   SessionId expected;
88   uint8_t precomputed[32] = {120, 175, 110, 210, 30,  111, 197, 231,
89                              253, 35,  163, 25,  159, 204, 80,  79,
90                              173, 180, 27,  166, 83,  53,  85,  161,
91                              228, 232, 97,  20,  242, 62,  142, 114};
92   expected.data = std::string(reinterpret_cast<const char*>(precomputed), 32);
93   SessionId session_id = ComputeSessionId(request1);
94   EXPECT_THAT(session_id.data, Eq(expected.data));
95 }
96 
97 }  // namespace
98 }  // namespace secagg
99 }  // namespace fcp
100