diff --git a/.gitignore b/.gitignore index dd9b992..aeb4c66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .DS_Store _bck* -main \ No newline at end of file +tmp +main +*.json diff --git a/poly.nim b/poly.nim index ee0222f..eb7a65b 100644 --- a/poly.nim +++ b/poly.nim @@ -41,7 +41,7 @@ func polyIsZero*(P: Poly) : bool = break return b -func polyEqual*(P, Q: Poly) : bool = +func polyIsEqual*(P, Q: Poly) : bool = let xs : seq[Fr] = P.coeffs ; let n = xs.len let ys : seq[Fr] = Q.coeffs ; let m = ys.len var b = true @@ -106,7 +106,8 @@ func polyScale*(s: Fr, P: Poly): Poly = func polyMulNaive*(P, Q : Poly): Poly = let xs = P.coeffs ; let n1 = xs.len let ys = Q.coeffs ; let n2 = ys.len - var zs : seq[Fr] ; let N = n1 + n2 - 1 + let N = n1 + n2 - 1 + var zs : seq[Fr] = newSeq[Fr](N) for k in 0..=1 ) + var cs : seq[Fr] = newSeq[Fr]( N+1 ) + cs[0] = negFr( oneFr ) + cs[N] = oneFr + return Poly(coeffs: cs) + +type + QuotRem*[T] = object + quot* : T + rem* : T + +# divide by the vanishing polynomial `(x^N - 1)` +# returns the quotient and remainder +func polyQuotRemByVanishing*(P: Poly, N: int): QuotRem[Poly] = + assert( N>=1 ) + let deg : int = polyDegree(P) + let src : seq[Fr] = P.coeffs + var quot : seq[Fr] = newSeq[Fr]( max(1, deg - N + 1) ) + var rem : seq[Fr] = newSeq[Fr]( N ) + + if deg < N: + rem = src + + else: + # compute quot + for j in countdown(deg-N, 0): + if j+N <= deg-N: + quot[j] = src[j+N] + quot[j+N] + else: + quot[j] = src[j+N] + # compute rem + for j in 0..