Exemplo n.º 1
0
    def __init__(self, sig: bytes = bytes()) -> None:
        self.value: G1Obj = G1Obj()
        if len(sig) == 0:
            MilagroPairing.ECP_BLS381_inf(byref(self.value))
            return

        g: Tuple[bool, bool, bool, Big384] = parse(sig)

        if g[0] != g[1]:
            raise Exception("Infinite flag set improperly.")

        if g[0]:
            MilagroPairing.ECP_BLS381_inf(byref(self.value))
            return

        if MilagroPairing.ECP_BLS381_setx(byref(self.value), g[3], 0) != 1:
            raise Exception("Invalid G1.")

        yNeg: FP1Obj = FP1Obj()
        MilagroPairing.FP_BLS381_neg(byref(yNeg), byref(self.value.y))

        a: Big384 = Big384()
        b: Big384 = Big384()
        MilagroCurve.FP_BLS381_redc(a, byref(self.value.y))
        MilagroCurve.FP_BLS381_redc(b, byref(yNeg))
        if (MilagroCurve.BIG_384_58_comp(a, b) == 1) != g[2]:
            if MilagroPairing.ECP_BLS381_setx(byref(self.value), g[3], 1) != 1:
                raise Exception("Setting a proven valid X failed.")
Exemplo n.º 2
0
    def __init__(self, value: Any = None) -> None:
        if isinstance(value, List):
            if len(value) != 1:
                raise Exception(
                    "Incompatible field element integer list passed to BLS12_381_F1."
                )
            value = value[0]

        self.value = FP1Obj()
        if (value is None) or (isinstance(value, int) and (value == 0)):
            MilagroCurve.FP_BLS381_zero(byref(self.value))
        elif isinstance(value, int):
            if value == 1:
                MilagroCurve.FP_BLS381_one(byref(self.value))
            else:
                temp: Big384 = Big384()
                MilagroCurve.BIG_384_58_fromBytes(
                    temp, c_char_p(value.to_bytes(48, byteorder="big")))
                MilagroCurve.FP_BLS381_rcopy(byref(self.value), temp)

        #Clone the value.
        elif isinstance(value, FP1Obj):
            self.value = clone(value)
        elif isinstance(value, BLS12_381_F1):
            self.value = clone(value.value)
        else:
            raise Exception("Unknown type passed to BLS12-381 F1 constructor.")
Exemplo n.º 3
0
    def __init__(self, key: Union[int, bytes]) -> None:
        #If a nickname was specified, generate a consistent Private Key based on it.
        if isinstance(key, int):
            key = blake2b(key.to_bytes(2 if key > 255 else 1, "little"),
                          digest_size=32).digest()

        key = key.rjust(48, b'\0')
        self.value: Big384 = Big384()
        MilagroCurve.BIG_384_58_fromBytesLen(self.value, c_char_p(key), 48)
        MilagroCurve.BIG_384_58_mod(self.value, r)
Exemplo n.º 4
0
    def serialize(self) -> bytes:
        x: Big384 = Big384()
        y: Big384 = Big384()
        MilagroCurve.ECP_BLS381_get(x, y, byref(self.value))

        yNeg: Big384 = Big384()
        MilagroCurve.FP_BLS381_neg(self.value.y, self.value.y)
        MilagroCurve.ECP_BLS381_get(x, yNeg, byref(self.value))
        MilagroCurve.FP_BLS381_neg(self.value.y, self.value.y)

        return bytes(serialize(x, MilagroCurve.BIG_384_58_comp(y, yNeg) == 1))
Exemplo n.º 5
0
 def __init__(self, x: Any, y: Optional[BLS12_381_F1] = None) -> None:
     self.value: G1Obj = G1Obj()
     if isinstance(x, G1Obj):
         MilagroCurve.ECP_BLS381_copy(byref(self.value), byref(x))
     elif isinstance(x, BLS12_381_G1):
         MilagroCurve.ECP_BLS381_copy(byref(self.value), byref(x.value))
     elif (isinstance(x, BLS12_381_F1) and isinstance(y, BLS12_381_F1)):
         xBig: Big384 = x.value.toBig384()
         yBig: Big384 = y.value.toBig384()
         if MilagroCurve.ECP_BLS381_set(byref(self.value), xBig, yBig) != 1:
             raise Exception("Passed invalid x/y to G1 constructor.")
     else:
         raise Exception("Unknown type passed to BLS12-381 G1 constructor.")
Exemplo n.º 6
0
    def isLargerThanNegative(self) -> bool:
        yNeg: FP2Obj = FP2Obj()
        MilagroPairing.FP2_BLS381_neg(byref(yNeg), byref(self))

        a: Big384 = self.b.toBig384()
        b: Big384 = yNeg.b.toBig384()
        cmpRes: int = MilagroCurve.BIG_384_58_comp(a, b)

        if cmpRes == 0:
            a = self.a.toBig384()
            b = yNeg.a.toBig384()
            cmpRes = MilagroCurve.BIG_384_58_comp(a, b)

        return cmpRes == 1
Exemplo n.º 7
0
def parse(
  gArg: bytes,
  second: bool = False
) -> Tuple[bool, bool, bool, Big384]:
  if len(gArg) != 48:
    raise Exception("Invalid length G.")

  flags = gArg[0]
  g: bytearray = bytearray(gArg)
  g[0] = g[0] & CLEAR_FLAGS

  if (not second) and (flags & C_FLAG == 0):
    raise Exception("Uncompressed G.")

  b: int = 1
  if flags & B_FLAG != 0:
    while b < 48:
      if g[b] != 0:
        break
      b += 1

  result: Big384 = Big384()
  MilagroCurve.BIG_384_58_fromBytesLen(result, c_char_p(bytes(g)), 48)

  return (flags & B_FLAG != 0, b == 48, flags & A_FLAG != 0, result)
Exemplo n.º 8
0
def msgToG(msg: bytes) -> G1Obj:
    result: G1Obj = G1Obj()
    hashed: OctetObj = OctetObj()

    shake: Any = shake_256()
    shake.update(msg)
    hashed.val = c_char_p(shake.digest(48))
    hashed.len = 48
    hashed.max = 48

    MilagroCurve.ECP_BLS381_mapit(byref(result), hashed)
    return result
Exemplo n.º 9
0
    def verify(self, agInfo: AggregationInfo) -> bool:
        if self.isInf():
            return False

        sig: G1Obj = self.value
        generator: G2Obj = G2Obj()
        sigPairing: FP12Obj = FP12Obj()

        MilagroCurve.ECP_BLS381_neg(byref(sig))
        MilagroPairing.ECP2_BLS381_generator(byref(generator))
        MilagroPairing.PAIR_BLS381_ate(byref(sigPairing), byref(generator),
                                       byref(sig))

        MilagroPairing.FP12_BLS381_ssmul(byref(sigPairing),
                                         byref(agInfo.value))
        MilagroPairing.PAIR_BLS381_fexp(byref(sigPairing))
        return int(MilagroPairing.FP12_BLS381_isunity(byref(sigPairing))) == 1
Exemplo n.º 10
0
    def serialize(self) -> bytes:
        yNeg: FP2Obj = FP2Obj()
        cmpRes: int
        MilagroPairing.FP2_BLS381_neg(byref(yNeg), byref(self.value.y))

        a: Big384 = Big384()
        b: Big384 = Big384()
        MilagroCurve.FP_BLS381_redc(a, byref(self.value.y.b))
        MilagroCurve.FP_BLS381_redc(b, byref(yNeg.b))
        cmpRes = MilagroCurve.BIG_384_58_comp(a, b)
        if cmpRes == 0:
            MilagroCurve.FP_BLS381_redc(a, byref(self.value.y.a))
            MilagroCurve.FP_BLS381_redc(b, byref(yNeg.a))
            cmpRes = MilagroCurve.BIG_384_58_comp(a, b)

        MilagroCurve.FP_BLS381_redc(a, byref(self.value.x.a))
        MilagroCurve.FP_BLS381_redc(b, byref(self.value.x.b))
        result: bytearray = serialize(b, cmpRes == 1)
        result += serialize(a, False)
        result[48] = result[48] & CLEAR_FLAGS
        return bytes(result)
Exemplo n.º 11
0
def serialize(g: Big384, largerY: bool) -> bytearray:
    buffer: Array[c_char] = create_string_buffer(48)
    MilagroCurve.BIG_384_58_toBytes(buffer, g)

    result: bytearray = bytearray(buffer)
    result[0] = result[0] | C_FLAG

    inf: int = 1
    if result[0] == C_FLAG:
        while inf < 48:
            if result[inf] != 0:
                break
            inf += 1
        if inf == 48:
            result[0] = result[0] | B_FLAG
            return result

    if largerY:
        result[0] = result[0] | A_FLAG

    return result
Exemplo n.º 12
0
    def __init__(self, key: bytes = bytes()) -> None:
        self.value: G2Obj = G2Obj()
        if len(key) == 0:
            MilagroPairing.ECP2_BLS381_inf(byref(self.value))
            return

        if len(key) != 96:
            raise Exception("Invalid length Public Key.")

        if key[48] != (key[48] & CLEAR_FLAGS):
            raise Exception("G2's second G has flags set.")

        g1: Tuple[bool, bool, bool, Big384] = parse(key[0:48])
        g2: Tuple[bool, bool, bool, Big384] = parse(key[48:96], True)

        if g1[0] != (g1[1] & g2[1]):
            raise Exception("Infinite flag set improperly.")

        if g1[0]:
            MilagroPairing.ECP2_BLS381_inf(byref(self.value))
            return

        x: FP2Obj = FP2Obj()
        MilagroPairing.FP2_BLS381_from_BIGs(byref(x), g2[3], g1[3])
        if MilagroPairing.ECP2_BLS381_setx(byref(self.value), byref(x)) == 0:
            raise Exception("Invalid G2.")

        yNeg: FP2Obj = FP2Obj()
        cmpRes: int
        MilagroPairing.FP2_BLS381_neg(byref(yNeg), byref(self.value.y))

        a: Big384 = Big384()
        b: Big384 = Big384()
        MilagroCurve.FP_BLS381_redc(a, byref(self.value.y.b))
        MilagroCurve.FP_BLS381_redc(b, byref(yNeg.b))
        cmpRes = MilagroCurve.BIG_384_58_comp(a, b)
        if cmpRes == 0:
            MilagroCurve.FP_BLS381_redc(a, byref(self.value.y.a))
            MilagroCurve.FP_BLS381_redc(b, byref(yNeg.a))
            cmpRes = MilagroCurve.BIG_384_58_comp(a, b)

        if (cmpRes == 1) != g1[2]:
            self.value.y = yNeg
Exemplo n.º 13
0
 def serialize(self) -> bytes:
     result: Array[c_char] = create_string_buffer(48)
     MilagroCurve.BIG_384_58_toBytes(result, self.value)
     return bytes(result)
Exemplo n.º 14
0
 def sign(self) -> int:
     buffer: Array[c_char] = create_string_buffer(48)
     MilagroCurve.BIG_384_58_toBytes(buffer, self.value.toBig384())
     return bytes(buffer)[-1] & 0b1
Exemplo n.º 15
0
def clone(value: FP1Obj) -> FP1Obj:
    result: FP1Obj = FP1Obj()
    MilagroCurve.BIG_384_58_copy(result.g, value.g)
    result.XES = value.XES
    return result
Exemplo n.º 16
0
 def sqrt(self) -> Any:
     result: FP1Obj = FP1Obj()
     MilagroCurve.FP_BLS381_sqrt(byref(result), byref(self.value))
     return BLS12_381_F1(result)
Exemplo n.º 17
0
 def __add__(self, other: Any) -> GroupElement:
     result: G1Obj = G1Obj()
     MilagroCurve.ECP_BLS381_copy(byref(result), byref(self.value))
     MilagroCurve.ECP_BLS381_add(byref(result), byref(other.value))
     return BLS12_381_G1(result)
Exemplo n.º 18
0
 def serialize(self) -> str:
     buffer: Array[c_char] = create_string_buffer(48)
     MilagroCurve.BIG_384_58_toBytes(buffer, self.value.toBig384())
     return bytes(buffer).hex()
Exemplo n.º 19
0
 def clearCofactor(self) -> GroupElement:
     result: BLS12_381_G1 = BLS12_381_G1(self.value)
     MilagroCurve.ECP_BLS381_mul(
         byref(result.value),
         BLS12_381_F1(15132376222941642753).value.toBig384())
     return result
Exemplo n.º 20
0
 def isInf(self) -> bool:
     return int(MilagroCurve.ECP_BLS381_isinf(byref(self.value))) == 1
Exemplo n.º 21
0
 def __mul__(self, other: Any) -> Any:
     result: FP1Obj = FP1Obj()
     MilagroCurve.FP_BLS381_mul(byref(result), byref(self.value),
                                byref(BLS12_381_F1(other).value))
     return BLS12_381_F1(result)
Exemplo n.º 22
0
 def __pow__(self, exp: int) -> Any:
     result: BLS12_381_F1 = BLS12_381_F1()
     MilagroCurve.FP_BLS381_pow(byref(result.value), byref(self.value),
                                BLS12_381_F1(exp).value.toBig384())
     return result
Exemplo n.º 23
0
 def __eq__(self, other: Any) -> bool:
     return MilagroCurve.FP_BLS381_equals(byref(self.value),
                                          byref(other.value)) == 1
Exemplo n.º 24
0
 def negative(self) -> Any:
     result: FP1Obj = FP1Obj()
     MilagroCurve.FP_BLS381_neg(byref(result), byref(self.value))
     return BLS12_381_F1(result)
Exemplo n.º 25
0
 def __init__(self, key: bytes) -> None:
     key = key.rjust(48, b'\0')
     self.value: Big384 = Big384()
     MilagroCurve.BIG_384_58_fromBytesLen(self.value, c_char_p(key), 48)
     MilagroCurve.BIG_384_58_mod(self.value, r)