Skip to content

Commit fb1fdfd

Browse files
committed
🔍 Key HumanID with its least significant 16 bits in Merkle Tree. (fixes #15)
1 parent a5ef688 commit fb1fdfd

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

mina/examples/Airdrop.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
method,
66
state
77
} from "o1js";
8-
import { EmptyRoot, HumanIDWitness, Signatures, authenticate, requireUnique } from "../humanIDv1";
8+
import { EmptyRoot, HumanIDWitness, Signatures, acceptHumanIDv1 } from "../humanIDv1";
99

1010
/**
1111
* Example airdrop zkApp, which gives 10 MINA rewards to the first 1000
@@ -24,8 +24,7 @@ class Airdrop extends SmartContract {
2424
sigs: Signatures,
2525
witness: HumanIDWitness,
2626
) {
27-
authenticate(humanIDv1, sigs, witness.calculateIndex());
28-
requireUnique(this.treeRoot, witness);
27+
acceptHumanIDv1(humanIDv1, sigs, this.treeRoot, witness);
2928
}
3029
}
3130

mina/humanIDv1.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Signatures extends Struct({
88

99
class HumanIDWitness extends MerkleWitness(16) { }
1010

11-
const requireUnique = (treeRoot: State<Field>, witness: HumanIDWitness) => {
11+
const addToMerkleTree = (treeRoot: State<Field>, witness: HumanIDWitness) => {
1212
const currentTreeRoot = treeRoot.getAndRequireEquals();
1313
currentTreeRoot.assertEquals(
1414
witness.calculateRoot(Field(0)),
@@ -17,11 +17,35 @@ const requireUnique = (treeRoot: State<Field>, witness: HumanIDWitness) => {
1717
treeRoot.set(witness.calculateRoot(Field(1)));
1818
}
1919

20-
const authenticate = (humanIDv1: Field, sigs: Signatures, truncatedHumanID: Field) => {
20+
const authenticate = (humanIDv1: Field, sigs: Signatures) => {
2121
// TODO(KimlikDAO-bot)
2222
return true;
2323
}
2424

2525
const EmptyRoot = Field(0xccdd9994da4ffb1d39fcdf50d2c2c6240c423d6ec332865eea991c7bf1e5a9cn);
2626

27-
export { EmptyRoot, HumanIDWitness, Signatures, authenticate, requireUnique };
27+
const requireConsistent = (humanIDv1: Field, truncatedHumanIDv1: Field) => {
28+
humanIDv1.sub(truncatedHumanIDv1).div(65536)
29+
.assertLessThan((1n << 238n) + 0x224698fc094cf91b992d30ed0000n);
30+
}
31+
32+
const acceptHumanIDv1 = (
33+
humanIDv1: Field,
34+
sigs: Signatures,
35+
treeRoot: State<Field>,
36+
witness: HumanIDWitness
37+
) => {
38+
authenticate(humanIDv1, sigs);
39+
requireConsistent(humanIDv1, witness.calculateIndex());
40+
addToMerkleTree(treeRoot, witness);
41+
}
42+
43+
export {
44+
EmptyRoot,
45+
HumanIDWitness,
46+
Signatures,
47+
acceptHumanIDv1,
48+
addToMerkleTree,
49+
authenticate,
50+
requireConsistent
51+
};

0 commit comments

Comments
 (0)