Skip to content

Instantly share code, notes, and snippets.

Avatar

sken sken77

View GitHub Profile
View KGenerator.scala
class KGenerator(digest: Digest) extends DSAKCalculator {
val hMac = new HMac(digest)
val V = new Array[Byte](hMac.getMacSize)
val K = new Array[Byte](hMac.getMacSize)
var n = BigInteger.ZERO
def isDeterministic: Boolean = true
def init(n: BigInteger, random: SecureRandom): Unit = throw new IllegalStateException("Operation not supported")
View schnorr.scala
class SchnorrSigner(ecc: ECCurve[Secp256k1]) extends Signer[Schnorr] {
val fieldSize = ecc.domain.getCurve.getField.getCharacteristic
/** Same additional data used in ABC and bchd for generating the same deterministic schnorr signing */
val additionalData = "Schnorr+SHA256 ".getBytes("UTF-8").toByteVector
private def hasSquareY(R: ECPoint) =
R.getYCoord.toBigInteger
.modPow(fieldSize.subtract(BigInteger.ONE).divide(BigInt(2L).bigInteger), fieldSize) == BigInteger.ONE
View shard.scala
object Sharding extends App {
import zio.console._
/**
* EXERCISE 27
*
* Create N workers reading from a Queue, if one of them fails, then wait
* for the other ones to process their current item, but terminate all the
* workers.
*/
View priorityqueue.scala
object StmPriorityQueue extends App {
import zio.console._
import zio.stm._
import zio.duration._
/**
* EXERCISE 25
*
* Using STM, design a priority queue, where lower integers are assumed
* to have higher priority than higher integers.
View lunchtime.scala
object StmLunchTime extends App {
import zio.console._
import zio.stm._
/**
* EXERCISE 21
*
* Using STM, implement the missing methods of Attendee.
*/
final case class Attendee(state: TRef[Attendee.State]) {
@sken77
sken77 / .txt
Created Oct 20, 2018
Functional Programming Principles in scala
View .txt
There are three types of type composition:
Sum composition
Product composition
ADT Algebraic data types (sum and product composition)
Product composition:
A: {a1, a2}
B: {b1}
View node.scala
server[IO](bindTo("localhost", 5800)).flatMap(in =>
in.map(a =>
a.reads(maxByteSize)
.through(q => Network.start(q).stream) //: Pull[IO, Message, Unit]
.through(Network.handleMsg) //: Pipe[IO, Message, Message]
.through(Network.encode) //: Pipe[IO, Message, Byte]
.to {
a.writes()
}
).joinUnbounded