Skip to content

Commit

Permalink
fix: circuit rounding tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonDoge committed Dec 30, 2024
1 parent bcbc5a9 commit c75fe97
Show file tree
Hide file tree
Showing 14 changed files with 1,430 additions and 1,439 deletions.
52 changes: 26 additions & 26 deletions anchor/programs/darklake/src/constants/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub const VERIFYINGKEY: Groth16Verifyingkey = Groth16Verifyingkey {
nr_pubinputs: 8,

vk_alpha_g1: [
33,190,193,128,200,187,115,0,148,236,193,115,244,53,212,91,192,103,89,218,137,18,203,240,248,154,157,92,69,172,123,96,
47,81,245,254,170,174,67,14,118,179,186,6,182,108,177,181,243,129,153,205,80,168,236,61,204,142,253,107,67,188,249,191,
38,184,63,22,160,251,25,40,172,141,219,240,104,75,194,162,22,212,67,22,214,188,227,212,42,112,220,129,38,40,114,67,
24,44,75,233,223,247,238,116,188,242,181,29,244,107,144,68,9,225,245,112,139,19,51,88,88,247,248,226,181,8,200,158,
],

vk_beta_g2: [
16,57,249,232,210,207,221,88,126,6,148,205,84,13,39,81,97,12,198,156,102,15,158,61,70,2,37,68,177,209,215,207,
20,38,208,122,51,25,254,153,32,32,150,183,46,14,14,61,141,81,110,26,87,150,6,232,31,193,60,205,203,108,177,123,
31,168,149,175,112,55,105,43,161,0,106,78,14,246,192,173,91,147,186,48,194,175,102,113,20,96,107,114,59,75,37,79,
24,132,57,195,94,100,178,58,226,27,80,253,184,54,54,134,51,231,253,180,202,16,150,187,9,168,53,144,168,143,158,149,
15,211,169,54,92,239,239,219,208,17,211,144,94,2,213,176,131,50,226,129,165,10,10,20,11,212,168,6,129,86,230,83,
4,141,232,125,225,15,217,34,86,114,131,94,127,143,173,215,204,161,3,190,195,244,6,200,222,143,193,175,167,126,193,198,
43,123,148,120,187,246,255,9,59,199,80,207,199,142,225,139,160,64,188,75,218,108,122,101,197,96,233,136,137,189,69,141,
0,244,197,115,250,54,155,114,120,236,73,186,159,88,245,117,22,129,83,187,96,250,69,103,251,97,168,176,129,14,162,140,
],

vk_gamme_g2: [
Expand All @@ -23,44 +23,44 @@ pub const VERIFYINGKEY: Groth16Verifyingkey = Groth16Verifyingkey {
],

vk_delta_g2: [
31,28,254,198,255,36,70,3,139,254,99,151,4,37,142,81,118,171,213,94,254,204,103,132,31,223,207,189,123,68,102,204,
24,38,31,223,163,118,170,133,66,171,156,59,231,83,51,141,224,246,28,166,182,98,101,55,139,16,220,183,25,127,64,78,
21,232,227,11,124,108,159,16,130,202,228,81,252,205,108,156,49,188,143,8,110,187,169,59,192,105,191,147,98,125,225,51,
13,249,253,182,243,186,141,179,169,30,121,55,48,176,197,197,219,27,163,22,163,164,134,132,178,76,66,184,35,3,148,37,
24,81,210,84,240,199,14,84,168,59,120,173,220,176,159,192,180,150,201,240,25,191,179,108,29,13,18,153,21,119,142,113,
18,46,26,141,236,117,205,249,200,152,252,117,166,235,241,162,192,56,146,125,155,254,163,183,38,33,164,254,105,151,212,108,
42,20,132,163,160,251,97,123,34,12,73,61,225,29,47,56,121,230,46,20,49,15,136,200,71,21,15,253,37,81,30,153,
20,233,214,188,188,214,186,204,201,242,113,245,235,14,4,0,96,98,243,136,210,224,228,157,103,226,255,248,174,51,14,228,
],

vk_ic: &[
[
0,201,105,144,46,237,134,254,151,252,147,99,8,239,46,231,197,208,12,145,124,11,137,187,48,19,203,92,66,161,214,158,
13,116,78,81,90,163,6,239,23,210,100,230,232,28,69,100,195,20,177,253,7,104,14,26,221,57,216,189,29,132,51,16,
17,126,168,252,160,150,154,185,131,49,255,201,175,107,221,62,198,71,139,58,102,135,34,152,233,58,178,144,103,140,117,233,
24,160,117,21,76,86,24,217,21,251,106,1,129,191,119,225,193,164,175,99,31,216,182,234,207,61,104,227,37,46,223,122,
],
[
29,87,131,46,151,194,225,189,88,189,188,66,122,230,239,67,5,33,187,101,10,163,61,108,59,240,215,3,15,62,85,143,
19,245,132,221,53,113,49,139,186,215,54,248,66,111,203,159,51,41,17,159,153,156,80,205,187,54,186,108,143,132,224,214,
16,239,227,207,87,116,53,23,111,72,59,137,154,190,31,228,114,18,124,240,214,76,208,8,175,39,5,158,249,160,43,121,
43,192,108,148,136,36,156,96,158,227,2,18,130,141,50,24,106,225,74,118,146,39,97,83,209,11,47,211,39,115,246,163,
],
[
2,153,136,80,117,0,12,101,237,68,116,34,12,131,44,154,106,68,189,20,235,173,115,138,235,109,34,61,37,130,117,220,
9,55,92,106,90,245,73,175,244,240,4,111,107,185,26,24,98,28,66,108,238,208,15,11,145,227,114,101,58,59,29,98,
40,120,253,164,36,150,31,247,49,228,252,126,236,101,66,214,217,183,189,97,46,199,118,138,157,52,41,216,138,0,153,41,
8,156,16,131,219,247,68,35,49,39,40,215,21,31,33,8,72,105,163,254,60,130,189,204,194,162,37,251,249,199,157,160,
],
[
14,174,28,206,145,39,15,255,247,254,220,92,84,128,200,145,18,5,234,16,22,72,168,125,79,228,20,151,136,110,225,35,
7,49,27,195,130,225,102,194,10,29,34,153,246,197,238,247,204,241,205,57,226,231,148,30,97,169,192,146,187,145,71,31,
0,178,253,253,198,49,159,114,224,137,120,202,88,129,214,118,15,128,110,38,226,109,155,44,71,147,228,20,214,238,141,126,
20,116,235,216,197,208,145,80,187,64,239,217,132,119,14,118,223,205,144,44,94,135,95,200,50,118,188,121,192,34,82,38,
],
[
7,26,46,103,23,155,19,104,101,215,223,85,33,191,147,156,211,254,130,199,43,226,204,52,9,184,13,219,186,227,193,26,
28,218,13,233,15,205,156,223,24,50,126,67,149,205,65,117,63,184,14,140,108,227,163,30,71,218,67,197,143,139,86,216,
46,162,160,87,105,93,173,66,31,9,98,161,182,89,16,154,44,148,41,230,12,41,255,75,0,141,112,182,62,232,160,196,
31,62,27,162,103,81,105,142,15,133,175,156,64,166,10,155,96,180,23,14,185,198,183,84,145,47,210,241,194,238,19,125,
],
[
22,175,37,80,164,62,30,157,95,197,207,56,13,208,175,235,176,56,0,213,43,20,52,13,79,218,212,243,107,55,132,251,
33,91,92,174,91,154,172,88,74,214,45,92,35,172,54,16,55,252,130,50,247,192,96,244,185,166,79,100,166,215,187,102,
33,157,136,37,32,192,242,208,77,85,220,105,72,192,109,143,172,188,15,56,176,145,80,72,41,140,139,191,89,9,119,133,
48,68,131,96,209,144,81,113,179,251,58,255,122,44,138,44,39,78,237,159,196,50,61,8,246,216,197,186,48,213,10,40,
],
[
30,36,95,199,226,183,77,27,226,211,38,164,198,228,149,83,113,151,102,73,0,60,122,240,176,118,112,153,216,150,168,235,
30,26,97,98,103,4,102,95,45,221,191,252,119,189,13,127,220,53,40,66,173,149,73,252,234,66,136,10,234,183,33,170,
31,122,183,237,180,113,247,20,14,198,0,213,207,177,163,183,193,253,136,224,215,160,234,132,145,250,117,117,172,208,214,209,
33,181,75,192,175,153,239,163,14,248,231,167,35,73,116,194,210,160,2,5,12,128,184,7,55,3,253,5,100,164,34,5,
],
[
2,102,211,153,2,118,43,100,121,159,76,88,181,14,121,51,120,116,83,227,223,246,57,231,161,179,61,71,126,26,12,35,
36,245,35,100,176,184,162,144,223,237,127,55,142,250,46,64,149,171,147,29,79,196,46,251,163,229,55,180,208,59,148,91,
33,102,106,25,112,136,57,74,244,156,204,143,149,133,21,79,52,76,89,17,228,255,92,43,67,170,149,38,23,74,116,211,
3,45,200,19,112,94,248,79,117,197,190,222,26,31,111,205,249,106,30,223,236,148,188,231,204,155,140,79,234,55,142,184,
],
]
};
60 changes: 19 additions & 41 deletions anchor/tests/jest/circuit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';

// Add this function at the top of your test file, outside of any describe blocks
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
// currently not used due to no flexibility in amount received allowed
function isWithinTolerance(
actual: bigint,
expected: bigint,
Expand Down Expand Up @@ -46,15 +47,9 @@ describe('ZK Constant Sum AMM Swap', () => {
const newReserveY = circuit.symbols['main.newReserveY'];
const amountReceived = circuit.symbols['main.amountReceived'];

expect(isWithinTolerance(BigInt(witness[newReserveX.varIdx]), 1100n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[newReserveY.varIdx]), 909n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[amountReceived.varIdx]), 91n)).toBe(
true,
);
expect(BigInt(witness[newReserveX.varIdx])).toEqual(1100n);
expect(BigInt(witness[newReserveY.varIdx])).toEqual(910n);
expect(BigInt(witness[amountReceived.varIdx])).toEqual(90n);
});

it('should perform a valid swap with larger values', async () => {
Expand All @@ -75,15 +70,9 @@ describe('ZK Constant Sum AMM Swap', () => {
const newReserveY = circuit.symbols['main.newReserveY'];
const amountReceived = circuit.symbols['main.amountReceived'];

expect(
isWithinTolerance(BigInt(witness[newReserveX.varIdx]), 1200000n),
).toBe(true);
expect(
isWithinTolerance(BigInt(witness[newReserveY.varIdx]), 1741667n),
).toBe(true);
expect(
isWithinTolerance(BigInt(witness[amountReceived.varIdx]), 158333n),
).toBe(true);
expect(BigInt(witness[newReserveX.varIdx])).toEqual(1200000n);
expect(BigInt(witness[newReserveY.varIdx])).toEqual(1741667n);
expect(BigInt(witness[amountReceived.varIdx])).toEqual(158333n);
});

// Add a new test for swapping Y to X
Expand All @@ -105,15 +94,9 @@ describe('ZK Constant Sum AMM Swap', () => {
const newReserveY = circuit.symbols['main.newReserveY'];
const amountReceived = circuit.symbols['main.amountReceived'];

expect(isWithinTolerance(BigInt(witness[newReserveX.varIdx]), 909n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[newReserveY.varIdx]), 1100n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[amountReceived.varIdx]), 91n)).toBe(
true,
);
expect(BigInt(witness[newReserveX.varIdx])).toEqual(910n);
expect(BigInt(witness[newReserveY.varIdx])).toEqual(1100n);
expect(BigInt(witness[amountReceived.varIdx])).toEqual(90n);
});

it('should round output down if fraction exists', async () => {
Expand All @@ -134,15 +117,9 @@ describe('ZK Constant Sum AMM Swap', () => {
const newReserveY = circuit.symbols['main.newReserveY'];
const amountReceived = circuit.symbols['main.amountReceived'];

expect(isWithinTolerance(BigInt(witness[newReserveX.varIdx]), 909n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[newReserveY.varIdx]), 1100n)).toBe(
true,
);
expect(isWithinTolerance(BigInt(witness[amountReceived.varIdx]), 91n)).toBe(
true,
);
expect(BigInt(witness[newReserveX.varIdx])).toEqual(910n);
expect(BigInt(witness[newReserveY.varIdx])).toEqual(1100n);
expect(BigInt(witness[amountReceived.varIdx])).toEqual(90n);
});
});

Expand All @@ -161,10 +138,11 @@ describe('ReciprocalDivision', () => {

const testCases = [
{ dividend: 1000n, divisor: 10n, expected: 100n },
{ dividend: 1000n, divisor: 3n, expected: 333n },
{ dividend: 1000n, divisor: 3n, expected: 334n },
{ dividend: 1000000n, divisor: 1000n, expected: 1000n },
{ dividend: 1234567n, divisor: 1000n, expected: 1234n },
{ dividend: 1000000000n, divisor: 3n, expected: 333333333n },
{ dividend: 1000000000000001n, divisor: 1000000000000n, expected: 1001n },
{ dividend: 1234567n, divisor: 1000n, expected: 1235n },
{ dividend: 1000000000n, divisor: 3n, expected: 333333334n },
{ dividend: 2000000000000000n, divisor: 1100000n, expected: 1818181819n },
];

Expand All @@ -183,8 +161,8 @@ describe('ReciprocalDivision', () => {
const quotientSymbol = divisionCircuit.symbols['main.quotient'];
const quotient = witness[quotientSymbol.varIdx];

// Allow for small rounding errors
const tolerance = 1n;
// Don't allow any deviation
const tolerance = 0n;
expect(quotient).toBeGreaterThanOrEqual(expected - tolerance);
expect(quotient).toBeLessThanOrEqual(expected + tolerance);
});
Expand Down
15 changes: 11 additions & 4 deletions circuits/division.circom
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma circom 2.0.0;
include "node_modules/circomlib/circuits/bitify.circom";
include "node_modules/circomlib/circuits/comparators.circom";

template ReciprocalDivision(n) {
template ReciprocalDivision(n, isCeil) {
signal input dividend;
signal input divisor;
signal output quotient;
Expand All @@ -15,15 +15,22 @@ template ReciprocalDivision(n) {

// Perform division
signal remainder;
quotient <-- dividend \ divisor;
signal baseQuotient <-- dividend \ divisor;
remainder <-- dividend % divisor;

// Constrain the result
dividend === quotient * divisor + remainder;
dividend === baseQuotient * divisor + remainder;

// Ensure remainder is less than divisor
component lessThan = LessThan(n);
lessThan.in[0] <== remainder;
lessThan.in[1] <== divisor;
lessThan.out === 1;

// Check if remainder is non-zero
component isRemainderZero = IsZero();
isRemainderZero.in <== remainder;

// Add 1 to quotient if remainder is non-zero and isCeil is true
quotient <== baseQuotient + ((1 - isRemainderZero.out) * isCeil);
}
3 changes: 2 additions & 1 deletion circuits/division_test.circom
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pragma circom 2.0.0;

include "division.circom";

component main = ReciprocalDivision(252);
// rounding up
component main = ReciprocalDivision(252, 1);
Binary file modified circuits/pot_cont.ptau
Binary file not shown.
Binary file modified circuits/pot_final.ptau
Binary file not shown.
3 changes: 2 additions & 1 deletion circuits/swap.circom
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ template ZKConstantProductAMM() {
signal newInputReserve <== inputReserve + inputAmount;

// Calculate new output balance (y = k / x)
component division = ReciprocalDivision(252);
// The rounding is up, so that the pool wouldn't lose funds
component division = ReciprocalDivision(252, 1);
division.dividend <== constantProduct;
division.divisor <== newInputReserve;
signal newOutputReserve <== division.quotient;
Expand Down
Binary file modified circuits/swap.r1cs
Binary file not shown.
Loading

0 comments on commit c75fe97

Please sign in to comment.