This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
vrf.cpp
72 lines (57 loc) · 2.53 KB
/
vrf.cpp
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
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include <string>
#include <array>
#include "utils.hpp"
extern "C" {
#include <sr25519/sr25519.h>
}
TEST(VrfTest, Verify) {
auto keypair = randomKeypair();
std::array<uint8_t, SR25519_VRF_OUTPUT_SIZE + SR25519_VRF_PROOF_SIZE>
out_and_proof;
auto message = "Hello, world!"_v;
auto limit = std::vector<uint8_t>(16, 0xFF);
auto res1 =
sr25519_vrf_sign_if_less(out_and_proof.data(), keypair.data(),
message.data(), message.size(), limit.data());
ASSERT_EQ(res1.result, Sr25519SignatureResult::Ok);
ASSERT_TRUE(res1.is_less);
auto res2 = sr25519_vrf_verify(
keypair.data() + 64, message.data(), message.size(), out_and_proof.data(),
out_and_proof.data() + SR25519_VRF_OUTPUT_SIZE, limit.data());
ASSERT_EQ(res2.result, Sr25519SignatureResult::Ok);
ASSERT_TRUE(res2.is_less);
out_and_proof[5] += 3;
auto res3 = sr25519_vrf_verify(
keypair.data() + 64, message.data(), message.size(), out_and_proof.data(),
out_and_proof.data() + SR25519_VRF_OUTPUT_SIZE, limit.data());
ASSERT_NE(res3.result, Sr25519SignatureResult::Ok);
}
TEST(VrfTest, ResultNotLess) {
auto keypair = "915bb406968655c3412df5773c3de3dee9f6da84668b5de8d2f34d0304d20b0bac5ea3a293dfd93859ee64a5b825937753864c19be857f045758dcae10259ba1049b21bb9cb88471b9dadb50b925135cfb291a463043635b58599a2d01b1fd18"_unhex;
std::array<uint8_t, SR25519_VRF_OUTPUT_SIZE + SR25519_VRF_PROOF_SIZE>
out_and_proof;
auto message = "Hello, world!"_v;
auto limit = std::vector<uint8_t>(16, 0x55);
auto res1 =
sr25519_vrf_sign_if_less(out_and_proof.data(), keypair.data(),
message.data(), message.size(), limit.data());
ASSERT_EQ(res1.result, Sr25519SignatureResult::Ok);
EXPECT_FALSE(res1.is_less);
}
TEST(VrfTest, SignAndCheck) {
auto keypair = "915bb406968655c3412df5773c3de3dee9f6da84668b5de8d2f34d0304d20b0bac5ea3a293dfd93859ee64a5b825937753864c19be857f045758dcae10259ba1049b21bb9cb88471b9dadb50b925135cfb291a463043635b58599a2d01b1fd18"_unhex;
std::array<uint8_t, SR25519_VRF_OUTPUT_SIZE + SR25519_VRF_PROOF_SIZE>
out_and_proof;
auto message = "Hello, world!"_v;
auto limit = std::vector<uint8_t>(16, 0xAA);
auto res1 =
sr25519_vrf_sign_if_less(out_and_proof.data(), keypair.data(),
message.data(), message.size(), limit.data());
ASSERT_EQ(res1.result, Sr25519SignatureResult::Ok);
EXPECT_TRUE(res1.is_less);
}