Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters are ignored in PairingGroup.random when the type of the element to be generated randomly is GT #313

Open
BatchClayderman opened this issue Jan 9, 2025 · 0 comments

Comments

@BatchClayderman
Copy link

Parameters are ignored in PairingGroup.random when the type of the element to be generated randomly is GT.

According to the developer document, it is expected to generate 3 elements of GT when calling group.random(GT, 3), where the symbol group is an object belonging to PairingGroup. However, when executing a, b, c = group.random(GT, 3), it does not work as a, b, c = group.random(ZR, 3) and other types. In the document, there are no related statements indicating that it is a special case when the function is called for generating multiple elements belonging to GT.

The related codes are from https://github.com/JHUISI/charm/blob/dev/charm/toolbox/pairinggroup.py.

    def random(self, _type=ZR, count=1, seed=None):
        """selects a random element in ZR, G1, G2 and GT"""
        if _type == GT: return self.__randomGT()
        elif _type in [ZR, G1, G2]:
            if seed != None and count == 1:
                return random(self.Pairing, _type, seed)
            elif count > 1:
                return tuple([random(self.Pairing, _type) for i in range(count)])                
            return random(self.Pairing, _type)
        return None

        
    def __randomGT(self):
        if not hasattr(self, 'gt'):
            self.gt = pair(self.random(G1), self.random(G2))
        z = self.random(ZR)
        return self.gt ** z

If there are no special meanings for count or seed in this case, the following codes may be helpful.

    def random(self:object, _type:int = ZR, count:int = 1, seed:object = None) -> object:
        """selects a random element in ZR, G1, G2 and GT"""
        if _type == GT: return self.__randomGT(count = count, seed = seed)
        elif _type in [ZR, G1, G2]:
            if seed != None and count == 1:
                return random(self.Pairing, _type, seed)
            elif count > 1:
                return tuple([random(self.Pairing, _type) for i in range(count)])                
            return random(self.Pairing, _type)
        return None

        
    def __randomGT(self:object, count:int = 1, seed:object = None) -> object:
        if not hasattr(self, 'gt'): # it is recommended to generate that when an instance is constructed
            self.gt = pair(self.random(G1, seed = seed), self.random(G2, seed = seed))
            # self.gt = pair(self.random(G1), self.random(G2))
        z = self.random(ZR, count = count, seed = seed)
        return tuple(self.gt ** ele for ele in z) if count > 1 else self.gt ** z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant