Skip to content

Commit

Permalink
fix randomFr
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincharm committed Aug 11, 2024
1 parent 5123f83 commit bdbcdea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ export function randomFq(): Fq {
}

export function randomFr(): Fr {
return new Fr(randomBigIntModP(31, R))
return new Fr(randomBigIntModP(32, R))
}

export function randomBigIntModP(bytes: number, p: bigint): bigint {
const randUpperBound = 2n ** BigInt(bytes * 8)
if (randUpperBound < p) {
throw new Error(`Insufficient bytes for modulus ${p}`)
}
const upperBound = randUpperBound - (randUpperBound % p)
let rand: bigint
while ((rand = randomBigInt(bytes)) >= upperBound) {}
Expand Down
6 changes: 6 additions & 0 deletions test/ff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai'
import fs from 'node:fs/promises'
import path from 'node:path'
import { Fq, Fq12, Fq2, Fq6, Fr } from '../src/ff'
import { randomFr } from '../src/utils'

async function readTestVectors<S, P>(
name: string,
Expand Down Expand Up @@ -98,6 +99,11 @@ const reviveFq12 = ([x, y, z]: SFq12[]): PFq12Vector => [

describe('finite fields', () => {
describe('Fr', () => {
it('random', () => {
const r = randomFr()
expect(r.value).to.not.equal(0n)
})

it('inv', () => {
const expected: [Fr, Fr][] = [
[1n, 1n],
Expand Down

0 comments on commit bdbcdea

Please sign in to comment.