-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Pablo Deymonnaz <[email protected]>
- Loading branch information
1 parent
b2f955a
commit 2fe319f
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |