-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move H2C implementation to a separate file
- Loading branch information
Showing
3 changed files
with
111 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package jcmint; | ||
|
||
import javacard.framework.JCSystem; | ||
import javacard.framework.Util; | ||
import jcmint.jcmathlib.*; | ||
|
||
public class Denomination { | ||
public final BigNat secret; | ||
public final byte[] partialKeys; | ||
|
||
public Denomination(ResourceManager rm) { | ||
secret = new BigNat((short) 32, JCSystem.MEMORY_TYPE_PERSISTENT, rm); | ||
partialKeys = new byte[65 * Consts.MAX_PARTIES]; | ||
} | ||
|
||
public void setup(short parties, byte[] secret, short secretOffset, byte[] partialKeys, short partialKeysOffset) { | ||
this.secret.fromByteArray(secret, secretOffset, (short) 32); | ||
Util.arrayCopyNonAtomic(partialKeys, partialKeysOffset, this.partialKeys, (short) 0, (short) (65 * parties)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package jcmint; | ||
|
||
import javacard.framework.ISOException; | ||
import javacard.framework.JCSystem; | ||
import javacard.framework.Util; | ||
import javacard.security.MessageDigest; | ||
import jcmint.jcmathlib.*; | ||
|
||
public class HashToCurve { | ||
private final MessageDigest md = MessageDigest.getInstance(MessageDigest.ALG_SHA_256, false); | ||
private final byte[] prefixBuffer = JCSystem.makeTransientByteArray((short) 36, JCSystem.CLEAR_ON_RESET); | ||
private final byte[] ramArray = JCSystem.makeTransientByteArray((short) 32, JCSystem.CLEAR_ON_RESET); | ||
|
||
public void hash(byte[] data, short offset, ECPoint output) { | ||
Util.arrayFillNonAtomic(prefixBuffer, (short) 32, (short) 4, (byte) 0); | ||
md.reset(); | ||
md.update(Consts.H2C_DOMAIN_SEPARATOR, (short) 0, (short) Consts.H2C_DOMAIN_SEPARATOR.length); | ||
md.doFinal(data, offset, (short) 32, prefixBuffer, (short) 0); | ||
|
||
for (short counter = 0; counter < (short) 256; ++counter) { // TODO consider increasing max number of iters | ||
md.reset(); | ||
prefixBuffer[32] = (byte) (counter & 0xff); | ||
md.doFinal(prefixBuffer, (short) 0, (short) prefixBuffer.length, ramArray, (short) 0); | ||
if (output.fromX(ramArray, (short) 0, (short) 32)) | ||
break; | ||
} | ||
if (!output.isYEven()) | ||
output.negate(); | ||
} | ||
|
||
public void hashPrecomputed(byte[] input, short inputOffset, byte[] result, short resultOffset, ECPoint output) { | ||
Util.arrayFillNonAtomic(prefixBuffer, (short) 32, (short) 4, (byte) 0); | ||
md.reset(); | ||
md.update(Consts.H2C_DOMAIN_SEPARATOR, (short) 0, (short) Consts.H2C_DOMAIN_SEPARATOR.length); | ||
md.doFinal(input, inputOffset, (short) 32, prefixBuffer, (short) 0); | ||
|
||
md.reset(); | ||
md.doFinal(prefixBuffer, (short) 0, (short) prefixBuffer.length, ramArray, (short) 0); | ||
|
||
if (Util.arrayCompare(ramArray, (short) 0, result, (short) (resultOffset + 1), (short) 32) != 0) { | ||
ISOException.throwIt(Consts.E_INVALID_PRECOMPUTE); | ||
} | ||
|
||
output.setW(result, resultOffset, (short) 65); | ||
|
||
if (!output.isYEven()) | ||
output.negate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters