Skip to content

Commit

Permalink
Introduces a new data type called Real64 to more easily deal with dec…
Browse files Browse the repository at this point in the history
…imals. Real64 is based on Int64 and incorporates a SCALE factor that basically moves the decimal point by six positions to the right and rounds the result. This commit also adds more operations to the Vector3 type as well as more test cases.
  • Loading branch information
racampos committed Jan 4, 2024
1 parent aa2a0ad commit e1ef763
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 305 deletions.
23 changes: 23 additions & 0 deletions examples/src/real64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as ZK3D from '../../build/src/index.js';

const n1 = ZK3D.Real64.from(0.123);
const n2 = ZK3D.Real64.from(0.456);

console.log(n1.toString());
console.log(n2.toString());

console.log('Testing addition...');
console.log('Adding n1 and n2...');
console.log(n1.clone().add(n2).toString());

console.log('Testing subtraction...');
console.log('Subtracting n2 from n1...');
console.log(n1.clone().sub(n2).toString());

console.log('Testing multiplication...');
console.log('Multiplying n1 and n2...');
console.log(n1.clone().mul(n2).toString());

console.log('Testing division...');
console.log('Dividing n1 by n2...');
console.log(n1.clone().div(n2).toString());
154 changes: 71 additions & 83 deletions examples/src/vector_transformation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Field, provable, Provable, Int64 } from 'o1js';
import { Field, provable, Provable } from 'o1js';
import * as ZK3D from '../../build/src/index.js';
import * as THREE from 'three';

// Define test vectors
const v1 = [1, 2, 3];
const v2 = [4, 5, 6];
const v1 = [0.001234, 2.345, 0.3456];
const v2 = [4.567, 0.05678, 6.789];
// Define threejs vectors
const v1t = new THREE.Vector3(v1[0], v1[1], v1[2]);
const v2t = new THREE.Vector3(v2[0], v2[1], v2[2]);
Expand All @@ -13,92 +13,80 @@ const v1z = ZK3D.Vector3.fromNumbers(v1[0], v1[1], v1[2]);
const v2z = ZK3D.Vector3.fromNumbers(v2[0], v2[1], v2[2]);

// Test vector addition
console.log("Testing vector addition...");
console.log("threejs: ", v1t.add(v2t));
console.log("zk3d: ", v1z.add(v2z).toString());
console.log("\nTesting vector addition...");
console.log("threejs: ", v1t.clone().add(v2t));
console.log("zk3d: ", v1z.clone().add(v2z).toString());

// Test vector subtraction
console.log("Testing vector subtraction...");
console.log("threejs: ", v1t.sub(v2t));
console.log("zk3d: ", v1z.sub(v2z));
console.log("\nTesting vector subtraction...");
console.log("threejs: ", v1t.clone().sub(v2t));
console.log("zk3d: ", v1z.clone().sub(v2z).toString());

// Test vector multiplication
console.log("Testing vector multiplication...");
console.log("threejs: ", v1t.multiply(v2t));
console.log("zk3d: ", v1z.multiply(v2z));
console.log("\nTesting vector multiplication...");
console.log("threejs: ", v1t.clone().multiply(v2t));
console.log("zk3d: ", v1z.clone().multiply(v2z).toString());

// Test multiplication by scalar
console.log("Testing multiplication by scalar...");
console.log("\nTesting multiplication by scalar...");
const scalar = 3;
console.log("threejs: ", v1t.multiplyScalar(scalar));
console.log("zk3d: ", v1z.multiplyScalar(Int64.from(scalar)));
console.log("threejs: ", v1t.clone().multiplyScalar(scalar));
console.log("zk3d: ", v1z.clone().multiplyScalar(ZK3D.Real64.from(scalar)).toString());

// Test dot product
// console.log("Testing dot product...");
// console.log("threejs: ", v1t.do .dot(v2t));
// console.log("zk3d: ", v1z.dot(v2z));



// // provable
// let Matrix3x3 = provable([
// [Field, Field, Field],
// [Field, Field, Field],
// [Field, Field, Field],
// ]);
// // Provable.Array -- types somewhat more loosely but can be easier to write
// let Matrix3x4 = Provable.Array(Provable.Array(Field, 4), 3);
// let Matrix4x3 = Provable.Array(Provable.Array(Field, 3), 4);

// /* @param x an n*m matrix, encoded as x[i][k] for row i column k.
// * @param y an m*o matrix, both encoded as y[k][j] for row j column j.
// * Returns an n*o matrix.
// */
// function matrixMul(x: Field[][], y: Field[][]): Field[][] {
// let n = x.length;
// let m = y.length; // has to be === x[0].length
// let o = y[0].length;

// let result: Field[][] = [];

// // Compute the output matrix.
// for (let i = 0; i < n; i++) {
// result[i] = [];
// for (let j = 0; j < o; j++) {
// result[i][j] = Field(0);
// for (let k = 0; k < m; k++) {
// result[i][j] = result[i][j].add(x[i][k].mul(y[k][j]));
// }
// }
// }
// return result;
// }

// function circuit(): Field[][] {
// let x = Provable.witness(Matrix3x4, () => {
// return [
// [Field.random(), Field.random(), Field.random(), Field.random()],
// [Field.random(), Field.random(), Field.random(), Field.random()],
// [Field.random(), Field.random(), Field.random(), Field.random()],
// ];
// });
// let y = Provable.witness(Matrix4x3, () => {
// return [
// [Field.random(), Field.random(), Field.random()],
// [Field.random(), Field.random(), Field.random()],
// [Field.random(), Field.random(), Field.random()],
// [Field.random(), Field.random(), Field.random()],
// ];
// });
// return matrixMul(x, y);
// }

// let { rows } = Provable.constraintSystem(circuit);
// let result: Field[][];
// Provable.runAndCheck(() => {
// let result_ = circuit();
// Provable.asProver(() => {
// result = result_.map((x) => x.map((y) => y.toConstant()));
// });
// });
// console.log({ rows, result: Matrix3x3.toJSON(result!) });
console.log("\nTesting dot product...");
console.log("threejs: ", v1t.dot(v2t));
console.log("zk3d: ", v1z.clone().dot(v2z).toString());

// Test cross product
console.log("\nTesting cross product...");
console.log("threejs: ", v1t.clone().cross(v2t));
console.log("zk3d: ", v1z.clone().cross(v2z).toString());

// Test normalization
console.log("\nTesting normalization...");
console.log("threejs: ", v1t.clone().normalize());
console.log("zk3d: ", v1z.clone().normalizeSq().toString());

// Test length
console.log("\nTesting length squared...");
console.log("threejs: ", v1t.clone().lengthSq());
console.log("zk3d: ", v1z.clone().lengthSq().toString());

// Test distanceToSquared
console.log("\nTesting distanceToSquared...");
console.log("threejs: ", v1t.clone().distanceToSquared(v2t));
console.log("zk3d: ", v1z.clone().distanceToSquared(v2z).toString());

// Test lerp
console.log("\nTesting lerp...");
const alpha = 0.5;
console.log("threejs: ", v1t.clone().lerp(v2t, alpha));
console.log("zk3d: ", v1z.clone().lerp(v2z, ZK3D.Real64.from(alpha)).toString());

// Test lerpVectors
console.log("\nTesting lerpVectors...");
console.log("threejs: ", v1t.clone().lerpVectors(v2t, v1t, alpha));
console.log("zk3d: ", v1z.clone().lerpVectors(v2z, v1z, ZK3D.Real64.from(alpha)).toString());

// Test negate
console.log("\nTesting negate...");
console.log("threejs: ", v1t.clone().negate());
console.log("zk3d: ", v1z.clone().negate().toString());

// Test divide
console.log("\nTesting divide...");
console.log("threejs: ", v1t.clone().divide(v2t));
console.log("zk3d: ", v1z.clone().divide(v2z).toString());

// Test divideScalar
console.log("\nTesting divideScalar...");
console.log("threejs: ", v1t.clone().divideScalar(scalar));
console.log("zk3d: ", v1z.clone().divideScalar(ZK3D.Real64.from(scalar)).toString());

// Test applyMatrix4
// console.log("\nTesting applyMatrix4...");
// const mt = new THREE.Matrix4();
// mt.makeRotationX(0.5);
// console.log("threejs: ", v1t.clone().applyMatrix4(mt));
// const mz = new ZK3D.Matrix4 .set(mt.elements[0], mt.elements[1], mt.elements[2], mt.elements[3], mt.elements[4], mt.elements[5], mt.elements[6], mt.elements[7], mt.elements[8], mt.elements[9], mt.elements[10], mt.elements[11], mt.elements[12], mt.elements[13], mt.elements[14], mt.elements[15]);
Loading

0 comments on commit e1ef763

Please sign in to comment.