Skip to content

Commit a91a955

Browse files
feat: test for ofac,date and olderthan
1 parent ed841d7 commit a91a955

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { expect } from 'chai';
2+
import { wasm as wasmTester } from 'circom_tester';
3+
import * as path from 'path';
4+
5+
describe('date', async () => {
6+
let circuit;
7+
8+
before(async () => {
9+
circuit = await wasmTester(
10+
path.join(__dirname, "is_valid.test.circom"),
11+
{
12+
include: ['node_modules', "node_modules/@zk-kit/binary-merkle-root.circom/src"],
13+
}
14+
);
15+
});
16+
17+
it('should return true if the year is less', async () => {
18+
const inputs = {
19+
current_date: [1, 9, 9, 4, 0, 4, 1, 2],
20+
validity_date_ascii: "19950412".split("").map((x) => x.charCodeAt(0)),
21+
}
22+
23+
console.log(inputs);
24+
25+
const witness = await circuit.calculateWitness(inputs);
26+
await circuit.checkConstraints(witness);
27+
28+
expect(witness[0]).to.equal(1n);
29+
});
30+
31+
it('should return false if the year is greater', async () => {
32+
const inputs = {
33+
current_date: [1, 9, 9, 6, 0, 4, 1, 2],
34+
validity_date_ascii: "19950412".split("").map((x) => x.charCodeAt(0)),
35+
}
36+
37+
try {
38+
const witness = await circuit.calculateWitness(inputs);
39+
await circuit.checkConstraints(witness);
40+
41+
throw new Error("should return false if the year is greater: FAILED");
42+
} catch (error) {
43+
expect(error).to.exist;
44+
}
45+
});
46+
47+
it('should return true if the year is equal and month is less', async () => {
48+
const inputs = {
49+
current_date: [1, 9, 9, 6, 0, 3, 1, 2],
50+
validity_date_ascii: "19960412".split("").map((x) => x.charCodeAt(0)),
51+
}
52+
53+
const witness = await circuit.calculateWitness(inputs);
54+
await circuit.checkConstraints(witness);
55+
56+
expect(witness[0]).to.equal(1n);
57+
});
58+
59+
it('should return false if the year is equal and month is greater', async () => {
60+
const inputs = {
61+
current_date: [1, 9, 9, 6, 0, 5, 1, 2],
62+
validity_date_ascii: "19960412".split("").map((x) => x.charCodeAt(0)),
63+
}
64+
65+
try {
66+
const witness = await circuit.calculateWitness(inputs);
67+
await circuit.checkConstraints(witness);
68+
69+
throw new Error("should return false if the year is equal and month is greater: FAILED");
70+
} catch (error) {
71+
expect(error).to.exist;
72+
}
73+
});
74+
75+
it('should return true if the year is equal and month is equal and day is less', async () => {
76+
const inputs = {
77+
current_date: [1, 9, 9, 6, 0, 4, 0, 2],
78+
validity_date_ascii: "19960412".split("").map((x) => x.charCodeAt(0)),
79+
}
80+
81+
const witness = await circuit.calculateWitness(inputs);
82+
await circuit.checkConstraints(witness);
83+
84+
expect(witness[0]).to.equal(1n);
85+
});
86+
87+
it('should return false if the year is equal and month is equal and day is greater', async () => {
88+
const inputs = {
89+
current_date: [1, 9, 9, 6, 0, 4, 0, 3],
90+
validity_date_ascii: "19960412".split("").map((x) => x.charCodeAt(0)),
91+
}
92+
93+
try {
94+
const witness = await circuit.calculateWitness(inputs);
95+
await circuit.checkConstraints(witness);
96+
97+
throw new Error("should return false if the year is equal and month is equal and day is greater: FAILED");
98+
} catch (error) {
99+
expect(error).to.exist;
100+
}
101+
});
102+
103+
it ('should return false if the year is equal and month is equal and day is equal', async () => {
104+
const inputs = {
105+
current_date: [1, 9, 9, 6, 0, 4, 0, 2],
106+
validity_date_ascii: "19960412".split("").map((x) => x.charCodeAt(0)),
107+
}
108+
109+
try {
110+
const witness = await circuit.calculateWitness(inputs);
111+
await circuit.checkConstraints(witness);
112+
113+
throw new Error("should return false if the year is equal and month is equal and day is equal: FAILED");
114+
} catch (error) {
115+
expect(error).to.exist;
116+
}
117+
});
118+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from 'chai';
2+
import { wasm as wasmTester } from 'circom_tester';
3+
import * as path from 'path';
4+
5+
describe('isOlderThan', async () => {
6+
let circuit;
7+
8+
before(async () => {
9+
circuit = await wasmTester(
10+
path.join(__dirname, "is_older_than.test.circom"),
11+
{
12+
include: ['node_modules', "node_modules/@zk-kit/binary-merkle-root.circom/src", "node_modules/circomlib/circuits"],
13+
}
14+
);
15+
});
16+
17+
it('should return true if the user is older than the majority', async () => {
18+
console.log(['1', '9', '9', '4', '0', '4', '0', '2'].map((x) => x.charCodeAt(0)));
19+
const inputs = {
20+
majorityASCII: [48, 48, 48 + 2], //2 years old
21+
currDate: [1, 9, 9, 6, 0, 4, 0, 2],
22+
birthDateASCII: ['1', '9', '9', '4', '0', '4', '0', '1'].map((x) => x.charCodeAt(0)),
23+
};
24+
25+
const witness = await circuit.calculateWitness(inputs);
26+
await circuit.checkConstraints(witness);
27+
28+
const output = await circuit.getOutput(witness, ['out']);
29+
expect(output.out).to.equal('1');
30+
});
31+
32+
it("should not return false if the user is younger than the majority", async () => {
33+
const inputs = {
34+
majorityASCII: [48, 48, 48 + 2], //2 years old
35+
currDate: [1, 9, 9, 5, 0, 4, 0, 2],
36+
birthDateASCII: ['1', '9', '9', '4', '0', '4', '0', '2'].map((x) => x.charCodeAt(0)),
37+
};
38+
39+
const witness = await circuit.calculateWitness(inputs);
40+
await circuit.checkConstraints(witness);
41+
42+
const output = await circuit.getOutput(witness, ['out']);
43+
expect(output.out).to.equal('0');
44+
});
45+
46+
it('should not return true if the user birthdate is in the majority year but the current date is not', async () => {
47+
const inputs = {
48+
majorityASCII: [48, 48, 48 + 2], //2 years old
49+
currDate: [1, 9, 9, 6, 0, 4, 0, 1],
50+
birthDateASCII: ['1', '9', '9', '4', '0', '4', '0', '3'].map((x) => x.charCodeAt(0)),
51+
};
52+
53+
const witness = await circuit.calculateWitness(inputs);
54+
await circuit.checkConstraints(witness);
55+
56+
const output = await circuit.getOutput(witness, ['out']);
57+
expect(output.out).to.equal('0');
58+
});
59+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pragma circom 2.1.9;
2+
3+
include "../../../../circuits/utils/selfrica/date/isOlderThan.circom";
4+
5+
component main = IsOlderThan();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include "../../../../circuits/utils/selfrica/date/isValid.circom";
2+
3+
component main = IsValidFullYear();
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import { expect } from 'chai';
2+
import { wasm as wasmTester } from 'circom_tester';
3+
import * as path from 'path';
4+
import { generateCircuitInputsOfac, NON_OFAC_DUMMY_INPUT, OFAC_DUMMY_INPUT} from "../../../../../common/src/utils/selfrica/generateInputs";
5+
import { serializeSmileData } from "../../../../../common/src/utils/selfrica/types";
6+
import { SMT } from '@openpassport/zk-kit-smt';
7+
import { poseidon2 } from 'poseidon-lite';
8+
import nameAndDobjson from '../../../consts/ofac/nameAndDobSelfricaSMT.json';
9+
import nameAndYobjson from '../../../consts/ofac/nameAndYobSelfricaSMT.json';
10+
11+
describe('OFAC - Name and DOB match', async function() {
12+
this.timeout(10000);
13+
let circuit;
14+
let namedob_smt = new SMT(poseidon2, true);
15+
let proofLevel = 2;
16+
17+
before(async () => {
18+
circuit = await wasmTester(
19+
path.join(__dirname, "ofac_name_dob_selfrica.test.circom"),
20+
{
21+
include: [
22+
'node_modules',
23+
'./node_modules/@zk-kit/binary-merkle-root.circom/src',
24+
'./node_modules/circomlib/circuits',
25+
],
26+
}
27+
);
28+
29+
namedob_smt.import(nameAndDobjson);
30+
});
31+
32+
it('should compile and load the circuit', async () => {
33+
expect(circuit).to.not.be.undefined;
34+
});
35+
36+
it('should return 0 if the person is in the ofac list', async () => {
37+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
38+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, namedob_smt, proofLevel);
39+
const inputs = {
40+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
41+
...ofacInputs,
42+
};
43+
44+
const witness = await circuit.calculateWitness(inputs);
45+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
46+
expect(ofacCheckResult).to.equal('0');
47+
});
48+
49+
it('should return 1 if the person is not in the ofac list', async () => {
50+
const dummy_smile_input = serializeSmileData(NON_OFAC_DUMMY_INPUT);
51+
const ofacInputs = generateCircuitInputsOfac(NON_OFAC_DUMMY_INPUT, namedob_smt, proofLevel);
52+
const inputs = {
53+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
54+
...ofacInputs,
55+
};
56+
57+
const witness = await circuit.calculateWitness(inputs);
58+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
59+
expect(ofacCheckResult).to.equal('1');
60+
});
61+
62+
it("should return 0 if the internal computed merkle root is wrong (wrong leaf key)", async () => {
63+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
64+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, namedob_smt, proofLevel);
65+
const inputs = {
66+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
67+
smt_leaf_key: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(),
68+
...ofacInputs,
69+
};
70+
71+
const witness = await circuit.calculateWitness(inputs);
72+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
73+
expect(ofacCheckResult).to.equal('0');
74+
});
75+
76+
it("should return 0 if the internal computed merkle root is wrong (wrong siblings)", async () => {
77+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
78+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, namedob_smt, proofLevel);
79+
ofacInputs.smt_siblings[0] = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString();
80+
const inputs = {
81+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
82+
...ofacInputs,
83+
};
84+
85+
const witness = await circuit.calculateWitness(inputs);
86+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
87+
expect(ofacCheckResult).to.equal('0');
88+
});
89+
90+
it("should return 0 if the merkle root is wrong", async () => {
91+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
92+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, namedob_smt, proofLevel);
93+
const inputs = {
94+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
95+
smt_root: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(),
96+
...ofacInputs,
97+
};
98+
99+
const witness = await circuit.calculateWitness(inputs);
100+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
101+
expect(ofacCheckResult).to.equal('0');
102+
});
103+
});
104+
105+
describe("OFAC - Name and YOB match", async function() {
106+
this.timeout(10000);
107+
let circuit;
108+
let nameyob_smt = new SMT(poseidon2, true);
109+
let proofLevel = 1;
110+
111+
before(async () => {
112+
circuit = await wasmTester(
113+
path.join(__dirname, "ofac_name_yob_selfrica.test.circom"),
114+
{
115+
include: [
116+
'node_modules',
117+
'./node_modules/@zk-kit/binary-merkle-root.circom/src',
118+
'./node_modules/circomlib/circuits',
119+
],
120+
}
121+
);
122+
123+
nameyob_smt.import(nameAndYobjson);
124+
});
125+
126+
it('should compile and load the circuit', async () => {
127+
expect(circuit).to.not.be.undefined;
128+
});
129+
130+
it('should return 0 if the person is in the ofac list', async () => {
131+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
132+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, nameyob_smt, proofLevel);
133+
const inputs = {
134+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
135+
...ofacInputs,
136+
};
137+
138+
const witness = await circuit.calculateWitness(inputs);
139+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
140+
expect(ofacCheckResult).to.equal('0');
141+
});
142+
143+
it('should return 1 if the person is not in the ofac list', async () => {
144+
const dummy_smile_input = serializeSmileData(NON_OFAC_DUMMY_INPUT);
145+
const ofacInputs = generateCircuitInputsOfac(NON_OFAC_DUMMY_INPUT, nameyob_smt, proofLevel);
146+
const inputs = {
147+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
148+
...ofacInputs,
149+
};
150+
151+
const witness = await circuit.calculateWitness(inputs);
152+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
153+
expect(ofacCheckResult).to.equal('1');
154+
});
155+
156+
it("should return 0 if the internal computed merkle root is wrong (wrong leaf key)", async () => {
157+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
158+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, nameyob_smt, proofLevel);
159+
const inputs = {
160+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
161+
smt_leaf_key: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(),
162+
...ofacInputs,
163+
};
164+
165+
const witness = await circuit.calculateWitness(inputs);
166+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
167+
expect(ofacCheckResult).to.equal('0');
168+
});
169+
170+
it("should return 0 if the internal computed merkle root is wrong (wrong siblings)", async () => {
171+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
172+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, nameyob_smt, proofLevel);
173+
ofacInputs.smt_siblings[0] = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString();
174+
const inputs = {
175+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
176+
...ofacInputs,
177+
};
178+
179+
const witness = await circuit.calculateWitness(inputs);
180+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
181+
expect(ofacCheckResult).to.equal('0');
182+
});
183+
184+
it("should return 0 if the merkle root is wrong", async () => {
185+
const dummy_smile_input = serializeSmileData(OFAC_DUMMY_INPUT);
186+
const ofacInputs = generateCircuitInputsOfac(OFAC_DUMMY_INPUT, nameyob_smt, proofLevel);
187+
const inputs = {
188+
smile_data: dummy_smile_input.split('').map((x) => x.charCodeAt(0)),
189+
smt_root: BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString(),
190+
...ofacInputs,
191+
};
192+
193+
const witness = await circuit.calculateWitness(inputs);
194+
const ofacCheckResult = (await circuit.getOutput(witness, ['ofacCheckResult'])).ofacCheckResult;
195+
expect(ofacCheckResult).to.equal('0');
196+
});
197+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pragma circom 2.1.9;
2+
3+
include "../../../../circuits/utils/selfrica/disclose/ofac/ofac_name_dob_selfrica.circom";
4+
5+
component main = OFAC_NAME_DOB_SELFRICA(64);

0 commit comments

Comments
 (0)