Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
address_derivation.cpp
Go to the documentation of this file.
2
3#include <cassert>
4
7
9
11{
12 // Check if we've already derived this address
13 if (cached_derivations.contains(address)) {
14 // Already processed this address - cache hit, don't emit event
15 return;
16 }
17
18 // First time seeing this address - do the actual derivation
19 FF salted_initialization_hash = poseidon2.hash(
21
22 FF partial_address =
23 poseidon2.hash({ GENERATOR_INDEX__PARTIAL_ADDRESS, instance.original_class_id, salted_initialization_hash });
24
25 std::vector<FF> public_keys_hash_fields = instance.public_keys.to_fields();
26 std::vector<FF> public_key_hash_vec{ GENERATOR_INDEX__PUBLIC_KEYS_HASH };
27 for (size_t i = 0; i < public_keys_hash_fields.size(); i += 2) {
28 public_key_hash_vec.push_back(public_keys_hash_fields[i]);
29 public_key_hash_vec.push_back(public_keys_hash_fields[i + 1]);
30 // is_infinity will be removed from address preimage, asumming false.
31 public_key_hash_vec.push_back(FF::zero());
32 }
33 FF public_keys_hash = poseidon2.hash(public_key_hash_vec);
34
35 FF preaddress = poseidon2.hash({ GENERATOR_INDEX__CONTRACT_ADDRESS_V1, public_keys_hash, partial_address });
36
37 EmbeddedCurvePoint preaddress_public_key = ecc.scalar_mul(EmbeddedCurvePoint::one(), preaddress);
38 EmbeddedCurvePoint address_point = ecc.add(preaddress_public_key, instance.public_keys.incoming_viewing_key);
39
40 assert(address == address_point.x());
41
42 // Cache this derivation so we don't repeat it
43 cached_derivations.insert(address);
44
45 events.emit({
46 .address = address,
47 .instance = instance,
48 .salted_initialization_hash = salted_initialization_hash,
49 .partial_address = partial_address,
50 .public_keys_hash = public_keys_hash,
51 .preaddress = preaddress,
52 .preaddress_public_key = preaddress_public_key,
53 .address_point = address_point,
54 });
55}
56
57} // namespace bb::avm2::simulation
#define GENERATOR_INDEX__PUBLIC_KEYS_HASH
#define GENERATOR_INDEX__PARTIAL_ADDRESS
#define GENERATOR_INDEX__CONTRACT_ADDRESS_V1
constexpr const BaseField & x() const noexcept
std::unordered_set< AztecAddress > cached_derivations
void assert_derivation(const AztecAddress &address, const ContractInstance &instance) override
EventEmitterInterface< AddressDerivationEvent > & events
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
AvmFlavorSettings::FF FF
Definition field.hpp:10
ContractClassId original_class_id
AffinePoint incoming_viewing_key
std::vector< FF > to_fields() const
static constexpr field zero()