예제 #1
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.")

        if self.value.y.isLargerThanNegative() != g1[2]:
            MilagroPairing.FP2_BLS381_neg(byref(self.value.y),
                                          byref(self.value.y))
예제 #2
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.")
예제 #3
0
    def toPublicKey(self) -> PublicKey:
        result: PublicKey = PublicKey.__new__(PublicKey)
        result.value = G2Obj()

        MilagroPairing.ECP2_BLS381_generator(byref(result.value))
        MilagroPairing.ECP2_BLS381_mul(byref(result.value), self.value)

        return result
예제 #4
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
예제 #5
0
파일: BLS.py 프로젝트: frozenghozt/Meros
 def sign(
   self,
   msgArg: bytes
 ) -> Signature:
   result: Signature = Signature.__new__(Signature)
   result.value = msgToG(msgArg)
   MilagroPairing.ECP_BLS381_mul(byref(result.value), byref(self.value))
   return result
예제 #6
0
    def __init__(self, key: PublicKey, msgArg: bytes) -> None:
        if key.isInf():
            raise Exception(
                "Infinite Public Key passed to newAggregationInfo.")

        msg: G1Obj = msgToG(msgArg)
        self.value: FP12Obj = FP12Obj()
        MilagroPairing.PAIR_BLS381_ate(byref(self.value), byref(key.value),
                                       byref(msg))
예제 #7
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
예제 #8
0
    def aggregate(sigs: List[Any]) -> Any:
        result: Signature = Signature.__new__(Signature)
        result.value = G1Obj()

        if not sigs:
            MilagroPairing.ECP_BLS381_inf(byref(result.value))
            return result

        MilagroPairing.ECP_BLS381_copy(byref(result.value),
                                       byref(sigs[0].value))
        for s in range(1, len(sigs)):
            if sigs[s].isInf():
                MilagroPairing.ECP_BLS381_inf(byref(result.value))
                return result

            MilagroPairing.ECP_BLS381_add(byref(result.value),
                                          byref(sigs[s].value))

        return result
예제 #9
0
    def aggregate(keys: List[Any]) -> Any:
        result: PublicKey = PublicKey.__new__(PublicKey)
        result.value = G2Obj()

        if not keys:
            MilagroPairing.ECP2_BLS381_inf(byref(result.value))
            return result

        result.value = keys[0].value
        for k in range(1, len(keys)):
            if keys[k].isInf():
                MilagroPairing.ECP2_BLS381_inf(byref(result.value))
                return result

            if not MilagroPairing.ECP2_BLS381_add(byref(result.value),
                                                  byref(keys[k].value)) != 0:
                raise Exception("Milagro failed to add G2s.")

        return result
예제 #10
0
    def aggregate(agInfos: List[Any]) -> Any:
        if not agInfos:
            raise Exception("No Aggregation Infos passed to aggregate.")

        result: AggregationInfo = AggregationInfo.__new__(AggregationInfo)
        result.value = agInfos[0].value
        for i in range(1, len(agInfos)):
            MilagroPairing.FP12_BLS381_mul(byref(result.value),
                                           byref(agInfos[i].value))

        return result
예제 #11
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.")

        if self.value.y.isLargerThanNegative() != g[2]:
            if MilagroPairing.ECP_BLS381_setx(byref(self.value), g[3], 1) != 1:
                raise Exception("Setting a proven valid X failed.")
예제 #12
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)
예제 #13
0
 def isInf(self) -> bool:
     return int(MilagroPairing.ECP2_BLS381_isinf(byref(self.value))) == 1