Skip to content

Commit

Permalink
class Polynomial (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Pablo Deymonnaz <[email protected]>
  • Loading branch information
pablodeymo and pablodeymo authored Sep 26, 2023
1 parent b2f955a commit 2fe319f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions evm_bridge/src/polynomial.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Field, Scalar } from "o1js";
import { Polynomial } from "./polynomial.js";

test("Evaluate polynomial", () => {
const coef = [Scalar.from(1), Scalar.from(2), Scalar.from(3)];
const p = new Polynomial(coef);
const x = Scalar.from(4);

let evaluation = p.evaluate(x);
let expected = Scalar.from(27);
expect(expected).toEqual(evaluation);
});
17 changes: 17 additions & 0 deletions evm_bridge/src/polynomial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Scalar } from "o1js"

export class Polynomial {
coef: Array<Scalar>

constructor(coef: Array<Scalar>) {
this.coef = coef
}

evaluate(x: Scalar): Scalar {
let result = Scalar.from(0)
for (let i = 0; i < this.coef.length; i++) {
result = result.mul(x).add(this.coef[i])
}
return result
}
}

0 comments on commit 2fe319f

Please sign in to comment.