Skip to content

Commit

Permalink
Merge pull request #249 from privacy-scaling-explorations/fix-pow
Browse files Browse the repository at this point in the history
fix(utils): fix pow for negative exponent
  • Loading branch information
cedoor committed Apr 18, 2024
2 parents a2df127 + 41b4fb8 commit 6f78156
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/utils/src/f1-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ export default class F1Field {
return this.one
}

if (e < 0n) {
base = this.inv(base)
e = -e
}

const n = scalar.bits(e)

if (n.length === 0) {
Expand Down
6 changes: 4 additions & 2 deletions packages/utils/tests/f1-field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("F1Field", () => {
expect(field.isZero(b)).toBeFalsy()
})

it("Should pow into the finite field", () => {
it("Should pow into the finite field", async () => {
const a = field.e(0n)
const b = field.e(1n)
const c = field.e(2n)
Expand All @@ -120,6 +120,8 @@ describe("F1Field", () => {
expect(field.pow(b, c)).toBe(1n)
expect(field.pow(a, b)).toBe(0n)
expect(field.pow(a, d)).toBe(0n)
expect(field.pow(d, a)).toBe(1n)
expect(field.pow(0n, -1n)).toBe(0n)
expect(field.pow(2n, -1n)).toBe(field.inv(2n))
expect(field.pow(5n, -30n)).toBe(1n)
})
})

0 comments on commit 6f78156

Please sign in to comment.