Skip to content

Commit

Permalink
feat: update API adapters to support hasing (impelment HashableZKLocu…
Browse files Browse the repository at this point in the history
…sAdopter)
  • Loading branch information
iluxonchik committed Jan 11, 2024
1 parent 727c29c commit 1ffa806
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 19 additions & 4 deletions contracts/src/api/adopters/ZKGeoPointToGeoPointAdopter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
See: https://github.com/microsoft/TypeScript/issues/30355
*/

import { Int64 } from "o1js";
import { Field, Int64, Poseidon } from "o1js";
import { InputNumber } from "../Types";
import { ZKLocusAdopter } from "./Interfaces";
import { HashableZKLocusAdopter, ZKLocusAdopter } from "./Interfaces";
import { ZKLatitude } from "../models/ZKLatitude";
import type { ZKGeoPoint } from "../models/ZKGeoPoint";
import { ZKLongitude } from "../models/ZKLongitude";
Expand All @@ -20,7 +20,7 @@ import { GeoPoint } from "../../model/Geography";
export type ZKGeoPointConstructor = new (...args: any[]) => ZKGeoPoint;

export default function <T extends ZKGeoPointConstructor>(Base: T) {
return class extends Base implements ZKLocusAdopter<{ latitude: InputNumber | ZKLongitude; longitude: InputNumber | ZKLongitude; }, { latitude: ZKLatitude; longitude: ZKLongitude; factor: ZKNumber; }, GeoPoint> {
return class extends Base implements HashableZKLocusAdopter<{ latitude: InputNumber | ZKLongitude; longitude: InputNumber | ZKLongitude; }, { latitude: ZKLatitude; longitude: ZKLongitude; factor: ZKNumber; }, GeoPoint, ZKGeoPoint, Field> {

rawValue(): { latitude: InputNumber | ZKLatitude; longitude: InputNumber | ZKLongitude; } {
return this.asRawValue;
Expand Down Expand Up @@ -54,6 +54,21 @@ export default function <T extends ZKGeoPointConstructor>(Base: T) {
factor: factorInt64,
});
}
};

hash(): Field {
return this.toZKValue().hash();
}


static combinedHash(elements: ZKGeoPoint[]): Field {
const hashes: Field[] = elements.map(element => element.toZKValue().hash());

return Poseidon.hash(hashes);
}

combinedHash(otherElements: ZKGeoPoint[]): Field {
const allPoints: ZKGeoPoint[] = [this, ...otherElements];
return this.combinedHash(allPoints);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import { GeoPoint, ThreePointPolygon } from "../../model/Geography";
import type { ZKThreePointPolygon } from "../models/ZKThreePointPolygon";
import { ZKGeoPoint } from "../models/ZKGeoPoint";
import { ZKLocusAdopter } from "./Interfaces";
import { HashableZKLocusAdopter} from "./Interfaces";
import { Field, Poseidon } from "o1js";


export default function <T extends new (...args: any[]) => ZKThreePointPolygon>(Base: T) {
return class extends Base implements ZKLocusAdopter<[ZKGeoPoint, ZKGeoPoint, ZKGeoPoint], [GeoPoint, GeoPoint, GeoPoint], ThreePointPolygon> {
return class extends Base implements HashableZKLocusAdopter<[ZKGeoPoint, ZKGeoPoint, ZKGeoPoint], [GeoPoint, GeoPoint, GeoPoint], ThreePointPolygon, ZKThreePointPolygon, Field> {
rawValue(): [ZKGeoPoint, ZKGeoPoint, ZKGeoPoint] {
return this.vertices;
}
Expand All @@ -38,5 +39,15 @@ export default function <T extends new (...args: any[]) => ZKThreePointPolygon>(

return threePointPolygon;
}

static fromThreePointPolygon(threePointPolygon: ThreePointPolygon): ZKThreePointPolygon {
const vertices = [
ZKGeoPoint.fromGeoPoint(threePointPolygon.vertice1),
ZKGeoPoint.fromGeoPoint(threePointPolygon.vertice2),
ZKGeoPoint.fromGeoPoint(threePointPolygon.vertice3)
];

return new this(vertices[0], vertices[1], vertices[2]);
}
};
}

0 comments on commit 1ffa806

Please sign in to comment.