Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created February 19, 2013 04:05
Show Gist options
  • Save chris-martin/4983021 to your computer and use it in GitHub Desktop.
Save chris-martin/4983021 to your computer and use it in GitHub Desktop.
case class Mod(q: BigInt) {
def apply(x: BigInt): BigIntModQ = x
class BigIntModQ private (val $: BigInt) {
def pow(exp: BigInt): BigIntModQ = $ modPow (exp, q)
}
object BigIntModQ {
implicit def apply(x: BigInt): BigIntModQ = new BigIntModQ(x mod q)
def unapply(x: BigIntModQ): BigInt = x.$
}
implicit object BigIntModQIsIntegral extends Integral[BigIntModQ] with Ordering[BigIntModQ] {
def plus(x: BigIntModQ, y: BigIntModQ): BigIntModQ = x.$ + y.$
def minus(x: BigIntModQ, y: BigIntModQ): BigIntModQ = x.$ - y.$
def times(x: BigIntModQ, y: BigIntModQ): BigIntModQ = x.$ * y.$
def quot(x: BigIntModQ, y: BigIntModQ): BigIntModQ = x.$ * (y.$ modInverse q) mod q
def rem(x: BigIntModQ, y: BigIntModQ): BigIntModQ = 0: BigInt
def negate(x: BigIntModQ): BigIntModQ = -x.$
def fromInt(x: Int): BigIntModQ = x: BigInt
def toInt(x: BigIntModQ): Int = x.$.intValue()
def toLong(x: BigIntModQ): Long = x.$.longValue()
def toFloat(x: BigIntModQ): Float = x.$.floatValue()
def toDouble(x: BigIntModQ): Double = x.$.doubleValue()
def compare(x: BigIntModQ, y: BigIntModQ): Int = x.$ compare y.$
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment