2

Camellia uses 6 sigmas in its key schedule with values

 Sigma1 = 0xA09E667F3BCC908B;
 Sigma2 = 0xB67AE8584CAA73B2;
 Sigma3 = 0xC6EF372FE94F82BE;
 Sigma4 = 0x54FF53A5F1D36F1C;
 Sigma5 = 0x10E527FADE682D1D;
 Sigma6 = 0xB05688C2B3E6C1FD;

The specification documents of the camellia says "Sigmas are defined as continuous values from the second hexadecimal place to the seventeenth hexadecimal place of the hexadecimal representation of the square root of the i-th prime."

The ith prime in this case are 2,3,5,7,11,13

However i cant find a way to compute these constants. Computing square root of prime is simple, but how these fractional square roots are converted into Hex notation? How it can be implemented in c#?

crypt
  • 143
  • 11
  • 1
    I've got it calculated in Java (C# is very much like Java wrt BigInteger & BigDecimal), but this is not the right place. Try stackoverflow, and make sure you tag it with the cryptography tag. – Maarten Bodewes Jul 26 '16 at 09:40
  • Remarks: there should be little reasons to ever calculate this with a specific runtime, in the end they are just constants. You may need to check those once, but that's it. Furthermore, creating a SQRT at arbitrary precision is still a bit of a puzzle. – Maarten Bodewes Jul 26 '16 at 09:46
  • @MaartenBodewes C# doesn't have a built-in `BigDecimal`, only a `BigInteger`. `Decimal` would have enough precision for this particular question, but it doesn't come with a `Sqrt` operation, so you'd have to write a binary search yourself. – CodesInChaos Jul 26 '16 at 10:17
  • @CodesInChaos Or you could just copy an existing one from this site :P – Maarten Bodewes Jul 26 '16 at 12:49
  • i understood it :) thanks a lot for the effort. This helped me in making own code for fractional part of the hex and your observation regarding c# for bigdecimel is correct. cant get more precision with double of 64 bit. – crypt Jul 27 '16 at 18:41

1 Answers1

3

What you need is some method of calculating the square root mostly.

Please see the following Java application.

private static final BigDecimal SQRT_DIG = new BigDecimal(150);
private static final BigDecimal SQRT_PRE = new BigDecimal(10).pow(SQRT_DIG.intValue());
private static final int HEX_DIGITS = 16;
private static final int HEX_DIGITS_REQUIRED = 16;
private static final int HEX_DIGITS_SKIPPED = 2;
// constant that puts the required number at the left hand of the decimal point
private static final BigDecimal DIGIT_MULTIPLICATION =
        new BigDecimal(BigInteger.valueOf(HEX_DIGITS).pow(HEX_DIGITS_SKIPPED + HEX_DIGITS_REQUIRED));

public static void main(String[] args) {

    // - starting value (prime)
    BigInteger currentBI = BigInteger.valueOf(2);

    // - loop 6 times
    for (int i = 1; i <= 6; i++) {
        // - get i'th prime
        while (!currentBI.isProbablePrime(Integer.MAX_VALUE)) {
            currentBI = currentBI.add(BigInteger.ONE);
        }

        // - as BigDecimal
        BigDecimal currentBD = BigDecimal.valueOf(currentBI.longValue());

        // - square root
        BigDecimal sqrt = bigSqrt(currentBD);

        // - put the required hex digits at the left hand of the decimal dot
        BigInteger digitsAtLeft = sqrt.multiply(DIGIT_MULTIPLICATION).toBigInteger();

        // - convert to hexadecimals and skip the first two digits as required
        String digits = digitsAtLeft.toString(HEX_DIGITS).substring(HEX_DIGITS_SKIPPED);

        // - print out
        System.out.printf("%d %d %f... %s%n",
                i, currentBI, sqrt, digits);

        // - current++
        currentBI = currentBI.add(BigInteger.ONE);
    }
}

/**
 * Uses Newton Raphson to compute the square root of a BigDecimal.
 * 
 * @author Luciano Culacciatti 
 * @url http://www.codeproject.com/Tips/257031/Implementing-SqrtRoot-in-BigDecimal
 */
public static BigDecimal bigSqrt(BigDecimal c){
    return sqrtNewtonRaphson(c,new BigDecimal(1),new BigDecimal(1).divide(SQRT_PRE));
}

private static BigDecimal sqrtNewtonRaphson  (BigDecimal c, BigDecimal xn, BigDecimal precision){
    BigDecimal fx = xn.pow(2).add(c.negate());
    BigDecimal fpx = xn.multiply(new BigDecimal(2));
    BigDecimal xn1 = fx.divide(fpx,2*SQRT_DIG.intValue(), RoundingMode.HALF_DOWN);
    xn1 = xn.add(xn1.negate());
    BigDecimal currentSquare = xn1.pow(2);
    BigDecimal currentPrecision = currentSquare.subtract(c);
    currentPrecision = currentPrecision.abs();
    if (currentPrecision.compareTo(precision) <= -1){
        return xn1;
    }
    return sqrtNewtonRaphson(c, xn1, precision);
}

This will output:

1 2 1.414214... a09e667f3bcc908b2
2 3 1.732051... b67ae8584caa73b25
3 5 2.236068... c6ef372fe94f82be7
4 7 2.645751... 54ff53a5f1d36f1ce
5 11 3.316625... 10e527fade682d1de
6 13 3.605551... b05688c2b3e6c1fdb

Problem is that C# doesn't know about BigDecimal but you can get a good precission decimal value, try the following square root calculation here.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I'll try and convert it to C# at home if you cannot. Please give it a try yourself and adjust this post if you find an answer. – Maarten Bodewes Jul 26 '16 at 11:19
  • You may need decimal to byte array conversion for this, see [here](http://social.technet.microsoft.com/wiki/contents/articles/19055.convert-system-decimal-to-and-from-byte-arrays-vb-c.aspx). – Maarten Bodewes Jul 26 '16 at 13:23
  • i understood it :) thanks a lot for the effort. [This helped me in making own code for fractional part of the hex](http://stackoverflow.com/questions/20650954/how-to-convert-decimal-fractions-to-hexadecimal-fractions) and your observation regarding c# is correct. cant get more precision with double of 64 bit. – crypt Jul 26 '16 at 18:48