Skip to content

Commit

Permalink
missing-methods: add CryptoContext.curve, CryptoFacade.encodePoint, a…
Browse files Browse the repository at this point in the history
…nd getCurve
  • Loading branch information
aslesarenko committed Apr 17, 2023
1 parent da65640 commit e991c92
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import java.math.BigInteger

/** A context for cryptographic operations. */
abstract class CryptoContext {
/** The underlying elliptic curve. */
def curve: Curve

/** The characteristics of the underlying finite field. */
def fieldCharacteristic: BigInteger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import java.math.BigInteger

/** JVM implementation of context for cryptographic operations using Bouncycastle. */
class CryptoContextJvm(x9params: X9ECParameters) extends CryptoContext {
private lazy val curve = x9params.getCurve
private lazy val _curve = x9params.getCurve

override def fieldCharacteristic: BigInteger = curve.getField.getCharacteristic
/** The underlying elliptic curve. */
override def curve: Curve = Platform.Curve(_curve)

override def fieldCharacteristic: BigInteger = _curve.getField.getCharacteristic

override def order: BigInteger = x9params.getN

Expand All @@ -17,14 +20,14 @@ class CryptoContextJvm(x9params: X9ECParameters) extends CryptoContext {
}

override def validatePoint(x: BigInteger, y: BigInteger): Ecp = {
Platform.Ecp(curve.validatePoint(x, y))
Platform.Ecp(_curve.validatePoint(x, y))
}

override def infinity(): Ecp = {
Platform.Ecp(curve.getInfinity)
Platform.Ecp(_curve.getInfinity)
}

override def decodePoint(encoded: Array[Byte]): Ecp = {
Platform.Ecp(curve.decodePoint(encoded))
Platform.Ecp(_curve.decodePoint(encoded))
}
}
13 changes: 13 additions & 0 deletions interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ object CryptoFacade {
/** Returns byte representation of the given field element. */
def encodeFieldElem(p: ECFieldElem): Array[Byte] = Platform.encodeFieldElem(p)

/** Returns byte representation of the given point.
*
* @param p point to encode
* @param compressed if true, generates a compressed point encoding
*/
def encodePoint(p: Ecp, compressed: Boolean): Array[Byte] = Platform.encodePoint(p, compressed)

/** Returns a [[Curve]] instance describing the elliptic curve of the point p
*
* @param p the elliptic curve point
*/
def getCurve(p: Ecp): Curve = Platform.getCurve(p)

/** Returns the x-coordinate.
*
* Caution: depending on the curve's coordinate system, this may not be the same value as in an
Expand Down
16 changes: 15 additions & 1 deletion interpreter/src/main/scala/sigmastate/crypto/Platform.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package sigmastate.crypto

import org.bouncycastle.crypto.ec.CustomNamedCurves
import org.bouncycastle.math.ec.{ECFieldElement, ECPoint}
import org.bouncycastle.math.ec.{ECPoint, ECFieldElement, ECCurve}

import java.math.BigInteger

/** JVM specific implementation of crypto methods*/
object Platform {
/** Returns a [[Curve]] instance describing the elliptic curve of the point p
* @param p the elliptic curve point
*/
def getCurve(p: Ecp): Curve = Curve(p.value.getCurve)

/** Returns the x-coordinate.
*
* Caution: depending on the curve's coordinate system, this may not be the same value as in an
Expand Down Expand Up @@ -46,6 +51,12 @@ object Platform {
/** Returns byte representation of the given field element. */
def encodeFieldElem(p: ECFieldElem): Array[Byte] = p.value.getEncoded

/** Returns byte representation of the given point.
* @param p point to encode
* @param compressed if true, generates a compressed point encoding
*/
def encodePoint(p: Ecp, compressed: Boolean): Array[Byte] = p.value.getEncoded(compressed)

/** Returns the value of bit 0 in BigInteger representation of this point. */
def signOf(p: ECFieldElem): Boolean = p.value.testBitZero()

Expand Down Expand Up @@ -94,6 +105,9 @@ object Platform {
/** Negate a point. */
def negatePoint(p: Ecp): Ecp = Ecp(p.value.negate())

/** Wrapper for curve type. */
case class Curve(private[crypto] val value: ECCurve)

/** Wrapper for point type. */
case class Ecp(private[crypto] val value: ECPoint)

Expand Down
4 changes: 4 additions & 0 deletions interpreter/src/main/scala/sigmastate/crypto/package.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package sigmastate

package object crypto {
/** Instance of Elliptic Curve. */
type Curve = Platform.Curve

/** Instance of Elliptic Curve point. */
type Ecp = Platform.Ecp

/** Instance of Elliptic Curve field element. */
type ECFieldElem = Platform.ECFieldElem
}

0 comments on commit e991c92

Please sign in to comment.