Skip to content

Commit

Permalink
fix: add inner function for re-use
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Sep 6, 2023
1 parent cec9292 commit a4ebc27
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions yarn-project/end-to-end/src/e2e_lending_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Fr,
computeMessageSecretHash,
} from '@aztec/aztec.js';
import { CircuitsWasm, CompleteAddress, GeneratorIndex, PrivateKey } from '@aztec/circuits.js';
import { CircuitsWasm, CompleteAddress, FunctionSelector, GeneratorIndex, PrivateKey } from '@aztec/circuits.js';
import { pedersenPlookupCommitInputs, pedersenPlookupCompressWithHashIndex } from '@aztec/circuits.js/barretenberg';
import { DebugLogger } from '@aztec/foundation/log';
import { LendingContract, NativeTokenContract, PriceFeedContract } from '@aztec/noir-contracts/types';
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('e2e_lending_contract', () => {
const depositAmount = 420n;

const messageHash = await hashPayload([
new Fr(0x90785014),
FunctionSelector.fromSignature('unshieldTokens(Field,Field,Field)').toField(),
recipientFull.address.toField(),
lendingContract.address.toField(),
new Fr(depositAmount),
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('e2e_lending_contract', () => {
{
const depositAmount = 421n;
const messageHash = await hashPayload([
new Fr(0x90785014),
FunctionSelector.fromSignature('unshieldTokens(Field,Field,Field)').toField(),
recipientFull.address.toField(),
lendingContract.address.toField(),
new Fr(depositAmount),
Expand Down Expand Up @@ -505,7 +505,7 @@ describe('e2e_lending_contract', () => {
{
const repayAmount = 20n;
const messageHash = await hashPayload([
new Fr(0x90785014),
FunctionSelector.fromSignature('unshieldTokens(Field,Field,Field)').toField(),
recipientFull.address.toField(),
lendingContract.address.toField(),
new Fr(repayAmount),
Expand Down Expand Up @@ -540,7 +540,7 @@ describe('e2e_lending_contract', () => {
{
const repayAmount = 21n;
const messageHash = await hashPayload([
new Fr(0x90785014),
FunctionSelector.fromSignature('unshieldTokens(Field,Field,Field)').toField(),
recipientFull.address.toField(),
lendingContract.address.toField(),
new Fr(repayAmount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,16 @@ contract NativeToken {
// We can do a simple nullifier to handle that in here. Spends only 32 bytes onchain.
// @todo @LHerskind Is to be solved as part of https://github.com/AztecProtocol/aztec-packages/issues/1743
let message_field: Field = std::hash::pedersen_with_separator([
0x90785014,
compute_selector("unshieldTokens(Field,Field,Field)"),
from,
to,
amount
],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0];

let _callStackItem0 = context.call_private_function(from, 0x29d25ca9, [message_field]);
// Ensure that we retrieved the magic number (selector of is_valid function), otherwise revert.
assert(_callStackItem0[0] == 0x29d25ca9);
let is_valid_selector = compute_selector("is_valid(Field)");
let _callStackItem0 = context.call_private_function(from, is_valid_selector, [message_field]);
assert(_callStackItem0[0] == is_valid_selector);
}

// Reduce user balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[6] = recipients[2];
serialised_args[7] = spend_note_offset as Field;

context.call_private_function(self.address, 0xbf748730, serialised_args)
context.call_private_function(self.address, 0x88bd156f, serialised_args)
}


Expand All @@ -50,7 +50,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = owner;

context.call_private_function(self.address, 0xa4fa3a6f, serialised_args)
context.call_private_function(self.address, 0x7ecb218a, serialised_args)
}


Expand All @@ -66,7 +66,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[1] = secret;
serialised_args[2] = owner;

context.call_private_function(self.address, 0xd68b55c1, serialised_args)
context.call_private_function(self.address, 0x9f7bacc8, serialised_args)
}


Expand All @@ -82,7 +82,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[2] = secrets[0];
serialised_args[3] = secrets[1];

context.call_private_function(self.address, 0x720f5cc9, serialised_args)
context.call_private_function(self.address, 0xcaf1f505, serialised_args)
}


Expand All @@ -96,7 +96,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = owner;

context.call_private_function(self.address, 0x1535439c, serialised_args)
context.call_private_function(self.address, 0x1dc9c3c0, serialised_args)
}


Expand All @@ -110,7 +110,7 @@ impl PrivateTokenAirdropPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = recipient;

context.call_private_function(self.address, 0xc0888d22, serialised_args)
context.call_private_function(self.address, 0x61dd7032, serialised_args)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl PrivateTokenPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = owner;

context.call_private_function(self.address, 0x1535439c, serialised_args)
context.call_private_function(self.address, 0x1dc9c3c0, serialised_args)
}


Expand All @@ -42,7 +42,7 @@ impl PrivateTokenPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = recipient;

context.call_private_function(self.address, 0xc0888d22, serialised_args)
context.call_private_function(self.address, 0x61dd7032, serialised_args)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ contract SchnorrAuthWitnessAccount {
payload.serialize(),
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)[0];
let witness = get_auth_witness(message_hash);
assert(recover_address(message_hash, witness) == context.this_address());
_inner_is_valid(message_hash, context.this_address());
payload.execute_calls(&mut context);
}

// keccak256("is_valid(field)") >> 224 -> 0x29d25ca9
#[aztec(private)]
fn is_valid(
message_hash: Field
) -> Field {
_inner_is_valid(message_hash, context.this_address());
0xe86ab4ff
}

fn _inner_is_valid(
message_hash: Field,
address: Field,
) {
let witness = get_auth_witness(message_hash);
assert(recover_address(message_hash, witness) == context.this_address());
0x29d25ca9
assert(recover_address(message_hash, witness) == address, "Invalid signature");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl TestPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = secretHash;

context.call_public_function(self.address, 0xbac98727, serialised_args)
context.call_public_function(self.address, 0x1c031d17, serialised_args)
}


Expand All @@ -63,7 +63,7 @@ impl TestPrivateContextInterface {
serialised_args[0] = amount;
serialised_args[1] = secretHash;

context.call_public_function(self.address, 0x42040a24, serialised_args)
context.call_public_function(self.address, 0x0217ef40, serialised_args)
}


Expand All @@ -75,7 +75,7 @@ impl TestPrivateContextInterface {
let mut serialised_args = [0; 1];
serialised_args[0] = aztec_address;

context.call_private_function(self.address, 0xaf15a45f, serialised_args)
context.call_private_function(self.address, 0xe5df1726, serialised_args)
}


Expand All @@ -87,7 +87,7 @@ impl TestPrivateContextInterface {
let mut serialised_args = [0; 1];
serialised_args[0] = address;

context.call_private_function(self.address, 0x88f0753b, serialised_args)
context.call_private_function(self.address, 0x553aaad4, serialised_args)
}


Expand Down Expand Up @@ -119,7 +119,7 @@ impl TestPrivateContextInterface {
let mut serialised_args = [0; 1];
serialised_args[0] = time;

context.call_public_function(self.address, 0xfff6026c, serialised_args)
context.call_public_function(self.address, 0x57587e4d, serialised_args)
}


Expand Down Expand Up @@ -152,7 +152,7 @@ impl TestPrivateContextInterface {
serialised_args[15] = aDeepStruct.manyNotes[2].amount;
serialised_args[16] = aDeepStruct.manyNotes[2].secretHash;

context.call_private_function(self.address, 0x81d7c118, serialised_args)
context.call_private_function(self.address, 0x7c97ca29, serialised_args)
}

}
Expand Down Expand Up @@ -182,7 +182,7 @@ impl TestPublicContextInterface {
serialised_args[0] = amount;
serialised_args[1] = secretHash;

context.call_public_function(self.address, 0xbac98727, serialised_args)
context.call_public_function(self.address, 0x1c031d17, serialised_args)
}


Expand All @@ -196,7 +196,7 @@ impl TestPublicContextInterface {
serialised_args[0] = amount;
serialised_args[1] = secretHash;

context.call_public_function(self.address, 0x42040a24, serialised_args)
context.call_public_function(self.address, 0x0217ef40, serialised_args)
}


Expand All @@ -208,7 +208,7 @@ impl TestPublicContextInterface {
let mut serialised_args = [0; 1];
serialised_args[0] = time;

context.call_public_function(self.address, 0xfff6026c, serialised_args)
context.call_public_function(self.address, 0x57587e4d, serialised_args)
}

}
Expand Down

0 comments on commit a4ebc27

Please sign in to comment.