Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify circuit code #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions app/axiom/average.circuit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
sum,
add,
sub,
mul,
div,
Expand Down Expand Up @@ -38,24 +38,22 @@ export const circuit = async (inputs: CircuitInputs) => {
// Perform the block number validation in the circuit as well
checkLessThan(mul(samples, spacing), inputs.blockNumber);

// Creates a length-8 array of block numbers to sample from, starting from input blockNumber and
// decreasing by the `spacing` size.
const blockNumbers = Array.from(
{length: samples},
(_: any, i: number) => sub(inputs.blockNumber, (spacing * i))
);

// Get all balances for the given address at the block numbers we are sampling from
let balances = [] as CircuitValue256[];
for (const blockNumber of blockNumbers) {
const balance = await getAccount(blockNumber, inputs.address).balance();
balances.push(balance);
// Get account balance at the sample block numbers
let sampledAccounts = new Array(samples);
for (let i = 0; i < samples; i++) {
const sampleBlockNumber: CircuitValue = sub(inputs.blockNumber, mul(spacing, i));
const account = getAccount(sampleBlockNumber, inputs.address);
sampledAccounts[i] = account;
}

// Calculate the total
const total = sum(balances.map((balance: CircuitValue256) => balance.value()));
// Accumulate all of the balances to `total`
let total = witness(0);
for (const account of sampledAccounts) {
const balance: CircuitValue256 = await account.balance();
total = add(total, balance.lo());
}

// Divide by the number of samples to get the average value
// Divide the total amount by the number of samples to get the average value
const average = div(total, samples);

// We call `addToCallback` on all values that we would like to be passed to our contract after the circuit has
Expand Down
6 changes: 3 additions & 3 deletions app/axiom/data/compiled.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"vk": "Ag0AAAAABgAA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S+poOBX7ZkebDlhej7gN0XZryuHfTchTs6y1IMEqCLHKCh+7yZ9BcKjcdWwF6s1o+e3dXv1kgk4xjNZaNAlHknrLr4Of0VGfEW+giUz9/4p676XjNKlqurN18vt/v2QgY=",
"config": {
"k": 13,
"numAdvice": 4,
Expand All @@ -8,7 +8,7 @@
"numLookupBits": 12,
"numVirtualInstance": 2
},
"querySchema": "0x73aa20264324d23b8587d8d33a67b82104fa660a90abc02a6fd5cc72fa07fe51",
"querySchema": "0xddc4deb9373da5dd4e24401b02a8a1cba66203a2d4e36a8a191c3338469be5b9",
"inputSchema": "eyJibG9ja051bWJlciI6IkNpcmN1aXRWYWx1ZSIsImFkZHJlc3MiOiJDaXJjdWl0VmFsdWUifQ==",
"circuit": "Y29uc3QgY2xpZW50XzEgPSBBWElPTV9DTElFTlRfSU1QT1JUCmFzeW5jIChpbnB1dHMpID0+IHsKICAgIC8vIE51bWJlciBvZiBzYW1wbGVzIHRvIHRha2UuIE5vdGUgdGhhdCB0aGlzIG11c3QgYmUgYSBjb25zdGFudCB2YWx1ZSBhbmQgTk9UIGFuIGlucHV0IGJlY2F1c2UgdGhlIHNpemUgb2YgCiAgICAvLyB0aGUgY2lyY3VpdCBtdXN0IGJlIGtub3duIGF0IGNvbXBpbGUgdGltZS4KICAgIGNvbnN0IHNhbXBsZXMgPSA4OwogICAgLy8gTnVtYmVyIG9mIGJsb2NrcyBiZXR3ZWVuIGVhY2ggc2FtcGxlLgogICAgY29uc3Qgc3BhY2luZyA9IDkwMDsKICAgIC8vIFZhbGlkYXRlIHRoYXQgdGhlIGJsb2NrIG51bWJlciBpcyBncmVhdGVyIHRoYW4gdGhlIG51bWJlciBvZiBzYW1wbGVzIHRpbWVzIHRoZSBzcGFjaW5nCiAgICBpZiAoaW5wdXRzLmJsb2NrTnVtYmVyLnZhbHVlKCkgPD0gKHNhbXBsZXMgKiBzcGFjaW5nKSkgewogICAgICAgIHRocm93IG5ldyBFcnJvcigiQmxvY2sgbnVtYmVyI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FRvQ2FsbGJhY2spKGF2ZXJhZ2UpOwp9"
}
6 changes: 6 additions & 0 deletions app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const axiomMain = async (input: UserInput<CircuitInputs>) => {
privateKey: process.env.PRIVATE_KEY_SEPOLIA as string,
mock: false,
});
axiom.setParams({
callback: {
target: "0x4A4e2D8f3fBb3525aD61db7Fc843c9bf097c362e",
extraData: "0x0000000000000000000000000000000000000000000000000000000000000000"
},
})
await axiom.init();
const args = await axiom.prove(input);

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "axiom-quickstart",
"version": "0.2.1",
"version": "0.2.2",
"description": "Axiom Quickstart",
"main": "index.js",
"scripts": {
Expand All @@ -10,8 +10,7 @@
"author": "Intrinsic Technologies",
"license": "ISC",
"dependencies": {
"@axiom-crypto/client": "0.2.2-rc2.2-alpha.1",
"@axiom-crypto/core": "2.3.0-rc2.2",
"@axiom-crypto/client": "0.2.2-rc2.2",
"dotenv": "^16.3.1",
"ethers": "^6.10.0"
},
Expand Down
Loading