All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mofn.com.unbound.common.crypto.mofn.PartyNode Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common.crypto.mofn;

import com.unbound.common.Converter;

import java.math.BigInteger;
import java.util.ArrayList;

public final class PartyNode
{
  static final int MAX_PARTY_COUNT = 128;
  static final int M_PARTY = 0;
  static final int M_AND = -1;
  static final int M_OR = -2;
  static final int M_SPARE_PARTY = -3;

  PartyNode parent = null;
  int index=0, m;
  String name;
  BigInteger vi;                       // coordinator     // commmitment to the secret
  BigInteger v;                        // group           // commitment CRS
  BigInteger s;                        // secret
  boolean presentPartyResult = false;
  BigInteger x, z, c;                  // party result    // Values sent to coordinator

  final ArrayList children = new ArrayList<>();

  private PartyNode() { }

  PartyNode(String name, int m)
  {
    this.name = name;
    this.m = m;
  }

  public static PartyNode Party(String name)  { return new PartyNode(name, M_PARTY); }
  public static PartyNode Group(String name, int m) { return new PartyNode(name, m); }
  public static PartyNode And(String name) { return new PartyNode(name, M_AND); }
  public static PartyNode Or(String name) { return new PartyNode(name, M_OR); }

  public void add(PartyNode node)
  {
    if (isPartyOrSpare()) throw new IllegalArgumentException("Can't add node to the party or spare");

    if (isMofN())
    {
      if (!node.isPartyOrSpare()) throw new IllegalArgumentException("Can't add group node to M-of-N node");
      node.index = 1 + children.size();
    }

    if (isAndOr() && node.isPartyOrSpare())
    {
      PartyNode MofN1x1 = new PartyNode("", 1);
      node.index = 1;
      node.parent = MofN1x1;
      MofN1x1.children.add(node);
      node = MofN1x1;
    }

    node.parent = this;
    children.add(node);
  }

  PartyNode findParty(String path, int index)
  {
    String[] names = path.split("/");
    PartyNode root = this;

    for (String name : names)
    {
      if (name.isEmpty()) continue;

      if (root.m==1 && root.name.isEmpty() && root.children.size()==1) root = root.children.get(0); // 1 of 1

      PartyNode node = null;
      for (PartyNode current : root.children)
      {
        boolean found;
        if (current.name.isEmpty() || current.isPartyOrSpare()) found = index==current.index;
        else found = current.name.equalsIgnoreCase(name);

        if (found) { node = current; break; }
      }

      if (node==null) return null;
      root = node;
    }

    return root;
  }

  boolean isParty()  { return m==0; }
  boolean isSpare()  { return m==M_SPARE_PARTY; }
  boolean isMofN()  { return m>0; }
  boolean isAndOr()  { return m==M_AND || m==M_OR; }
  boolean isPartyOrSpare() { return isParty() || isSpare(); }

  String getPath()
  {
    if (parent==null) return name;
    String path = parent.getPath();
    if (!name.isEmpty()) path += "/" + name;
    return path;
  }

  static PartyNode convertTree(Converter converter, PartyNode root)
  {
    int tree = converter.beginStructVersion0();

    if (!converter.isWrite()) root = new PartyNode();
    root.convert(converter);

    converter.endStruct(tree);
    return root;
  }

  void convert(Converter converter)
  {
    int struct = converter.beginStructVersion0();

    index = converter.convert(index);
    m = converter.convert(m);
    name = converter.convert(name);

    converter.convert(vi);
    converter.convert(v);
    short count = (short)children.size();
    count = converter.convert(count);

    if (!converter.isWrite())
    {
      freeChildren();
      if (m<-3) throw new IllegalArgumentException("Invalid party node: m<-3");
      if (m>MAX_PARTY_COUNT) throw new IllegalArgumentException("Invalid party node: m>MAX_PARTY_COUNT");
      if (count>MAX_PARTY_COUNT) throw new IllegalArgumentException("Invalid party node: count>MAX_PARTY_COUNT");
      if ((m==0 || m==-3) && count>0) throw new IllegalArgumentException("Invalid party node: (m==0 || m==-3) && count>0"); // PARTY
      if ((m==-1 || m==-2) && count<1) throw new IllegalArgumentException("Invalid party node: (m==-1 || m==-2) && count<1"); // AND, OR
      if (m>0 && m>count) throw new IllegalArgumentException("Invalid party node: m>0 && m>count"); // M OF N
      children.ensureCapacity(count);
    }

    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy