123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package com.oblivm.backend.circuits.arithmetic;
- import com.oblivm.backend.flexsc.CompEnv;
- public class VectorLib<T> {
- ArithmeticLib<T> lib;
- CompEnv<T> env;
- public VectorLib(CompEnv<T> e, ArithmeticLib<T> lib) {
- env = e;
- this.lib = lib;
- }
- public T[][] xor(T[][] a, T[][] b) {
- IntegerLib<T> ilib = new IntegerLib<T>(lib.getEnv());
- T[][] res = env.newTArray(a.length, 1);
- for (int i = 0; i < a.length; ++i)
- res[i] = ilib.xor(a[i], b[i]);
- return res;
- }
- public T[][] add(T[][] a, T[][] b) {
- T[][] res = env.newTArray(a.length, 1);
- for (int i = 0; i < a.length; ++i)
- res[i] = lib.add(a[i], b[i]);
- return res;
- }
- public T[][] sub(T[][] a, T[][] b) {
- T[][] res = env.newTArray(a.length, 1);
- for (int i = 0; i < a.length; ++i)
- res[i] = lib.sub(a[i], b[i]);
- return res;
- }
- public T[] innerProduct(T[][] a, T[][] b) {
- T[] res = lib.publicValue(0);
- for (int i = 0; i < a.length; ++i)
- res = lib.add(res, lib.multiply(a[i], b[i]));
- return res;
- }
- public T[][] scalarProduct(T[] scalar, T[][] v) {
- T[][] res = env.newTArray(v.length, 1);
- for (int i = 0; i < v.length; ++i)
- res[i] = lib.multiply(scalar, v[i]);
- return res;
- }
- public T[][] projection(T[][] a, T[][] e) {
- T[] ea = innerProduct(e, a);
- T[] ee = innerProduct(e, e);
- T[] scalar = lib.div(ea, ee);
- return scalarProduct(scalar, e);
- }
- public T[][] normalize(T[][] vec) {
- T[] scalar = innerProduct(vec, vec);
- scalar = lib.sqrt(scalar);
- scalar = lib.div(lib.publicValue(1), scalar);
- return scalarProduct(scalar, vec);
- }
- }
|