Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
address_derivation.test.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
18
19using ::testing::IsEmpty;
20using ::testing::Return;
21using ::testing::SizeIs;
22using ::testing::StrictMock;
23
25
26namespace bb::avm2::simulation {
27
28namespace {
29
30using simulation::FakePoseidon2;
31
32TEST(AvmSimulationAddressDerivationTest, Positive)
33{
34 EventEmitter<AddressDerivationEvent> address_derivation_event_emitter;
35 FakePoseidon2 poseidon2 = FakePoseidon2();
36 StrictMock<MockEcc> ecc;
37
38 AddressDerivation address_derivation(poseidon2, ecc, address_derivation_event_emitter);
39
40 ContractInstance instance = testing::random_contract_instance();
41 AztecAddress derived_address = compute_contract_address(instance);
42 std::vector<FF> salted_init_hash_inputs = {
43 GENERATOR_INDEX__PARTIAL_ADDRESS, instance.salt, instance.initialisation_hash, instance.deployer_addr
44 };
45 FF salted_init_hash = poseidon2::hash(salted_init_hash_inputs);
46
47 std::vector<FF> partial_address_inputs = { GENERATOR_INDEX__PARTIAL_ADDRESS,
48 instance.original_class_id,
49 salted_init_hash };
50 FF partial_address = poseidon2::hash(partial_address_inputs);
51
52 std::vector<FF> public_keys_hash_fields = instance.public_keys.to_fields();
53 std::vector<FF> public_key_hash_vec{ GENERATOR_INDEX__PUBLIC_KEYS_HASH };
54 for (size_t i = 0; i < public_keys_hash_fields.size(); i += 2) {
55 public_key_hash_vec.push_back(public_keys_hash_fields[i]);
56 public_key_hash_vec.push_back(public_keys_hash_fields[i + 1]);
57 // is_infinity will be removed from address preimage, asumming false.
58 public_key_hash_vec.push_back(FF::zero());
59 }
60 FF public_keys_hash = poseidon2::hash(public_key_hash_vec);
61
62 std::vector<FF> preaddress_inputs = { GENERATOR_INDEX__CONTRACT_ADDRESS_V1, public_keys_hash, partial_address };
63 FF preaddress = poseidon2::hash(preaddress_inputs);
64
66 EmbeddedCurvePoint preaddress_public_key = g1 * Fq(preaddress);
67 EXPECT_CALL(ecc, scalar_mul(g1, preaddress)).WillOnce(Return(preaddress_public_key));
68
69 EmbeddedCurvePoint address_point = preaddress_public_key + instance.public_keys.incoming_viewing_key;
70 EXPECT_CALL(ecc, add(preaddress_public_key, EmbeddedCurvePoint(instance.public_keys.incoming_viewing_key)))
71 .WillOnce(Return(address_point));
72
73 address_derivation.assert_derivation(derived_address, instance);
74
75 auto events = address_derivation_event_emitter.dump_events();
76 EXPECT_THAT(events, SizeIs(1));
77 EXPECT_THAT(events[0].instance, instance);
78 EXPECT_THAT(events[0].address, derived_address);
79 EXPECT_THAT(events[0].address_point.x(), derived_address);
80
81 // Second derivation for the same address should be a cache hit and should not emit an event
82 address_derivation.assert_derivation(derived_address, instance);
83 events = address_derivation_event_emitter.dump_events();
84 EXPECT_THAT(events, IsEmpty());
85}
86
87} // namespace
88} // namespace bb::avm2::simulation
#define GENERATOR_INDEX__PUBLIC_KEYS_HASH
#define GENERATOR_INDEX__PARTIAL_ADDRESS
#define GENERATOR_INDEX__CONTRACT_ADDRESS_V1
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
TEST(EmitUnencryptedLogTest, Basic)
ContractInstance random_contract_instance()
Definition fixtures.cpp:158
StandardAffinePoint< AvmFlavorSettings::EmbeddedCurve::AffineElement > EmbeddedCurvePoint
Definition field.hpp:12
AvmFlavorSettings::G1::Fq Fq
Definition field.hpp:11
typename Flavor::FF FF